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

A container for an identified causal query. More...

#include <causalFormula.h>

Collaboration diagram for gum::CausalFormula< GUM_SCALAR >:

Public Member Functions

 CausalFormula (const CausalModel< GUM_SCALAR > &cm, std::unique_ptr< ASTtree< GUM_SCALAR > > root, const Set< std::string > &on, const Set< std::string > &doing, const Set< std::string > &knowing=Set< std::string >{}, std::string_view explanation="")
 Constructs a CausalFormula object (variables given by names).
 CausalFormula (const CausalModel< GUM_SCALAR > &cm, std::unique_ptr< ASTtree< GUM_SCALAR > > root, const NodeSet &on, const NodeSet &doing, const NodeSet &knowing=NodeSet{}, std::string_view explanation="")
 Constructs a CausalFormula object (variables given by node ids).
 CausalFormula (const CausalFormula &other)
 Copy constructor — performs a deep copy of the AST.
 CausalFormula (CausalFormula &&)=default
 Move constructor — defaulted (suppressed by the user-defined copy constructor above).
Tensor< GUM_SCALAR > eval () const
 Evaluates the formula's AST to compute the resulting probability distribution.
std::string toString () const
 Generates a string representation of the formula's AST.
std::string toLatex (std::string_view doOperatorPrefix="do(", std::string_view doOperatorSuffix=")") const
 Generates a full LaTeX equation: Query = Formula.
std::string latexQuery (std::string_view doOperatorPrefix="do(", std::string_view doOperatorSuffix=")") const
 Generates a LaTeX representation of the original query, e.g., P(Y | do(X), Z).
std::unique_ptr< CausalFormula< GUM_SCALAR > > copy () const
 Creates a deep copy of the CausalFormula (including its AST).
bool isIdentified () const noexcept
 Whether the causal effect has been identified.
const ASTtree< GUM_SCALAR > & root () const
 Access the root AST node of the identified formula.
const CausalModel< GUM_SCALAR > & cm () const
const NodeSeton () const
const NodeSetdoing () const
const NodeSetknowing () const
const std::string & explanation () const
std::vector< std::string > onNames () const
 Convenience: return names corresponding to stored node ids (sorted).
std::vector< std::string > doingNames () const
std::vector< std::string > knowingNames () const

Private Member Functions

void _ensureVariablesExist () const
 Verifies that all variables (_on, _knowing, _doing) exist in the BN.
void _ensureNoVariablesOverlap () const
 Verifies that all variables (_on, _knowing, _doing) have no intersection.

Static Private Member Functions

static NodeSet _toNodeSetFromNames_ (const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &names)
 Convert a set of names to a set of node ids (validates existence).

Private Attributes

const CausalModel< GUM_SCALAR > & _cm
std::unique_ptr< ASTtree< GUM_SCALAR > > _root
const NodeSet _on
const NodeSet _doing
const NodeSet _knowing
const std::string _explanation

Detailed Description

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

A container for an identified causal query.

This class binds an identified formula (as an ASTtree) to the context of the query that produced it: the CausalModel and the sets of variables for on, doing, and knowing.

It provides methods to:

  • Evaluate the formula to get a numeric Tensor (eval).
  • Display the formula as a string (toString) or LaTeX (toLatex).
  • Display the original query that generated the formula (latexQuery).
Template Parameters
GUM_SCALARThe numeric type for tensor computations.

Definition at line 74 of file causalFormula.h.

Constructor & Destructor Documentation

◆ CausalFormula() [1/4]

template<GUM_Numeric GUM_SCALAR>
gum::CausalFormula< GUM_SCALAR >::CausalFormula ( const CausalModel< GUM_SCALAR > & cm,
std::unique_ptr< ASTtree< GUM_SCALAR > > root,
const Set< std::string > & on,
const Set< std::string > & doing,
const Set< std::string > & knowing = Set< std::string >{},
std::string_view explanation = "" )

Constructs a CausalFormula object (variables given by names).

Parameters
cmThe causal model against which the formula was identified.
rootThe identified formula as an Abstract Syntax Tree (ownership moved).
onThe set of outcome variable names (the "Y" in P(Y|...)).
doingThe set of intervention variable names (the "do(X)")).
knowingThe set of conditioning variable names (optional evidence).
explanationA human-readable explanation of the identification method used (optional).

