aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
gum::CausalModel< GUM_SCALAR > Class Template Reference

A causal model pairing a causal DAG with an optional observational BayesNet. More...

#include <causalModel.h>

Collaboration diagram for gum::CausalModel< GUM_SCALAR >:
[legend]

Public Member Functions

 CausalModel ()=delete
 Default constructor disabled: a causal model must be built from a BN.
 CausalModel (const BayesNet< GUM_SCALAR > &observationalBN)
 Construct a causal model with no latent variables.
 CausalModel (const BayesNet< GUM_SCALAR > &observationalBN, const LatentDescriptorVector &latentVarsDescriptor, bool assumeNonSpurious=false)
 Construct a causal model and add a list of latent confounders.
 CausalModel (const DAG &dag, const LatentDescriptorVector &latentVarsDescriptor={}, bool assumeNonSpurious=false)
 Construct a causal model from a plain (named) DAG, without any BayesNet.
 CausalModel (const CausalModel &other)
 Copy constructor.
 CausalModel (CausalModel &&other) noexcept
 Move constructor.
 ~CausalModel ()
 Destructor.
CausalModel & operator= (const CausalModel &other)=default
 Copy assignment.
CausalModel & operator= (CausalModel &&other) noexcept=default
 Move assignment.
void addLatentVariable (std::string_view latentName, const std::vector< std::string > &childrenOfLatent, bool assumeNonSpurious=false)
 Add a latent variable by names of its observed children.
void addLatentVariable (std::string_view latentName, const std::vector< NodeId > &childrenOfLatent, bool assumeNonSpurious=false)
 Add a latent variable by **NodeId**s of its observed children.
bool existsArc (NodeId x, NodeId y) const
 Whether a causal arc x → y exists (by ids) in the causal DAG.
bool existsArc (std::string_view x, std::string_view y) const
 Whether a causal arc x → y exists (by names) in the causal DAG.
void assumeSpurious (NodeId x, NodeId y)
 Mark an arc as spurious: present in observationalBN, removed from causalDAG.
void assumeSpurious (std::string_view x, std::string_view y)
void assumeNonSpurious (NodeId x, NodeId y)
 Mark an arc as non-spurious: present in observationalBN, added to causalDAG.
void assumeNonSpurious (std::string_view x, std::string_view y)
bool isAssumedSpurious (NodeId x, NodeId y) const
 Returns true if arc is present in observationalBN and absent in causalDAG.
bool isAssumedSpurious (std::string_view x, std::string_view y) const
std::optional< NodeSet > backDoor (NodeId cause, NodeId effect) const
 Find a backdoor adjustment set (Z) between cause and effect (IDs only).
std::optional< NodeSet > backDoor (std::string_view cause, std::string_view effect) const
std::optional< NodeSet > frontDoor (NodeId cause, NodeId effect) const
 Find a frontdoor adjustment set (Z) between cause and effect (IDs only).
std::optional< NodeSet > frontDoor (std::string_view cause, std::string_view effect) const
 Find a frontdoor adjustment set (Z) between cause and effect (IDs only).
CausalModel< GUM_SCALAR > inducedCausalSubModel (const CausalModel< GUM_SCALAR > &cm, NodeSet subset) const
 Induced causal submodel on a subset of nodes.
std::string toDot () const
 DOT representation of the causal DAG (observed + latent), with the look of the configuration.
std::string toDot (const dot::DotOptions &options) const
 DOT representation of the causal DAG (observed + latent), with the options given by the caller.
std::string toDot (const bool SHOW_LATENT_NAMES, const char *NODE_BG=nullptr, const char *NODE_FG=nullptr, const char *EDGE_COL=nullptr) const
 DOT representation of the causal DAG (observed + latent), with values given by position.
bool hasObservationalBN () const noexcept
 Whether this model was built from a BayesNet (CPTs available).
const BayesNet< GUM_SCALAR > & observationalBN () const
 Observational BN (observed variables only).
DAG causalDAG () const
 Causal DAG (observed + latent variables), with node names set.
Set< std::string > names () const
 All variable names appearing in the causal model (observed + latent).
NodeId idFromName (std::string_view name) const
 Node id from variable name (observed or latent).
std::string nameFromId (NodeId id) const
 Variable name from node id (observed or latent).
Bijection< NodeId, std::string > id2name (bool includeLatentVariable=false) const
 Bidirectional mapping between node ids and variable names.
NodeSet latentVariablesIds () const
 Node ids of all latent variables.
Set< std::string > latentVariablesNames () const
 Names of all latent variables.
NodeSet parents (NodeId x) const
 Parents of a node (by id) in the causal DAG (including latents).
NodeSet parents (std::string_view name) const
 Parents of a node (by name) in the causal DAG (including latents).
NodeSet children (NodeId x) const
 Children of a node (by id) in the causal DAG (including latents).
NodeSet children (std::string_view name) const
 Children of a node (by name) in the causal DAG (including latents).
NodeProperty< NodeId > connectedComponents () const
 Weakly connected components of the causal DAG.
const DiscreteVariable & variable (NodeId id) const
 From id to variable (observed only, by id).
const DiscreteVariable & variable (std::string_view name) const
 From name to variable (observed only, by id).

Private Member Functions

std::string _toDot_ (const dot::CausalDotStyle &style, const dot::DotOptions &options) const
 the dot of the causal DAG, with a look and options

Private Attributes

std::optional< BayesNet< GUM_SCALAR > > _observationalBN_
 The observed Bayesian network, when the model was built with CPTs. Absent when built from a plain DAG — see hasObservationalBN().
DAG _observedDAG_
 Frozen copy of the observed DAG (never mutated after construction); names are set on it directly (setName) — the single source of truth for observed variable names.
DAG _causalDAG_
 The underlying DAG representing the causal structure (observed + latent).
Bijection< NodeId, std::string > _ids_names_
 Bidirectional mapping between node ids and variable names (latents only).

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::CausalModel< GUM_SCALAR >

A causal model pairing a causal DAG with an optional observational BayesNet.

The class is DAG-centric for causality: all causal-structure queries and edits happen on _causalDAG_, which may include latent variables that do not appear in _observationalBN_. Name/Id bookkeeping is handled so you can work with either.

