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

Instance-based do-calculus utilities bound to a single CausalModel. More...

#include <doCalculus.h>

Public Types

using FormulaPtr = std::unique_ptr< ASTtree< GUM_SCALAR > >
 Owned pointer to an AST representing a probability expression.
using NameList = std::vector< std::string >
 Convenience container for name-based overloads.

Public Member Functions

 DoCalculus (const CausalModel< GUM_SCALAR > &cm) noexcept
const CausalModel< GUM_SCALAR > & model () const noexcept
FormulaPtr getBackDoorTree (NodeId cause, NodeId effect, const NodeSet &zset) const
FormulaPtr getBackDoorTree (std::string_view causeName, std::string_view effectName, const NameList &zNames) const
FormulaPtr getFrontDoorTree (NodeId cause, NodeId effect, const NodeSet &zset) const
FormulaPtr getFrontDoorTree (std::string_view causeName, std::string_view effectName, const NameList &zNames) const
FormulaPtr identifyingIntervention (const NodeSet &Y, const NodeSet &X) const
 ID: identify P(Y | do(X)) (NodeId-based).
FormulaPtr identifyingIntervention (const NameList &Y, const NameList &X) const
 ID: identify P(Y | do(X)) (name-based).
FormulaPtr doCalculus (const NodeSet &on, const NodeSet &doing) const
 Thin wrapper around ID: doCalculus(on, doing).
FormulaPtr doCalculus (const NameList &on, const NameList &doing) const
FormulaPtr doCalculusWithObservation (const NodeSet &on, const NodeSet &doing, const NodeSet &knowing) const
 IDC: identify P(on | do(doing), knowing).
FormulaPtr doCalculusWithObservation (const NameList &on, const NameList &doing, const NameList &knowing) const

Private Member Functions

std::vector< NodeSet > _cDecomposition_ (const CausalModel< GUM_SCALAR > &cm) const
 c-decomposition (confounding components) among observed nodes.
std::vector< NodeSet > _cDecompositionOn_ (const CausalModel< GUM_SCALAR > &sub) const
 c-decomposition on an induced submodel (observed nodes only).
std::vector< NodeId > _topoObserved_ (const CausalModel< GUM_SCALAR > &cm) const
 Topological order over observed nodes (ignoring latent parents).
FormulaPtr _ID_ (const CausalModel< GUM_SCALAR > &cm, const NodeSet &Y, const NodeSet &X, std::unique_ptr< ASTtree< GUM_SCALAR > > P) const
 ID core (recursive), with optional accumulated distribution P (AST) to carry in decompositions.

Static Private Member Functions

static NodeSet _ancestorsIn_ (const DAG &g, const NodeSet &T)
 Ancestors (in DAG g) of a set T (including T).
static DAG _removeIncomingInto_ (const DAG &dag, const NodeSet &X)
 DAG copy with all incoming arcs into X removed (G_{\overline X}).
static DAG _removeInIntoDoing_outOfKnowing_ (const DAG &dag, const NodeSet &doing, const NodeSet &knowing)
 IDC helper: DAG copy with incoming into doing removed AND outgoing from knowing removed.
static std::string _hedgeExceptionMsg (const CausalModel< GUM_SCALAR > &cm, const NodeSet &Y, const NodeSet &X, const NodeSet &V, const NodeSet &S)
 Format a hedge exception message.

Private Attributes

const CausalModel< GUM_SCALAR > & _cm

Detailed Description

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

Instance-based do-calculus utilities bound to a single CausalModel.

This class provides:

  • AST builders for standard adjustment formulas:
    • Backdoor: \(\sum_{Z} P(y \mid x, z)\,P(z)\)
    • Frontdoor: \(\sum_{Z} P(z \mid x)\,\sum_{x} P(y \mid x, z)\,P(x)\)
  • Public entry points for general identification (ID/IDC):
    • identifyingIntervention(Y, X)
    • doCalculus(on, doing)
    • doCalculusWithObservation(on, doing, knowing)

Returned ASTs are evaluated against the observational BN of the bound causal model.

Definition at line 78 of file doCalculus.h.

Member Typedef Documentation

◆ FormulaPtr

template<GUM_Numeric GUM_SCALAR>
using gum::DoCalculus< GUM_SCALAR >::FormulaPtr = std::unique_ptr< ASTtree< GUM_SCALAR > >

Owned pointer to an AST representing a probability expression.

Definition at line 81 of file doCalculus.h.

◆ NameList

template<GUM_Numeric GUM_SCALAR>
using gum::DoCalculus< GUM_SCALAR >::NameList = std::vector< std::string >