Definition at line 97 of file causalFormula_tpl.h.

102 :
108 }
A container for an identified causal query.
const NodeSet & on() const
const std::string _explanation
const NodeSet _knowing
static NodeSet _toNodeSetFromNames_(const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &names)
Convert a set of names to a set of node ids (validates existence).
const NodeSet _on
const CausalModel< GUM_SCALAR > & _cm
const NodeSet & knowing() const
const std::string & explanation() const
const CausalModel< GUM_SCALAR > & cm() const
void _ensureNoVariablesOverlap() const
Verifies that all variables (_on, _knowing, _doing) have no intersection.
const NodeSet & doing() const
std::unique_ptr< ASTtree< GUM_SCALAR > > _root
void _ensureVariablesExist() const
Verifies that all variables (_on, _knowing, _doing) exist in the BN.
const ASTtree< GUM_SCALAR > & root() const
Access the root AST node of the identified formula.
const NodeSet _doing

References _cm, _doing, _ensureNoVariablesOverlap(), _ensureVariablesExist(), _explanation, _knowing, _on, _root, _toNodeSetFromNames_(), cm(), doing(), explanation(), knowing(), on(), and root().

Referenced by CausalFormula(), and CausalFormula().

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

◆ CausalFormula() [2/4]

template<GUM_Numeric GUM_SCALAR>
gum::CausalFormula< GUM_SCALAR >::CausalFormula ( const CausalModel< GUM_SCALAR > & cm,
std::unique_ptr< ASTtree< GUM_SCALAR > > root,
const NodeSet & on,
const NodeSet & doing,
const NodeSet & knowing = NodeSet{},
std::string_view explanation = "" )

Constructs a CausalFormula object (variables given by node ids).

Parameters
cmThe causal model against which the formula was identified.
rootThe identified formula as an Abstract Syntax Tree (ownership moved).
onThe set of outcome variable ids (the "Y" in P(Y|...)).
doingThe set of intervention variable ids (the "do(X)")).
knowingThe set of conditioning variable ids (optional evidence).
explanationA human-readable explanation of the identification method used (optional).

Definition at line 111 of file causalFormula_tpl.h.

References _cm, _doing, _ensureNoVariablesOverlap(), _ensureVariablesExist(), _explanation, _knowing, _on, _root, cm(), doing(), explanation(), knowing(), on(), and root().

Here is the call graph for this function:

◆ CausalFormula() [3/4]

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

Copy constructor — performs a deep copy of the AST.

Required so that types containing CausalFormula (e.g. CausalImpact) remain copy-constructible. SWIG's SwigValueWrapper<T> stores the wrapped return value via copy construction, so without this the Windows/MSVC CI fails to compile any binding that returns CausalImpact by value.

Definition at line 124 of file causalFormula_tpl.h.

References CausalFormula(), _cm, _doing, _explanation, _knowing, _on, _root, and copy().

Here is the call graph for this function:

◆ CausalFormula() [4/4]

template<GUM_Numeric GUM_SCALAR>
gum::CausalFormula< GUM_SCALAR >::CausalFormula ( CausalFormula< GUM_SCALAR > && )
default

Move constructor — defaulted (suppressed by the user-defined copy constructor above).

References CausalFormula(), _ensureNoVariablesOverlap(), _ensureVariablesExist(), _toNodeSetFromNames_(), cm(), doing(), doingNames(), explanation(), knowing(), knowingNames(), on(), onNames(), and root().

Here is the call graph for this function:

Member Function Documentation

◆ _ensureNoVariablesOverlap()

template<GUM_Numeric GUM_SCALAR>
void gum::CausalFormula< GUM_SCALAR >::_ensureNoVariablesOverlap ( ) const
private

Verifies that all variables (_on, _knowing, _doing) have no intersection.

Exceptions
gum::OperationNotAllowedif any variable appears in two roles.

Definition at line 80 of file causalFormula_tpl.h.