A CausalModel can be built either from a full BayesNet (CPTs available, the classic use case) or from a plain named DAG (structure only, see the DAG-only constructor and hasObservationalBN()). Every structural operation (backdoor/frontdoor, d-separation, toDot, induced submodels, do-calculus tree construction) works in both cases; anything that reads a CPT (ASTtree::eval, CausalFormula::eval, Counterfactual) throws gum::OperationNotAllowed when no BayesNet is present.

Invariants / expectations

  • _observationalBN_ (when present) corresponds to the observed part (no latents).
  • _causalDAG_ contains all observed variables and any added latent variables.
  • When adding a latent with children, the latent becomes a parent of all listed children in the causal DAG. The assumeNonSpurious flag controls whether existing arcs between those children are preserved or may be adjusted (implementation dependent).

Definition at line 96 of file causalModel.h.

Constructor & Destructor Documentation

◆ CausalModel() [1/6]

template<GUM_Numeric GUM_SCALAR>
gum::CausalModel< GUM_SCALAR >::CausalModel ( )
delete

Default constructor disabled: a causal model must be built from a BN.

References assumeNonSpurious(), and observationalBN().

Referenced by CausalModel(), CausalModel(), CausalModel(), CausalModel(), ~CausalModel(), inducedCausalSubModel(), operator=(), and operator=().

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

◆ CausalModel() [2/6]

template<GUM_Numeric GUM_SCALAR>
gum::CausalModel< GUM_SCALAR >::CausalModel ( const BayesNet< GUM_SCALAR > & observationalBN)
explicit

Construct a causal model with no latent variables.

Parameters
observationalBNthe observed Bayesian network

Initializes the causal DAG as the BN's DAG (observed-only).

Definition at line 75 of file causalModel_tpl.h.

75 :
77 _causalDAG_(observationalBN.internalDag()) {
79 }
A causal model pairing a causal DAG with an optional observational BayesNet.
Definition causalModel.h:96
DAG _observedDAG_
Frozen copy of the observed DAG (never mutated after construction); names are set on it directly (set...
DAG _causalDAG_
The underlying DAG representing the causal structure (observed + latent).
CausalModel()=delete
Default constructor disabled: a causal model must be built from a BN.
std::optional< BayesNet< GUM_SCALAR > > _observationalBN_
The observed Bayesian network, when the model was built with CPTs. Absent when built from a plain DAG...
const BayesNet< GUM_SCALAR > & observationalBN() const
Observational BN (observed variables only).

References CausalModel(), _causalDAG_, _observationalBN_, _observedDAG_, and observationalBN().

Here is the call graph for this function:

◆ CausalModel() [3/6]

template<GUM_Numeric GUM_SCALAR>
gum::CausalModel< GUM_SCALAR >::CausalModel ( const BayesNet< GUM_SCALAR > & observationalBN,
const LatentDescriptorVector & latentVarsDescriptor,
bool assumeNonSpurious = false )
explicit

Construct a causal model and add a list of latent confounders.

Parameters
observationalBNthe observed Bayesian network
latentVarsDescriptorlist of (latentName, childrenIds) descriptors
assumeNonSpuriouswhether to preserve existing arcs among the latent's children

Each latent is created and added as a parent of the provided children in the causal DAG. The observed BN remains unchanged.

Definition at line 87 of file causalModel_tpl.h.

89 :
91 _causalDAG_(observationalBN.internalDag()) {
92 // add each latent (children given as names)
93 for (const auto& [latent, children]: latentVarsDescriptor) {
95 }
97 }
void addLatentVariable(std::string_view latentName, const std::vector< std::string > &childrenOfLatent, bool assumeNonSpurious=false)
Add a latent variable by names of its observed children.
NodeSet children(NodeId x) const
Children of a node (by id) in the causal DAG (including latents).
void assumeNonSpurious(NodeId x, NodeId y)
Mark an arc as non-spurious: present in observationalBN, added to causalDAG.

References CausalModel(), _causalDAG_, _observationalBN_, _observedDAG_, addLatentVariable(), assumeNonSpurious(), children(), and observationalBN().

Here is the call graph for this function:

◆ CausalModel() [4/6]

template<GUM_Numeric GUM_SCALAR>
gum::CausalModel< GUM_SCALAR >::CausalModel ( const DAG & dag,
const LatentDescriptorVector & latentVarsDescriptor = {},
bool assumeNonSpurious = false )
explicit

Construct a causal model from a plain (named) DAG, without any BayesNet.

Parameters
daga named DAG (every node must have a name, see gum::DAG::hasName)
latentVarsDescriptorlist of (latentName, childrenIds) descriptors
assumeNonSpuriouswhether to preserve existing arcs among the latent's children

The resulting model has no observational BayesNet: hasObservationalBN() returns false, and observationalBN()/variable()/CausalFormula::eval()/CausalImpact::eval()/Counterfactual throw gum::OperationNotAllowed. All structural operations (backdoor/frontdoor, d-separation, toDot, induced submodels, do-calculus tree construction) work normally.

Exceptions
gum::InvalidArgumentif some node of dag has no name.

Definition at line 117 of file causalModel_tpl.h.

119 :
121 for (auto id: dag.nodes()) {
122 if (!dag.hasName(id)) {
124 "CausalModel: every node of the DAG must have a name (node " + std::to_string(id)
125 + " has none).");
126 }
127 }
128 for (const auto& [latent, children]: latentVarsDescriptor) {
130 }
132 }

References _causalDAG_, _observedDAG_, assumeNonSpurious(), and gum::NodeGraphPart::nodes().

Here is the call graph for this function:

◆ CausalModel() [5/6]

template<GUM_Numeric GUM_SCALAR>
gum::CausalModel< GUM_SCALAR >::CausalModel ( const CausalModel< GUM_SCALAR > & other)

Copy constructor.

Definition at line 101 of file causalModel_tpl.h.

101 :
105 };
Bijection< NodeId, std::string > _ids_names_
Bidirectional mapping between node ids and variable names (latents only).

References CausalModel(), _causalDAG_, _ids_names_, _observationalBN_, and _observedDAG_.

Here is the call graph for this function:

◆ CausalModel() [6/6]

template<GUM_Numeric GUM_SCALAR>
gum::CausalModel< GUM_SCALAR >::CausalModel ( CausalModel< GUM_SCALAR > && other)
noexcept