Convenience container for name-based overloads.

Definition at line 84 of file doCalculus.h.

Constructor & Destructor Documentation

◆ DoCalculus()

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

Definition at line 88 of file doCalculus.h.

88: _cm{cm} {}
Instance-based do-calculus utilities bound to a single CausalModel.
Definition doCalculus.h:78
const CausalModel< GUM_SCALAR > & _cm
Definition doCalculus.h:127

References _cm.

Member Function Documentation

◆ _ancestorsIn_()

template<GUM_Numeric GUM_SCALAR>
NodeSet gum::DoCalculus< GUM_SCALAR >::_ancestorsIn_ ( const DAG & g,
const NodeSet & T )
staticprivate

Ancestors (in DAG g) of a set T (including T).

Definition at line 379 of file doCalculus_tpl.h.

379 {
380 NodeSet res = T * g.nodes().asNodeSet(); // keep only nodes that actually exist in g
381
383 for (auto t: res)
384 q.push(t);
385
386 while (!q.empty()) {
387 NodeId u = q.front();
388 q.pop();
389 for (auto p: g.parents(u)) {
390 if (!res.contains(p)) {
391 res.insert(p);
392 q.push(p);
393 }
394 }
395 }
396 return res;
397 }

References gum::NodeGraphPart::asNodeSet(), gum::Set< Key >::contains(), gum::Set< Key >::insert(), gum::NodeGraphPart::nodes(), and gum::ArcGraphPart::parents().

Referenced by _ID_().

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

◆ _cDecomposition_()

template<GUM_Numeric GUM_SCALAR>
std::vector< NodeSet > gum::DoCalculus< GUM_SCALAR >::_cDecomposition_ ( const CausalModel< GUM_SCALAR > & cm) const
private

c-decomposition (confounding components) among observed nodes.

Definition at line 286 of file doCalculus_tpl.h.

286 {
287 // c-components: undirected connectivity among observed children of the same latent.
288 const NodeSet lat = cm.latentVariablesIds();
289 const NodeSet V = cm.causalDAG().nodes().asNodeSet() - lat;
290
291 // adjacency among observed nodes, via latent co-children
293 for (auto v: V)
294 adj[v]; // ensure key exists
295
296 for (auto h: lat) {
297 const auto& ch = cm.children(h);
298 // connect all observed pairs of children with an undirected edge
299 for (auto it1 = ch.begin(); it1 != ch.end(); ++it1) {
300 if (lat.contains(*it1)) continue;
301 auto it2 = it1;
302 ++it2;
303 for (; it2 != ch.end(); ++it2) {
304 if (lat.contains(*it2)) continue;
305 adj[*it1].push_back(*it2);
306 adj[*it2].push_back(*it1);
307 }
308 }
309 }
310
311 // BFS/DFS over observed nodes
314 for (auto v: V) {
315 if (seen.count(v)) continue;
318 q.push(v);
319 seen.insert(v);
320 comp.insert(v);
321 while (!q.empty()) {
322 auto u = q.front();
323 q.pop();
324 for (auto w: adj[u]) {
325 if (!seen.count(w)) {
326 seen.insert(w);
327 comp.insert(w);
328 q.push(w);
329 }
330 }
331 }
332 comps.push_back(std::move(comp));
333 }
334 return comps;
335 }

References gum::NodeGraphPart::asNodeSet(), gum::CausalModel< GUM_SCALAR >::causalDAG(), gum::CausalModel< GUM_SCALAR >::children(), gum::Set< Key >::contains(), gum::Set< Key >::insert(), gum::CausalModel< GUM_SCALAR >::latentVariablesIds(), and gum::NodeGraphPart::nodes().

Referenced by _cDecompositionOn_(), and _ID_().

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

◆ _cDecompositionOn_()

template<GUM_Numeric GUM_SCALAR>
std::vector< NodeSet > gum::DoCalculus< GUM_SCALAR >::_cDecompositionOn_ ( const CausalModel< GUM_SCALAR > & sub) const
private

c-decomposition on an induced submodel (observed nodes only).

Definition at line 339 of file doCalculus_tpl.h.

339 {
340 return _cDecomposition_(sub);
341 }
std::vector< NodeSet > _cDecomposition_(const CausalModel< GUM_SCALAR > &cm) const
c-decomposition (confounding components) among observed nodes.

References _cDecomposition_().

Referenced by _ID_().

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

◆ _hedgeExceptionMsg()