80 {
81 // Ensure pairwise disjointness: on ∩ doing = ∅, on ∩ knowing = ∅, doing ∩ knowing = ∅
82 for (const auto& id: _on) {
83 if (_doing.contains(id))
84 GUM_ERROR(OperationNotAllowed, "Variable cannot be both in 'on' and 'doing'");
85 if (_knowing.contains(id))
86 GUM_ERROR(OperationNotAllowed, "Variable cannot be both in 'on' and 'knowing'");
87 }
88 for (const auto& id: _doing) {
89 if (_knowing.contains(id))
90 GUM_ERROR(OperationNotAllowed, "Variable cannot be both in 'doing' and 'knowing'");
91 }
92 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _doing, _knowing, _on, and GUM_ERROR.

Referenced by CausalFormula(), CausalFormula(), and CausalFormula().

Here is the caller graph for this function:

◆ _ensureVariablesExist()

template<GUM_Numeric GUM_SCALAR>
void gum::CausalFormula< GUM_SCALAR >::_ensureVariablesExist ( ) const
private

Verifies that all variables (_on, _knowing, _doing) exist in the BN.

Exceptions
gum::NotFoundif any variable id is not found.

Definition at line 68 of file causalFormula_tpl.h.

68 {
69 const auto& bn = _cm.observationalBN();
70 // Touch the variable object to ensure validity (throws NotFound if invalid)
71 for (const auto& id: _on)
72 (void)bn.variable(id);
73 for (const auto& id: _doing)
74 (void)bn.variable(id);
75 for (const auto& id: _knowing)
76 (void)bn.variable(id);
77 }

References _cm, _doing, _knowing, and _on.

Referenced by CausalFormula(), CausalFormula(), and CausalFormula().

Here is the caller graph for this function:

◆ _toNodeSetFromNames_()

template<GUM_Numeric GUM_SCALAR>
NodeSet gum::CausalFormula< GUM_SCALAR >::_toNodeSetFromNames_ ( const CausalModel< GUM_SCALAR > & cm,
const Set< std::string > & names )
staticprivate

Convert a set of names to a set of node ids (validates existence).

Definition at line 57 of file causalFormula_tpl.h.

58 {
60 const auto& bn = cm.observationalBN();
61 for (const auto& n: names) {
62 ids.insert(bn.idFromName(n)); // throws NotFound if unknown
63 }
64 return ids;
65 }

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

Referenced by CausalFormula(), and CausalFormula().

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

◆ cm()

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

Definition at line 275 of file causalFormula_tpl.h.

275 {
276 return _cm;
277 }

References _cm.

Referenced by CausalFormula(), CausalFormula(), CausalFormula(), and _toNodeSetFromNames_().

Here is the caller graph for this function:

◆ copy()

template<GUM_Numeric GUM_SCALAR>
std::unique_ptr< CausalFormula< GUM_SCALAR > > gum::CausalFormula< GUM_SCALAR >::copy ( ) const

Creates a deep copy of the CausalFormula (including its AST).

Definition at line 229 of file causalFormula_tpl.h.

229 {
231 _root->copy(),
232 _on,
233 _doing,
234 _knowing);
235 }

References _cm, _doing, _knowing, _on, and _root.

Referenced by CausalFormula().

Here is the caller graph for this function:

◆ doing()

template<GUM_Numeric GUM_SCALAR>
const NodeSet & gum::CausalFormula< GUM_SCALAR >::doing ( ) const

Definition at line 285 of file causalFormula_tpl.h.

285 {
286 return _doing;
287 }

References _doing.

Referenced by CausalFormula(), CausalFormula(), and CausalFormula().

Here is the caller graph for this function:

◆ doingNames()

template<GUM_Numeric GUM_SCALAR>
std::vector< std::string > gum::CausalFormula< GUM_SCALAR >::doingNames ( ) const

Definition at line 251 of file causalFormula_tpl.h.

251 {
252 const auto& bn = _cm.observationalBN();
254 out.reserve(_doing.size());
255 for (const auto& id: _doing)
256 out.emplace_back(bn.variable(id).name());
257 std::sort(out.begin(), out.end());
258 return out;
259 }

References _cm, and _doing.

Referenced by CausalFormula(), and latexQuery().

Here is the caller graph for this function:

◆ eval()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::CausalFormula< GUM_SCALAR >::eval ( ) const

Evaluates the formula's AST to compute the resulting probability distribution.

Definition at line 143 of file causalFormula_tpl.h.

143 {
144 if (!_root) {
146 "CausalFormula::eval() called on a non-identified formula.");
147 }
148 return _root->eval(_cm.observationalBN());
149 }

