aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
gum::learning::IKTBNLearner< GUM_SCALAR > Class Template Referenceabstract

Pure-virtual configuration interface shared by all k-TBN learners. More...

#include <agrum/KTBN/learning/IKTBNLearner.h>

Inheritance diagram for gum::learning::IKTBNLearner< GUM_SCALAR >:
[legend]

Public Member Functions

virtual ~IKTBNLearner ()=default
 virtual destructor (polymorphic base class)
Main learning method
virtual KTBN< GUM_SCALAR > learnKTBN ()=0
 Learn a k-TBN (structure + CPTs) with the recorded configuration. The fixed-k learner learns for its single k; the adaptive learner also selects the best k in [2, kMax].
Score selection
virtual IKTBNLearner< GUM_SCALAR > & useScoreAIC ()=0
virtual IKTBNLearner< GUM_SCALAR > & useScoreBD ()=0
virtual IKTBNLearner< GUM_SCALAR > & useScoreBDeu ()=0
virtual IKTBNLearner< GUM_SCALAR > & useScoreBIC ()=0
virtual IKTBNLearner< GUM_SCALAR > & useScoreLog2Likelihood ()=0
virtual IKTBNLearner< GUM_SCALAR > & useScoreMDL ()=0
virtual void useScorefNML ()=0
Algorithm selection
virtual IKTBNLearner< GUM_SCALAR > & useGreedyHillClimbing ()=0
virtual IKTBNLearner< GUM_SCALAR > & useExtendedGreedyHillClimbing ()=0
virtual IKTBNLearner< GUM_SCALAR > & useLocalSearchWithTabuList (Size tabu_size=100, Size nb_decrease=2)=0
virtual IKTBNLearner< GUM_SCALAR > & useMIIC ()=0
MIIC correction
virtual IKTBNLearner< GUM_SCALAR > & useNMLCorrection ()=0
virtual IKTBNLearner< GUM_SCALAR > & useMDLCorrection ()=0
virtual IKTBNLearner< GUM_SCALAR > & useNoCorrection ()=0
Prior selection
virtual IKTBNLearner< GUM_SCALAR > & useSmoothingPrior (double weight=1.0)=0
Structural constraints (by name only)
virtual IKTBNLearner< GUM_SCALAR > & addForbiddenArc (std::string_view tailNode, std::string_view headNode)=0
 Forbid tailNode -> headNode (engine names, e.g. "X[1]", "C").
virtual IKTBNLearner< GUM_SCALAR > & addForbiddenArc (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)=0
 Forbid one (base, slice) -> (base, slice) arc (KTBN::ATEMPORAL for static).
virtual IKTBNLearner< GUM_SCALAR > & eraseForbiddenArc (std::string_view tailNode, std::string_view headNode)=0
 Undo a previous addForbiddenArc (engine names).
virtual IKTBNLearner< GUM_SCALAR > & eraseForbiddenArc (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)=0
 Undo a previous addForbiddenArc for a specific (base, slice) pair.
virtual IKTBNLearner< GUM_SCALAR > & addMandatoryArc (std::string_view tailNode, std::string_view headNode)=0
 Force tailNode to be a parent of headNode (engine names).
virtual IKTBNLearner< GUM_SCALAR > & addMandatoryArc (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)=0
 Force one arc, lag stated explicitly via slices (KTBN::ATEMPORAL for static).
virtual IKTBNLearner< GUM_SCALAR > & eraseMandatoryArc (std::string_view tailNode, std::string_view headNode)=0
 Undo a previous addMandatoryArc (engine names).
virtual IKTBNLearner< GUM_SCALAR > & eraseMandatoryArc (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)=0
 Undo a previous addMandatoryArc.
virtual IKTBNLearner< GUM_SCALAR > & addForbiddenIntraSliceArc (std::string_view tailBase, std::string_view headBase)=0
 Forbid tailBase -> headBase at every intra-slice position.
virtual IKTBNLearner< GUM_SCALAR > & eraseForbiddenIntraSliceArc (std::string_view tailBase, std::string_view headBase)=0
 Undo a previous addForbiddenIntraSliceArc.
