aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
causalFormula.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
45
46#ifndef GUM_CAUSAL_FORMULA_H
47#define GUM_CAUSAL_FORMULA_H
48
49#include <memory>
50#include <string>
51#include <vector>
52
55
56namespace gum {
57
73 template < GUM_Numeric GUM_SCALAR >
75 public:
87 std::unique_ptr< ASTtree< GUM_SCALAR > > root,
91 std::string_view explanation = "");
92
104 std::unique_ptr< ASTtree< GUM_SCALAR > > root,
105 const NodeSet& on,
106 const NodeSet& doing,
107 const NodeSet& knowing = NodeSet{},
108 std::string_view explanation = "");
109
118 CausalFormula(const CausalFormula& other);
119
122
124 Tensor< GUM_SCALAR > eval() const;
125
127 std::string toString() const;
128
130 std::string toLatex(std::string_view doOperatorPrefix = "do(",
131 std::string_view doOperatorSuffix = ")") const;
132
137 std::string latexQuery(std::string_view doOperatorPrefix = "do(",
138 std::string_view doOperatorSuffix = ")") const;
139
141 std::unique_ptr< CausalFormula< GUM_SCALAR > > copy() const;
142
152 bool isIdentified() const noexcept;
153
164 const ASTtree< GUM_SCALAR >& root() const;
165
166 // --- Accessors ---
167 const CausalModel< GUM_SCALAR >& cm() const;
168
169 const NodeSet& on() const;
170
171 const NodeSet& doing() const;
172
173 const NodeSet& knowing() const;
174
175 const std::string& explanation() const;
176
178 std::vector< std::string > onNames() const;
179 std::vector< std::string > doingNames() const;
180 std::vector< std::string > knowingNames() const;
181
182
183 private:
185 static NodeSet _toNodeSetFromNames_(const CausalModel< GUM_SCALAR >& cm,
186 const Set< std::string >& names);
187
192 void _ensureVariablesExist() const;
193
198 void _ensureNoVariablesOverlap() const;
199
200 const CausalModel< GUM_SCALAR >& _cm;
201 std::unique_ptr< ASTtree< GUM_SCALAR > > _root;
205 const std::string _explanation;
206 };
207
208#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
209 extern template class CausalFormula< double >;
210#endif
211
212} // namespace gum
213
215
216#endif // GUM_CAUSAL_FORMULA_H
Template implementation for CausalFormula.
Root abstract node for the AST of algebraic expressions.
Definition doAST.h:91
Tensor< GUM_SCALAR > eval() const
Evaluates the formula's AST to compute the resulting probability distribution.
const NodeSet & on() const
std::vector< std::string > doingNames() 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
std::vector< std::string > knowingNames() const
CausalFormula(CausalFormula &&)=default
Move constructor — defaulted (suppressed by the user-defined copy constructor above).
std::string toLatex(std::string_view doOperatorPrefix="do(", std::string_view doOperatorSuffix=")") const
Generates a full LaTeX equation: Query = Formula.
const CausalModel< GUM_SCALAR > & _cm
const NodeSet & knowing() const
std::vector< std::string > onNames() const
Convenience: return names corresponding to stored node ids (sorted).
const std::string & explanation() const
const CausalModel< GUM_SCALAR > & cm() const
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).
void _ensureNoVariablesOverlap() const
Verifies that all variables (_on, _knowing, _doing) have no intersection.
const NodeSet & doing() const
bool isIdentified() const noexcept
Whether the causal effect has been identified.
std::unique_ptr< ASTtree< GUM_SCALAR > > _root
std::string toString() const
Generates a string representation of the formula's AST.
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
std::unique_ptr< CausalFormula< GUM_SCALAR > > copy() const
Creates a deep copy of the CausalFormula (including its AST).
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).
A causal model pairing an observational BayesNet with a causal DAG.
Definition causalModel.h:84
Abstract Syntax Tree (AST) for algebraic probability expressions used in do-Calculus.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.