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

Builds a CausalFormula for a query (d-sep -> backdoor -> frontdoor -> (optional) do-calculus). More...

#include <causalImpact.h>

Collaboration diagram for gum::CausalImpact< GUM_SCALAR >:

Public Member Functions

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).
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 CausalFormula< GUM_SCALAR > & getResult () const
 Access the underlying CausalFormula result.
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
 CausalImpact (const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &on, const Set< std::string > &doing, const Set< std::string > &knowing=Set< std::string >(), bool directDoCalculus=false)
 Constructs a CausalImpact object using variable names.
 CausalImpact (const CausalModel< GUM_SCALAR > &cm, const NodeSet &on, const NodeSet &doing, const NodeSet &knowing=NodeSet(), bool directDoCalculus=false)
 Constructs a CausalImpact object using node IDs.
 CausalImpact (const CausalImpact &)=default
 Copy/move constructors — defaulted; require CausalFormula to be copy/move-constructible.
 CausalImpact (CausalImpact &&)=default

Static Private Member Functions

static Set< std::string > _idsToNames_ (const CausalModel< GUM_SCALAR > &cm, const NodeSet &ids)
static NodeSet _namesToIds_ (const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &names)
static bool _disjoint_ (const Set< std::string > &a, const Set< std::string > &b, const Set< std::string > &c)
static CausalFormula< GUM_SCALAR > _buildFromNames_ (const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &on, const Set< std::string > &doing, const Set< std::string > &knowing, bool directDoCalculus)
static CausalFormula< GUM_SCALAR > _buildFromIds_ (const CausalModel< GUM_SCALAR > &cm, const NodeSet &on, const NodeSet &doing, const NodeSet &knowing, bool directDoCalculus)

Private Attributes

bool _directDoCalculus_ = false
 If true, skip backdoor/frontdoor and use do-calculus directly.
CausalFormula< GUM_SCALAR > _resultFormula_
 Identified symbolic formula. If not identifiable, its AST may be nullptr and its explanation string (6th ctor arg) will say so.

Detailed Description

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

Builds a CausalFormula for a query (d-sep -> backdoor -> frontdoor -> (optional) do-calculus).

With your CausalFormula’s API (no default ctor, non-assignable), we compute the AST first and initialize result in the initializer list (single shot).

Definition at line 82 of file causalImpact.h.

Constructor & Destructor Documentation

◆ CausalImpact() [1/4]

template<GUM_Numeric GUM_SCALAR>
gum::CausalImpact< GUM_SCALAR >::CausalImpact ( const CausalModel< GUM_SCALAR > & cm,
const Set< std::string > & on,
const Set< std::string > & doing,
const Set< std::string > & knowing = Set< std::string >(),
bool directDoCalculus = false )

Constructs a CausalImpact object using variable names.

Parameters
cmThe causal model to use for identification.
onThe set of variable names representing the outcome(s) of interest.
doingThe set of variable names representing the intervention(s) (do-variables).
knowingThe set of variable names representing observed covariates (optional).
directDoCalculusIf true, bypasses Backdoor and Frontdoor and directly uses the ID algorithm for identification.
Warning
Setting directDoCalculus = true is NOT advisable. This will bypass the Backdoor and Frontdoor criteria and use the ID algorithm directly.

Definition at line 151 of file causalImpact_tpl.h.

