aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
causalImpact.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
41#ifndef GUM_CAUSAL_IMPACT_H
42#define GUM_CAUSAL_IMPACT_H
43
44#include <memory>
45#include <string>
46#include <vector>
47
51
52namespace gum {
53
54 template < GUM_Numeric GUM_SCALAR >
55 class CausalImpact;
56 template < GUM_Numeric GUM_SCALAR >
57 class Tensor;
58
64 template < GUM_Numeric GUM_SCALAR >
65 std::tuple< CausalImpact< GUM_SCALAR >, Tensor< GUM_SCALAR >, std::string >
67 const Set< std::string >& on,
68 const Set< std::string >& doing,
69 const Set< std::string >& knowing = Set< std::string >(),
72
81 template < GUM_Numeric GUM_SCALAR >
83 public:
84 // --- Forwarded accessors for wrapping ---
86 Tensor< GUM_SCALAR > eval() const;
87
89 std::string toString() const;
90
92 std::string toLatex(std::string_view doOperatorPrefix = "do(",
93 std::string_view doOperatorSuffix = ")") const;
94
99 std::string latexQuery(std::string_view doOperatorPrefix = "do(",
100 std::string_view doOperatorSuffix = ")") const;
101
111 [[nodiscard]] bool isIdentified() const noexcept;
112
123 const ASTtree< GUM_SCALAR >& root() const;
124
125 // --- Accessors ---
130 const CausalFormula< GUM_SCALAR >& getResult() const;
131
132 const CausalModel< GUM_SCALAR >& cm() const;
133
134 const NodeSet& on() const;
135
136 const NodeSet& doing() const;
137
138 const NodeSet& knowing() const;
139
140 const std::string& explanation() const;
141
143 std::vector< std::string > onNames() const;
144
145 std::vector< std::string > doingNames() const;
146
147 std::vector< std::string > knowingNames() const;
148
162 const Set< std::string >& on,
165 bool directDoCalculus = false);
166
180 const NodeSet& on,
181 const NodeSet& doing,
182 const NodeSet& knowing = NodeSet(),
183 bool directDoCalculus = false);
184
186 CausalImpact(const CausalImpact&) = default;
188
189 private:
191 bool _directDoCalculus_ = false;
192
195 CausalFormula< GUM_SCALAR > _resultFormula_;
196
197
198 // helpers
201 const Set< std::string >& names);
202 static bool _disjoint_(const Set< std::string >& a,
203 const Set< std::string >& b,
204 const Set< std::string >& c);
205
206 // core builders that return the fully-formed formula (for initializer list)
207 static CausalFormula< GUM_SCALAR > _buildFromNames_(const CausalModel< GUM_SCALAR >& cm,
208 const Set< std::string >& on,
211 bool directDoCalculus);
212
213 static CausalFormula< GUM_SCALAR > _buildFromIds_(const CausalModel< GUM_SCALAR >& cm,
214 const NodeSet& on,
215 const NodeSet& doing,
216 const NodeSet& knowing,
217 bool directDoCalculus);
218 };
219
230 template < GUM_Numeric GUM_SCALAR >
231 inline Instantiation
232 makeInstantiationFromValues(const Tensor< GUM_SCALAR >& tensor,
234
235
236#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
237 extern template class CausalImpact< double >;
238#endif
239
240} // namespace gum
241
243
244#endif // GUM_CAUSAL_IMPACT_H
d-separation utilities
Represents an identified causal formula and its query context.
Builds a CausalFormula for a query (d-sep -> backdoor -> frontdoor -> (optional) do-calculus).
CausalImpact(CausalImpact &&)=default
static CausalFormula< GUM_SCALAR > _buildFromIds_(const CausalModel< GUM_SCALAR > &cm, const NodeSet &on, const NodeSet &doing, const NodeSet &knowing, bool directDoCalculus)
static bool _disjoint_(const Set< std::string > &a, const Set< std::string > &b, const Set< std::string > &c)
CausalFormula< GUM_SCALAR > _resultFormula_
Identified symbolic formula. If not identifiable, its AST may be nullptr and its explanation string (...
const NodeSet & on() const
std::string toLatex(std::string_view doOperatorPrefix="do(", std::string_view doOperatorSuffix=")") const
Generates a full LaTeX equation: Query = Formula.
const CausalFormula< GUM_SCALAR > & getResult() const
Access the underlying CausalFormula result.
static Set< std::string > _idsToNames_(const CausalModel< GUM_SCALAR > &cm, const NodeSet &ids)
std::string toString() const
Generates a string representation of the formula's AST.
std::vector< std::string > doingNames() const
Tensor< GUM_SCALAR > eval() const
Evaluates the formula's AST to compute the resulting probability distribution.
const ASTtree< GUM_SCALAR > & root() const
Access the root AST node of the identified formula.
CausalImpact(const CausalImpact &)=default
Copy/move constructors — defaulted; require CausalFormula to be copy/move-constructible.
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.
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).
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)
static NodeSet _namesToIds_(const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &names)
std::vector< std::string > onNames() const
Convenience: return names corresponding to stored node ids (sorted).
const NodeSet & doing() const
bool isIdentified() const noexcept
Whether the causal effect has been identified.
bool _directDoCalculus_
If true, skip backdoor/frontdoor and use do-calculus directly.
const std::string & explanation() const
std::vector< std::string > knowingNames() const
const CausalModel< GUM_SCALAR > & cm() const
A causal model pairing an observational BayesNet with a causal DAG.
Definition causalModel.h:84
Class for assigning/browsing values to tuples of discrete variables.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
Do-calculus utilities and AST builders bound to a single CausalModel.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Instantiation makeInstantiationFromValues(const Tensor< GUM_SCALAR > &tensor, const HashTable< std::string, std::string > &values)
Creates an instance for a tensor based on a HashTable.
std::tuple< CausalImpact< GUM_SCALAR >, Tensor< GUM_SCALAR >, std::string > causalImpact(const CausalModel< GUM_SCALAR > &cm, const Set< std::string > &on, const Set< std::string > &doing, const Set< std::string > &knowing=Set< std::string >(), const HashTable< std::string, std::string > &values=HashTable< std::string, std::string >())
Standalone helper that mirrors the Python function causalImpact(...).