template<GUM_Numeric GUM_SCALAR>
std::string gum::DoCalculus< GUM_SCALAR >::_hedgeExceptionMsg ( const CausalModel< GUM_SCALAR > & cm,
const NodeSet & Y,
const NodeSet & X,
const NodeSet & V,
const NodeSet & S )
staticprivate

Format a hedge exception message.

Definition at line 252 of file doCalculus_tpl.h.

256 {
257 // turn NodeSets into sorted name lists
258 auto namesOf = [&](const NodeSet& Ns) {
260 out.reserve(Ns.size());
261 for (auto id: Ns)
262 out.push_back(cm.nameFromId(id));
263 std::sort(out.begin(), out.end());
264 return out;
265 };
266
267 const auto yN = namesOf(Y);
268 const auto xN = namesOf(X);
269 const auto vN = namesOf(V);
270 const auto sN = namesOf(S);
271
272 return std::format("Not identifiable: hedge detected. "
273 "Target P{} | do{}. Witness: V = {} is a single c-component and S = {} "
274 "is a c-component in both G and G\\X (forms a hedge). "
275 "Interpretation: unblocked confounding remains under intervention; "
276 "the effect cannot be expressed from the observational distribution "
277 "using current graph structure.",
282 }

References gum::CausalModel< GUM_SCALAR >::nameFromId().

Referenced by _ID_().

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

◆ _ID_()

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::_ID_ ( const CausalModel< GUM_SCALAR > & cm,
const NodeSet & Y,
const NodeSet & X,
std::unique_ptr< ASTtree< GUM_SCALAR > > P ) const
private

ID core (recursive), with optional accumulated distribution P (AST) to carry in decompositions.

Definition at line 529 of file doCalculus_tpl.h.

532 {
533 const NodeSet lat = cm.latentVariablesIds();
534 const NodeSet V = cm.causalDAG().nodes().asNodeSet() - lat;
535
536 // --- 1) if X is empty ---
537 if (X.empty()) {
538 if (!P) {
539 // return P(Y)
541 for (auto y: Y)
542 Yn.insert(cm.nameFromId(y));
544 }
545 // return sum_{V\Y} P
546 NodeSet Vy = V - Y;
547 if (Vy.empty()) return std::move(P);
549 tosum.reserve(Vy.size());
550 for (auto v: Vy)
551 tosum.push_back(cm.nameFromId(v));
553 }
554
555 // --- 2) restrict to ancestors of Y in G ---
556 {
557 DAG g = cm.causalDAG();
559 A += Y;
560 // Only observed ancestors (V is observed-only)
561 NodeSet Aobs = A - lat;
562 if (Aobs.size() != V.size()) {
563 // sum out V \ Aobs from P if present, recurse on induced submodel G[Aobs]
564 if (P) {
565 NodeSet VminusA = V - Aobs;
566 if (!VminusA.empty()) {
568 tosum.reserve(VminusA.size());
569 for (auto v: VminusA)
570 tosum.push_back(cm.nameFromId(v));
572 }
573 }
574 auto sub = cm.inducedCausalSubModel(cm, Aobs);
575 NodeSet XA = X * Aobs;
576 return _ID_(sub, Y, XA, std::move(P));
577 }
578 }
579
580
581 // --- 3) W := (V\X) \ An_{G_{\overline X}}(Y); if non-empty, move W to X and recurse ---
582 {
583 DAG gXbar = _removeIncomingInto_(cm.causalDAG(), X);
585 AnY += Y;
586 NodeSet W = (V - X) - AnY;
587 if (!W.empty()) { return _ID_(cm, Y, X + W, std::move(P)); }
588 }
589
590 // --- 4) c-decompose G[V\X]; if >1 component, multiply and sum ---
591 {
592 auto gvx = cm.inducedCausalSubModel(cm, V - X);
594
595 if (icomp.size() > 1) {
596 // t = Π_i ID(S_i, V \ S_i)
598 for (size_t i = 0; i < icomp.size(); ++i) {
599 NodeSet Si = icomp[i];
600 NameList Sn;
601 for (auto s: Si)
602 Sn.push_back(cm.nameFromId(s));
603
606 auto term = _ID_(cm, Si, V - Si, std::move(Pcopy));
607 if (!t) t = std::move(term);
609 }
610 NodeSet Vyx = V - (X + Y);
611 if (Vyx.empty()) return t;
613 tosum.reserve(Vyx.size());
614 for (auto v: Vyx)
615 tosum.push_back(cm.nameFromId(v));
617 }
618
619 // Only one component:
620 NodeSet S = icomp.front();
621 auto cdg = _cDecomposition_(cm);
622
623 // --- 5) Hedge check: if full graph is one c-component, not identifiable
624 if (cdg.size() == 1) {
625 NodeSet all = cdg.front();
626 if (all.size() == V.size()) {
627 // build names for message
629 for (auto v: V)
630 Vn.insert(cm.nameFromId(v));
631 for (auto s: S)
632 Sn.insert(cm.nameFromId(s));
633 auto msg = _hedgeExceptionMsg(cm, Y, X, V, S);
635 }
636 }
637
638 // --- 6) If S is also a c-component in G: product of local conditionals over S
639 {
640 // test membership: S ∈ cdg ?
641 bool sInCdg = false;
642 for (const auto& comp: cdg) {
643 if (comp == S) {
644 sInCdg = true;
645 break;
646 }
647 }
648
649 if (sInCdg) {
650 // P_new := Π_{v∈S in topo order} P(v | v_π) under current P (if provided),
651 // otherwise P from the BN (chain rule)
652 auto order = _topoObserved_(cm);
653 const DAG dag = cm.causalDAG();
654 const auto id2n = cm.id2name(/*includeLatentVariables=*/false);
655
657
658 // Make a name lookup for quick topo prefix
660 for (Size i = 0; i < order.size(); ++i)
661 pos[order[i]] = i;
662
663 // build in topo order filtered to S
665 ordering.reserve(S.size());
666 for (auto v: order)
667 if (S.contains(v)) ordering.push_back(v);
668
669 for (Size j = 0; j < ordering.size(); ++j) {
670 auto v = ordering[j];
672 if (!P) {
673 // from BN: P(v | predecessors) using chain rule prefix
674 Size k = pos[v];
676 for (Size i = 0; i < k; ++i)
677 condNames.insert(cm.nameFromId(order[i]));
679 lhs.insert(cm.nameFromId(v));
681 else
683 id2n,
684 lhs,
685 condNames);
686 } else {
687 // from accumulated P: factorize Q[S] by full-order prefix marginals
688 Size k = pos[v];
690 prefixNames.reserve(k + 1);
691 for (Size i = 0; i <= k; ++i)
692 prefixNames.push_back(cm.nameFromId(order[i]));
693
694 // numerator = marginal of P on current prefix
697 for (auto u: V) {
698 const auto& un = cm.nameFromId(u);
699 if (std::find(prefixNames.begin(), prefixNames.end(), un) == prefixNames.end()) {
700 sumNum.push_back(un);
701 }
702 }
703 if (!sumNum.empty()) {
705 }
706
707 if (k == 0) {
709 lhs.insert(cm.nameFromId(v));
711
712 } else {
713 // denominator = marginal of P on previous prefix
716 prevPrefix.reserve(k);
717 for (Size i = 0; i < k; ++i)
718 prevPrefix.push_back(cm.nameFromId(order[i]));
720 for (auto u: V) {
721 const auto& un = cm.nameFromId(u);
722 if (std::find(prevPrefix.begin(), prevPrefix.end(), un) == prevPrefix.end()) {
723 sumDen.push_back(un);
724 }
725 }
726 if (!sumDen.empty()) {
728 }
730 }
731 }
732
733 if (!prod) prod = std::move(term);
735 }
736
737 NodeSet Sy = S - Y;
738 if (Sy.empty()) return prod;
740 tosum.reserve(Sy.size());
741 for (auto v: Sy)
742 tosum.push_back(cm.nameFromId(v));
744 }
745 }
746
747 // --- 7) Else: pick a c-component S' of G, build Q[S'] then recurse on G[S']
748 {
749 // choose the c-component of G that contains S (SP Step 7: S ⊆ S')
750 NodeSet Spr;
751 bool found = false;
752 for (const auto& comp: cdg) {
753 if ((S - comp).empty()) {
754 Spr = comp;
755 found = true;
756 break;
757 }
758 }
759 if (!found) {
760 // Fallback should not happen; be conservative
761 Spr = S;
762 }
763
764 auto order = _topoObserved_(cm);
765 const DAG dag = cm.causalDAG();
766 const auto id2n = cm.id2name(/*includeLatentVariables=*/false);
767
769
771 for (Size i = 0; i < order.size(); ++i)
772 pos[order[i]] = i;
773
775 ordering.reserve(Spr.size());
776 for (auto v: order)
777 if (Spr.contains(v)) ordering.push_back(v);
778
779 for (Size j = 0; j < ordering.size(); ++j) {
780 auto v = ordering[j];
782 if (!P) {
783 Size k = pos[v];
785 for (Size i = 0; i < k; ++i)
786 condNames.insert(cm.nameFromId(order[i]));
788 lhs.insert(cm.nameFromId(v));
790 else
792 } else {
793 // from accumulated P: factorize Q[Spr] by full-order prefix marginals
794 Size k = pos[v];
796 prefixNames.reserve(k + 1);
797 for (Size i = 0; i <= k; ++i)
798 prefixNames.push_back(cm.nameFromId(order[i]));
799
800 // numerator = marginal of P on current prefix
803 for (auto u: V) {
804 const auto& un = cm.nameFromId(u);
805 if (std::find(prefixNames.begin(), prefixNames.end(), un) == prefixNames.end()) {
806 sumNum.push_back(un);
807 }
808 }
809 if (!sumNum.empty()) {
811 }
812
813 if (k == 0) {
815 lhs.insert(cm.nameFromId(v));
817
818 } else {
819 // denominator = marginal of P on previous prefix
822 prevPrefix.reserve(k);
823 for (Size i = 0; i < k; ++i)
824 prevPrefix.push_back(cm.nameFromId(order[i]));
826 for (auto u: V) {
827 const auto& un = cm.nameFromId(u);
828 if (std::find(prevPrefix.begin(), prevPrefix.end(), un) == prevPrefix.end()) {
829 sumDen.push_back(un);
830 }
831 }
832 if (!sumDen.empty()) {
834 }
836 }
837 }
838
839 if (!prod) prod = std::move(term);
841 }
842
843 auto sub = cm.inducedCausalSubModel(cm, Spr);
844 NodeSet Yspr = Y * Spr; // <— important
845 NodeSet Xspr = X * Spr;
846
847 if (Yspr.empty()) {
849 "Internal error in ID step 7: chosen C-component is disjoint from Y.");
850 }
851
852 return _ID_(sub, Yspr, Xspr, std::move(prod));
853 }
854 }
855 }
FormulaPtr _ID_(const CausalModel< GUM_SCALAR > &cm, const NodeSet &Y, const NodeSet &X, std::unique_ptr< ASTtree< GUM_SCALAR > > P) const
ID core (recursive), with optional accumulated distribution P (AST) to carry in decompositions.
static DAG _removeIncomingInto_(const DAG &dag, const NodeSet &X)
DAG copy with all incoming arcs into X removed (G_{\overline X}).
std::vector< NodeId > _topoObserved_(const CausalModel< GUM_SCALAR > &cm) const
Topological order over observed nodes (ignoring latent parents).
std::vector< std::string > NameList
Convenience container for name-based overloads.
Definition doCalculus.h:84
std::vector< NodeSet > _cDecompositionOn_(const CausalModel< GUM_SCALAR > &sub) const
c-decomposition on an induced submodel (observed nodes only).
static NodeSet _ancestorsIn_(const DAG &g, const NodeSet &T)
Ancestors (in DAG g) of a set T (including T).
static std::string _hedgeExceptionMsg(const CausalModel< GUM_SCALAR > &cm, const NodeSet &Y, const NodeSet &X, const NodeSet &V, const NodeSet &S)
Format a hedge exception message.