155 :
Builds a CausalFormula for a query (d-sep -> backdoor -> frontdoor -> (optional) do-calculus).
CausalFormula< GUM_SCALAR > _resultFormula_
Identified symbolic formula. If not identifiable, its AST may be nullptr and its explanation string (...
const NodeSet & on() const
const NodeSet & knowing() const
static CausalFormula< GUM_SCALAR > _buildFromNames_(const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &on, const Set< std::string > &doing, const Set< std::string > &knowing, bool directDoCalculus)
const NodeSet & doing() const
bool _directDoCalculus_
If true, skip backdoor/frontdoor and use do-calculus directly.
const CausalModel< GUM_SCALAR > & cm() const

References _buildFromNames_(), _directDoCalculus_, _resultFormula_, cm(), doing(), knowing(), and on().

Referenced by CausalImpact(), and CausalImpact().

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

◆ CausalImpact() [2/4]

template<GUM_Numeric GUM_SCALAR>
gum::CausalImpact< GUM_SCALAR >::CausalImpact ( const CausalModel< GUM_SCALAR > & cm,
const NodeSet & on,
const NodeSet & doing,
const NodeSet & knowing = NodeSet(),
bool directDoCalculus = false )

Constructs a CausalImpact object using node IDs.

Parameters
cmThe causal model to use for identification.
onThe set of node IDs representing the outcome(s) of interest.
doingThe set of node IDs representing the intervention(s) (do-variables).
knowingThe set of node IDs representing observed covariates (optional).
directDoCalculusIf true, bypasses Backdoor and Frontdoor and directly uses the ID algorithm for identification.
Warning
Setting directDoCalculus = true is NOT advisable. This will bypass the Backdoor and Frontdoor criteria and use the ID algorithm directly.

Definition at line 160 of file causalImpact_tpl.h.

164 :
static CausalFormula< GUM_SCALAR > _buildFromIds_(const CausalModel< GUM_SCALAR > &cm, const NodeSet &on, const NodeSet &doing, const NodeSet &knowing, bool directDoCalculus)

References cm().

Here is the call graph for this function:

◆ CausalImpact() [3/4]

template<GUM_Numeric GUM_SCALAR>
gum::CausalImpact< GUM_SCALAR >::CausalImpact ( const CausalImpact< GUM_SCALAR > & )
default

Copy/move constructors — defaulted; require CausalFormula to be copy/move-constructible.

References CausalImpact().

Here is the call graph for this function:

◆ CausalImpact() [4/4]

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

References CausalImpact().

Here is the call graph for this function:

Member Function Documentation

◆ _buildFromIds_()

template<GUM_Numeric GUM_SCALAR>
CausalFormula< GUM_SCALAR > gum::CausalImpact< GUM_SCALAR >::_buildFromIds_ ( const CausalModel< GUM_SCALAR > & cm,
const NodeSet & on,
const NodeSet & doing,
const NodeSet & knowing,
bool directDoCalculus )
staticprivate

Definition at line 286 of file causalImpact_tpl.h.

290 {
291 // Reuse the names path to share logic/explanations
292 const auto onN = _idsToNames_(cm, on);
293 const auto doingN = _idsToNames_(cm, doing);
294 const auto knowingN = _idsToNames_(cm, knowing);
296 }
static Set< std::string > _idsToNames_(const CausalModel< GUM_SCALAR > &cm, const NodeSet &ids)

References _buildFromNames_(), _idsToNames_(), cm(), doing(), knowing(), and on().

Here is the call graph for this function:

◆ _buildFromNames_()

template<GUM_Numeric GUM_SCALAR>
CausalFormula< GUM_SCALAR > gum::CausalImpact< GUM_SCALAR >::_buildFromNames_ ( const CausalModel< GUM_SCALAR > & cm,
const Set< std::string > & on,
const Set< std::string > & doing,
const Set< std::string > & knowing,
bool directDoCalculus )
staticprivate

Definition at line 172 of file causalImpact_tpl.h.

176 {
177 if (!_disjoint_(on, doing, knowing)) {
179 "The 3 parts of the query (on, doing, knowing) must be pairwise disjoint.");
180 }
181
182 const NodeSet i_on = _namesToIds_(cm, on);
185
187
188 if (!directDoCalculus) {
189 // single-X, single-Y, empty-K: try BACKDOOR then FRONTDOOR
190 if (doing.size() == 1 && on.size() == 1 && knowing.empty()) {
191 const auto& Xname = *doing.begin();
192 const auto& Yname = *on.begin();
193 const auto Xid = cm.idFromName(Xname);
194 const auto Yid = cm.idFromName(Yname);
195
196 // --- Backdoor (Z may be empty; validate in G_{\underline X}) ---
197 {
198 auto optZ = cm.backDoor(Xid, Yid); // nullopt = no set found
199 if (optZ.has_value()
200 && Separation::isBackdoorSeparated(cm.causalDAG(),
201 NodeSet{Xid},
202 NodeSet{Yid},
203 *optZ)) {
204 NodeSet Z = *optZ;
206 auto ast = dc.getBackDoorTree(Xid, Yid, Z);
208 std::move(ast),
209 on,
210 doing,
211 knowing,
212 std::string("backdoor ")
213 + _formatNodeSetNames_(cm, Z) + " found.");
214 }
215 }
216
217 // --- Frontdoor (no trivial FD; require non-empty mediator set) ---
218 {
219 auto optZ = cm.frontDoor(Xid, Yid);
220 if (optZ.has_value() && !optZ->empty()) {
221 NodeSet Z = *optZ;
223 auto ast = dc.getFrontDoorTree(Xid, Yid, Z);
225 std::move(ast),
226 on,
227 doing,
228 knowing,
229 std::string("frontdoor ")
230 + _formatNodeSetNames_(cm, Z) + " found.");
231 }
232 }
233 }
234
235 // fallback: no causal effect in G_bar(X)
236 const DAG gXbar = _removeIncomingIntoLocal_(cm.causalDAG(), i_doing);
238 // P(Y | K) in the observational BN
240 on,
241 knowing);
243 std::move(ast),
244 on,
245 doing,
246 knowing,
248 }
249 }
250
251 // general do-calculus (when available in your DoCalculus)
252 try {
255 = knowing.empty()
256 ? dc.doCalculus(i_on, i_doing) // NodeSet overload
257 : dc.doCalculusWithObservation(i_on, i_doing, i_knowing); // NodeSet overload
258
260 std::move(ast),
261 on,
262 doing,
263 knowing,
264 "Identified via do-calculus (ID/IDC).");
265 } catch (const NotImplementedYet&) {
266 // Not identifiable with current methods: return a “null-AST” formula with a message.
268 cm,
270 on,
271 doing,
272 knowing,
273 "Effect not identifiable with available methods (do-calculus not yet implemented).");
274 } catch (const HedgeException& e) {
277 on,
278 doing,
279 knowing,
280 std::string("Effect not identifiable: ") + e.what());
281 }
282 }
static bool _disjoint_(const Set< std::string > &a, const Set< std::string > &b, const Set< std::string > &c)
static NodeSet _namesToIds_(const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &names)
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::string _dsepExplanation_(const Set< std::string > &on, const Set< std::string > &doing, const Set< std::string > &knowing)
std::string _formatNodeSetNames_(const CausalModel< GUM_SCALAR > &cm, const NodeSet &zset)
DAG _removeIncomingIntoLocal_(const DAG &g, const NodeSet &xset)

References _disjoint_(), _namesToIds_(), cm(), doing(), GUM_ERROR, knowing(), and on().

Referenced by CausalImpact(), and _buildFromIds_().

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

◆ _disjoint_()

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalImpact< GUM_SCALAR >::_disjoint_ ( const Set< std::string > & a,
const Set< std::string > & b,
const Set< std::string > & c )
staticprivate

Definition at line 132 of file causalImpact_tpl.h.

134 {
135 if (std::any_of(a.begin(), a.end(), [&b, &c](const auto& x) {
136 return b.contains(x) || c.contains(x);
137 })) {
138 return false;
139 }
140 if (std::any_of(b.begin(), b.end(), [&c](const auto& x) { return c.contains(x); })) {
141 return false;
142 }
143
144 return true;
145 }

Referenced by _buildFromNames_().

Here is the caller graph for this function:

◆ _idsToNames_()

template<GUM_Numeric GUM_SCALAR>
gum::Set< std::string > gum::CausalImpact< GUM_SCALAR >::_idsToNames_ ( const CausalModel< GUM_SCALAR > & cm,
const NodeSet & ids )
staticprivate

Definition at line 113 of file causalImpact_tpl.h.

114 {
116 for (auto id: ids)
117 out.insert(cm.nameFromId(id));
118 return out;
119 }

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

Referenced by _buildFromIds_().

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

◆ _namesToIds_()

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

Definition at line 123 of file causalImpact_tpl.h.

124 {
125 NodeSet out;
126 for (const auto& n: names)
127 out.insert(cm.idFromName(n));
128 return out;
129 }

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

Referenced by _buildFromNames_().

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::CausalImpact< GUM_SCALAR >::cm ( ) const

Definition at line 385 of file causalImpact_tpl.h.

385 {
386 return _resultFormula_.cm();
387 }

References _resultFormula_.

Referenced by CausalImpact(), CausalImpact(), _buildFromIds_(), _buildFromNames_(), _idsToNames_(), and _namesToIds_().