References _cm, _root, and GUM_ERROR.

◆ explanation()

template<GUM_Numeric GUM_SCALAR>
const std::string & gum::CausalFormula< GUM_SCALAR >::explanation ( ) const

Definition at line 295 of file causalFormula_tpl.h.

295 {
296 return _explanation;
297 }

References _explanation.

Referenced by CausalFormula(), CausalFormula(), and CausalFormula().

Here is the caller graph for this function:

◆ isIdentified()

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalFormula< GUM_SCALAR >::isIdentified ( ) const
noexcept

Whether the causal effect has been identified.

Returns true iff this formula has a valid AST (i.e., identification succeeded and evaluation is possible). When this returns false, calling root() or eval() will throw.

Returns
true if an identification AST exists, false otherwise.

Definition at line 132 of file causalFormula_tpl.h.

132 {
133 return static_cast< bool >(_root);
134 }

References _root.

◆ knowing()

template<GUM_Numeric GUM_SCALAR>
const NodeSet & gum::CausalFormula< GUM_SCALAR >::knowing ( ) const

Definition at line 290 of file causalFormula_tpl.h.

290 {
291 return _knowing;
292 }

References _knowing.

Referenced by CausalFormula(), CausalFormula(), and CausalFormula().

Here is the caller graph for this function:

◆ knowingNames()

template<GUM_Numeric GUM_SCALAR>
std::vector< std::string > gum::CausalFormula< GUM_SCALAR >::knowingNames ( ) const

Definition at line 262 of file causalFormula_tpl.h.

262 {
263 const auto& bn = _cm.observationalBN();
265 out.reserve(_knowing.size());
266 for (const auto& id: _knowing)
267 out.emplace_back(bn.variable(id).name());
268 std::sort(out.begin(), out.end());
269 return out;
270 }

References _cm, and _knowing.

Referenced by CausalFormula(), and latexQuery().

Here is the caller graph for this function:

◆ latexQuery()

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalFormula< GUM_SCALAR >::latexQuery ( std::string_view doOperatorPrefix = "do(",
std::string_view doOperatorSuffix = ")" ) const

Generates a LaTeX representation of the original query, e.g., P(Y | do(X), Z).

Note
This version does not yet support specific variable values.

Definition at line 181 of file causalFormula_tpl.h.

182 {
183 const auto& bn = _cm.observationalBN();
184
185 auto namesSorted = [&](const NodeSet& S) {
187 res.reserve(S.size());
188 for (const auto& id: S)
189 res.emplace_back(bn.variable(id).name());
190 std::sort(res.begin(), res.end());
191 return res;
192 };
193
194 const auto onNames = namesSorted(_on);
195 const auto doingNames = namesSorted(_doing);
196 const auto knowingNames = namesSorted(_knowing);
197
199 ss << "P\\left(";
200
201 for (size_t i = 0; i < onNames.size(); ++i) {
202 ss << onNames[i];
203 if (i + 1 < onNames.size()) ss << ",";
204 }
205
206 if (!doingNames.empty() || !knowingNames.empty()) {
207 ss << " \\mid ";
208
209 if (!doingNames.empty()) {
210 ss << doOperatorPrefix << doingNames.front();
211 for (size_t i = 1; i < doingNames.size(); ++i)
212 ss << "," << doingNames[i];
214 }
215
216 if (!knowingNames.empty()) {
217 if (!doingNames.empty()) ss << ",";
218 ss << knowingNames.front();
219 for (size_t i = 1; i < knowingNames.size(); ++i)
220 ss << "," << knowingNames[i];
221 }
222 }
223
224 ss << "\\right)";
225 return ss.str();
226 }
std::vector< std::string > doingNames() const
std::vector< std::string > knowingNames() const
std::vector< std::string > onNames() const
Convenience: return names corresponding to stored node ids (sorted).

References _cm, _doing, _knowing, _on, doingNames(), knowingNames(), and onNames().

Referenced by toLatex().

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

◆ on()

template<GUM_Numeric GUM_SCALAR>
const NodeSet & gum::CausalFormula< GUM_SCALAR >::on ( ) const

Definition at line 280 of file causalFormula_tpl.h.

280 {
281 return _on;
282 }

References _on.

Referenced by CausalFormula(), CausalFormula(), and CausalFormula().