virtual IKTBNLearner< GUM_SCALAR > & addForbiddenArcAllSlices (std::string_view tailBase, std::string_view headBase)=0
 Forbid tailBase -> headBase at every causally-possible slice pair.
virtual IKTBNLearner< GUM_SCALAR > & eraseForbiddenArcAllSlices (std::string_view tailBase, std::string_view headBase)=0
 Undo a previous addForbiddenArcAllSlices.
virtual IKTBNLearner< GUM_SCALAR > & addNoParentNode (std::string_view base, int slice)=0
 Declare a single (base, slice) node as a root (no parents).
virtual IKTBNLearner< GUM_SCALAR > & addNoParentNode (std::string_view name)=0
 Declare a single node (bracket notation, e.g. "X[2]" or "C") as a root.
virtual IKTBNLearner< GUM_SCALAR > & eraseNoParentNode (std::string_view base, int slice)=0
 Undo a previous addNoParentNode for a single (base, slice) node.
virtual IKTBNLearner< GUM_SCALAR > & eraseNoParentNode (std::string_view name)=0
 Undo addNoParentNode for a node given by bracket notation.
virtual IKTBNLearner< GUM_SCALAR > & addNoChildrenNode (std::string_view base, int slice)=0
 Declare a single (base, slice) node as a leaf (no children).
virtual IKTBNLearner< GUM_SCALAR > & addNoChildrenNode (std::string_view name)=0
 Declare a single node (bracket notation, e.g. "X[2]" or "C") as a leaf.
virtual IKTBNLearner< GUM_SCALAR > & eraseNoChildrenNode (std::string_view base, int slice)=0
 Undo a previous addNoChildrenNode for a single (base, slice) node.
virtual IKTBNLearner< GUM_SCALAR > & eraseNoChildrenNode (std::string_view name)=0
 Undo addNoChildrenNode for a node given by bracket notation.
virtual IKTBNLearner< GUM_SCALAR > & addPossibleEdge (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)=0
 Add a candidate edge for MIIC (only edges explicitly listed are explored).
virtual IKTBNLearner< GUM_SCALAR > & addPossibleEdge (std::string_view tail, std::string_view head)=0
 Add a candidate edge for MIIC using engine names (e.g. "X[1]", "C").
virtual IKTBNLearner< GUM_SCALAR > & erasePossibleEdge (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)=0
 Undo a previous addPossibleEdge.
virtual IKTBNLearner< GUM_SCALAR > & erasePossibleEdge (std::string_view tail, std::string_view head)=0
 Undo a previous addPossibleEdge using engine names (e.g. "X[1]", "C").
virtual IKTBNLearner< GUM_SCALAR > & allowArcAdditions (bool allow=true)=0
 Allow or forbid arc additions during structure search.
virtual IKTBNLearner< GUM_SCALAR > & allowArcDeletions (bool allow=true)=0
 Allow or forbid arc deletions during structure search.
virtual IKTBNLearner< GUM_SCALAR > & allowArcReversals (bool allow=true)=0
 Allow or forbid arc reversals during structure search.
virtual IKTBNLearner< GUM_SCALAR > & setMaxIndegree (Size max_indegree)=0
 Cap the number of parents of any single node.

Name encoding: (base, slice) <-> engine name

virtual const std::unordered_set< std::string > & _atemporalVarNames_ () const =0
 The base names of the atemporal (static) variables. The only subclass-specific input to determineNode(): the fixed-k learner reads it from its prior k-TBN, the adaptive learner from the atemporal set recorded at construction.
virtual bool _isKnownBase_ (std::string_view base) const =0
 Whether base is one of this learner's variables, temporal or atemporal. The second subclass-specific input to the shared helpers. A predicate rather than a temporalVarNames() counterpart to atemporalVarNames(): the adaptive learner holds the base names and the atemporal ones but never a temporal-only set, and would have to materialise and cache a third one just to return a reference.
std::string _encode_ (std::string_view base, int slice) const
 (base, slice) -> engine name ("A[1]" / atemporal engine name). Pure function, shared by every learner.