Move constructor.

Definition at line 109 of file causalModel_tpl.h.

References CausalModel(), _causalDAG_, _ids_names_, _observationalBN_, and _observedDAG_.

Here is the call graph for this function:

◆ ~CausalModel()

template<GUM_Numeric GUM_SCALAR>
gum::CausalModel< GUM_SCALAR >::~CausalModel ( )

Destructor.

Definition at line 82 of file causalModel_tpl.h.

82 {
84 }

References CausalModel().

Here is the call graph for this function:

Member Function Documentation

◆ _toDot_()

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalModel< GUM_SCALAR >::_toDot_ ( const dot::CausalDotStyle & style,
const dot::DotOptions & options ) const
private

the dot of the causal DAG, with a look and options

Definition at line 441 of file causalModel_tpl.h.

442 {
445
446 // the name of a node of the causal DAG
447 const auto label = [this](NodeId n) {
448 try {
449 return nameFromId(n);
450 } catch (gum::NotFound&) { return "L" + std::to_string(n); }
451 };
452
453 // a font color which is adaptive is resolved against the background of the kind of node
454 auto latentStyle = static_cast< dot::DotStyle >(style);
455 latentStyle.nodeBgColor = style.latentBgColor;
456
457 // First, latent nodes
458 for (const auto n: _causalDAG_.nodes()) {
459 if (!latentVariablesIds().contains(n)) continue;
461 options.node(n),
462 "",
463 "(" + std::to_string(n) + ") " + label(n));
464 dot::addDefaultAttribute(attributes, "shape", style.showLatentNames ? "ellipse" : "point");
465 dot::addDefaultAttribute(attributes, "fontcolor", style.latentFgColor);
466 dot::addDefaultAttribute(attributes, "fillcolor", style.latentBgColor);
467 writer.node(label(n), attributes);
468 }
469
470 // Then, observed nodes
471 for (const auto n: _causalDAG_.nodes()) {
472 if (latentVariablesIds().contains(n)) continue;
473 writer.node(label(n),
475 options.node(n),
476 "",
477 "(" + std::to_string(n) + ") " + label(n)));
478 }
479
480 // Edges
481 for (const auto& arc: _causalDAG_.arcs()) {
482 auto attributes = dot::arcAttributes(options.arc(arc.tail(), arc.head()));
483 if (latentVariablesIds().contains(arc.tail()) || latentVariablesIds().contains(arc.head())) {
484 dot::addDefaultAttribute(attributes, "style", "dashed");
485 } else {
486 // two lines of the same color for an arc of the observed variables
487 dot::addDefaultAttribute(attributes, "color", style.arcColor + ":" + style.arcColor);
488 }
489 writer.edge(label(arc.tail()), label(arc.head()), attributes);
490 }
491
492 return writer.str();
493 }
NodeSet latentVariablesIds() const
Node ids of all latent variables.
std::string nameFromId(NodeId id) const
Variable name from node id (observed or latent).
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...

References _causalDAG_, _toDot_(), gum::dot::addDefaultAttribute(), gum::dot::DotOptions::arc(), gum::dot::arcAttributes(), gum::dot::DotStyle::arcColor, gum::contains(), gum::dot::DotWriter::edge(), gum::dot::CausalDotStyle::latentBgColor, gum::dot::CausalDotStyle::latentFgColor, latentVariablesIds(), nameFromId(), gum::dot::DotOptions::node(), gum::dot::DotWriter::node(), gum::dot::nodeAttributes(), gum::dot::DotStyle::nodeBgColor, gum::dot::CausalDotStyle::showLatentNames, gum::dot::DotWriter::str(), and gum::dot::writePreamble().

Referenced by _toDot_(), toDot(), and toDot().

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

◆ addLatentVariable() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::CausalModel< GUM_SCALAR >::addLatentVariable ( std::string_view latentName,
const std::vector< NodeId > & childrenOfLatent,
bool assumeNonSpurious = false )

Add a latent variable by **NodeId**s of its observed children.

Parameters
latentNamenew latent variable name
childrenOfLatentnode ids of observed children
assumeNonSpuriouspreserve existing arcs among the children if true.

A new latent node is inserted into the causal DAG and connected as a parent of each listed child. The observed BN is not modified.

Warning
When assumeNonSpurious == false and the latent has children \(\{X_1,\ldots,X_n\}\), any existing arcs \(X_i \rightarrow X_j\) among those children may be removed from the causal DAG. To preserve some child→child arcs while dropping others, consider creating separate latents: one with assumeNonSpurious = true for the subset to preserve, and another with assumeNonSpurious = false for the subset to drop. A finer-grained edit API may be added later.

Definition at line 152 of file causalModel_tpl.h.

154 {
155 // must have at least 2 children
156 if (childrenOfLatent.size() < 2) {
157 GUM_ERROR(InvalidArgument, "A latent variable must affect at least 2 children.");
158 }
159
160 // reject names that collide with observed or existing latent variables
161 if (_observedDAG_.idFromName(std::string(latentName)).has_value()) {
163 "Latent variable name conflicts with an observed variable: "
165 }
166 if (_ids_names_.existsSecond(std::string(latentName))) {
168 "Latent variable name already used by another latent: " + std::string(latentName));
169 }
170
171 // create a fresh node id for the latent in the causal DAG
172 const NodeId id_latent = _causalDAG_.addNode();
173
174 // record bookkeeping
176
177 // add arcs latent -> each child
178 for (const auto& cid: childrenOfLatent) {
179 // children are observed nodes: they must already exist in causal DAG (copied from observed
180 // BN)
181 _causalDAG_.addArc(id_latent, cid);
182 }
183
184 // remove any direct causal arc among children unless assumeNonSpurious=true
185 if (!assumeNonSpurious) {
186 for (auto i = childrenOfLatent.begin(); i != childrenOfLatent.end(); ++i) {
187 auto j = i;
188 ++j;
189 for (auto jj = j; jj != childrenOfLatent.end(); ++jj) {
190 const NodeId a = *i;
191 const NodeId b = *jj;
192 // remove either direction if present
193 Arc ab(a, b), ba(b, a);
194 if (_causalDAG_.existsArc(ab)) _causalDAG_.eraseArc(ab);
195 else if (_causalDAG_.existsArc(ba)) _causalDAG_.eraseArc(ba);
196 }
197 }
198 }
199 }

References _observedDAG_, addLatentVariable(), assumeNonSpurious(), and GUM_ERROR.

Here is the call graph for this function:

◆ addLatentVariable() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::CausalModel< GUM_SCALAR >::addLatentVariable ( std::string_view latentName,
const std::vector< std::string > & childrenOfLatent,
bool assumeNonSpurious = false )

Add a latent variable by names of its observed children.

Parameters
latentNamenew latent variable name
childrenOfLatentnames of observed children
assumeNonSpuriouspreserve existing arcs among the children if true

A new latent node is inserted into the causal DAG and connected as a parent of each listed child. The observed BN is not modified.

Warning
When assumeNonSpurious == false and the latent has children \(\{X_1,\ldots,X_n\}\), any existing arcs \(X_i \rightarrow X_j\) among those children may be removed from the causal DAG. To preserve some child→child arcs while dropping others, consider creating separate latents: one with assumeNonSpurious = true for the subset to preserve, and another with assumeNonSpurious = false for the subset to drop. A finer-grained edit API may be added later.

Definition at line 139 of file causalModel_tpl.h.

142 {
144 childIds.reserve(childrenOfLatent.size());
145 for (const auto& nm: childrenOfLatent) {
146 childIds.push_back(idFromName(nm));
147 }
149 }
NodeId idFromName(std::string_view name) const
Node id from variable name (observed or latent).

References addLatentVariable(), assumeNonSpurious(), and idFromName().

Referenced by CausalModel(), addLatentVariable(), addLatentVariable(), and operator=().

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

◆ assumeNonSpurious() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::CausalModel< GUM_SCALAR >::assumeNonSpurious ( NodeId x,
NodeId y )

Mark an arc as non-spurious: present in observationalBN, added to causalDAG.

Throws if arc is not present in observationalBN.

Definition at line 257 of file causalModel_tpl.h.

257 {
258 // Validate arc in observationalBN
259 if (!_observedDAG_.existsArc(Arc(x, y))) {
261 "Arc(" + std::to_string(x) + "," + std::to_string(y)
262 + ") not present in observationalBN");
263 }
264 // Add arc to causalDAG if absent
265 if (!_causalDAG_.existsArc(Arc(x, y))) { _causalDAG_.addArc(x, y); }
266 }

References _observedDAG_, assumeNonSpurious(), and GUM_ERROR.

Referenced by CausalModel(), CausalModel(), CausalModel(), addLatentVariable(), addLatentVariable(), assumeNonSpurious(), assumeNonSpurious(), and operator=().

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

◆ assumeNonSpurious() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::CausalModel< GUM_SCALAR >::assumeNonSpurious ( std::string_view x,
std::string_view y )

Definition at line 269 of file causalModel_tpl.h.

269 {
271 }

References assumeNonSpurious(), and idFromName().

Here is the call graph for this function:

◆ assumeSpurious() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::CausalModel< GUM_SCALAR >::assumeSpurious ( NodeId x,
NodeId y )

Mark an arc as spurious: present in observationalBN, removed from causalDAG.

Throws if arc is not present in observationalBN.

Definition at line 240 of file causalModel_tpl.h.

240 {
241 // Validate arc in observationalBN
242 if (!_observedDAG_.existsArc(Arc(x, y))) {
244 "Arc(" + std::to_string(x) + "," + std::to_string(y)
245 + ") not present in observationalBN");
246 }
247 // Remove arc from causalDAG if present
248 if (_causalDAG_.existsArc(Arc(x, y))) { _causalDAG_.eraseArc(Arc(x, y)); }
249 }
Arc(NodeId tail, NodeId head)
basic constructor. Creates tail -> head.

References _causalDAG_, _observedDAG_, assumeSpurious(), and GUM_ERROR.

Referenced by assumeSpurious(), assumeSpurious(), and operator=().

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

◆ assumeSpurious() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::CausalModel< GUM_SCALAR >::assumeSpurious ( std::string_view x,
std::string_view y )

Definition at line 252 of file causalModel_tpl.h.

252 {
254 }
void assumeSpurious(NodeId x, NodeId y)
Mark an arc as spurious: present in observationalBN, removed from causalDAG.

References assumeSpurious(), and idFromName().

Here is the call graph for this function:

◆ backDoor() [1/2]

template<GUM_Numeric GUM_SCALAR>
std::optional< NodeSet > gum::CausalModel< GUM_SCALAR >::backDoor ( NodeId cause,
NodeId effect ) const

Find a backdoor adjustment set (Z) between cause and effect (IDs only).

Enumerates admissible backdoor sets in the current causal DAG (excluding latent variables) and returns the first valid one found. Returns std::nullopt if no backdoor set exists at all. Note: an empty set is a valid backdoor when X has no back-door paths.

Preconditions:

  • cause and effect must be observed variables of the model.
Parameters
causeNodeId of the cause (X).
effectNodeId of the effect (Y).
Returns
std::optional<NodeSet> The backdoor adjustment set (Z) as NodeIds, or std::nullopt if no backdoor set exists.

Definition at line 381 of file causalModel_tpl.h.

381 {
382 // Preconditions: cause/effect must be observed (non-latent).
384 if (lat.contains(cause) || lat.contains(effect)) {
386 "CausalModel::backDoor: 'cause' and 'effect' must be observed (non-latent).");
387 }
388
390 if (candidates.empty()) return std::nullopt;
391 return candidates.front(); // first valid set (may itself be empty)
392 }
static NodeSetVec enumerateBackdoorSets(const DAG &dag, NodeId X, NodeId Y, const NodeSet &excluded_nodes=NodeSet(), std::size_t max_cardinality=0, bool only_minimal=true, bool stopAtFirst=false)
Enumerate valid backdoor adjustment sets.

References backDoor(), and latentVariablesIds().

Referenced by gum::CausalImpact< GUM_SCALAR >::_buildFromNames_(), backDoor(), backDoor(), and operator=().

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

◆ backDoor() [2/2]

template<GUM_Numeric GUM_SCALAR>
std::optional< NodeSet > gum::CausalModel< GUM_SCALAR >::backDoor ( std::string_view cause,
std::string_view effect ) const

Definition at line 584 of file causalModel_tpl.h.

585 {
587 }
std::optional< NodeSet > backDoor(NodeId cause, NodeId effect) const
Find a backdoor adjustment set (Z) between cause and effect (IDs only).

References backDoor(), and idFromName().

Here is the call graph for this function:

◆ causalDAG()

template<GUM_Numeric GUM_SCALAR>
DAG gum::CausalModel< GUM_SCALAR >::causalDAG ( ) const

Causal DAG (observed + latent variables), with node names set.

Definition at line 576 of file causalModel_tpl.h.

576 {
577 DAG g = _causalDAG_;
578 for (auto id: g)
579 g.setName(id, nameFromId(id));
580 return g;
581 }

References _causalDAG_, causalDAG(), nameFromId(), and gum::NodeGraphPart::setName().

Referenced by gum::DoCalculus< GUM_SCALAR >::_cDecomposition_(), gum::DoCalculus< GUM_SCALAR >::_ID_(), gum::DoCalculus< GUM_SCALAR >::_topoObserved_(), causalDAG(), and operator=().

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

◆ children() [1/2]

template<GUM_Numeric GUM_SCALAR>
NodeSet gum::CausalModel< GUM_SCALAR >::children ( NodeId x) const

Children of a node (by id) in the causal DAG (including latents).

Definition at line 367 of file causalModel_tpl.h.

367 {
368 return _causalDAG_.children(x);
369 }

References _causalDAG_, and children().

Referenced by CausalModel(), gum::DoCalculus< GUM_SCALAR >::_cDecomposition_(), children(), children(), and operator=().

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

◆ children() [2/2]

template<GUM_Numeric GUM_SCALAR>
NodeSet gum::CausalModel< GUM_SCALAR >::children ( std::string_view name) const

Children of a node (by name) in the causal DAG (including latents).

Definition at line 372 of file causalModel_tpl.h.

372 {
373 return children(idFromName(name));
374 }

References children(), and idFromName().

Here is the call graph for this function:

◆ connectedComponents()

template<GUM_Numeric GUM_SCALAR>
NodeProperty< NodeId > gum::CausalModel< GUM_SCALAR >::connectedComponents ( ) const

Weakly connected components of the causal DAG.

Returns
A table mapping each node to the (arbitrary but consistent) id of its component.

Definition at line 294 of file causalModel_tpl.h.

294 {
296 }
NodeProperty< NodeId > connectedComponents(const G &g)
Returns a node-to-component-id mapping for the (weakly) connected components of g.

References _causalDAG_, connectedComponents(), and gum::graph::connectedComponents().

Referenced by connectedComponents(), and operator=().

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

◆ existsArc() [1/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalModel< GUM_SCALAR >::existsArc ( NodeId x,
NodeId y ) const

Whether a causal arc x → y exists (by ids) in the causal DAG.

Definition at line 202 of file causalModel_tpl.h.

202 {
203 return _causalDAG_.existsArc(Arc(x, y));
204 }

References _causalDAG_, and existsArc().

Referenced by existsArc(), existsArc(), and operator=().

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

◆ existsArc() [2/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalModel< GUM_SCALAR >::existsArc ( std::string_view x,
std::string_view y ) const

Whether a causal arc x → y exists (by names) in the causal DAG.

Definition at line 207 of file causalModel_tpl.h.

207 {
208 const NodeId ix = idFromName(x);
209 const NodeId iy = idFromName(y);
210 return existsArc(ix, iy);
211 }
bool existsArc(NodeId x, NodeId y) const
Whether a causal arc x → y exists (by ids) in the causal DAG.

References existsArc(), and idFromName().

Here is the call graph for this function:

◆ frontDoor() [1/2]

template<GUM_Numeric GUM_SCALAR>
std::optional< NodeSet > gum::CausalModel< GUM_SCALAR >::frontDoor ( NodeId cause,
NodeId effect ) const

Find a frontdoor adjustment set (Z) between cause and effect (IDs only).

Enumerates admissible frontdoor sets in the current causal DAG (excluding latent variables) and returns the first valid one found. Returns std::nullopt if no frontdoor set exists at all. Note: an empty set is a valid frontdoor in degenerate cases.

Preconditions:

  • cause and effect must be observed variables of the model.
Parameters
causeNodeId of the cause (X).
effectNodeId of the effect (Y).
Returns
std::optional<NodeSet> The frontdoor adjustment set (Z) as NodeIds, or std::nullopt if no frontdoor set exists.

Definition at line 395 of file causalModel_tpl.h.

395 {
396 // Preconditions: cause/effect must be observed (non-latent).
398 if (lat.contains(cause) || lat.contains(effect)) {
400 "CausalModel::frontDoor: 'cause' and 'effect' must be observed (non-latent).");
401 }
402
404 if (candidates.empty()) return std::nullopt;
405 return candidates.front(); // first valid set (may itself be empty)
406 }
static NodeSetVec enumerateFrontdoorSets(const DAG &dag, NodeId X, NodeId Y, const NodeSet &excluded_nodes=NodeSet(), std::size_t max_cardinality=0, bool only_minimal=true, bool stopAtFirst=false)
Enumerate valid frontdoor adjustment sets.

References gum::Set< Key >::contains(), frontDoor(), and latentVariablesIds().

Referenced by frontDoor(), frontDoor(), and operator=().

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

◆ frontDoor() [2/2]

template<GUM_Numeric GUM_SCALAR>
std::optional< NodeSet > gum::CausalModel< GUM_SCALAR >::frontDoor ( std::string_view cause,
std::string_view effect ) const

Find a frontdoor adjustment set (Z) between cause and effect (IDs only).

Enumerates admissible frontdoor sets in the current causal DAG (excluding latent variables) and returns the first valid one found. Returns std::nullopt if no frontdoor set exists at all. Note: an empty set is a valid frontdoor in degenerate cases.

Preconditions:

  • cause and effect must be observed variables of the model.
Parameters
causename (string_view) of the cause (X).
effectname (string_view) of the effect (Y).
Returns
std::optional<NodeSet> The frontdoor adjustment set (Z) as NodeIds, or std::nullopt if no frontdoor set exists.

Definition at line 590 of file causalModel_tpl.h.

591 {
593 }
std::optional< NodeSet > frontDoor(NodeId cause, NodeId effect) const
Find a frontdoor adjustment set (Z) between cause and effect (IDs only).

References frontDoor(), and idFromName().

Here is the call graph for this function:

◆ hasObservationalBN()

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalModel< GUM_SCALAR >::hasObservationalBN ( ) const
noexcept

Whether this model was built from a BayesNet (CPTs available).

Definition at line 561 of file causalModel_tpl.h.

561 {
562 return _observationalBN_.has_value();
563 }

References _observationalBN_, and hasObservationalBN().

Referenced by hasObservationalBN(), and operator=().

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

◆ id2name()

template<GUM_Numeric GUM_SCALAR>
Bijection< NodeId, std::string > gum::CausalModel< GUM_SCALAR >::id2name ( bool includeLatentVariable = false) const

Bidirectional mapping between node ids and variable names.

Definition at line 538 of file causalModel_tpl.h.

538 {
540
541 // Add observed variables
542 for (const auto n: _observedDAG_.nodes()) {
544 }
545
546 // Add latent variables if requested
548 for (auto iter = _ids_names_.beginSafe(); iter != _ids_names_.endSafe(); ++iter) {
549 result.insert(iter.first(), iter.second());
550 }
551 }
552
553 return result;
554 }

References _ids_names_, _observedDAG_, id2name(), and gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::insert().

Referenced by gum::DoCalculus< GUM_SCALAR >::_ID_(), id2name(), and operator=().

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

◆ idFromName()

template<GUM_Numeric GUM_SCALAR>
NodeId gum::CausalModel< GUM_SCALAR >::idFromName ( std::string_view name) const

Node id from variable name (observed or latent).

Definition at line 513 of file causalModel_tpl.h.

513 {
514 if (_ids_names_.existsSecond(std::string(name))) {
515 return _ids_names_.first(std::string(name));
516 } else {
518 if (!opt.has_value()) {
520 "CausalModel::idFromName: no observed or latent variable named '"
521 + std::string(name) + "'.");
522 }
523 return *opt;
524 }
525 }

References _ids_names_, _observedDAG_, GUM_ERROR, and idFromName().

Referenced by gum::CausalImpact< GUM_SCALAR >::_buildFromNames_(), gum::CausalImpact< GUM_SCALAR >::_namesToIds_(), gum::CausalFormula< GUM_SCALAR >::_toNodeSetFromNames_(), addLatentVariable(), assumeNonSpurious(), assumeSpurious(), backDoor(), children(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::counterfactualModel(), existsArc(), frontDoor(), idFromName(), isAssumedSpurious(), operator=(), and variable().

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

◆ inducedCausalSubModel()

template<GUM_Numeric GUM_SCALAR>
CausalModel< GUM_SCALAR > gum::CausalModel< GUM_SCALAR >::inducedCausalSubModel ( const CausalModel< GUM_SCALAR > & cm,
NodeSet subset ) const

Induced causal submodel on a subset of nodes.

Parameters
cma source causal model
subsetnode set to keep (observed + latent)
Returns
A new CausalModel induced by the subset (both BN/DAG restricted appropriately).

Definition at line 300 of file causalModel_tpl.h.

301 {
302 // If caller gave an empty subset, use "all observed" nodes
303 if (subset.empty()) {
304 for (auto n: cm._observedDAG_.nodes())
305 subset.insert(n);
306 }
307
308 // Keep only OBSERVED nodes (drop latents explicitly)
310 for (auto L: cm.latentVariablesIds())
311 nodes.erase(L);
312
313 // --- Build a named DAG restricted to `nodes` (structure only: no BN, no CPTs).
314 // NodeIds are preserved so that NodeSets expressed in `cm`'s id space (Y, X, ...) stay valid
315 // in the induced sub-model.
316 DAG sub;
317 for (auto n: nodes) {
318 sub.addNodeWithId(n);
319 sub.setName(n, cm.nameFromId(n));
320 }
321
322 // add arcs among the kept observed nodes (from the *causal* DAG)
323 const auto causalDag = cm.causalDAG();
324 for (const auto& a: causalDag.arcs()) {
325 const NodeId x = a.tail();
326 const NodeId y = a.head();
327 if (nodes.contains(x) && nodes.contains(y)) { sub.addArc(x, y); }
328 }
329
330 // --- Rebuild latent descriptor restricted to kept nodes
332 for (auto L: cm.latentVariablesIds()) {
334 mapped_children.reserve(4);
335
336 // intersect latent children with kept observed nodes
337 for (auto c: cm.children(L)) {
338 if (nodes.contains(c)) mapped_children.push_back(c);
339 }
340
341 // keep the latent ONLY if it still has at least 2 children
342 if (mapped_children.size() >= 2) {
344 }
345 }
346
347 // assumeNonSpurious=true : pyagrum uses True here (do not remove arcs among children)
348 return CausalModel< GUM_SCALAR >(sub, latentDesc, /*assumeNonSpurious=*/true);
349 }
DAG causalDAG() const
Causal DAG (observed + latent variables), with node names set.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart

References CausalModel(), _observedDAG_, gum::Set< Key >::empty(), inducedCausalSubModel(), gum::Set< Key >::insert(), and gum::NodeGraphPart::nodes().

Referenced by gum::DoCalculus< GUM_SCALAR >::_ID_(), inducedCausalSubModel(), and operator=().

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

◆ isAssumedSpurious() [1/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalModel< GUM_SCALAR >::isAssumedSpurious ( NodeId x,
NodeId y ) const

Returns true if arc is present in observationalBN and absent in causalDAG.

Definition at line 274 of file causalModel_tpl.h.

274 {
275 return _observedDAG_.existsArc(Arc(x, y)) && !_causalDAG_.existsArc(Arc(x, y));
276 }

References _causalDAG_, _observedDAG_, and isAssumedSpurious().

Referenced by isAssumedSpurious(), isAssumedSpurious(), and operator=().

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

◆ isAssumedSpurious() [2/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalModel< GUM_SCALAR >::isAssumedSpurious ( std::string_view x,
std::string_view y ) const

Definition at line 279 of file causalModel_tpl.h.

279 {
281 }
bool isAssumedSpurious(NodeId x, NodeId y) const
Returns true if arc is present in observationalBN and absent in causalDAG.

References idFromName(), and isAssumedSpurious().

Here is the call graph for this function:

◆ latentVariablesIds()

template<GUM_Numeric GUM_SCALAR>
NodeSet gum::CausalModel< GUM_SCALAR >::latentVariablesIds ( ) const

Node ids of all latent variables.

Definition at line 218 of file causalModel_tpl.h.

218 {
220 for (auto it = _ids_names_.begin(); it != _ids_names_.end(); ++it) {
221 latentIds.insert(it.first());
222 }
223 return latentIds;
224 }

References _ids_names_, gum::Set< Key >::insert(), and latentVariablesIds().

Referenced by gum::DoCalculus< GUM_SCALAR >::_cDecomposition_(), gum::DoCalculus< GUM_SCALAR >::_ID_(), _toDot_(), gum::DoCalculus< GUM_SCALAR >::_topoObserved_(), backDoor(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::counterfactualModel(), frontDoor(), latentVariablesIds(), and operator=().

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

◆ latentVariablesNames()

template<GUM_Numeric GUM_SCALAR>
Set< std::string > gum::CausalModel< GUM_SCALAR >::latentVariablesNames ( ) const

Names of all latent variables.

Definition at line 227 of file causalModel_tpl.h.

227 {
229 for (auto it = _ids_names_.begin(); it != _ids_names_.end(); ++it) {
230 latentNames.insert(it.second());
231 }
232 return latentNames;
233 }

References _ids_names_, gum::Set< Key >::insert(), and latentVariablesNames().

Referenced by latentVariablesNames(), and operator=().

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

◆ nameFromId()

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalModel< GUM_SCALAR >::nameFromId ( NodeId id) const

Variable name from node id (observed or latent).

Definition at line 528 of file causalModel_tpl.h.

528 {
529 if (_ids_names_.existsFirst(id)) {
530 return _ids_names_.second(id);
531 } else {
532 return _observedDAG_.nameFromId(id);
533 }
534 }

References _ids_names_, _observedDAG_, and nameFromId().

Referenced by gum::DoCalculus< GUM_SCALAR >::_hedgeExceptionMsg(), gum::DoCalculus< GUM_SCALAR >::_ID_(), gum::CausalImpact< GUM_SCALAR >::_idsToNames_(), _toDot_(), causalDAG(), gum::CausalModel< GUM_ELEMENT >::inducedCausalSubModel(), nameFromId(), and operator=().

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

◆ names()

template<GUM_Numeric GUM_SCALAR>
Set< std::string > gum::CausalModel< GUM_SCALAR >::names ( ) const

All variable names appearing in the causal model (observed + latent).

Definition at line 500 of file causalModel_tpl.h.

500 {
502 for (const auto n: _observedDAG_.nodes()) {
503 names.insert(_observedDAG_.nameFromId(n));
504 }
505 // latent names (from bijection)
506 for (auto it = _ids_names_.begin(); it != _ids_names_.end(); ++it) {
507 names.insert(it.second());
508 }
509 return names;
510 }
Set< std::string > names() const
All variable names appearing in the causal model (observed + latent).

References _ids_names_, _observedDAG_, gum::Set< Key >::insert(), and names().

Referenced by names(), and operator=().

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

◆ observationalBN()

template<GUM_Numeric GUM_SCALAR>
const BayesNet< GUM_SCALAR > & gum::CausalModel< GUM_SCALAR >::observationalBN ( ) const

Observational BN (observed variables only).

Exceptions
gum::OperationNotAllowedif the model was built from a plain DAG (see hasObservationalBN()).

Definition at line 566 of file causalModel_tpl.h.

566 {
567 if (!_observationalBN_.has_value()) {
569 "CausalModel::observationalBN: this model has no observational BayesNet "
570 "(built from a DAG).");
571 }
572 return *_observationalBN_;
573 }

References _observationalBN_, GUM_ERROR, and observationalBN().

Referenced by CausalModel(), CausalModel(), CausalModel(), gum::Counterfactual< GUM_ELEMENT >::_adaptToOriginalVariables_(), gum::Counterfactual< GUM_ELEMENT >::_idAssignToNameAssign_(), gum::Counterfactual< GUM_ELEMENT >::_idsToNames_(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::counterfactualModel(), observationalBN(), operator=(), variable(), and variable().

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

◆ operator=() [1/2]

template<GUM_Numeric GUM_SCALAR>
CausalModel & gum::CausalModel< GUM_SCALAR >::operator= ( CausalModel< GUM_SCALAR > && other)
defaultnoexcept

◆ operator=() [2/2]

template<GUM_Numeric GUM_SCALAR>
CausalModel & gum::CausalModel< GUM_SCALAR >::operator= ( const CausalModel< GUM_SCALAR > & other)
default

Copy assignment.

References CausalModel().

Here is the call graph for this function:

◆ parents() [1/2]

template<GUM_Numeric GUM_SCALAR>
NodeSet gum::CausalModel< GUM_SCALAR >::parents ( NodeId x) const

Parents of a node (by id) in the causal DAG (including latents).

Definition at line 356 of file causalModel_tpl.h.

356 {
357 // delegate to the causal DAG (includes latent->observed arcs)
358 return _causalDAG_.parents(x);
359 }

References _causalDAG_, and parents().

Referenced by operator=(), parents(), and parents().

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

◆ parents() [2/2]

template<GUM_Numeric GUM_SCALAR>
NodeSet gum::CausalModel< GUM_SCALAR >::parents ( std::string_view name) const

Parents of a node (by name) in the causal DAG (including latents).

Definition at line 362 of file causalModel_tpl.h.

362 {
363 return parents(idFromName(name));
364 }
NodeSet parents(NodeId x) const
Parents of a node (by id) in the causal DAG (including latents).

References parents().

Here is the call graph for this function:

◆ toDot() [1/3]

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalModel< GUM_SCALAR >::toDot ( ) const

DOT representation of the causal DAG (observed + latent), with the look of the configuration.

The colors of the nodes are the ones of the section causal (nodeBgColor, nodeFgColor for the observed variables, latentBgColor, latentFgColor for the latent ones), the latent variables are drawn as points unless causal.showLatentNames is true, and the color of the arcs is notebook.arcColor.

Returns
A string containing a Graphviz/DOT graph.

Definition at line 412 of file causalModel_tpl.h.

412 {
413 return toDot(dot::DotOptions());
414 }
std::string toDot() const
DOT representation of the causal DAG (observed + latent), with the look of the configuration.

References toDot().

Referenced by operator=(), toDot(), toDot(), and toDot().

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

◆ toDot() [2/3]

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalModel< GUM_SCALAR >::toDot ( const bool SHOW_LATENT_NAMES,
const char * NODE_BG = nullptr,
const char * NODE_FG = nullptr,
const char * EDGE_COL = nullptr ) const

DOT representation of the causal DAG (observed + latent), with values given by position.

Deprecated
Use toDot() and the configuration, or toDot(const dot::DotOptions&).
Parameters
SHOW_LATENT_NAMESIf true, display latent names explicitly.
NODE_BGNode background color (hex code like "#404040" or Graphviz color name), for all the nodes ; the configuration if nullptr.
NODE_FGNode label/text color, for all the nodes ; the configuration if nullptr.
EDGE_COLEdge color ; the configuration if nullptr.
Returns
A string containing a Graphviz/DOT graph.

Definition at line 422 of file causalModel_tpl.h.

425 {
427 style.showLatentNames = SHOW_LATENT_NAMES;
428 if (EDGE_COL != nullptr) style.arcColor = EDGE_COL;
429 if (NODE_BG != nullptr) {
430 style.nodeBgColor = NODE_BG;
431 style.latentBgColor = NODE_BG;
432 }
433 if (NODE_FG != nullptr) {
434 style.nodeFgColor = NODE_FG;
435 style.latentFgColor = NODE_FG;
436 }
437 return _toDot_(style, dot::DotOptions());
438 }
std::string _toDot_(const dot::CausalDotStyle &style, const dot::DotOptions &options) const
the dot of the causal DAG, with a look and options
static CausalDotStyle read()
Definition dotStyle.cpp:155

References _toDot_(), gum::dot::CausalDotStyle::read(), and toDot().

Here is the call graph for this function:

◆ toDot() [3/3]

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalModel< GUM_SCALAR >::toDot ( const dot::DotOptions & options) const

DOT representation of the causal DAG (observed + latent), with the options given by the caller.

The nodes are named after the variables (L<id> for a latent variable without name), with the tooltip "(id) name" ; the options are indexed by the identifiers of the nodes of the causal DAG.

Returns
A string containing a Graphviz/DOT graph.

Definition at line 417 of file causalModel_tpl.h.

417 {
419 }

References _toDot_(), gum::dot::CausalDotStyle::read(), and toDot().

Here is the call graph for this function:

◆ variable() [1/2]

template<GUM_Numeric GUM_SCALAR>
const DiscreteVariable & gum::CausalModel< GUM_SCALAR >::variable ( NodeId id) const

From id to variable (observed only, by id).

Throws if id does not correspond to an observed variable.

Parameters
idthe node id
Exceptions
throwsgum::NotFound if id does not correspond to an observed variable in the model
throwsgum::OperationNotAllowed if the model has no observational BN (see hasObservationalBN()).
Returns
the variable if exists

Definition at line 596 of file causalModel_tpl.h.

596 {
597 return observationalBN().variable(id);
598 }

References observationalBN(), and variable().

Referenced by operator=(), variable(), and variable().

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

◆ variable() [2/2]

template<GUM_Numeric GUM_SCALAR>
const DiscreteVariable & gum::CausalModel< GUM_SCALAR >::variable ( std::string_view name) const

From name to variable (observed only, by id).

Throws if id does not correspond to an observed variable.

Parameters
name
Exceptions
throwsgum::NotFound if id does not correspond to an observed variable in the model
throwsgum::OperationNotAllowed if the model has no observational BN (see hasObservationalBN()).
Returns
the variable if exists

Definition at line 601 of file causalModel_tpl.h.

601 {
602 return observationalBN().variable(idFromName(name));
603 }

References idFromName(), observationalBN(), and variable().

Here is the call graph for this function:

Member Data Documentation

◆ _causalDAG_

template<GUM_Numeric GUM_SCALAR>
DAG gum::CausalModel< GUM_SCALAR >::_causalDAG_
private

The underlying DAG representing the causal structure (observed + latent).

Definition at line 110 of file causalModel.h.

Referenced by CausalModel(), CausalModel(), CausalModel(), CausalModel(), CausalModel(), _toDot_(), assumeSpurious(), causalDAG(), children(), connectedComponents(), existsArc(), isAssumedSpurious(), and parents().

◆ _ids_names_

template<GUM_Numeric GUM_SCALAR>
Bijection< NodeId, std::string > gum::CausalModel< GUM_SCALAR >::_ids_names_
private

Bidirectional mapping between node ids and variable names (latents only).

Definition at line 113 of file causalModel.h.

Referenced by CausalModel(), CausalModel(), id2name(), idFromName(), latentVariablesIds(), latentVariablesNames(), nameFromId(), and names().

◆ _observationalBN_

template<GUM_Numeric GUM_SCALAR>
std::optional< BayesNet< GUM_SCALAR > > gum::CausalModel< GUM_SCALAR >::_observationalBN_
private

The observed Bayesian network, when the model was built with CPTs. Absent when built from a plain DAG — see hasObservationalBN().

Definition at line 103 of file causalModel.h.

Referenced by CausalModel(), CausalModel(), CausalModel(), CausalModel(), hasObservationalBN(), and observationalBN().

◆ _observedDAG_

template<GUM_Numeric GUM_SCALAR>
DAG gum::CausalModel< GUM_SCALAR >::_observedDAG_
private

Frozen copy of the observed DAG (never mutated after construction); names are set on it directly (setName) — the single source of truth for observed variable names.

Definition at line 107 of file causalModel.h.

Referenced by CausalModel(), CausalModel(), CausalModel(), CausalModel(), CausalModel(), addLatentVariable(), assumeNonSpurious(), assumeSpurious(), id2name(), idFromName(), inducedCausalSubModel(), isAssumedSpurious(), nameFromId(), and names().


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