aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
doCalculus.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
49#ifndef GUM_DO_CALCULUS_H
50#define GUM_DO_CALCULUS_H
51
52#include <memory>
53#include <string>
54#include <vector>
55
58
59namespace gum {
60
77 template < GUM_Numeric GUM_SCALAR >
78 class DoCalculus {
79 public:
81 using FormulaPtr = std::unique_ptr< ASTtree< GUM_SCALAR > >;
82
84 using NameList = std::vector< std::string >;
85
86 /* ------------------------ Construction ------------------------ */
87
88 explicit DoCalculus(const CausalModel< GUM_SCALAR >& cm) noexcept : _cm{cm} {}
89
90 const CausalModel< GUM_SCALAR >& model() const noexcept;
91
92 /* -------------------- Backdoor / Frontdoor -------------------- */
93
94 FormulaPtr getBackDoorTree(NodeId cause, NodeId effect, const NodeSet& zset) const;
95
96 FormulaPtr getBackDoorTree(std::string_view causeName,
97 std::string_view effectName,
98 const NameList& zNames) const;
99
100 FormulaPtr getFrontDoorTree(NodeId cause, NodeId effect, const NodeSet& zset) const;
101
102 FormulaPtr getFrontDoorTree(std::string_view causeName,
103 std::string_view effectName,
104 const NameList& zNames) const;
105
106 /* --------------------- General identification ----------------- */
107
109 FormulaPtr identifyingIntervention(const NodeSet& Y, const NodeSet& X) const;
110
112 FormulaPtr identifyingIntervention(const NameList& Y, const NameList& X) const;
113
115 FormulaPtr doCalculus(const NodeSet& on, const NodeSet& doing) const;
116 FormulaPtr doCalculus(const NameList& on, const NameList& doing) const;
117
120 const NodeSet& doing,
121 const NodeSet& knowing) const;
123 const NameList& doing,
124 const NameList& knowing) const;
125
126 private:
127 const CausalModel< GUM_SCALAR >& _cm;
128
129 /* ---------- Helpers for ID/IDC (DAG-only; never mutate CausalModel) ---------- */
130
132 std::vector< NodeSet > _cDecomposition_(const CausalModel< GUM_SCALAR >& cm) const;
133
135 std::vector< NodeSet > _cDecompositionOn_(const CausalModel< GUM_SCALAR >& sub) const;
136
138 std::vector< NodeId > _topoObserved_(const CausalModel< GUM_SCALAR >& cm) const;
139
141 static NodeSet _ancestorsIn_(const DAG& g, const NodeSet& T);
142
144 static DAG _removeIncomingInto_(const DAG& dag, const NodeSet& X);
145
147 static DAG _removeInIntoDoing_outOfKnowing_(const DAG& dag,
148 const NodeSet& doing,
149 const NodeSet& knowing);
150
153 FormulaPtr _ID_(const CausalModel< GUM_SCALAR >& cm,
154 const NodeSet& Y,
155 const NodeSet& X,
156 std::unique_ptr< ASTtree< GUM_SCALAR > > P) const;
157
159 static std::string _hedgeExceptionMsg(const CausalModel< GUM_SCALAR >& cm,
160 const NodeSet& Y,
161 const NodeSet& X,
162 const NodeSet& V,
163 const NodeSet& S);
164 };
165
166#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
167 extern template class DoCalculus< double >;
168#endif
169
170} // namespace gum
171
173
174#endif // GUM_DO_CALCULUS_H
Root abstract node for the AST of algebraic expressions.
Definition doAST.h:91
A causal model pairing an observational BayesNet with a causal DAG.
Definition causalModel.h:84
Base class for dag.
Definition DAG.h:121
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.
FormulaPtr doCalculusWithObservation(const NodeSet &on, const NodeSet &doing, const NodeSet &knowing) const
IDC: identify P(on | do(doing), knowing).
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}).
const CausalModel< GUM_SCALAR > & _cm
Definition doCalculus.h:127
std::vector< NodeId > _topoObserved_(const CausalModel< GUM_SCALAR > &cm) const
Topological order over observed nodes (ignoring latent parents).
DoCalculus(const CausalModel< GUM_SCALAR > &cm) noexcept
Definition doCalculus.h:88
std::vector< std::string > NameList
Convenience container for name-based overloads.
Definition doCalculus.h:84
FormulaPtr getFrontDoorTree(NodeId cause, NodeId effect, const NodeSet &zset) const
std::vector< NodeSet > _cDecompositionOn_(const CausalModel< GUM_SCALAR > &sub) const
c-decomposition on an induced submodel (observed nodes only).
std::unique_ptr< ASTtree< GUM_SCALAR > > FormulaPtr
Owned pointer to an AST representing a probability expression.
Definition doCalculus.h:81
static NodeSet _ancestorsIn_(const DAG &g, const NodeSet &T)
Ancestors (in DAG g) of a set T (including T).
FormulaPtr doCalculus(const NodeSet &on, const NodeSet &doing) const
Thin wrapper around ID: doCalculus(on, doing).
std::vector< NodeSet > _cDecomposition_(const CausalModel< GUM_SCALAR > &cm) const
c-decomposition (confounding components) among observed nodes.
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.
FormulaPtr identifyingIntervention(const NodeSet &Y, const NodeSet &X) const
ID: identify P(Y | do(X)) (NodeId-based).
FormulaPtr getBackDoorTree(NodeId cause, NodeId effect, const NodeSet &zset) const
const CausalModel< GUM_SCALAR > & model() const noexcept
Abstract Syntax Tree (AST) for algebraic probability expressions used in do-Calculus.
Do-calculus utilities and AST builders bound to a single CausalModel.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.