std::pair< std::string, int > _determineNode_ (const std::string &name) const
 engine name -> (base, slice); atemporal names map to KTBN::ATEMPORAL. Shared by every learner; only the atemporal test varies (via atemporalVarNames()).
void _checkArcTemporallyFeasible_ (std::string_view tail, std::string_view head, std::string_view action) const
 Reject an arc the k-TBN definition can never contain, so eraseForbiddenArc and addMandatoryArc both fail early (before any internal learner is touched) with a uniform message. action is the verb phrase completing "cannot <action> <tail> -> <head>: ..." (e.g. "force the mandatory arc", "un-forbid the arc"). Two arcs are refused: a temporal -> atemporal arc (a time-varying variable can never parent a static one) and a backward-in-time arc (head strictly before tail).
void _checkBaseIsTemporal_ (std::string_view base, std::string_view context) const
 Throw InvalidArgument unless base is a known temporal base. context completes "cannot appear in <context>" (e.g. "an intra-slice constraint", "a kernel-relative arc"): every caller rejects an atemporal base for the same underlying reason – it has no per-slice instance – so they share the sentence and vary the setting.
static void _checkMinimalOrder_ (Size order, std::string_view label)
 Throw InvalidArgument unless order is at least 2, label naming the offending parameter ("k" for the fixed-k learner, "kMax" for the adaptive one).
static std::unordered_set< std::string > _scanConstantColumns_ (std::string_view dirPath, std::string_view csvBaseName, Size nbSamples, const std::vector< std::string > &missingSymbols)
 Scans every trajectory and returns the base names classified atemporal: those whose value never changes across the rows of a single trajectory (checked independently per trajectory, so the value may still differ between trajectories). Shared by every atemporal-inferring CSV constructor (KTBNLearner, KTBNAdaptiveLearner): identical scanning logic regardless of which subclass calls it, so duplicating it per subclass would only invite the two copies to drift. Callers are responsible for validating their own arguments (e.g. k/kMax >= 2) before calling this — it does no such check itself, only opens files, so a bad argument would otherwise only be caught after a wasted scan.

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::learning::IKTBNLearner< GUM_SCALAR >

Pure-virtual configuration interface shared by all k-TBN learners.

See also
gum::learning::KTBNLearner (fixed k), gum::learning::KTBNAdaptiveLearner (k is learned too).

Definition at line 110 of file IKTBNLearner.h.

Constructor & Destructor Documentation

◆ ~IKTBNLearner()

template<GUM_Numeric GUM_SCALAR>
virtual gum::learning::IKTBNLearner< GUM_SCALAR >::~IKTBNLearner ( )
virtualdefault

virtual destructor (polymorphic base class)

Member Function Documentation

◆ _atemporalVarNames_()

template<GUM_Numeric GUM_SCALAR>
virtual const std::unordered_set< std::string > & gum::learning::IKTBNLearner< GUM_SCALAR >::_atemporalVarNames_ ( ) const
protectedpure virtual

The base names of the atemporal (static) variables. The only subclass-specific input to determineNode(): the fixed-k learner reads it from its prior k-TBN, the adaptive learner from the atemporal set recorded at construction.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

Referenced by _checkBaseIsTemporal_(), and _determineNode_().

Here is the caller graph for this function:

◆ _checkArcTemporallyFeasible_()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::IKTBNLearner< GUM_SCALAR >::_checkArcTemporallyFeasible_ ( std::string_view tail,
std::string_view head,
std::string_view action ) const
protected

Reject an arc the k-TBN definition can never contain, so eraseForbiddenArc and addMandatoryArc both fail early (before any internal learner is touched) with a uniform message. action is the verb phrase completing "cannot <action> <tail> -> <head>: ..." (e.g. "force the mandatory arc", "un-forbid the arc"). Two arcs are refused: a temporal -> atemporal arc (a time-varying variable can never parent a static one) and a backward-in-time arc (head strictly before tail).

Definition at line 95 of file IKTBNLearner_tpl.h.