References _ancestorsIn_(), _cDecomposition_(), _cDecompositionOn_(), _hedgeExceptionMsg(), _ID_(), _removeIncomingInto_(), _topoObserved_(), gum::NodeGraphPart::asNodeSet(), gum::CausalModel< GUM_SCALAR >::causalDAG(), gum::Set< Key >::contains(), gum::Set< Key >::empty(), GUM_ERROR, gum::CausalModel< GUM_SCALAR >::id2name(), gum::CausalModel< GUM_SCALAR >::inducedCausalSubModel(), gum::Set< Key >::insert(), gum::CausalModel< GUM_SCALAR >::latentVariablesIds(), gum::CausalModel< GUM_SCALAR >::nameFromId(), gum::NodeGraphPart::nodes(), and gum::Set< Key >::size().

Referenced by _ID_(), and identifyingIntervention().

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

◆ _removeIncomingInto_()

template<GUM_Numeric GUM_SCALAR>
DAG gum::DoCalculus< GUM_SCALAR >::_removeIncomingInto_ ( const DAG & dag,
const NodeSet & X )
staticprivate

DAG copy with all incoming arcs into X removed (G_{\overline X}).

Definition at line 400 of file doCalculus_tpl.h.

400 {
401 DAG g = dag; // copy
402 for (auto x: X) {
403 auto ps = g.parents(x); // snapshot
404 for (auto p: ps)
405 g.eraseArc(Arc(p, x));
406 }
407 return g;
408 }
Arc(NodeId tail, NodeId head)
basic constructor. Creates tail -> head.

