![]() |
aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
|
Class representing a k-order dynamic Bayesian network (k-DBN). More...
#include <agrum/KTBN/KTBN.h>
Public Member Functions | |
Constructors and Destructor | |
| KTBN (Size k=2) | |
| Default constructor. | |
| virtual | ~KTBN () |
| Destructor. | |
| KTBN (const KTBN< GUM_SCALAR > &source) | |
| Copy constructor. | |
| KTBN (KTBN< GUM_SCALAR > &&source) noexcept | |
| Move constructor. | |
| KTBN< GUM_SCALAR > & | operator= (const KTBN< GUM_SCALAR > &source) |
| Copy assignment operator. | |
| KTBN< GUM_SCALAR > & | operator= (KTBN< GUM_SCALAR > &&source) noexcept |
| Move assignment operator. | |
Accessors | |
| Size | k () const |
| Size | size () const |
| Size | sizeArcs () const |
| bool | empty () const |
| void | clear () |
| Removes all variables and arcs, keeping the order \(k\). | |
Variable management | |
| void | add (const DiscreteVariable &var, bool temporal=true) |
| Adds a variable to the k-DBN. | |
| void | add (std::string_view fast_description, bool temporal=true, unsigned int default_nbrmod=2) |
| Adds a variable using the "fast" textual description syntax. | |
| void | addTemporal (const DiscreteVariable &var) |
| Convenience shortcut for add(var, true). | |
| void | addAtemporal (const DiscreteVariable &var) |
| Convenience shortcut for add(var, false). | |
| void | addTemporal (std::string_view fast_description, unsigned int default_nbrmod=2) |
| Convenience shortcut for add(fast_description, true, default_nbrmod). | |
| void | addAtemporal (std::string_view fast_description, unsigned int default_nbrmod=2) |
| Convenience shortcut for add(fast_description, false, default_nbrmod). | |
| void | erase (std::string_view base) |
| Removes a variable and all its incident arcs. | |
| void | changeVariableName (std::string_view oldBase, std::string_view newBase) |
| Renames a variable (temporal process or atemporal variable). | |
Variable queries | |
| bool | exists (std::string_view base) const |
| const std::unordered_set< std::string > & | temporalVarNames () const |
| const std::unordered_set< std::string > & | atemporalVarNames () const |
| Size | nbTemporalVars () const |
| Size | nbAtemporalVars () const |
| std::vector< std::pair< std::string, int > > | nodes () const |
| std::vector< std::pair< std::string, int > > | parents (std::string_view base, int slice) const |
Parents of a node as (base, slice) pairs (ATEMPORAL if atemporal). | |
| std::vector< std::pair< std::string, int > > | parents (std::string_view node_name) const |
Returns the parents using an engine name ("X[1]", "C", …). | |
| std::vector< std::pair< std::string, int > > | children (std::string_view base, int slice) const |
Children of a node as (base, slice) pairs (ATEMPORAL if atemporal). | |
| std::vector< std::pair< std::string, int > > | children (std::string_view node_name) const |
Returns the children using an engine name ("X[1]", "C", …). | |
| const DiscreteVariable & | variable (std::string_view base, int slice) const |
| Returns the gum::DiscreteVariable of a (process, slice) couple. | |
| const DiscreteVariable & | variable (std::string_view node_name) const |
Same, using an engine name: resolved via _determineNode_, so "X[1]" and bare "C" are both accepted. | |
| int | timeSlice (const DiscreteVariable &var) const |
The time slice of var, or ATEMPORAL if it is atemporal. | |
| std::string | baseName (const DiscreteVariable &var) const |
Returns the base name (without bracket encoding) of var. | |
Arc management | |
| void | addArc (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice) |
| Adds an arc between two (process, slice) endpoints. | |
| void | addArc (std::string_view tail, std::string_view head) |
Adds an arc using internal engine names (bracket notation: "X[0]", "C", …). | |
| void | eraseArc (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice) |
| Removes an arc between two (process, slice) endpoints. | |
| void | eraseArc (std::string_view tail, std::string_view head) |
| Removes an arc using internal engine names (bracket notation). | |
| bool | existsArc (std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice) const |
| bool | existsArc (std::string_view tail, std::string_view head) const |
| std::vector< std::pair< std::pair< std::string, int >, std::pair< std::string, int > > > | arcs () const |
Conditional probability tables | |
| const Tensor< GUM_SCALAR > & | cpt (std::string_view base, int slice) const |
| Returns the CPT of a (process, slice) couple. | |
| const Tensor< GUM_SCALAR > & | cpt (std::string_view node_name) const |
Returns the CPT using an engine name ("X[1]", "C", …). | |
| void | fillCPT (std::string_view base, int slice, const std::map< std::pair< std::string, int >, KTBNModality > &parents, const std::vector< GUM_SCALAR > &distribution) const |
| Fills one conditional distribution P(node | parent configuration). | |
| void | fillCPT (std::string_view node_name, const std::map< std::variant< std::string, std::pair< std::string, int > >, KTBNModality > &parents, const std::vector< GUM_SCALAR > &distribution) const |
Fills one conditional distribution using internal bracket-notation engine name for the target node — the bracket notation visible in toDot() and toString(): "X[t]" for a temporal variable at slice t, and the bare variable name for an atemporal variable (e.g. | |
| void | generateCPTs () const |
| Randomly generates every CPT of the template. | |
| void | generateCPT (std::string_view base, int slice) const |
| Randomly generates the CPT of a single node. | |
| void | generateCPT (std::string_view node_name) const |
Same, using an engine name ("X[1]", "C", …). | |
Transformations | |
| BayesNet< GUM_SCALAR > | toBN () const |
| BayesNet< GUM_SCALAR > | unroll (Size nbTimeSlices) const |
| Unrolls the k-DBN into a standard gum::BayesNet. | |
Various | |
| std::string | toString () const |
| std::string | toDot () const |
| Returns a Graphviz DOT string with one cluster per time slice. | |
| std::string | toUnrolledDot (Size T, bool highlightReplicated=false) const |
Returns a Graphviz DOT string of the k-DBN unrolled over T time slices. | |
| std::string | bnToDot () const |
| Returns the Graphviz DOT string of the underlying storage BayesNet. | |
| std::string | summaryGraph () const |
| Returns the Graphviz DOT string of the summary graph: the projection of the transition kernel alone (the pattern that actually repeats through time when unrolling), not the whole template. | |
Static Public Attributes | |
| static constexpr int | ATEMPORAL = -1 |
| Conventional time-slice value denoting an atemporal (static) variable. | |
Private Member Functions | |
| std::string | _encode_ (std::string_view base, int slice) const |
| Encodes (base, slice) → engine name: base[t], or base if atemporal. | |
| std::pair< std::string, int > | _decodeName_ (std::string_view name) const |
| Purely syntactic parse of an engine name → (base, slice). Slice is ATEMPORAL when there is no [digits] suffix. Does not consult the cached name sets, so a bracket-named atemporal node decodes as temporal here. | |
| std::pair< std::string, int > | _determineNode_ (const std::string &name) const |
Cache-aware classification of a node name → (base, slice): nodes registered in _atemporal_ (atemporal and orphan-bracket) map to ATEMPORAL, every other name is parsed by decodeName. | |
| std::vector< std::pair< std::string, int > > | _determineNodeSet_ (const NodeSet &ids) const |
| Maps a set of node ids to (base, slice) pairs (via determineNode). | |
| NodeId | _validateVariable_ (std::string_view base, int slice) const |
| Resolves and validates a (base, slice) endpoint into its NodeId. | |
| void | _validateAdd_ (const std::string &base, bool temporal) const |
Checks that a variable named base can be added. | |
| void | _determineNodesFromBN_ (const std::unordered_set< std::string > &atemporalNodes, std::vector< std::string > *warnings) |
| Rebuilds the cached name sets from the storage engine content (used by fromBN()/load(); decodes names once). | |
| std::string | _timeSlicesToDot_ (const BayesNet< GUM_SCALAR > &bn, bool highlightReplicated) const |
Renders bn as time-slice-clustered DOT. Shared engine behind toDot() (on _bn_) and toUnrolledDot() (on unroll(T)). | |
Static Private Member Functions | |
| static std::pair< std::string, bool > | _resolveGumFormat_ (std::string_view filename) |
| Resolves a user filename to (filepath, binary): ensures a .jgum/.bgum extension (.bgum appended by default) and reports whether the format is binary. Single source of truth for the GUM format convention shared by save() and load(). | |
| static std::string | _escapeDot_ (std::string_view name) |
| Escapes double quotes for a DOT identifier or label. Shared by timeSlicesToDot() and summaryGraph(). | |
Private Attributes | |
| Size | _k_ |
| The order (number of time slices in the template). | |
| BayesNet< GUM_SCALAR > | _bn_ |
| The underlying Bayesian network used as a storage engine for the template. | |
| std::unordered_set< std::string > | _temporal_ |
| Base names of the registered temporal processes. | |
| std::unordered_set< std::string > | _atemporal_ |
| Base names of the registered atemporal variables. | |
Persistence and conversion | |
| void | save (std::string_view filename) const |
| Saves the template in the GUM format (text .jgum or binary .bgum). | |
| static KTBN< GUM_SCALAR > | load (std::string_view filename) |
| Loads a k-DBN from a GUM file produced by save(). | |
| static KTBN< GUM_SCALAR > | fromBN (const BayesNet< GUM_SCALAR > &bn, const std::unordered_set< std::string > &atemporalNodes={}, std::vector< std::string > *warnings=nullptr) |
| Builds a k-DBN from an existing gum::BayesNet, reading its node names under one of two mutually exclusive conventions. | |
Class representing a k-order dynamic Bayesian network (k-DBN).
A k-order dynamic Bayesian network (k-DBN, sometimes called a k-Time-slice Bayesian Network or k-TBN) generalizes the classical 2-TBN: the conditional distribution of a variable at time \(t\) may depend on the \(k\) most recent time slices \(t, t-1, \ldots, t-k+1\) instead of a single step backward.
Rather than storing an (infinite) unrolled network, a KTBN only stores a template made of exactly \(k\) time slices. This template captures both:
Two kinds of random variables are distinguished:
base is named base[t] (e.g. "X[0]", "X[1]", ...). Atemporal variables keep their bare name. The class holds no gum::NodeId index table: a (process, slice) couple is resolved to its node by encoding it and asking the engine, and a node's slice is recovered by decoding its name. The public API is always expressed in terms of (process, slice) couples; the bracket encoding is an internal detail."X[0]" is forbidden when a temporal process "X" exists (and vice-versa), because both would map to the same BN node "X[0]". The reservation covers every bracket-suffixed name over an existing process, not only the slices that process actually holds: "X[999]" is refused for \(k=3\) as well, since the slice index is parsed without an upper bound and such a name would otherwise shadow the process. add() and changeVariableName() enforce these rules and throw DuplicateLabel or InvalidArgument on violation.Default constructor.
| k | The order of the k-DBN, i.e. the number of time slices stored in the template. Must be \(\geq 1\). |
| InvalidArgument | if k is 0. |
Definition at line 131 of file KTBN_tpl.h.
References KTBN(), _k_, GUM_ERROR, and k().
Referenced by KTBN(), KTBN(), KTBN(), ~KTBN(), operator=(), and operator=().
Destructor.
Definition at line 137 of file KTBN_tpl.h.
References KTBN().
| gum::KTBN< GUM_SCALAR >::KTBN | ( | const KTBN< GUM_SCALAR > & | source | ) |
Copy constructor.
Definition at line 142 of file KTBN_tpl.h.
References KTBN(), _atemporal_, _bn_, _k_, and _temporal_.
|
noexcept |
Move constructor.
Definition at line 149 of file KTBN_tpl.h.
References KTBN(), _atemporal_, _bn_, _k_, and _temporal_.
|
private |
Purely syntactic parse of an engine name → (base, slice). Slice is ATEMPORAL when there is no [digits] suffix. Does not consult the cached name sets, so a bracket-named atemporal node decodes as temporal here.
Definition at line 84 of file KTBN_tpl.h.
References ATEMPORAL, and GUM_ERROR.
Referenced by _determineNode_(), _determineNodesFromBN_(), _timeSlicesToDot_(), _validateAdd_(), changeVariableName(), and summaryGraph().
|
private |
Cache-aware classification of a node name → (base, slice): nodes registered in _atemporal_ (atemporal and orphan-bracket) map to ATEMPORAL, every other name is parsed by decodeName.
Definition at line 109 of file KTBN_tpl.h.
References _atemporal_, _decodeName_(), and ATEMPORAL.
Referenced by _determineNodeSet_(), _determineNodesFromBN_(), addArc(), arcs(), baseName(), children(), cpt(), eraseArc(), existsArc(), generateCPT(), parents(), timeSlice(), and variable().
|
private |
Maps a set of node ids to (base, slice) pairs (via determineNode).
Definition at line 118 of file KTBN_tpl.h.
References _bn_, _determineNode_(), and gum::Set< Key >::size().
Referenced by children(), and parents().
|
private |
Rebuilds the cached name sets from the storage engine content (used by fromBN()/load(); decodes names once).
Definition at line 890 of file KTBN_tpl.h.
References _atemporal_, _bn_, _decodeName_(), _determineNode_(), _encode_(), _k_, _temporal_, ATEMPORAL, gum::HashTable< Key, Val >::cbegin(), gum::HashTable< Key, Val >::cend(), gum::DiscreteVariable::clone(), gum::DiscreteVariable::domain(), gum::HashTable< Key, Val >::exists(), exists(), GUM_ERROR, gum::HashTable< Key, Val >::insert(), and gum::Variable::name().
|
private |
Encodes (base, slice) → engine name: base[t], or base if atemporal.
Definition at line 78 of file KTBN_tpl.h.
References ATEMPORAL.
Referenced by _determineNodesFromBN_(), _timeSlicesToDot_(), _validateAdd_(), _validateVariable_(), add(), changeVariableName(), erase(), fillCPT(), and unroll().
|
staticprivate |
Escapes double quotes for a DOT identifier or label. Shared by timeSlicesToDot() and summaryGraph().
Definition at line 1131 of file KTBN_tpl.h.
Referenced by _timeSlicesToDot_(), and summaryGraph().
|
staticprivate |
Resolves a user filename to (filepath, binary): ensures a .jgum/.bgum extension (.bgum appended by default) and reports whether the format is binary. Single source of truth for the GUM format convention shared by save() and load().
Definition at line 786 of file KTBN_tpl.h.
Referenced by load(), and save().
|
private |
Renders bn as time-slice-clustered DOT. Shared engine behind toDot() (on _bn_) and toUnrolledDot() (on unroll(T)).
Definition at line 1146 of file KTBN_tpl.h.
References _decodeName_(), _encode_(), _escapeDot_(), _k_, ATEMPORAL, and nodes().
Referenced by toDot(), and toUnrolledDot().
|
private |
Checks that a variable named base can be added.
| temporal | true for a temporal process, false for an atemporal variable. |
| DuplicateLabel | / InvalidArgument on a name collision (see add()). |
Definition at line 215 of file KTBN_tpl.h.
References _atemporal_, _decodeName_(), _encode_(), _k_, _temporal_, ATEMPORAL, and GUM_ERROR.
Referenced by add().
|
private |
Resolves and validates a (base, slice) endpoint into its NodeId.
Definition at line 434 of file KTBN_tpl.h.
References _atemporal_, _bn_, _encode_(), _k_, _temporal_, ATEMPORAL, and GUM_ERROR.
Referenced by addArc(), children(), cpt(), eraseArc(), existsArc(), fillCPT(), generateCPT(), parents(), and variable().
| void gum::KTBN< GUM_SCALAR >::add | ( | const DiscreteVariable & | var, |
| bool | temporal = true ) |
Adds a variable to the k-DBN.
A temporal variable (process) is cloned into \(k\) instances (one per slice); an atemporal variable is added once.
| var | The variable to add (added by copy). |
| temporal | Whether the variable is temporal. |
| DuplicateLabel | if a variable with that base name already exists (any kind — base names are globally unique). |
| InvalidArgument | if a temporal base[t] would collide with an existing atemporal node of that encoded name, or vice-versa. |
Definition at line 264 of file KTBN_tpl.h.
References _atemporal_, _bn_, _encode_(), _k_, _temporal_, _validateAdd_(), gum::DiscreteVariable::clone(), and gum::Variable::name().
Referenced by add(), addAtemporal(), addAtemporal(), addTemporal(), and addTemporal().
| void gum::KTBN< GUM_SCALAR >::add | ( | std::string_view | fast_description, |
| bool | temporal = true, | ||
| unsigned int | default_nbrmod = 2 ) |
Adds a variable using the "fast" textual description syntax.
Definition at line 284 of file KTBN_tpl.h.
References add(), and gum::fastVariable().
| void gum::KTBN< GUM_SCALAR >::addArc | ( | std::string_view | tail, |
| std::string_view | head ) |
Adds an arc using internal engine names (bracket notation: "X[0]", "C", …).
| NotFound | if an endpoint name is not in the template. |
| OutOfBounds | / OperationNotAllowed / DuplicateElement / InvalidDirectedCycle — same conditions as the (base, slice) overload. |
Definition at line 529 of file KTBN_tpl.h.
References _determineNode_(), and addArc().
| void gum::KTBN< GUM_SCALAR >::addArc | ( | std::string_view | tailBase, |
| int | tailSlice, | ||
| std::string_view | headBase, | ||
| int | headSlice ) |
Adds an arc between two (process, slice) endpoints.
| tailBase | Base name of the tail variable. |
| tailSlice | Slice of the tail (KTBN::ATEMPORAL for an atemporal tail). |
| headBase | Base name of the head variable. |
| headSlice | Slice of the head (KTBN::ATEMPORAL for an atemporal head). |
| NotFound | if an endpoint does not exist. |
| OutOfBounds | if a slice argument is out of \([0,k-1]\) for a temporal endpoint. |
| OperationNotAllowed | if the arc violates temporal causality (a temporal variable pointing to an atemporal one, or an arc from a future slice to a past slice). |
| DuplicateElement | if the arc already exists. |
| InvalidDirectedCycle | if the arc would create a cycle. |
Definition at line 490 of file KTBN_tpl.h.
References _bn_, _validateVariable_(), ATEMPORAL, and GUM_ERROR.
Referenced by addArc().
| INLINE void gum::KTBN< GUM_SCALAR >::addAtemporal | ( | const DiscreteVariable & | var | ) |
Convenience shortcut for add(var, false).
Definition at line 297 of file KTBN_tpl.h.
References add().
| INLINE void gum::KTBN< GUM_SCALAR >::addAtemporal | ( | std::string_view | fast_description, |
| unsigned int | default_nbrmod = 2 ) |
Convenience shortcut for add(fast_description, false, default_nbrmod).
Definition at line 308 of file KTBN_tpl.h.
References add().
| INLINE void gum::KTBN< GUM_SCALAR >::addTemporal | ( | const DiscreteVariable & | var | ) |
Convenience shortcut for add(var, true).
Definition at line 292 of file KTBN_tpl.h.
References add().
| INLINE void gum::KTBN< GUM_SCALAR >::addTemporal | ( | std::string_view | fast_description, |
| unsigned int | default_nbrmod = 2 ) |
Convenience shortcut for add(fast_description, true, default_nbrmod).
Definition at line 302 of file KTBN_tpl.h.
References add().
| std::vector< std::pair< std::pair< std::string, int >, std::pair< std::string, int > > > gum::KTBN< GUM_SCALAR >::arcs | ( | ) | const |
Definition at line 551 of file KTBN_tpl.h.
References _bn_, and _determineNode_().
Referenced by toString().
| INLINE const std::unordered_set< std::string > & gum::KTBN< GUM_SCALAR >::atemporalVarNames | ( | ) | const |
Definition at line 329 of file KTBN_tpl.h.
References _atemporal_.
| INLINE std::string gum::KTBN< GUM_SCALAR >::baseName | ( | const DiscreteVariable & | var | ) | const |
Returns the base name (without bracket encoding) of var.
| NotFound | if var is not a node of this k-DBN. |
Definition at line 480 of file KTBN_tpl.h.
References _bn_, _determineNode_(), and gum::Variable::name().
| INLINE std::string gum::KTBN< GUM_SCALAR >::bnToDot | ( | ) | const |
Returns the Graphviz DOT string of the underlying storage BayesNet.
Nodes are labelled with their internal engine names (bracket notation: base[t] for temporal nodes, bare name for atemporal nodes). No time-slice clustering is applied.
Definition at line 1214 of file KTBN_tpl.h.
References _bn_.
| void gum::KTBN< GUM_SCALAR >::changeVariableName | ( | std::string_view | oldBase, |
| std::string_view | newBase ) |
Renames a variable (temporal process or atemporal variable).
The kind is determined automatically from the registered name sets. For a temporal process all \(k\) slice names are re-encoded with the new base.
| NotFound | if no variable named oldBase exists. |
| DuplicateLabel | if a variable named newBase already exists (any kind). |
| InvalidArgument | if newBase is empty or would create a BN node-name collision. |
Definition at line 398 of file KTBN_tpl.h.
References _atemporal_, _bn_, _decodeName_(), _encode_(), _k_, _temporal_, ATEMPORAL, and GUM_ERROR.
| std::vector< std::pair< std::string, int > > gum::KTBN< GUM_SCALAR >::children | ( | std::string_view | base, |
| int | slice ) const |
Children of a node as (base, slice) pairs (ATEMPORAL if atemporal).
Definition at line 369 of file KTBN_tpl.h.
References _bn_, _determineNodeSet_(), and _validateVariable_().
Referenced by children().
| std::vector< std::pair< std::string, int > > gum::KTBN< GUM_SCALAR >::children | ( | std::string_view | node_name | ) | const |
Returns the children using an engine name ("X[1]", "C", …).
Definition at line 376 of file KTBN_tpl.h.
References _determineNode_(), and children().
| void gum::KTBN< GUM_SCALAR >::clear | ( | ) |
Removes all variables and arcs, keeping the order \(k\).
Definition at line 204 of file KTBN_tpl.h.
References _atemporal_, _bn_, and _temporal_.
| INLINE const Tensor< GUM_SCALAR > & gum::KTBN< GUM_SCALAR >::cpt | ( | std::string_view | base, |
| int | slice ) const |
Returns the CPT of a (process, slice) couple.
The returned reference is const but its content is mutable — use any standard gum::Tensor method to fill it:
Values for the vector overload are in the CPT's natural iteration order: the node's own variable varies fastest, parents follow in the order they appear in cpt().variable(1..n). Use cpt().variable(i).name() to inspect the ordering before filling.
Definition at line 566 of file KTBN_tpl.h.
References _bn_, and _validateVariable_().
Referenced by cpt(), fillCPT(), and fillCPT().
| INLINE const Tensor< GUM_SCALAR > & gum::KTBN< GUM_SCALAR >::cpt | ( | std::string_view | node_name | ) | const |
Returns the CPT using an engine name ("X[1]", "C", …).
Definition at line 572 of file KTBN_tpl.h.
References _determineNode_(), and cpt().
true if the template contains no variable. Definition at line 199 of file KTBN_tpl.h.
References _bn_.
| void gum::KTBN< GUM_SCALAR >::erase | ( | std::string_view | base | ) |
Removes a variable and all its incident arcs.
The kind (temporal or atemporal) is determined automatically from the registered name sets. For a temporal process all \(k\) slice nodes are removed.
| NotFound | if no variable with that name exists. |
Definition at line 382 of file KTBN_tpl.h.
References _atemporal_, _bn_, _encode_(), _k_, _temporal_, and GUM_ERROR.
| void gum::KTBN< GUM_SCALAR >::eraseArc | ( | std::string_view | tail, |
| std::string_view | head ) |
Removes an arc using internal engine names (bracket notation).
| NotFound | if an endpoint name is unknown or the arc does not exist. |
Definition at line 536 of file KTBN_tpl.h.
References _determineNode_(), and eraseArc().
| void gum::KTBN< GUM_SCALAR >::eraseArc | ( | std::string_view | tailBase, |
| int | tailSlice, | ||
| std::string_view | headBase, | ||
| int | headSlice ) |
Removes an arc between two (process, slice) endpoints.
The arc must exist: erasing an absent arc throws (via the underlying CPT update), even when both endpoints are valid variables.
| NotFound | / OperationNotAllowed / OutOfBounds if an endpoint variable does not exist (same rules as addArc()), or if the arc itself does not exist. |
Definition at line 512 of file KTBN_tpl.h.
References _bn_, and _validateVariable_().
Referenced by eraseArc().
| INLINE bool gum::KTBN< GUM_SCALAR >::exists | ( | std::string_view | base | ) | const |
true if a variable with this base name exists. Definition at line 318 of file KTBN_tpl.h.
References _atemporal_, and _temporal_.
Referenced by _determineNodesFromBN_().
| bool gum::KTBN< GUM_SCALAR >::existsArc | ( | std::string_view | tail, |
| std::string_view | head ) const |
true if the arc exists; uses internal engine names (bracket notation). | NotFound | if an endpoint name is unknown. |
Definition at line 543 of file KTBN_tpl.h.
References _determineNode_(), and existsArc().
| bool gum::KTBN< GUM_SCALAR >::existsArc | ( | std::string_view | tailBase, |
| int | tailSlice, | ||
| std::string_view | headBase, | ||
| int | headSlice ) const |
true if the arc exists in the template. Definition at line 520 of file KTBN_tpl.h.
References _bn_, and _validateVariable_().
Referenced by existsArc().
| void gum::KTBN< GUM_SCALAR >::fillCPT | ( | std::string_view | base, |
| int | slice, | ||
| const std::map< std::pair< std::string, int >, KTBNModality > & | parents, | ||
| const std::vector< GUM_SCALAR > & | distribution ) const |
Fills one conditional distribution P(node | parent configuration).
The order-safe, bracket-free way to fill a CPT: the node and its parents are addressed by their (base, slice) identity, so the result does not depend on the positional order of cpt().fillWith({...}).
Each parent is a dictionary entry keyed by its (base, slice) identity (use ATEMPORAL as slice for a static parent). ALL parents of the node must be listed, in any order. distribution holds the probabilities over the node's own modalities for that parent configuration.
A parent's value is either a modality index or a modality label, and the two may be mixed — see gum::KTBNModality, whose warning on numeric modalities applies here.
| base | Base name of the target node. |
| slice | Slice of the target node (ATEMPORAL for static). |
| parents | One (base, slice) -> value entry per parent, any order. |
| distribution | Probabilities over the node's modalities; its size must equal the node's domain size. |
| NotFound | / OutOfBounds if the node or a parent does not exist, if a parent index is out of range, or if a parent label is not one of that parent's modalities (which of the two is raised for an unknown label depends on the variable type). |
| SizeError | if distribution size differs from the node domain size, or if not every parent is specified. |
| InvalidArgument | if an entry's node is not a parent of the target. |
Definition at line 594 of file KTBN_tpl.h.
References _bn_, _validateVariable_(), gum::Instantiation::chgVal(), cpt(), gum::DiscreteVariable::domainSize(), GUM_ERROR, and parents().
| void gum::KTBN< GUM_SCALAR >::fillCPT | ( | std::string_view | node_name, |
| const std::map< std::variant< std::string, std::pair< std::string, int > >, KTBNModality > & | parents, | ||
| const std::vector< GUM_SCALAR > & | distribution ) const |
Fills one conditional distribution using internal bracket-notation engine name for the target node — the bracket notation visible in toDot() and toString(): "X[t]" for a temporal variable at slice t, and the bare variable name for an atemporal variable (e.g.
"C"). Each parent is keyed either by its engine name ("X[0]") or by its (base, slice) identity — both spellings may be mixed in the same dictionary.
std::pair{...}: std::variant's converting constructor takes one value convertible to an alternative, not a nested brace list, so a bare {"X", 0} cannot implicitly construct the pair alternative the way it would a std::pair or std::tuple element.As in the (base, slice) overload, a parent's value is either a modality index or a modality label — see gum::KTBNModality.
| node_name | Internal engine name of the target node. |
| parents | One engine_name-or-(base,slice) -> value entry per parent, any order. |
| distribution | Probabilities over the node's modalities; its size must equal the node's domain size. |
| NotFound | if node_name or a parent does not exist. |
| SizeError | / InvalidArgument / OutOfBounds — same conditions as the (base, slice) overload. |
Definition at line 637 of file KTBN_tpl.h.
References _bn_, _encode_(), gum::Instantiation::chgVal(), gum::Set< Key >::contains(), cpt(), gum::DiscreteVariable::domainSize(), GUM_ERROR, gum::Set< Key >::insert(), and parents().
|
static |
Builds a k-DBN from an existing gum::BayesNet, reading its node names under one of two mutually exclusive conventions.
If any node name already carries the engine's own bracket notation (base[t]), the whole network is read that way: exactly the legacy behaviour, temporal node names assumed canonical (a plain decimal slice, no leading zeros – the form produced by add() / unroll() / toBN()). Otherwise – no node name carries a bracket at all – every node is read under the bracket-free convention: a name ending in a run of digits denotes a temporal variable at the timeslice given by that integer (base = everything before the digits, e.g. "X0" and "X12" both belong to process "X", at slices 0 and 12); any other name is atemporal.
The order \(k\) is inferred as one plus the largest slice index found. A group of same-base nodes that does not cover every slice \(0..k-1\) is not an error: each of its nodes becomes an atemporal variable, original name kept, and a message is appended to warnings. When no temporal process survives, \(k\) falls back to 1.
| bn | The source Bayesian network (copied). |
| atemporalNodes | Node names – exactly as they appear in bn – to classify atemporal outright. Only needed to lift an ambiguity: a name the active convention already reads as atemporal changes nothing by being listed, while listing a temporal-shaped name says "I meant this atemporally" where the reclassification above would only guess (and warn). |
| warnings | If non-null, receives one message per reclassified group. |
| NotFound | if a name in atemporalNodes is not a node of bn. |
| OperationNotAllowed | if the temporal structure is inconsistent: two variables mapping to the same (process, slice), a base name used both as an atemporal variable and as a temporal process, an arc from a temporal node into an atemporal one, or an arc from the future to the past. |
"X0" alone under the bracket-free convention, or "X[0]" alone under the bracket one) yields \(k=1\), where a single slice is a complete process: it is kept temporal, silently. List it in atemporalNodes to say otherwise. Definition at line 880 of file KTBN_tpl.h.
Referenced by gum::learning::KTBNLearner< GUM_SCALAR >::_assemble_(), and load().
| INLINE void gum::KTBN< GUM_SCALAR >::generateCPT | ( | std::string_view | base, |
| int | slice ) const |
Randomly generates the CPT of a single node.
Definition at line 583 of file KTBN_tpl.h.
References _bn_, and _validateVariable_().
Referenced by generateCPT().
| INLINE void gum::KTBN< GUM_SCALAR >::generateCPT | ( | std::string_view | node_name | ) | const |
Same, using an engine name ("X[1]", "C", …).
Definition at line 588 of file KTBN_tpl.h.
References _determineNode_(), and generateCPT().
| INLINE void gum::KTBN< GUM_SCALAR >::generateCPTs | ( | ) | const |
Randomly generates every CPT of the template.
Definition at line 578 of file KTBN_tpl.h.
References _bn_.
Definition at line 184 of file KTBN_tpl.h.
References _k_.
Referenced by KTBN().
|
static |
Loads a k-DBN from a GUM file produced by save().
Text iff the name ends with .jgum, otherwise binary (gaining a .bgum extension if missing). If the file carries the KTBN classification properties it is restored exactly; otherwise it is re-derived from node names via fromBN().
| filename | The GUM file. |
| IOError | if the file cannot be read or is not valid. |
| OperationNotAllowed | if the classification properties are absent and the fallback fromBN() finds an inconsistent temporal structure. |
Definition at line 824 of file KTBN_tpl.h.
References _resolveGumFormat_(), fromBN(), GUM_ERROR, gum::GumBNReader< GUM_SCALAR >::proceed(), gum::GumBNReader< GUM_SCALAR >::showElegantErrorsAndWarnings(), gum::GumBNReader< GUM_SCALAR >::showErrorCounts(), and gum::split().
Definition at line 339 of file KTBN_tpl.h.
References _atemporal_.
Definition at line 334 of file KTBN_tpl.h.
References _temporal_.
| std::vector< std::pair< std::string, int > > gum::KTBN< GUM_SCALAR >::nodes | ( | ) | const |
Definition at line 344 of file KTBN_tpl.h.
References _atemporal_, _k_, _temporal_, ATEMPORAL, and size().
Referenced by _timeSlicesToDot_().
| KTBN< GUM_SCALAR > & gum::KTBN< GUM_SCALAR >::operator= | ( | const KTBN< GUM_SCALAR > & | source | ) |
Copy assignment operator.
Definition at line 156 of file KTBN_tpl.h.
References KTBN(), _atemporal_, _bn_, _k_, and _temporal_.
|
noexcept |
Move assignment operator.
Definition at line 168 of file KTBN_tpl.h.
References KTBN(), _atemporal_, _bn_, _k_, and _temporal_.
| std::vector< std::pair< std::string, int > > gum::KTBN< GUM_SCALAR >::parents | ( | std::string_view | base, |
| int | slice ) const |
Parents of a node as (base, slice) pairs (ATEMPORAL if atemporal).
Definition at line 356 of file KTBN_tpl.h.
References _bn_, _determineNodeSet_(), and _validateVariable_().
Referenced by fillCPT(), fillCPT(), parents(), and unroll().
| std::vector< std::pair< std::string, int > > gum::KTBN< GUM_SCALAR >::parents | ( | std::string_view | node_name | ) | const |
Returns the parents using an engine name ("X[1]", "C", …).
Definition at line 363 of file KTBN_tpl.h.
References _determineNode_(), and parents().
| void gum::KTBN< GUM_SCALAR >::save | ( | std::string_view | filename | ) | const |
Saves the template in the GUM format (text .jgum or binary .bgum).
The extension selects the format: .jgum writes the text (JSON) variant, anything else the binary (msgpack) one, gaining a .bgum extension if missing. The temporal/atemporal classification and the order \(k\) are stored as properties, so load() reconstructs the k-DBN exactly — including k=1 processes and bracket-named atemporal variables, which fromBN() cannot disambiguate from node names alone.
| filename | The destination file. |
Definition at line 796 of file KTBN_tpl.h.
References _atemporal_, _bn_, _k_, _resolveGumFormat_(), _temporal_, and gum::BNWriter< GUM_SCALAR >::write().
Definition at line 189 of file KTBN_tpl.h.
References _bn_.
Referenced by nodes().
Definition at line 194 of file KTBN_tpl.h.
References _bn_.
| std::string gum::KTBN< GUM_SCALAR >::summaryGraph | ( | ) | const |
Returns the Graphviz DOT string of the summary graph: the projection of the transition kernel alone (the pattern that actually repeats through time when unrolling), not the whole template.
Every process (temporal or atemporal) becomes a single node. Only arcs whose head lies in the last time slice ( \(k-1\)) are kept – an arc between two earlier slices belongs to the initial-condition structure (slices \(0, \ldots, k-2\)), not to the repeated pattern, and is dropped. Several arcs may connect the same pair of nodes when the kernel depends on more than one lag (e.g. both X[k-2] and X[k-3] pointing to X[k-1]): each is kept and labelled with its lag (head slice minus tail slice). An arc from an atemporal variable has no lag and is left unlabelled.
Definition at line 1219 of file KTBN_tpl.h.
References _atemporal_, _bn_, _decodeName_(), _escapeDot_(), _k_, _temporal_, and ATEMPORAL.
| INLINE const std::unordered_set< std::string > & gum::KTBN< GUM_SCALAR >::temporalVarNames | ( | ) | const |
Definition at line 324 of file KTBN_tpl.h.
References _temporal_.
| INLINE int gum::KTBN< GUM_SCALAR >::timeSlice | ( | const DiscreteVariable & | var | ) | const |
The time slice of var, or ATEMPORAL if it is atemporal.
| NotFound | if var is not a node of this k-DBN. |
Definition at line 474 of file KTBN_tpl.h.
References _bn_, _determineNode_(), and gum::Variable::name().
Definition at line 692 of file KTBN_tpl.h.
References _bn_.
| std::string gum::KTBN< GUM_SCALAR >::toDot | ( | ) | const |
Returns a Graphviz DOT string with one cluster per time slice.
Definition at line 1119 of file KTBN_tpl.h.
References _bn_, and _timeSlicesToDot_().
| std::string gum::KTBN< GUM_SCALAR >::toString | ( | ) | const |
Definition at line 1084 of file KTBN_tpl.h.
References _atemporal_, _bn_, _k_, _temporal_, and arcs().
| std::string gum::KTBN< GUM_SCALAR >::toUnrolledDot | ( | Size | T, |
| bool | highlightReplicated = false ) const |
Returns a Graphviz DOT string of the k-DBN unrolled over T time slices.
| T | Total number of time slices to display. Must be \(\geq k\). |
| highlightReplicated | If true, shade slices \(\geq k\) (lightcyan) differently from the template slices \(0, \ldots, k-1\) (gray). |
| OperationNotAllowed | if T < k. |
Definition at line 1124 of file KTBN_tpl.h.
References _k_, _timeSlicesToDot_(), GUM_ERROR, and unroll().
| BayesNet< GUM_SCALAR > gum::KTBN< GUM_SCALAR >::unroll | ( | Size | nbTimeSlices | ) | const |
Unrolls the k-DBN into a standard gum::BayesNet.
The result has exactly nbTimeSlices time slices. Slices \(0, \ldots,
k-1\) are copied verbatim from the template; every additional slice \(t \geq k\) replicates the incoming pattern of slice \(k-1\) (the transition kernel), shifting each temporal parent's slice accordingly so that lags are preserved. Variables are named with the same base[slice] bracket-notation convention (atemporal variables keep their name).
| nbTimeSlices | Total number of time slices of the unrolled network. |
| OperationNotAllowed | if nbTimeSlices is smaller than \(k\). |
Definition at line 697 of file KTBN_tpl.h.
References _atemporal_, _bn_, _encode_(), _k_, _temporal_, ATEMPORAL, gum::HashTable< Key, Val >::clear(), gum::DiscreteVariable::clone(), GUM_ERROR, gum::HashTable< Key, Val >::insert(), and parents().
Referenced by toUnrolledDot().
| INLINE const DiscreteVariable & gum::KTBN< GUM_SCALAR >::variable | ( | std::string_view | base, |
| int | slice ) const |
Returns the gum::DiscreteVariable of a (process, slice) couple.
Use KTBN::ATEMPORAL as slice for atemporal variables.
| NotFound | if no such variable exists. |
| OutOfBounds | if slice is out of \([0,k-1]\) for a temporal variable. |
| OperationNotAllowed | if the temporal/atemporal kind does not match slice. |
Definition at line 462 of file KTBN_tpl.h.
References _bn_, and _validateVariable_().
Referenced by variable().
| INLINE const DiscreteVariable & gum::KTBN< GUM_SCALAR >::variable | ( | std::string_view | node_name | ) | const |
Same, using an engine name: resolved via _determineNode_, so "X[1]" and bare "C" are both accepted.
Definition at line 468 of file KTBN_tpl.h.
References _determineNode_(), and variable().
|
private |
Base names of the registered atemporal variables.
Definition at line 713 of file KTBN.h.
Referenced by KTBN(), KTBN(), _determineNode_(), _determineNodesFromBN_(), _validateAdd_(), _validateVariable_(), add(), atemporalVarNames(), changeVariableName(), clear(), erase(), exists(), nbAtemporalVars(), nodes(), operator=(), operator=(), save(), summaryGraph(), toString(), and unroll().
The underlying Bayesian network used as a storage engine for the template.
Definition at line 707 of file KTBN.h.
Referenced by KTBN(), KTBN(), _determineNodeSet_(), _determineNodesFromBN_(), _validateVariable_(), add(), addArc(), arcs(), baseName(), bnToDot(), changeVariableName(), children(), clear(), cpt(), empty(), erase(), eraseArc(), existsArc(), fillCPT(), fillCPT(), generateCPT(), generateCPTs(), operator=(), operator=(), parents(), save(), size(), sizeArcs(), summaryGraph(), timeSlice(), toBN(), toDot(), toString(), unroll(), and variable().
The order (number of time slices in the template).
Definition at line 704 of file KTBN.h.
Referenced by KTBN(), KTBN(), KTBN(), _determineNodesFromBN_(), _timeSlicesToDot_(), _validateAdd_(), _validateVariable_(), add(), changeVariableName(), erase(), k(), nodes(), operator=(), operator=(), save(), summaryGraph(), toString(), toUnrolledDot(), and unroll().
|
private |
Base names of the registered temporal processes.
Definition at line 710 of file KTBN.h.
Referenced by KTBN(), KTBN(), _determineNodesFromBN_(), _validateAdd_(), _validateVariable_(), add(), changeVariableName(), clear(), erase(), exists(), nbTemporalVars(), nodes(), operator=(), operator=(), save(), summaryGraph(), temporalVarNames(), toString(), and unroll().
|
staticconstexpr |
Conventional time-slice value denoting an atemporal (static) variable.
Definition at line 200 of file KTBN.h.
Referenced by gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::_build_(), gum::learning::KTBNLearner< GUM_SCALAR >::_build_(), gum::learning::IKTBNLearner< GUM_SCALAR >::_checkArcTemporallyFeasible_(), gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::_decode_(), _decodeName_(), _determineNode_(), gum::learning::IKTBNLearner< GUM_SCALAR >::_determineNode_(), _determineNodesFromBN_(), gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::_drawSamples_(), _encode_(), gum::learning::IKTBNLearner< GUM_SCALAR >::_encode_(), gum::learning::KTBNLearner< GUM_SCALAR >::_forEachAllSlicesPair_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_forEachScoredNode_(), gum::learning::KTBNLearner< GUM_SCALAR >::_forOwningLearner_(), gum::KTBNGenerator< GUM_SCALAR >::_legalArcs_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_raiseKMinForSlice_(), _timeSlicesToDot_(), _validateAdd_(), _validateVariable_(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::_verifyBase_(), addArc(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::addForbiddenArcAllSlices(), gum::learning::KTBNLearner< GUM_SCALAR >::addNoChildrenNode(), gum::learning::KTBNLearner< GUM_SCALAR >::addNoParentNode(), gum::learning::KTBNLearner< GUM_SCALAR >::addPossibleEdge(), changeVariableName(), gum::learning::KTBNLearner< GUM_SCALAR >::domainSize(), gum::learning::KTBNAdaptiveLearner< GUM_SCALAR >::eraseForbiddenArcAllSlices(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseNoChildrenNode(), gum::learning::KTBNLearner< GUM_SCALAR >::eraseNoParentNode(), gum::learning::KTBNLearner< GUM_SCALAR >::erasePossibleEdge(), gum::learning::KTBNLearner< GUM_SCALAR >::learnParameters(), nodes(), gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::setVarOrderTopological(), gum::learning::KTBNLearner< GUM_SCALAR >::state(), summaryGraph(), and unroll().