aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
causalModel.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
42#ifndef GUM_CAUSAL_MODEL_H
43#define GUM_CAUSAL_MODEL_H
44
45#include <optional>
46#include <string>
47
48#include <agrum/BN/BayesNet.h>
49
50namespace gum {
51
52 // forward declaration:
53 template < GUM_Numeric GUM_SCALAR >
54 class ASTtree;
55
57
63 using LatentDescriptorIds = std::pair< std::string, std::vector< NodeId > >;
64
66 using LatentDescriptorVector = std::vector< LatentDescriptorIds >;
67
83 template < GUM_Numeric GUM_SCALAR >
85 private:
87 BayesNet< GUM_SCALAR > _observationalBN_;
88
91
94
95 public:
97 CausalModel() = delete;
98
105 explicit CausalModel(const BayesNet< GUM_SCALAR >& observationalBN);
106
116 explicit CausalModel(const BayesNet< GUM_SCALAR >& observationalBN,
117 const LatentDescriptorVector& latentVarsDescriptor,
118 bool assumeNonSpurious = false);
119
121 CausalModel(const CausalModel& other);
122
124 CausalModel(CausalModel&& other) noexcept;
125
127 ~CausalModel();
128
130 CausalModel& operator=(const CausalModel& other) = default;
131
133 CausalModel& operator=(CausalModel&& other) noexcept = default;
134
152 void addLatentVariable(std::string_view latentName,
153 const std::vector< std::string >& childrenOfLatent,
154 bool assumeNonSpurious = false);
155
173 void addLatentVariable(std::string_view latentName,
174 const std::vector< NodeId >& childrenOfLatent,
175 bool assumeNonSpurious = false);
176
178 bool existsArc(NodeId x, NodeId y) const;
179
181 bool existsArc(std::string_view x, std::string_view y) const;
182
187 void assumeSpurious(NodeId x, NodeId y);
188 void assumeSpurious(std::string_view x, std::string_view y);
189
195 void assumeNonSpurious(std::string_view x, std::string_view y);
196
200 bool isAssumedSpurious(NodeId x, NodeId y) const;
201 bool isAssumedSpurious(std::string_view x, std::string_view y) const;
202
203 // ======================================================================
204 // Door-criteria conveniences
205 // ======================================================================
206
224 std::optional< NodeSet > backDoor(NodeId cause, NodeId effect) const;
225
226 std::optional< NodeSet > backDoor(std::string_view cause, std::string_view effect) const;
227
245 std::optional< NodeSet > frontDoor(NodeId cause, NodeId effect) const;
246
264 std::optional< NodeSet > frontDoor(std::string_view cause, std::string_view effect) const;
265
273 NodeSet subset) const;
274
284 std::string toDot(const bool SHOW_LATENT_NAMES = false,
285 const char* NODE_BG = "#404040",
286 const char* NODE_FG = "white",
287 const char* EDGE_COL = "#4A4A4A") const;
288
290 const BayesNet< GUM_SCALAR >& observationalBN() const;
291
293 DAG causalDAG() const;
294
297
299 NodeId idFromName(std::string_view name) const;
300
302 std::string nameFromId(NodeId id) const;
303
305 Bijection< NodeId, std::string > id2name(bool includeLatentVariable = false) const;
306
309
312
314 NodeSet parents(NodeId x) const;
315
317 NodeSet parents(std::string_view name) const;
318
320 NodeSet children(NodeId x) const;
321
323 NodeSet children(std::string_view name) const;
324
330
341 const DiscreteVariable& variable(NodeId id) const;
342
353 const DiscreteVariable& variable(std::string_view name) const;
354 };
355
356#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
357 extern template class CausalModel< double >;
358#endif
359
360} // namespace gum
361
363
364#endif // GUM_CAUSAL_MODEL_H
Class representing Bayesian networks.
Template implementation of CausalModel/causalModel.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
DAG causalDAG() const
Causal DAG (observed + latent variables), with node names set.
std::string toDot(const bool SHOW_LATENT_NAMES=false, const char *NODE_BG="#404040", const char *NODE_FG="white", const char *EDGE_COL="#4A4A4A") const
DOT representation of the causal DAG (observed + latent).
void addLatentVariable(std::string_view latentName, const std::vector< std::string > &childrenOfLatent, bool assumeNonSpurious=false)
Add a latent variable by names of its observed children.
CausalModel< GUM_SCALAR > inducedCausalSubModel(const CausalModel< GUM_SCALAR > &cm, NodeSet subset) const
Induced causal submodel on a subset of nodes.
~CausalModel()
Destructor.
NodeSet latentVariablesIds() const
Node ids of all latent variables.
DAG _causalDAG_
The underlying DAG representing the causal structure (observed + latent).
Definition causalModel.h:90
HashTable< NodeId, NodeSet > connectedComponents() const
Weakly connected components of the causal DAG.
bool isAssumedSpurious(NodeId x, NodeId y) const
Returns true if arc is present in observationalBN and absent in causalDAG.
Set< std::string > latentVariablesNames() const
Names of all latent variables.
std::optional< NodeSet > frontDoor(NodeId cause, NodeId effect) const
Find a frontdoor adjustment set (Z) between cause and effect (IDs only).
Bijection< NodeId, std::string > id2name(bool includeLatentVariable=false) const
Bidirectional mapping between node ids and variable names.
Bijection< NodeId, std::string > _ids_names_
Bidirectional mapping between node ids and variable names (observed + latent).
Definition causalModel.h:93
NodeId idFromName(std::string_view name) const
Node id from variable name (observed or latent).
CausalModel & operator=(const CausalModel &other)=default
Copy assignment.
bool existsArc(NodeId x, NodeId y) const
Whether a causal arc x → y exists (by ids) in the causal DAG.
std::string nameFromId(NodeId id) const
Variable name from node id (observed or latent).
NodeSet children(NodeId x) const
Children of a node (by id) in the causal DAG (including latents).
const DiscreteVariable & variable(NodeId id) const
From id to variable (observed only, by id).
BayesNet< GUM_SCALAR > _observationalBN_
The underlying BayesNet representing the observed part of the model.
Definition causalModel.h:87
CausalModel()=delete
Default constructor disabled: a causal model must be built from a BN.
Set< std::string > names() const
All variable names appearing in the causal model (observed + latent).
CausalModel & operator=(CausalModel &&other) noexcept=default
Move assignment.
void assumeNonSpurious(NodeId x, NodeId y)
Mark an arc as non-spurious: present in observationalBN, added to causalDAG.
std::optional< NodeSet > backDoor(NodeId cause, NodeId effect) const
Find a backdoor adjustment set (Z) between cause and effect (IDs only).
void assumeSpurious(NodeId x, NodeId y)
Mark an arc as spurious: present in observationalBN, removed from causalDAG.
NodeSet parents(NodeId x) const
Parents of a node (by id) in the causal DAG (including latents).
const BayesNet< GUM_SCALAR > & observationalBN() const
Observational BN (observed variables only).
Base class for dag.
Definition DAG.h:121
Base class for discrete random variable.
The class for generic Hash Tables.
Definition hashTable.h:640
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
std::pair< std::string, std::vector< NodeId > > LatentDescriptorIds
LatentDescriptorIds = (latentName, (child1Id, child2Id, ...)).
Definition causalModel.h:63
std::vector< LatentDescriptorIds > LatentDescriptorVector
Collection of latent descriptors (see LatentDescriptorIds).
Definition causalModel.h:66