97 {
102 "cannot " << action << " " << tail << " -> " << head
103 << ": a temporal variable can never be a parent of an atemporal one; "
104 "this constraint is part of the k-TBN definition")
107 "cannot " << action << " " << tail << " -> " << head
108 << ": its head is at an earlier time slice than its tail, which "
109 "violates temporal causality")
110 }
Pure-virtual configuration interface shared by all k-TBN learners.
std::pair< std::string, int > _determineNode_(const std::string &name) const
engine name -> (base, slice); atemporal names map to KTBN::ATEMPORAL. Shared by every learner; only t...

References _determineNode_(), gum::KTBN< GUM_SCALAR >::ATEMPORAL, and GUM_ERROR.

Referenced by _isKnownBase_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addMandatoryArc(), gum::learning::KTBNLearner< GUM_SCALAR >::addMandatoryArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseForbiddenArc(), and gum::learning::KTBNLearner< GUM_SCALAR >::eraseForbiddenArc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _checkBaseIsTemporal_()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::IKTBNLearner< GUM_SCALAR >::_checkBaseIsTemporal_ ( std::string_view base,
std::string_view context ) const
protected

Throw InvalidArgument unless base is a known temporal base. context completes "cannot appear in <context>" (e.g. "an intra-slice constraint", "a kernel-relative arc"): every caller rejects an atemporal base for the same underlying reason – it has no per-slice instance – so they share the sentence and vary the setting.

Definition at line 196 of file IKTBNLearner_tpl.h.

197 {
198 const std::string b{base};
199 if (!_isKnownBase_(b))
201 "unknown base variable '" << base
202 << "': it is not one of this learner's variables")
205 "atemporal variable '" << base << "' cannot appear in " << context
206 << ": it has no per-slice instance")
207 }
virtual bool _isKnownBase_(std::string_view base) const =0
Whether base is one of this learner's variables, temporal or atemporal. The second subclass-specific ...
virtual const std::unordered_set< std::string > & _atemporalVarNames_() const =0
The base names of the atemporal (static) variables. The only subclass-specific input to determineNode...

References _atemporalVarNames_(), _isKnownBase_(), gum::contains(), and GUM_ERROR.

Referenced by _isKnownBase_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_verifyKernelArc_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addForbiddenIntraSliceArc(), gum::learning::KTBNLearner< GUM_SCALAR >::addForbiddenIntraSliceArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseForbiddenIntraSliceArc(), and gum::learning::KTBNLearner< GUM_SCALAR >::eraseForbiddenIntraSliceArc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _checkMinimalOrder_()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::IKTBNLearner< GUM_SCALAR >::_checkMinimalOrder_ ( Size order,
std::string_view label )
staticprotected

Throw InvalidArgument unless order is at least 2, label naming the offending parameter ("k" for the fixed-k learner, "kMax" for the adaptive one).

This is a learner constraint, not a model one – gum::KTBN, gum::KTBNGenerator and gum::KTBNInference all accept k = 1 – which is why it lives here rather than on the model. Static because four of its call sites are themselves static helpers invoked from member-initialiser lists, before any object exists.

Definition at line 210 of file IKTBNLearner_tpl.h.

210 {
211 if (order < 2)
213 "a k-TBN learner requires "
214 << label << " >= 2: k=1 is a static Bayesian network, use BNLearner instead")
215 }

References GUM_ERROR.