References gum::Arc::Arc(), gum::ArcGraphPart::eraseArc(), and gum::ArcGraphPart::parents().

Referenced by _ID_().

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

◆ _removeInIntoDoing_outOfKnowing_()

template<GUM_Numeric GUM_SCALAR>
DAG gum::DoCalculus< GUM_SCALAR >::_removeInIntoDoing_outOfKnowing_ ( const DAG & dag,
const NodeSet & doing,
const NodeSet & knowing )
staticprivate

IDC helper: DAG copy with incoming into doing removed AND outgoing from knowing removed.

Definition at line 411 of file doCalculus_tpl.h.

413 {
414 DAG g = dag; // copy
415 for (auto x: doing) {
416 auto ps = g.parents(x);
417 for (auto p: ps)
418 g.eraseArc(Arc(p, x));
419 }
420 for (auto z: knowing) {
421 auto ch = g.children(z);
422 for (auto c: ch)
423 g.eraseArc(Arc(z, c));
424 }
425 return g;
426 }

References gum::Arc::Arc(), gum::ArcGraphPart::children(), gum::ArcGraphPart::eraseArc(), and gum::ArcGraphPart::parents().

Referenced by doCalculusWithObservation().

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

◆ _topoObserved_()

template<GUM_Numeric GUM_SCALAR>
std::vector< NodeId > gum::DoCalculus< GUM_SCALAR >::_topoObserved_ ( const CausalModel< GUM_SCALAR > & cm) const
private

