![]() |
aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
|
Computes a counterfactual distribution by building a twin model, then evaluating a causal effect on that twin and adapting the result back to the original model’s variables. More...
#include <counterfactual.h>
Public Types | |
| using | VarName = std::string |
| using | ValName = std::string |
Public Member Functions | |
| Counterfactual (const CausalModel< GUM_ELEMENT > &cm, const Set< std::string > &on, const Set< std::string > &whatif, const HashTable< VarName, ValName > &profile=HashTable< VarName, ValName >(), const HashTable< VarName, ValName > &values=HashTable< VarName, ValName >()) | |
| Construct a counterfactual query using variable names. | |
| Counterfactual (const CausalModel< GUM_ELEMENT > &cm, const NodeSet &onIds, const NodeSet &whatifIds, const HashTable< NodeId, Idx > &profileIds=HashTable< NodeId, Idx >(), const HashTable< NodeId, Idx > &valuesIds=HashTable< NodeId, Idx >()) | |
| Construct a counterfactual query using IDs (nodes/value IDs). | |
| void | run () |
| Rebuild the internal CausalImpact (ctor already calls this). | |
| const CausalModel< GUM_ELEMENT > & | originalModel () const |
| const CausalModel< GUM_ELEMENT > & | twinModel () const |
| const CausalImpact< GUM_ELEMENT > & | impact () const |
| Symbolic result produced on the twin model. | |
| const Tensor< GUM_ELEMENT > & | value () const |
| Numeric result adapted to the original model’s variables. | |
| const Set< std::string > & | on () const |
| const Set< std::string > & | whatif () const |
| const HashTable< VarName, ValName > & | profile () const |
| const HashTable< VarName, ValName > & | values () const |
| std::string | toString () const |
Static Public Member Functions | |
| static CausalModel< GUM_ELEMENT > | counterFactualModel (const CausalModel< GUM_ELEMENT > &cm, const HashTable< VarName, ValName > &profile, const Set< std::string > &whatif) |
| static CausalModel< GUM_ELEMENT > | counterFactualModel (const CausalModel< GUM_ELEMENT > &cm, const HashTable< NodeId, Idx > &profileIds, const NodeSet &whatifIds) |
Private Member Functions | |
| const CausalFormula< GUM_ELEMENT > & | _ciResult () const |
Static Private Member Functions | |
| static Tensor< GUM_ELEMENT > | _adaptToOriginalVariables_ (const Tensor< GUM_ELEMENT > &adj, const CausalModel< GUM_ELEMENT > &cm) |
| static Set< std::string > | _idsToNames_ (const CausalModel< GUM_ELEMENT > &cm, const NodeSet &ids) |
| static HashTable< VarName, ValName > | _idAssignToNameAssign_ (const CausalModel< GUM_ELEMENT > &cm, const HashTable< NodeId, Idx > &idAssign) |
Private Attributes | |
| const CausalModel< GUM_ELEMENT > & | _cm |
| Set< std::string > | _on |
| Set< std::string > | _whatif |
| HashTable< VarName, ValName > | _profile |
| HashTable< VarName, ValName > | _values |
| CausalModel< GUM_ELEMENT > | _twin |
| Tensor< GUM_ELEMENT > | _adaptedValue |
| std::unique_ptr< CausalImpact< GUM_ELEMENT > > | _ci |
Computes a counterfactual distribution by building a twin model, then evaluating a causal effect on that twin and adapting the result back to the original model’s variables.
High-level workflow:
Constructors:
Results:
Notes:
| GUM_ELEMENT | Numeric type used in potentials (e.g., double). |
Definition at line 102 of file counterfactual.h.
| using gum::Counterfactual< GUM_ELEMENT >::ValName = std::string |
Definition at line 105 of file counterfactual.h.
| using gum::Counterfactual< GUM_ELEMENT >::VarName = std::string |
Definition at line 104 of file counterfactual.h.
| gum::Counterfactual< GUM_ELEMENT >::Counterfactual | ( | const CausalModel< GUM_ELEMENT > & | cm, |
| const Set< std::string > & | on, | ||
| const Set< std::string > & | whatif, | ||
| const HashTable< VarName, ValName > & | profile = HashTable< VarName, ValName >(), | ||
| const HashTable< VarName, ValName > & | values = HashTable< VarName, ValName >() ) |
Construct a counterfactual query using variable names.
Builds a twin model from cm using profile as observational evidence to update idiosyncratic roots, then evaluates the causal effect P(on | do(whatif), …). If values is provided, the evaluated tensor is optionally sliced by these assignments (only for variables present in the result), and finally adapted to the original BN’s variables.
| cm | Source causal model (provides the observational BN). |
| on | Set of variable names to query in the effect. |
| whatif | Set of variable names intervened by do(·). |
| profile | Name→label evidence used when computing posteriors in the original BN for idiosyncratic roots. May be empty. |
| values | Name→label assignments for the intervention values to use during numeric evaluation and optional slicing. May be empty. |
Definition at line 53 of file counterfactual_tpl.h.
References _adaptedValue, _ci, _cm, _on, _profile, _twin, _values, _whatif, counterFactualModel(), on(), profile(), run(), values(), and whatif().
Referenced by Counterfactual().
| gum::Counterfactual< GUM_ELEMENT >::Counterfactual | ( | const CausalModel< GUM_ELEMENT > & | cm, |
| const NodeSet & | onIds, | ||
| const NodeSet & | whatifIds, | ||
| const HashTable< NodeId, Idx > & | profileIds = HashTable< NodeId, Idx >(), | ||
| const HashTable< NodeId, Idx > & | valuesIds = HashTable< NodeId, Idx >() ) |
Construct a counterfactual query using IDs (nodes/value IDs).
Same semantics as the names-based constructor, but using node identifiers and value identifiers directly.
| cm | Source causal model. |
| onIds | Node IDs to query in the effect. |
| whatifIds | Node IDs to intervene on via do(·). |
| profileIds | NodeId→ValueId evidence for posterior update of idiosyncratic roots. May be empty. |
| valuesIds | NodeId→ValueId assignments for the intervention values during numeric evaluation and optional slicing. May be empty. |
Definition at line 64 of file counterfactual_tpl.h.
References Counterfactual(), _idAssignToNameAssign_(), and _idsToNames_().
|
staticprivate |
Definition at line 189 of file counterfactual_tpl.h.
References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::CausalModel< GUM_SCALAR >::observationalBN(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().
Referenced by run().
|
private |
Definition at line 206 of file counterfactual_tpl.h.
References _ci.
Referenced by toString().
|
staticprivate |
Definition at line 289 of file counterfactual_tpl.h.
References gum::HashTable< Key, Val >::insert(), and gum::CausalModel< GUM_SCALAR >::observationalBN().
Referenced by Counterfactual().
|
staticprivate |
Definition at line 275 of file counterfactual_tpl.h.
References gum::Set< Key >::insert(), and gum::CausalModel< GUM_SCALAR >::observationalBN().
Referenced by Counterfactual().
|
static |
Definition at line 127 of file counterfactual_tpl.h.
References gum::GraphicalModelInference< GUM_SCALAR >::addEvidence(), gum::HashTable< Key, Val >::empty(), gum::Set< Key >::erase(), gum::Set< Key >::insert(), gum::CausalModel< GUM_SCALAR >::latentVariablesIds(), gum::GraphicalModelInference< GUM_SCALAR >::makeInference(), gum::CausalModel< GUM_SCALAR >::observationalBN(), and gum::JointTargetedInference< GUM_SCALAR >::posterior().
|
static |
Definition at line 78 of file counterfactual_tpl.h.
References gum::GraphicalModelInference< GUM_SCALAR >::addEvidence(), gum::Set< Key >::erase(), gum::CausalModel< GUM_SCALAR >::idFromName(), gum::Set< Key >::insert(), gum::CausalModel< GUM_SCALAR >::latentVariablesIds(), gum::GraphicalModelInference< GUM_SCALAR >::makeInference(), gum::CausalModel< GUM_SCALAR >::observationalBN(), gum::JointTargetedInference< GUM_SCALAR >::posterior(), profile(), and whatif().
Referenced by Counterfactual().
| const CausalImpact< GUM_ELEMENT > & gum::Counterfactual< GUM_ELEMENT >::impact | ( | ) | const |
Symbolic result produced on the twin model.
Exposes the CausalImpact built over the twin model. Variable names follow those of the original BN.
Definition at line 379 of file counterfactual_tpl.h.
References _ci.
| const Set< std::string > & gum::Counterfactual< GUM_ELEMENT >::on | ( | ) | const |
Definition at line 389 of file counterfactual_tpl.h.
References _on.
Referenced by Counterfactual().
| const CausalModel< GUM_ELEMENT > & gum::Counterfactual< GUM_ELEMENT >::originalModel | ( | ) | const |
| const HashTable< typename Counterfactual< GUM_ELEMENT >::VarName, typename Counterfactual< GUM_ELEMENT >::ValName > & gum::Counterfactual< GUM_ELEMENT >::profile | ( | ) | const |
Definition at line 401 of file counterfactual_tpl.h.
References _profile.
Referenced by Counterfactual(), and counterFactualModel().
| void gum::Counterfactual< GUM_ELEMENT >::run | ( | ) |
Rebuild the internal CausalImpact (ctor already calls this).
Definition at line 169 of file counterfactual_tpl.h.
References _adaptedValue, _adaptToOriginalVariables_(), _ci, _cm, _on, _twin, _values, _whatif, and gum::makeInstantiationFromValues().
Referenced by Counterfactual().
| std::string gum::Counterfactual< GUM_ELEMENT >::toString | ( | ) | const |
Definition at line 213 of file counterfactual_tpl.h.
References _adaptedValue, _ci, _ciResult(), _on, _profile, _values, and _whatif.
| const CausalModel< GUM_ELEMENT > & gum::Counterfactual< GUM_ELEMENT >::twinModel | ( | ) | const |
Definition at line 374 of file counterfactual_tpl.h.
References _twin.
| const Tensor< GUM_ELEMENT > & gum::Counterfactual< GUM_ELEMENT >::value | ( | ) | const |
Numeric result adapted to the original model’s variables.
The tensor’s variable sequence mirrors that of the evaluated effect on the twin, but each variable object belongs to the original BN (so downstream code may use original variable references).
Definition at line 384 of file counterfactual_tpl.h.
References _adaptedValue.
Referenced by gum::counterfactual().
| const HashTable< typename Counterfactual< GUM_ELEMENT >::VarName, typename Counterfactual< GUM_ELEMENT >::ValName > & gum::Counterfactual< GUM_ELEMENT >::values | ( | ) | const |
Definition at line 408 of file counterfactual_tpl.h.
References _values.
Referenced by Counterfactual().
| const Set< std::string > & gum::Counterfactual< GUM_ELEMENT >::whatif | ( | ) | const |
Definition at line 394 of file counterfactual_tpl.h.
References _whatif.
Referenced by Counterfactual(), and counterFactualModel().
|
private |
Definition at line 224 of file counterfactual.h.
Referenced by Counterfactual(), run(), toString(), and value().
|
private |
Definition at line 226 of file counterfactual.h.
Referenced by Counterfactual(), _ciResult(), impact(), run(), and toString().
|
private |
Definition at line 217 of file counterfactual.h.
Referenced by Counterfactual(), originalModel(), and run().
|
private |
Definition at line 218 of file counterfactual.h.
Referenced by Counterfactual(), on(), run(), and toString().
|
private |
Definition at line 220 of file counterfactual.h.
Referenced by Counterfactual(), profile(), and toString().
|
private |
Definition at line 223 of file counterfactual.h.
Referenced by Counterfactual(), run(), and twinModel().
|
private |
Definition at line 221 of file counterfactual.h.
Referenced by Counterfactual(), run(), toString(), and values().
|
private |
Definition at line 219 of file counterfactual.h.
Referenced by Counterfactual(), run(), toString(), and whatif().