Here is the caller graph for this function:

◆ onNames()

template<GUM_Numeric GUM_SCALAR>
std::vector< std::string > gum::CausalFormula< GUM_SCALAR >::onNames ( ) const

Convenience: return names corresponding to stored node ids (sorted).

Definition at line 240 of file causalFormula_tpl.h.

240 {
241 const auto& bn = _cm.observationalBN();
243 out.reserve(_on.size());
244 for (const auto& id: _on)
245 out.emplace_back(bn.variable(id).name());
246 std::sort(out.begin(), out.end());
247 return out;
248 }

References _cm, and _on.

Referenced by CausalFormula(), and latexQuery().

Here is the caller graph for this function:

◆ root()

template<GUM_Numeric GUM_SCALAR>
const ASTtree< GUM_SCALAR > & gum::CausalFormula< GUM_SCALAR >::root ( ) const

Access the root AST node of the identified formula.

Use this to inspect the symbolic structure of the identified effect.

Warning
This raises an exception if the effect is not identified.
Exceptions
gum::OperationNotAllowedif no AST is available (i.e., the effect is not identifiable with the current methods).
Returns
const ASTtree<GUM_SCALAR>& reference to the AST root.

Definition at line 137 of file causalFormula_tpl.h.

137 {
138 if (!_root) GUM_ERROR(OperationNotAllowed, "No AST: effect not identified.");
139 return *_root;
140 }

References _root, and GUM_ERROR.

Referenced by CausalFormula(), CausalFormula(), and CausalFormula().

Here is the caller graph for this function:

◆ toLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalFormula< GUM_SCALAR >::toLatex ( std::string_view doOperatorPrefix = "do(",
std::string_view doOperatorSuffix = ")" ) const

Generates a full LaTeX equation: Query = Formula.

Definition at line 158 of file causalFormula_tpl.h.

159 {
160 if (!_root) return "[unidentifiable]";
161
162 // Track variable name occurrences for prime management in AST LaTeX
164
165 const auto& bn = _cm.observationalBN();
166 auto addNames = [&](const NodeSet& S) {
167 for (const auto& id: S) {
168 const auto& v = bn.variable(id);
169 const auto& name = v.name();
170 if (!nameOccur.exists(name)) nameOccur.insert(name, 1);
171 }
172 };
173 addNames(_on);
176
177 return latexQuery(doOperatorPrefix, doOperatorSuffix) + " = " + _root->toLatex(nameOccur);
178 }
std::string latexQuery(std::string_view doOperatorPrefix="do(", std::string_view doOperatorSuffix=")") const
Generates a LaTeX representation of the original query, e.g., P(Y | do(X), Z).

References _cm, _doing, _knowing, _on, _root, gum::HashTable< Key, Val >::exists(), gum::HashTable< Key, Val >::insert(), and latexQuery().

Here is the call graph for this function:

◆ toString()

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalFormula< GUM_SCALAR >::toString ( ) const

Generates a string representation of the formula's AST.

Definition at line 152 of file causalFormula_tpl.h.

152 {
153 if (!_root) return "[unidentifiable]";
154 return _root->toString();
155 }

References _root.

Member Data Documentation

◆ _cm

template<GUM_Numeric GUM_SCALAR>
const CausalModel< GUM_SCALAR >& gum::CausalFormula< GUM_SCALAR >::_cm
private

◆ _doing

template<GUM_Numeric GUM_SCALAR>
const NodeSet gum::CausalFormula< GUM_SCALAR >::_doing
private

◆ _explanation

template<GUM_Numeric GUM_SCALAR>
const std::string gum::CausalFormula< GUM_SCALAR >::_explanation
private

Definition at line 205 of file causalFormula.h.

Referenced by CausalFormula(), CausalFormula(), CausalFormula(), and explanation().

◆ _knowing

template<GUM_Numeric GUM_SCALAR>
const NodeSet gum::CausalFormula< GUM_SCALAR >::_knowing
private

◆ _on

template<GUM_Numeric GUM_SCALAR>
const NodeSet gum::CausalFormula< GUM_SCALAR >::_on
private

◆ _root

template<GUM_Numeric GUM_SCALAR>
std::unique_ptr< ASTtree< GUM_SCALAR > > gum::CausalFormula< GUM_SCALAR >::_root
private

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