Referenced by gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::KTBNAdaptiveLearner(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::KTBNAdaptiveLearner(), gum::learning::KTBNLearner< GUM_SCALAR >::_buildPriorFromBN_(), gum::learning::KTBNLearner< GUM_SCALAR >::_buildPriorFromCSV_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_inferAtemporalVars_(), gum::learning::KTBNLearner< GUM_SCALAR >::_inferAtemporalVars_(), and _isKnownBase_().

Here is the caller graph for this function:

◆ _determineNode_()

template<GUM_Numeric GUM_SCALAR>
std::pair< std::string, int > gum::learning::IKTBNLearner< GUM_SCALAR >::_determineNode_ ( const std::string & name) const
protected

engine name -> (base, slice); atemporal names map to KTBN::ATEMPORAL. Shared by every learner; only the atemporal test varies (via atemporalVarNames()).

Definition at line 64 of file IKTBNLearner_tpl.h.

64 {
65 // check atemporal set first: a name registered as atemporal always maps
66 // to ATEMPORAL, even if it syntactically looks like "base[t]"
68
69 // syntactic parse: look for a trailing "[digits]" suffix
70 const std::size_t bracketPos = name.rfind('[');
72
74 name.size() - bracketPos - 1};
75 if (bracketContent.empty() || bracketContent.back() != ']')
77
78 const std::string_view digits = bracketContent.substr(0, bracketContent.size() - 1);
79 if (digits.empty()) return {name, KTBN< GUM_SCALAR >::ATEMPORAL};
80 for (const char c: digits)
81 if (std::isdigit(static_cast< unsigned char >(c)) == 0)
83
84 int slice{};
85 try {
87 } catch (const std::out_of_range&) {
89 "Node name '" << name << "' has a slice index too large to represent as int.")
90 }
91 return {name.substr(0, bracketPos), slice};
92 }

References _atemporalVarNames_(), gum::KTBN< GUM_SCALAR >::ATEMPORAL, gum::contains(), and GUM_ERROR.