Here is the caller graph for this function:

◆ doing()

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

Definition at line 395 of file causalImpact_tpl.h.

395 {
396 return _resultFormula_.doing();
397 }

References _resultFormula_.

Referenced by CausalImpact(), _buildFromIds_(), and _buildFromNames_().

Here is the caller graph for this function:

◆ doingNames()

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

Definition at line 415 of file causalImpact_tpl.h.

415 {
416 return _resultFormula_.doingNames();
417 }

References _resultFormula_.

◆ eval()

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

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

Definition at line 348 of file causalImpact_tpl.h.

348 {
349 return _resultFormula_.eval();
350 }

References _resultFormula_.

Referenced by gum::causalImpact().

Here is the caller graph for this function:

◆ explanation()

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

Definition at line 405 of file causalImpact_tpl.h.

405 {
406 return _resultFormula_.explanation();
407 }

References _resultFormula_.

Referenced by gum::causalImpact().

Here is the caller graph for this function:

◆ getResult()

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

Access the underlying CausalFormula result.

Returns
const reference to the identified CausalFormula.

Definition at line 380 of file causalImpact_tpl.h.

380 {
381 return _resultFormula_;
382 }

References _resultFormula_.

◆ isIdentified()

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalImpact< GUM_SCALAR >::isIdentified ( ) const
nodiscardnoexcept

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 370 of file causalImpact_tpl.h.

370 {
371 return _resultFormula_.isIdentified();
372 }

References _resultFormula_.

Referenced by gum::causalImpact().

Here is the caller graph for this function:

◆ knowing()

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

Definition at line 400 of file causalImpact_tpl.h.

400 {
401 return _resultFormula_.knowing();
402 }

References _resultFormula_.

Referenced by CausalImpact(), _buildFromIds_(), and _buildFromNames_().

Here is the caller graph for this function:

◆ knowingNames()

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

Definition at line 420 of file causalImpact_tpl.h.

420 {
421 return _resultFormula_.knowingNames();
422 }

References _resultFormula_.

◆ latexQuery()

template<GUM_Numeric GUM_SCALAR>
std::string gum::CausalImpact< 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 364 of file causalImpact_tpl.h.

365 {
367 }

References _resultFormula_.

◆ on()

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

Definition at line 390 of file causalImpact_tpl.h.

390 {
391 return _resultFormula_.on();
392 }

References _resultFormula_.

Referenced by CausalImpact(), _buildFromIds_(), and _buildFromNames_().

Here is the caller graph for this function:

◆ onNames()

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

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

Definition at line 410 of file causalImpact_tpl.h.

410 {
411 return _resultFormula_.onNames();
412 }

References _resultFormula_.

◆ root()

template<GUM_Numeric GUM_SCALAR>
const ASTtree< GUM_SCALAR > & gum::CausalImpact< 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 375 of file causalImpact_tpl.h.

375 {
376 return _resultFormula_.root();
377 }

References _resultFormula_.

◆ toLatex()

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

Generates a full LaTeX equation: Query = Formula.

Definition at line 358 of file causalImpact_tpl.h.

359 {
361 }

References _resultFormula_.

◆ toString()

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

Generates a string representation of the formula's AST.

Definition at line 353 of file causalImpact_tpl.h.

353 {
354 return _resultFormula_.toString();
355 }

References _resultFormula_.

Member Data Documentation

◆ _directDoCalculus_

template<GUM_Numeric GUM_SCALAR>
bool gum::CausalImpact< GUM_SCALAR >::_directDoCalculus_ = false
private

If true, skip backdoor/frontdoor and use do-calculus directly.

Definition at line 191 of file causalImpact.h.

Referenced by CausalImpact().

◆ _resultFormula_

template<GUM_Numeric GUM_SCALAR>
CausalFormula< GUM_SCALAR > gum::CausalImpact< GUM_SCALAR >::_resultFormula_
private

Identified symbolic formula. If not identifiable, its AST may be nullptr and its explanation string (6th ctor arg) will say so.

Definition at line 195 of file causalImpact.h.

Referenced by CausalImpact(), cm(), doing(), doingNames(), eval(), explanation(), getResult(), isIdentified(), knowing(), knowingNames(), latexQuery(), on(), onNames(), root(), toLatex(), and toString().


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