Topological order over observed nodes (ignoring latent parents).

Definition at line 345 of file doCalculus_tpl.h.

345 {
346 const NodeSet lat = cm.latentVariablesIds();
347 const NodeSet V = cm.causalDAG().nodes().asNodeSet() - lat;
348 const DAG dag = cm.causalDAG();
349
350 // indegree in the observed DAG (ignore latent-induced parent arcs)
352 for (auto v: V)
353 indeg[v] = (dag.parents(v) - lat).size();
354
356 order.reserve(V.size());
357
358 // Kahn on observed DAG only
360 for (const auto& kv: indeg)
361 if (kv.second == 0) q.push(kv.first);
362
363 while (!q.empty()) {
364 const NodeId u = q.front();
365 q.pop();
366 order.push_back(u);
367
368 for (auto c: dag.children(u)) {
369 if (indeg.contains(c) && indeg[c] > 0) {
370 --indeg[c];
371 if (indeg[c] == 0) q.push(c);
372 }
373 }
374 }
375 return order;
376 }

References gum::NodeGraphPart::asNodeSet(), gum::CausalModel< GUM_SCALAR >::causalDAG(), gum::ArcGraphPart::children(), gum::CausalModel< GUM_SCALAR >::latentVariablesIds(), gum::NodeGraphPart::nodes(), gum::ArcGraphPart::parents(), and gum::Set< Key >::size().

Referenced by _ID_().

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

◆ doCalculus() [1/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::doCalculus ( const NameList & on,
const NameList & doing ) const

Definition at line 458 of file doCalculus_tpl.h.

458 {
460 }
FormulaPtr identifyingIntervention(const NodeSet &Y, const NodeSet &X) const
ID: identify P(Y | do(X)) (NodeId-based).

References identifyingIntervention().

Here is the call graph for this function:

◆ doCalculus() [2/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::doCalculus ( const NodeSet & on,
const NodeSet & doing ) const

Thin wrapper around ID: doCalculus(on, doing).

Definition at line 452 of file doCalculus_tpl.h.

452 {
454 }

References identifyingIntervention().

Here is the call graph for this function:

◆ doCalculusWithObservation() [1/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::doCalculusWithObservation ( const NameList & on,
const NameList & doing,
const NameList & knowing ) const

Definition at line 510 of file doCalculus_tpl.h.

512 {
513 NodeSet ON, DO, KN;
514 for (const auto& s: on)
515 ON.insert(_cm.idFromName(s));
516 for (const auto& s: doing)
517 DO.insert(_cm.idFromName(s));
518 for (const auto& s: knowing)
519 KN.insert(_cm.idFromName(s));
521 }
FormulaPtr doCalculusWithObservation(const NodeSet &on, const NodeSet &doing, const NodeSet &knowing) const
IDC: identify P(on | do(doing), knowing).

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

Here is the call graph for this function:

◆ doCalculusWithObservation() [2/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::doCalculusWithObservation ( const NodeSet & on,
const NodeSet & doing,
const NodeSet & knowing ) const

IDC: identify P(on | do(doing), knowing).

Definition at line 464 of file doCalculus_tpl.h.

466 {
467 // IDC reduction: try to promote any Z∈knowing to doing if Z ⟂ on | doing ∪ (knowing\{Z})
468 // in the graph where incoming into doing and outgoing from knowing are removed.
469 if (!knowing.empty()) {
471
472 for (auto z: knowing) {
473 NodeSet Zi;
474 Zi.insert(z);
477 // recurse with z moved to doing
481 }
482 }
483 }
484
485 // Fallback: ratio P(on ∪ knowing | do(doing)) / P(knowing | do(doing))
486 NameList Yw, W;
487 for (auto y: on)
488 Yw.push_back(_cm.nameFromId(y));
489 for (auto w: knowing)
490 W.push_back(_cm.nameFromId(w));
491
492 NameList Xn;
493 for (auto x: doing)
494 Xn.push_back(_cm.nameFromId(x));
495
496 // numerator: ID on Y ∪ W ; denominator: ID on W (if W empty, denominator is 1)
498
500 if (knowing.empty()) {
501 // P(Y|do(X)) when knowing is empty: just return numerator
502 return num;
503 }
506 }
static DAG _removeInIntoDoing_outOfKnowing_(const DAG &dag, const NodeSet &doing, const NodeSet &knowing)
IDC helper: DAG copy with incoming into doing removed AND outgoing from knowing removed.

References _cm, _removeInIntoDoing_outOfKnowing_(), doCalculusWithObservation(), gum::Set< Key >::empty(), identifyingIntervention(), gum::Set< Key >::insert(), and gum::Separation::isDSeparated().

Referenced by doCalculusWithObservation(), and doCalculusWithObservation().

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

◆ getBackDoorTree() [1/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::getBackDoorTree ( NodeId cause,
NodeId effect,
const NodeSet & zset ) const

Definition at line 92 of file doCalculus_tpl.h.

94 {
95 // Preconditions: cause/effect must be observed (non-latent), Z must not contain latents.
96 const NodeSet lat = _cm.latentVariablesIds();
97 if (lat.contains(cause) || lat.contains(effect)) {
99 "getBackDoorTree: 'cause' and 'effect' must be observed (non-latent).");
100 }
101 for (auto z: zset) {
102 if (lat.contains(z)) {
103 GUM_ERROR(InvalidArgument, "getBackDoorTree: zset contains a latent variable.");
104 }
105 }
106
107 // ids -> names
108 const auto& dag = _cm.causalDAG();
109 auto mapO = _cm.id2name(/*includeLatentVariables=*/false);
110 const std::string xName = _cm.nameFromId(cause);
111 const std::string yName = _cm.nameFromId(effect);
112
115 zOrder.reserve(zset.size());
116 for (auto z: zset) {
117 const std::string zn = _cm.nameFromId(z);
118 zNames.insert(zn);
119 zOrder.push_back(zn);
120 }
121 std::sort(zOrder.begin(), zOrder.end());
122
123 // P(y | x, z)
125 lhsY.insert(yName);
127 knw.insert(xName);
128 for (const auto& zn: zNames)
129 knw.insert(zn);
131
132 if (zNames.empty()) {
133 // returns P(y | x)
134 return Pyxz;
135 }
136
137 // P(z)
139
140 // P(y|x,z) * P(z), then sum over Z
142 terms.emplace_back(std::move(Pyxz));
143 terms.emplace_back(std::move(Pz));
145
147 }

References _cm, gum::Set< Key >::contains(), gum::Set< Key >::empty(), GUM_ERROR, gum::Set< Key >::insert(), gum::productOfTrees(), and gum::Set< Key >::size().

Referenced by gum::CausalImpact< GUM_ELEMENT >::_disjoint_(), and getBackDoorTree().

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

◆ getBackDoorTree() [2/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::getBackDoorTree ( std::string_view causeName,
std::string_view effectName,
const NameList & zNames ) const

Definition at line 151 of file doCalculus_tpl.h.

153 {
154 const NodeId x = _cm.idFromName(causeName);
155 const NodeId y = _cm.idFromName(effectName);
156
157 NodeSet Z;
158 for (const auto& zn: zNames)
159 Z.insert(_cm.idFromName(zn));
160
161 return getBackDoorTree(x, y, Z);
162 }
FormulaPtr getBackDoorTree(NodeId cause, NodeId effect, const NodeSet &zset) const

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

Here is the call graph for this function:

◆ getFrontDoorTree() [1/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::getFrontDoorTree ( NodeId cause,
NodeId effect,
const NodeSet & zset ) const

Definition at line 166 of file doCalculus_tpl.h.

168 {
169 // --- Preconditions ---
170 const NodeSet lat = _cm.latentVariablesIds();
171 if (lat.contains(cause) || lat.contains(effect)) {
173 "getFrontDoorTree: 'cause' and 'effect' must be observed (non-latent).");
174 }
175 if (zset.empty()) {
176 GUM_ERROR(InvalidArgument, "getFrontDoorTree: zset must be non-empty for frontdoor.");
177 }
178 if (zset.contains(cause) || zset.contains(effect)) {
179 GUM_ERROR(InvalidArgument, "getFrontDoorTree: zset must be disjoint from {cause,effect}.");
180 }
181 for (auto z: zset) {
182 if (lat.contains(z)) {
183 GUM_ERROR(InvalidArgument, "getFrontDoorTree: zset contains a latent variable.");
184 }
185 }
186
187 // --- ids → names ---
188 const std::string xName = _cm.nameFromId(cause);
189 const std::string yName = _cm.nameFromId(effect);
190
193 zOrder.reserve(zset.size());
194 for (auto z: zset) {
195 const auto zn = _cm.nameFromId(z);
196 zNames.insert(zn);
197 zOrder.push_back(zn);
198 }
199 std::sort(zOrder.begin(), zOrder.end()); // deterministic
200
201 // --- Build P(Z | X) using DAG ctor (observed-only mapping inferred from model) ---
202 const auto& dag = _cm.causalDAG();
203 auto mapO = _cm.id2name(/*includeLatentVariables=*/false);
204
206 condX.insert(xName);
208
209 // --- Build sum_x P(Y | X, Z) * P(X) ---
211 ySet.insert(yName);
212
214 condXZ.insert(xName);
216
218 xSet.insert(xName);
220
221 auto inner_prod
225
226 // --- P(Z|X) * (sum_x ...) then sum over Z ---
227 auto outer_prod
230 }

References _cm, gum::Set< Key >::contains(), gum::Set< Key >::empty(), GUM_ERROR, gum::Set< Key >::insert(), and gum::Set< Key >::size().

Referenced by getFrontDoorTree().

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

◆ getFrontDoorTree() [2/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::getFrontDoorTree ( std::string_view causeName,
std::string_view effectName,
const NameList & zNames ) const

Definition at line 234 of file doCalculus_tpl.h.

236 {
237 const NodeId x = _cm.idFromName(causeName);
238 const NodeId y = _cm.idFromName(effectName);
239
240 NodeSet Z;
241 for (const auto& zn: zNames)
242 Z.insert(_cm.idFromName(zn));
243
244 return getFrontDoorTree(x, y, Z);
245 }
FormulaPtr getFrontDoorTree(NodeId cause, NodeId effect, const NodeSet &zset) const

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

Here is the call graph for this function:

◆ identifyingIntervention() [1/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::identifyingIntervention ( const NameList & Y,
const NameList & X ) const

ID: identify P(Y | do(X)) (name-based).

Definition at line 440 of file doCalculus_tpl.h.

441 {
442 NodeSet YY, XX;
443 for (const auto& s: Y)
444 YY.insert(_cm.idFromName(s));
445 for (const auto& s: X)
446 XX.insert(_cm.idFromName(s));
448 }

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

Here is the call graph for this function:

◆ identifyingIntervention() [2/2]

template<GUM_Numeric GUM_SCALAR>
DoCalculus< GUM_SCALAR >::FormulaPtr gum::DoCalculus< GUM_SCALAR >::identifyingIntervention ( const NodeSet & Y,
const NodeSet & X ) const

ID: identify P(Y | do(X)) (NodeId-based).

Definition at line 434 of file doCalculus_tpl.h.

434 {
435 return _ID_(_cm, Y, X, nullptr);
436 }

References _cm, and _ID_().

Referenced by doCalculus(), doCalculus(), doCalculusWithObservation(), and identifyingIntervention().

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

◆ model()

template<GUM_Numeric GUM_SCALAR>
const CausalModel< GUM_SCALAR > & gum::DoCalculus< GUM_SCALAR >::model ( ) const
noexcept

Definition at line 82 of file doCalculus_tpl.h.

82 {
83 return _cm;
84 }

References _cm.

Member Data Documentation

◆ _cm


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