Referenced by gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_applyConstraints_(), _checkArcTemporallyFeasible_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_forEachScoredNode_(), gum::learning::KTBNLearner< GUM_SCALAR >::_forOwningLearner_(), _isKnownBase_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_recomputeKMin_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addForbiddenArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addMandatoryArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addNoChildrenNode(), gum::learning::KTBNLearner< GUM_SCALAR >::addNoChildrenNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addNoParentNode(), gum::learning::KTBNLearner< GUM_SCALAR >::addNoParentNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addPossibleEdge(), gum::learning::KTBNLearner< GUM_SCALAR >::addPossibleEdge(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseForbiddenArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseMandatoryArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseNoChildrenNode(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseNoChildrenNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseNoParentNode(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseNoParentNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::erasePossibleEdge(), gum::learning::KTBNLearner< GUM_SCALAR >::erasePossibleEdge(), and gum::learning::KTBNLearner< GUM_SCALAR >::names().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _encode_()

template<GUM_Numeric GUM_SCALAR>
std::string gum::learning::IKTBNLearner< GUM_SCALAR >::_encode_ ( std::string_view base,
int slice ) const
protected

(base, slice) -> engine name ("A[1]" / atemporal engine name). Pure function, shared by every learner.

Definition at line 57 of file IKTBNLearner_tpl.h.

57 {
59 return std::string{base} + '[' + std::to_string(slice) + ']';
60 }

References gum::KTBN< GUM_SCALAR >::ATEMPORAL.

Referenced by gum::learning::KTBNLearner< GUM_SCALAR >::_assemble_(), gum::learning::KTBNLearner< GUM_SCALAR >::_build_(), _isKnownBase_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addForbiddenArc(), gum::learning::KTBNLearner< GUM_SCALAR >::addForbiddenArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addMandatoryArc(), gum::learning::KTBNLearner< GUM_SCALAR >::addMandatoryArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addNoChildrenNode(), gum::learning::KTBNLearner< GUM_SCALAR >::addNoChildrenNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addNoParentNode(), gum::learning::KTBNLearner< GUM_SCALAR >::addNoParentNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addPossibleEdge(), gum::learning::KTBNLearner< GUM_SCALAR >::addPossibleEdge(), gum::learning::KTBNLearner< GUM_SCALAR >::domainSize(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseForbiddenArc(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseForbiddenArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseMandatoryArc(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseMandatoryArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseNoChildrenNode(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseNoChildrenNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseNoParentNode(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseNoParentNode(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::erasePossibleEdge(), gum::learning::KTBNLearner< GUM_SCALAR >::erasePossibleEdge(), and gum::learning::KTBNLearner< GUM_SCALAR >::learnParameters().

Here is the caller graph for this function:

◆ _isKnownBase_()

template<GUM_Numeric GUM_SCALAR>
virtual bool gum::learning::IKTBNLearner< GUM_SCALAR >::_isKnownBase_ ( std::string_view base) const
protectedpure virtual

Whether base is one of this learner's variables, temporal or atemporal. The second subclass-specific input to the shared helpers. A predicate rather than a temporalVarNames() counterpart to atemporalVarNames(): the adaptive learner holds the base names and the atemporal ones but never a temporal-only set, and would have to materialise and cache a third one just to return a reference.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

References _checkArcTemporallyFeasible_(), _checkBaseIsTemporal_(), _checkMinimalOrder_(), _determineNode_(), _encode_(), and _scanConstantColumns_().

Referenced by _checkBaseIsTemporal_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _scanConstantColumns_()

template<GUM_Numeric GUM_SCALAR>
std::unordered_set< std::string > gum::learning::IKTBNLearner< GUM_SCALAR >::_scanConstantColumns_ ( std::string_view dirPath,
std::string_view csvBaseName,
Size nbSamples,
const std::vector< std::string > & missingSymbols )
staticprotected

Scans every trajectory and returns the base names classified atemporal: those whose value never changes across the rows of a single trajectory (checked independently per trajectory, so the value may still differ between trajectories). Shared by every atemporal-inferring CSV constructor (KTBNLearner, KTBNAdaptiveLearner): identical scanning logic regardless of which subclass calls it, so duplicating it per subclass would only invite the two copies to drift. Callers are responsible for validating their own arguments (e.g. k/kMax >= 2) before calling this — it does no such check itself, only opens files, so a bad argument would otherwise only be caught after a wasted scan.

Column indices still "live" (not yet proven non-constant) are tracked as a shrinking list rather than rescanned from scratch, and scanning stops opening further trajectories once that list is empty — falsification is one-way, so nothing left to test can ever become atemporal again.

Definition at line 113 of file IKTBNLearner_tpl.h.

117 {
118 namespace fs = std::filesystem;
119 const fs::path dir{dirPath};
122
123 std::vector< std::string > header; // captured from trajectory 1
124 std::vector< std::size_t > candidates; // column indices not yet falsified; shrinks only
125 std::vector< std::string > firstSeen; // per column: first non-missing value THIS trajectory
127 haveFirstSeen; // per column: whether firstSeen[c] is set THIS trajectory
128
129 for (Size i = 0; i < nbSamples; ++i) {
130 // once every column has been falsified, nothing left to test can
131 // ever become atemporal again, so remaining trajectories are never
132 // even opened. Not part of the for-condition: `candidates` does not
133 // exist yet before trajectory 1 populates it.
134 if (i > 0 && candidates.empty()) break;
135
136 const fs::path file = dir / (stem + std::to_string(i + 1) + ".csv");
138 if (!is.is_open()) GUM_ERROR(IOError, "Cannot open " << file.string());
139
140 CSVParser parser(is, file.string());
141 parser.next();
142 const auto& raw = parser.current();
143
144 if (i == 0) {
145 header.assign(raw.begin(), raw.end());
146 candidates.resize(header.size());
147 std::iota(candidates.begin(), candidates.end(), std::size_t{0});
148 } else {
149 bool same = (raw.size() == header.size());
150 for (std::size_t c = 0; same && c < header.size(); ++c)
151 same = (raw[c] == header[c]);
152 if (!same)
154 "Header of " << file.string() << " differs from trajectory 1");
155 }
156
157 haveFirstSeen.assign(header.size(), false);
158 firstSeen.assign(header.size(), {});
159
160 while (parser.next()) {
161 const auto& tokens = parser.current();
162 if (tokens.size() != header.size())
164 "Trajectory " << (i + 1) << ", row " << parser.nbLine() << ": expected "
165 << header.size() << " columns, got " << tokens.size());
166 // iterate only the still-live candidates, swap-erasing any just
167 // falsified so later rows (and later trajectories) never revisit it
168 for (std::size_t idx = 0; idx < candidates.size();) {
169 const std::size_t c = candidates[idx];
170 if (missing.contains(tokens[c])) {
171 ++idx; // uninformative row for this column, still a candidate
172 continue;
173 }
174 if (!haveFirstSeen[c]) {
175 firstSeen[c] = tokens[c];
176 haveFirstSeen[c] = true;
177 ++idx;
178 } else if (tokens[c] != firstSeen[c]) {
179 candidates[idx] = candidates.back(); // falsified: drop, O(1)
180 candidates.pop_back();
181 } else {
182 ++idx;
183 }
184 }
185 if (candidates.empty()) break; // nothing left to test in this file either
186 }
187 }
188
190 for (const std::size_t c: candidates)
191 atemporalVars.insert(header[c]);
192 return atemporalVars;
193 }

References gum::learning::CSVParser::current(), GUM_ERROR, gum::learning::CSVParser::nbLine(), and gum::learning::CSVParser::next().

Referenced by gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_inferAtemporalVars_(), gum::learning::KTBNLearner< GUM_SCALAR >::_inferAtemporalVars_(), and _isKnownBase_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addForbiddenArc() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addForbiddenArc ( std::string_view tailBase,
int tailSlice,
std::string_view headBase,
int headSlice )
pure virtual

Forbid one (base, slice) -> (base, slice) arc (KTBN::ATEMPORAL for static).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addForbiddenArc() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addForbiddenArc ( std::string_view tailNode,
std::string_view headNode )
pure virtual

Forbid tailNode -> headNode (engine names, e.g. "X[1]", "C").

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addForbiddenArcAllSlices()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addForbiddenArcAllSlices ( std::string_view tailBase,
std::string_view headBase )
pure virtual

Forbid tailBase -> headBase at every causally-possible slice pair.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addForbiddenIntraSliceArc()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addForbiddenIntraSliceArc ( std::string_view tailBase,
std::string_view headBase )
pure virtual

Forbid tailBase -> headBase at every intra-slice position.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addMandatoryArc() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addMandatoryArc ( std::string_view tailBase,
int tailSlice,
std::string_view headBase,
int headSlice )
pure virtual

Force one arc, lag stated explicitly via slices (KTBN::ATEMPORAL for static).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addMandatoryArc() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addMandatoryArc ( std::string_view tailNode,
std::string_view headNode )
pure virtual

Force tailNode to be a parent of headNode (engine names).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addNoChildrenNode() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addNoChildrenNode ( std::string_view base,
int slice )
pure virtual

Declare a single (base, slice) node as a leaf (no children).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addNoChildrenNode() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addNoChildrenNode ( std::string_view name)
pure virtual

Declare a single node (bracket notation, e.g. "X[2]" or "C") as a leaf.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addNoParentNode() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addNoParentNode ( std::string_view base,
int slice )
pure virtual

Declare a single (base, slice) node as a root (no parents).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addNoParentNode() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addNoParentNode ( std::string_view name)
pure virtual

Declare a single node (bracket notation, e.g. "X[2]" or "C") as a root.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addPossibleEdge() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addPossibleEdge ( std::string_view tail,
std::string_view head )
pure virtual

Add a candidate edge for MIIC using engine names (e.g. "X[1]", "C").

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ addPossibleEdge() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::addPossibleEdge ( std::string_view tailBase,
int tailSlice,
std::string_view headBase,
int headSlice )
pure virtual

Add a candidate edge for MIIC (only edges explicitly listed are explored).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ allowArcAdditions()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::allowArcAdditions ( bool allow = true)
pure virtual

Allow or forbid arc additions during structure search.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ allowArcDeletions()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::allowArcDeletions ( bool allow = true)
pure virtual

Allow or forbid arc deletions during structure search.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ allowArcReversals()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::allowArcReversals ( bool allow = true)
pure virtual

Allow or forbid arc reversals during structure search.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseForbiddenArc() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseForbiddenArc ( std::string_view tailBase,
int tailSlice,
std::string_view headBase,
int headSlice )
pure virtual

Undo a previous addForbiddenArc for a specific (base, slice) pair.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseForbiddenArc() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseForbiddenArc ( std::string_view tailNode,
std::string_view headNode )
pure virtual

Undo a previous addForbiddenArc (engine names).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseForbiddenArcAllSlices()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseForbiddenArcAllSlices ( std::string_view tailBase,
std::string_view headBase )
pure virtual

Undo a previous addForbiddenArcAllSlices.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseForbiddenIntraSliceArc()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseForbiddenIntraSliceArc ( std::string_view tailBase,
std::string_view headBase )
pure virtual

Undo a previous addForbiddenIntraSliceArc.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseMandatoryArc() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseMandatoryArc ( std::string_view tailBase,
int tailSlice,
std::string_view headBase,
int headSlice )
pure virtual

◆ eraseMandatoryArc() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseMandatoryArc ( std::string_view tailNode,
std::string_view headNode )
pure virtual

Undo a previous addMandatoryArc (engine names).

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseNoChildrenNode() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseNoChildrenNode ( std::string_view base,
int slice )
pure virtual

Undo a previous addNoChildrenNode for a single (base, slice) node.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseNoChildrenNode() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseNoChildrenNode ( std::string_view name)
pure virtual

Undo addNoChildrenNode for a node given by bracket notation.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseNoParentNode() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseNoParentNode ( std::string_view base,
int slice )
pure virtual

Undo a previous addNoParentNode for a single (base, slice) node.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ eraseNoParentNode() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::eraseNoParentNode ( std::string_view name)
pure virtual

Undo addNoParentNode for a node given by bracket notation.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ erasePossibleEdge() [1/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::erasePossibleEdge ( std::string_view tail,
std::string_view head )
pure virtual

Undo a previous addPossibleEdge using engine names (e.g. "X[1]", "C").

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ erasePossibleEdge() [2/2]

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::erasePossibleEdge ( std::string_view tailBase,
int tailSlice,
std::string_view headBase,
int headSlice )
pure virtual

◆ learnKTBN()

template<GUM_Numeric GUM_SCALAR>
virtual KTBN< GUM_SCALAR > gum::learning::IKTBNLearner< GUM_SCALAR >::learnKTBN ( )
pure virtual

Learn a k-TBN (structure + CPTs) with the recorded configuration. The fixed-k learner learns for its single k; the adaptive learner also selects the best k in [2, kMax].

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ setMaxIndegree()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::setMaxIndegree ( Size max_indegree)
pure virtual

Cap the number of parents of any single node.

Implemented in gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >, and gum::learning::KTBNLearner< GUM_SCALAR >.

◆ useExtendedGreedyHillClimbing()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useExtendedGreedyHillClimbing ( )
pure virtual

◆ useGreedyHillClimbing()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useGreedyHillClimbing ( )
pure virtual

◆ useLocalSearchWithTabuList()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useLocalSearchWithTabuList ( Size tabu_size = 100,
Size nb_decrease = 2 )
pure virtual

◆ useMDLCorrection()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useMDLCorrection ( )
pure virtual

◆ useMIIC()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useMIIC ( )
pure virtual

◆ useNMLCorrection()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useNMLCorrection ( )
pure virtual

◆ useNoCorrection()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useNoCorrection ( )
pure virtual

◆ useScoreAIC()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useScoreAIC ( )
pure virtual

◆ useScoreBD()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useScoreBD ( )
pure virtual

◆ useScoreBDeu()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useScoreBDeu ( )
pure virtual

◆ useScoreBIC()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useScoreBIC ( )
pure virtual

◆ useScorefNML()

template<GUM_Numeric GUM_SCALAR>
virtual void gum::learning::IKTBNLearner< GUM_SCALAR >::useScorefNML ( )
pure virtual

◆ useScoreLog2Likelihood()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useScoreLog2Likelihood ( )
pure virtual

◆ useScoreMDL()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useScoreMDL ( )
pure virtual

◆ useSmoothingPrior()

template<GUM_Numeric GUM_SCALAR>
virtual IKTBNLearner< GUM_SCALAR > & gum::learning::IKTBNLearner< GUM_SCALAR >::useSmoothingPrior ( double weight = 1.0)
pure virtual

The documentation for this class was generated from the following files: