![]() |
aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
|
A causal model pairing an observational BayesNet with a causal DAG. More...
#include <causalModel.h>
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 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 bool SHOW_LATENT_NAMES=false, const char *NODE_BG="#404040", const char *NODE_FG="white", const char *EDGE_COL="#4A4A4A") const |
| DOT representation of the causal DAG (observed + latent). | |
| 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). | |
| HashTable< NodeId, NodeSet > | 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 Attributes | |
| BayesNet< GUM_SCALAR > | _observationalBN_ |
| The underlying BayesNet representing the observed part of the model. | |
| 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 (observed + latent). | |
A causal model pairing an observational BayesNet with a causal DAG.
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.
Definition at line 84 of file causalModel.h.
|
delete |
Default constructor disabled: a causal model must be built from a BN.
References CausalModel(), assumeNonSpurious(), and observationalBN().
Referenced by CausalModel(), CausalModel(), CausalModel(), CausalModel(), CausalModel(), ~CausalModel(), operator=(), and operator=().
|
explicit |
Construct a causal model with no latent variables.
| observationalBN | the observed Bayesian network |
Initializes the causal DAG as the BN's DAG (observed-only).
Definition at line 61 of file causalModel_tpl.h.
References CausalModel(), _causalDAG_, _observationalBN_, and observationalBN().
|
explicit |
Construct a causal model and add a list of latent confounders.
| observationalBN | the observed Bayesian network |
| latentVarsDescriptor | list of (latentName, childrenIds) descriptors |
| assumeNonSpurious | whether 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 72 of file causalModel_tpl.h.
References CausalModel(), _causalDAG_, _observationalBN_, addLatentVariable(), assumeNonSpurious(), children(), and observationalBN().
| gum::CausalModel< GUM_SCALAR >::CausalModel | ( | const CausalModel< GUM_SCALAR > & | other | ) |
Copy constructor.
Definition at line 85 of file causalModel_tpl.h.
References CausalModel(), _causalDAG_, _ids_names_, and _observationalBN_.
|
noexcept |
Move constructor.
Definition at line 93 of file causalModel_tpl.h.
References CausalModel(), _causalDAG_, _ids_names_, and _observationalBN_.
| gum::CausalModel< GUM_SCALAR >::~CausalModel | ( | ) |
Destructor.
Definition at line 67 of file causalModel_tpl.h.
References CausalModel().
| 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.
| latentName | new latent variable name |
| childrenOfLatent | node ids of observed children |
| assumeNonSpurious | preserve 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.
Definition at line 117 of file causalModel_tpl.h.
References _observationalBN_, assumeNonSpurious(), and GUM_ERROR.
| 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.
| latentName | new latent variable name |
| childrenOfLatent | names of observed children |
| assumeNonSpurious | preserve 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.
Definition at line 104 of file causalModel_tpl.h.
References addLatentVariable(), assumeNonSpurious(), and idFromName().
Referenced by CausalModel(), and addLatentVariable().
| 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 222 of file causalModel_tpl.h.
Referenced by CausalModel(), CausalModel(), addLatentVariable(), addLatentVariable(), assumeNonSpurious(), and operator=().
| void gum::CausalModel< GUM_SCALAR >::assumeNonSpurious | ( | std::string_view | x, |
| std::string_view | y ) |
Definition at line 234 of file causalModel_tpl.h.
References assumeNonSpurious(), and idFromName().
| 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 205 of file causalModel_tpl.h.
References _causalDAG_, _observationalBN_, and GUM_ERROR.
Referenced by assumeSpurious().
| void gum::CausalModel< GUM_SCALAR >::assumeSpurious | ( | std::string_view | x, |
| std::string_view | y ) |
Definition at line 217 of file causalModel_tpl.h.
References assumeSpurious(), and idFromName().
| 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:
Definition at line 389 of file causalModel_tpl.h.
References _causalDAG_, gum::Set< Key >::contains(), gum::DoorCriteria::enumerateBackdoorSets(), GUM_ERROR, and latentVariablesIds().
Referenced by backDoor().
| std::optional< NodeSet > gum::CausalModel< GUM_SCALAR >::backDoor | ( | std::string_view | cause, |
| std::string_view | effect ) const |
Definition at line 562 of file causalModel_tpl.h.
References backDoor(), and idFromName().
| DAG gum::CausalModel< GUM_SCALAR >::causalDAG | ( | ) | const |
Causal DAG (observed + latent variables), with node names set.
Definition at line 554 of file causalModel_tpl.h.
References _causalDAG_, nameFromId(), and gum::NodeGraphPart::setName().
Referenced by gum::DoCalculus< GUM_SCALAR >::_ID_().
| NodeSet gum::CausalModel< GUM_SCALAR >::children | ( | NodeId | x | ) | const |
Children of a node (by id) in the causal DAG (including latents).
Definition at line 375 of file causalModel_tpl.h.
References _causalDAG_.
Referenced by CausalModel(), gum::DoCalculus< GUM_SCALAR >::_cDecomposition_(), and children().
| 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 380 of file causalModel_tpl.h.
References children(), and idFromName().
| HashTable< NodeId, NodeSet > gum::CausalModel< GUM_SCALAR >::connectedComponents | ( | ) | const |
Weakly connected components of the causal DAG.
Definition at line 261 of file causalModel_tpl.h.
References _causalDAG_, gum::Set< Key >::begin(), gum::Set< Key >::empty(), and gum::Set< Key >::insert().
| 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 167 of file causalModel_tpl.h.
References _causalDAG_.
Referenced by existsArc().
| 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 172 of file causalModel_tpl.h.
References existsArc(), and idFromName().
| 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:
Definition at line 403 of file causalModel_tpl.h.
References _causalDAG_, gum::Set< Key >::contains(), gum::DoorCriteria::enumerateFrontdoorSets(), GUM_ERROR, and latentVariablesIds().
Referenced by frontDoor().
| 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 | name (string_view) of the cause (X). |
| effect | name (string_view) of the effect (Y). |
Definition at line 568 of file causalModel_tpl.h.
References frontDoor(), and idFromName().
| Bijection< NodeId, std::string > gum::CausalModel< GUM_SCALAR >::id2name | ( | bool | includeLatentVariable = false | ) | const |
Bidirectional mapping between node ids and variable names.
Definition at line 526 of file causalModel_tpl.h.
References _ids_names_, _observationalBN_, and gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::insert().
| NodeId gum::CausalModel< GUM_SCALAR >::idFromName | ( | std::string_view | name | ) | const |
Node id from variable name (observed or latent).
Definition at line 507 of file causalModel_tpl.h.
References _ids_names_, and _observationalBN_.
Referenced by addLatentVariable(), assumeNonSpurious(), assumeSpurious(), backDoor(), children(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::counterfactualModel(), existsArc(), frontDoor(), parents(), and variable().
| CausalModel< GUM_SCALAR > gum::CausalModel< GUM_SCALAR >::inducedCausalSubModel | ( | const CausalModel< GUM_SCALAR > & | cm, |
| NodeSet | subset ) const |
Induced causal submodel on a subset of nodes.
| cm | a source causal model |
| subset | node set to keep (observed + latent) |
Definition at line 305 of file causalModel_tpl.h.
Referenced by gum::DoCalculus< GUM_SCALAR >::_ID_().
| 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 239 of file causalModel_tpl.h.
References _causalDAG_, and _observationalBN_.
| bool gum::CausalModel< GUM_SCALAR >::isAssumedSpurious | ( | std::string_view | x, |
| std::string_view | y ) const |
Definition at line 245 of file causalModel_tpl.h.
| NodeSet gum::CausalModel< GUM_SCALAR >::latentVariablesIds | ( | ) | const |
Node ids of all latent variables.
Definition at line 183 of file causalModel_tpl.h.
References _ids_names_, and gum::Set< Key >::insert().
Referenced by gum::DoCalculus< GUM_SCALAR >::_cDecomposition_(), gum::DoCalculus< GUM_SCALAR >::_ID_(), backDoor(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::counterfactualModel(), frontDoor(), and toDot().
| Set< std::string > gum::CausalModel< GUM_SCALAR >::latentVariablesNames | ( | ) | const |
| std::string gum::CausalModel< GUM_SCALAR >::nameFromId | ( | NodeId | id | ) | const |
Variable name from node id (observed or latent).
Definition at line 516 of file causalModel_tpl.h.
References _ids_names_, and _observationalBN_.
Referenced by gum::DoCalculus< GUM_SCALAR >::_hedgeExceptionMsg(), gum::DoCalculus< GUM_SCALAR >::_ID_(), causalDAG(), and toDot().
| Set< std::string > gum::CausalModel< GUM_SCALAR >::names | ( | ) | const |
All variable names appearing in the causal model (observed + latent).
Definition at line 494 of file causalModel_tpl.h.
References _ids_names_, _observationalBN_, and names().
Referenced by names().
| const BayesNet< GUM_SCALAR > & gum::CausalModel< GUM_SCALAR >::observationalBN | ( | ) | const |
Observational BN (observed variables only).
Definition at line 549 of file causalModel_tpl.h.
References _observationalBN_.
Referenced by CausalModel(), CausalModel(), CausalModel(), gum::Counterfactual< GUM_ELEMENT >::_adaptToOriginalVariables_(), gum::DoCalculus< GUM_SCALAR >::_cDecomposition_(), gum::DoCalculus< GUM_SCALAR >::_ID_(), gum::Counterfactual< GUM_ELEMENT >::_idAssignToNameAssign_(), gum::Counterfactual< GUM_ELEMENT >::_idsToNames_(), gum::DoCalculus< GUM_SCALAR >::_topoObserved_(), gum::CausalModel< GUM_ELEMENT >::children(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::Counterfactual< GUM_ELEMENT >::counterFactualModel(), gum::counterfactualModel(), and gum::CausalModel< GUM_ELEMENT >::latentVariablesIds().
|
defaultnoexcept |
Move assignment.
References CausalModel(), and assumeNonSpurious().
|
default |
| NodeSet gum::CausalModel< GUM_SCALAR >::parents | ( | NodeId | x | ) | const |
Parents of a node (by id) in the causal DAG (including latents).
Definition at line 364 of file causalModel_tpl.h.
References _causalDAG_.
Referenced by parents().
| 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 370 of file causalModel_tpl.h.
References idFromName(), and parents().
| std::string gum::CausalModel< GUM_SCALAR >::toDot | ( | const bool | SHOW_LATENT_NAMES = false, |
| const char * | NODE_BG = "#404040", | ||
| const char * | NODE_FG = "white", | ||
| const char * | EDGE_COL = "#4A4A4A" ) const |
DOT representation of the causal DAG (observed + latent).
| SHOW_LATENT_NAMES | If true, display latent names explicitly. |
| NODE_BG | Node background color (hex code like "#404040" or Graphviz color name like "lightgray"). |
| NODE_FG | Node label/text color (hex code or Graphviz color name). |
| EDGE_COL | Edge color (hex code or Graphviz color name). |
Definition at line 420 of file causalModel_tpl.h.
References _causalDAG_, gum::contains(), latentVariablesIds(), and nameFromId().
| 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.
| id | the node id |
| throws | gum::NotFound if id does not correspond to an observed variable in the model |
Definition at line 574 of file causalModel_tpl.h.
References _observationalBN_.
| 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.
| name |
| throws | gum::NotFound if id does not correspond to an observed variable in the model |
Definition at line 579 of file causalModel_tpl.h.
References _observationalBN_, and idFromName().
|
private |
The underlying DAG representing the causal structure (observed + latent).
Definition at line 90 of file causalModel.h.
Referenced by CausalModel(), CausalModel(), CausalModel(), CausalModel(), assumeSpurious(), backDoor(), causalDAG(), children(), connectedComponents(), existsArc(), frontDoor(), isAssumedSpurious(), parents(), and toDot().
|
private |
Bidirectional mapping between node ids and variable names (observed + latent).
Definition at line 93 of file causalModel.h.
Referenced by CausalModel(), CausalModel(), id2name(), idFromName(), latentVariablesIds(), nameFromId(), and names().
|
private |
The underlying BayesNet representing the observed part of the model.
Definition at line 87 of file causalModel.h.
Referenced by CausalModel(), CausalModel(), CausalModel(), CausalModel(), addLatentVariable(), assumeSpurious(), id2name(), idFromName(), isAssumedSpurious(), nameFromId(), names(), observationalBN(), variable(), and variable().