aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
causalModel_tpl.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
46
47#pragma once
48#include <optional>
49#include <sstream>
50
55
56namespace gum {
57
58 namespace {
59 // Copy a BN's internal DAG and stamp node names on it (a BN's own internal DAG carries no
60 // names; they live in its variableNodeMap()).
61 template < GUM_Numeric GUM_SCALAR >
62 DAG _observedDagFromBN_(const BayesNet< GUM_SCALAR >& bn) {
63 DAG g = bn.internalDag();
64 for (auto id: g.nodes())
65 g.setName(id, bn.variable(id).name());
66 return g;
67 }
68 } // anonymous namespace
69
70 // ===============================
71 // Constructors / destructor
72 // ===============================
73
74 template < GUM_Numeric GUM_SCALAR >
77 _causalDAG_(observationalBN.internalDag()) {
78 GUM_CONSTRUCTOR(CausalModel)
79 }
80
81 template < GUM_Numeric GUM_SCALAR >
85
86 template < GUM_Numeric GUM_SCALAR >
88 const LatentDescriptorVector& latentVarsDescriptor,
89 bool assumeNonSpurious) :
91 _causalDAG_(observationalBN.internalDag()) {
92 // add each latent (children given as names)
93 for (const auto& [latent, children]: latentVarsDescriptor) {
95 }
96 GUM_CONSTRUCTOR(CausalModel)
97 }
98
99 /// Copy constructor
100 template < GUM_Numeric GUM_SCALAR >
106
108 template < GUM_Numeric GUM_SCALAR >
110 _observationalBN_(std::move(other._observationalBN_)),
111 _observedDAG_(std::move(other._observedDAG_)), _causalDAG_(std::move(other._causalDAG_)),
112 _ids_names_(std::move(other._ids_names_)) {
113 GUM_CONS_MOV(CausalModel)
114 };
115
116 template < GUM_Numeric GUM_SCALAR >
118 const LatentDescriptorVector& latentVarsDescriptor,
119 bool assumeNonSpurious) :
120 _observedDAG_(dag), _causalDAG_(dag) {
121 for (auto id: dag.nodes()) {
122 if (!dag.hasName(id)) {
123 GUM_ERROR(InvalidArgument,
124 "CausalModel: every node of the DAG must have a name (node " + std::to_string(id)
125 + " has none).");
126 }
127 }
128 for (const auto& [latent, children]: latentVarsDescriptor) {
129 addLatentVariable(latent, children, assumeNonSpurious);
130 }
131 GUM_CONSTRUCTOR(CausalModel)
132 }
133
134 // ===============================
135 // Latent variables
136 // ===============================
137
138 template < GUM_Numeric GUM_SCALAR >
140 std::string_view latentName,
141 const std::vector< std::string >& childrenOfLatent,
142 bool assumeNonSpurious) {
143 std::vector< NodeId > childIds;
144 childIds.reserve(childrenOfLatent.size());
145 for (const auto& nm: childrenOfLatent) {
146 childIds.push_back(idFromName(nm));
147 }
148 addLatentVariable(latentName, childIds, assumeNonSpurious);
149 }
150
151 template < GUM_Numeric GUM_SCALAR >
152 void CausalModel< GUM_SCALAR >::addLatentVariable(std::string_view latentName,
153 const std::vector< NodeId >& childrenOfLatent,
154 bool assumeNonSpurious) {
155 // must have at least 2 children
156 if (childrenOfLatent.size() < 2) {
157 GUM_ERROR(InvalidArgument, "A latent variable must affect at least 2 children.");
158 }
159
160 // reject names that collide with observed or existing latent variables
161 if (_observedDAG_.idFromName(std::string(latentName)).has_value()) {
163 "Latent variable name conflicts with an observed variable: "
164 + std::string(latentName));
165 }
166 if (_ids_names_.existsSecond(std::string(latentName))) {
168 "Latent variable name already used by another latent: " + std::string(latentName));
169 }
170
171 // create a fresh node id for the latent in the causal DAG
172 const NodeId id_latent = _causalDAG_.addNode();
173
174 // record bookkeeping
175 _ids_names_.insert(id_latent, std::string(latentName));
176
177 // add arcs latent -> each child
178 for (const auto& cid: childrenOfLatent) {
179 // children are observed nodes: they must already exist in causal DAG (copied from observed
180 // BN)
181 _causalDAG_.addArc(id_latent, cid);
182 }
183
184 // remove any direct causal arc among children unless assumeNonSpurious=true
185 if (!assumeNonSpurious) {
186 for (auto i = childrenOfLatent.begin(); i != childrenOfLatent.end(); ++i) {
187 auto j = i;
188 ++j;
189 for (auto jj = j; jj != childrenOfLatent.end(); ++jj) {
190 const NodeId a = *i;
191 const NodeId b = *jj;
192 // remove either direction if present
193 Arc ab(a, b), ba(b, a);
194 if (_causalDAG_.existsArc(ab)) _causalDAG_.eraseArc(ab);
195 else if (_causalDAG_.existsArc(ba)) _causalDAG_.eraseArc(ba);
196 }
197 }
198 }
199 }
200
201 template < GUM_Numeric GUM_SCALAR >
203 return _causalDAG_.existsArc(Arc(x, y));
204 }
205
206 template < GUM_Numeric GUM_SCALAR >
207 bool CausalModel< GUM_SCALAR >::existsArc(std::string_view x, std::string_view y) const {
208 const NodeId ix = idFromName(x);
209 const NodeId iy = idFromName(y);
210 return existsArc(ix, iy);
211 }
212
213 // ===============================
214 // Latent getters
215 // ===============================
216
217 template < GUM_Numeric GUM_SCALAR >
219 NodeSet latentIds;
220 for (auto it = _ids_names_.begin(); it != _ids_names_.end(); ++it) {
221 latentIds.insert(it.first());
222 }
223 return latentIds;
225
226 template < GUM_Numeric GUM_SCALAR >
228 Set< std::string > latentNames;
229 for (auto it = _ids_names_.begin(); it != _ids_names_.end(); ++it) {
230 latentNames.insert(it.second());
232 return latentNames;
233 }
234
235 // ===============================
236 // Spurious arc management
237 // ===============================
238
239 template < GUM_Numeric GUM_SCALAR >
241 // Validate arc in observationalBN
242 if (!_observedDAG_.existsArc(Arc(x, y))) {
244 "Arc(" + std::to_string(x) + "," + std::to_string(y)
245 + ") not present in observationalBN");
246 }
247 // Remove arc from causalDAG if present
248 if (_causalDAG_.existsArc(Arc(x, y))) { _causalDAG_.eraseArc(Arc(x, y)); }
249 }
250
251 template < GUM_Numeric GUM_SCALAR >
252 void CausalModel< GUM_SCALAR >::assumeSpurious(std::string_view x, std::string_view y) {
254 }
255
256 template < GUM_Numeric GUM_SCALAR >
258 // Validate arc in observationalBN
259 if (!_observedDAG_.existsArc(Arc(x, y))) {
261 "Arc(" + std::to_string(x) + "," + std::to_string(y)
262 + ") not present in observationalBN");
263 }
264 // Add arc to causalDAG if absent
265 if (!_causalDAG_.existsArc(Arc(x, y))) { _causalDAG_.addArc(x, y); }
266 }
267
268 template < GUM_Numeric GUM_SCALAR >
269 void CausalModel< GUM_SCALAR >::assumeNonSpurious(std::string_view x, std::string_view y) {
271 }
272
273 template < GUM_Numeric GUM_SCALAR >
275 return _observedDAG_.existsArc(Arc(x, y)) && !_causalDAG_.existsArc(Arc(x, y));
276 }
277
278 template < GUM_Numeric GUM_SCALAR >
279 bool CausalModel< GUM_SCALAR >::isAssumedSpurious(std::string_view x, std::string_view y) const {
281 }
283 // ===============================
284 // Connected components
285 // ===============================
286
287 // -----------------------------------------------------------------------------
288 // Connected components (weak connectivity) on the CAUSAL DAG). Delegates to the generic
289 // gum::graph::connectedComponents (reachability.h), already used by DAGmodel/DiGraph/
290 // MixedGraph -- same NodeProperty<NodeId> shape, so it gets the existing SWIG bindings
291 // (and connectedComponentsList/Count) for free.
292 // -----------------------------------------------------------------------------
293 template < GUM_Numeric GUM_SCALAR >
297
298 template < GUM_Numeric GUM_SCALAR >
301 NodeSet subset) const {
302 // If caller gave an empty subset, use "all observed" nodes
303 if (subset.empty()) {
304 for (auto n: cm._observedDAG_.nodes())
305 subset.insert(n);
306 }
307
308 // Keep only OBSERVED nodes (drop latents explicitly)
309 NodeSet nodes = subset;
310 for (auto L: cm.latentVariablesIds())
311 nodes.erase(L);
312
313 // --- Build a named DAG restricted to `nodes` (structure only: no BN, no CPTs).
314 // NodeIds are preserved so that NodeSets expressed in `cm`'s id space (Y, X, ...) stay valid
315 // in the induced sub-model.
316 DAG sub;
317 for (auto n: nodes) {
318 sub.addNodeWithId(n);
319 sub.setName(n, cm.nameFromId(n));
320 }
321
322 // add arcs among the kept observed nodes (from the *causal* DAG)
323 const auto causalDag = cm.causalDAG();
324 for (const auto& a: causalDag.arcs()) {
325 const NodeId x = a.tail();
326 const NodeId y = a.head();
327 if (nodes.contains(x) && nodes.contains(y)) { sub.addArc(x, y); }
328 }
329
330 // --- Rebuild latent descriptor restricted to kept nodes
331 LatentDescriptorVector latentDesc;
332 for (auto L: cm.latentVariablesIds()) {
333 std::vector< NodeId > mapped_children;
334 mapped_children.reserve(4);
335
336 // intersect latent children with kept observed nodes
337 for (auto c: cm.children(L)) {
338 if (nodes.contains(c)) mapped_children.push_back(c);
339 }
340
341 // keep the latent ONLY if it still has at least 2 children
342 if (mapped_children.size() >= 2) {
343 latentDesc.emplace_back(cm.nameFromId(L), std::move(mapped_children));
344 }
345 }
346
347 // assumeNonSpurious=true : pyagrum uses True here (do not remove arcs among children)
348 return CausalModel< GUM_SCALAR >(sub, latentDesc, /*assumeNonSpurious=*/true);
349 }
350
351 // ===============================
352 // Parents / Children (by id / by name)
353 // ===============================
354
355 template < GUM_Numeric GUM_SCALAR >
357 // delegate to the causal DAG (includes latent->observed arcs)
358 return _causalDAG_.parents(x);
359 }
361 template < GUM_Numeric GUM_SCALAR >
362 NodeSet CausalModel< GUM_SCALAR >::parents(std::string_view name) const {
363 return parents(idFromName(name));
364 }
365
366 template < GUM_Numeric GUM_SCALAR >
370
371 template < GUM_Numeric GUM_SCALAR >
372 NodeSet CausalModel< GUM_SCALAR >::children(std::string_view name) const {
373 return children(idFromName(name));
374 }
376 /* =======================================================================
377 * Backdoor / Frontdoor conveniences (IDs only)
378 * ======================================================================= */
379
380 template < GUM_Numeric GUM_SCALAR >
381 std::optional< NodeSet > CausalModel< GUM_SCALAR >::backDoor(NodeId cause, NodeId effect) const {
382 // Preconditions: cause/effect must be observed (non-latent).
383 const NodeSet lat = latentVariablesIds();
384 if (lat.contains(cause) || lat.contains(effect)) {
386 "CausalModel::backDoor: 'cause' and 'effect' must be observed (non-latent).");
387 }
388
389 const auto candidates = DoorCriteria::enumerateBackdoorSets(_causalDAG_, cause, effect, lat);
390 if (candidates.empty()) return std::nullopt;
391 return candidates.front(); // first valid set (may itself be empty)
392 }
393
394 template < GUM_Numeric GUM_SCALAR >
395 std::optional< NodeSet > CausalModel< GUM_SCALAR >::frontDoor(NodeId cause, NodeId effect) const {
396 // Preconditions: cause/effect must be observed (non-latent).
397 const NodeSet lat = latentVariablesIds();
398 if (lat.contains(cause) || lat.contains(effect)) {
400 "CausalModel::frontDoor: 'cause' and 'effect' must be observed (non-latent).");
401 }
402
403 const auto candidates = DoorCriteria::enumerateFrontdoorSets(_causalDAG_, cause, effect, lat);
404 if (candidates.empty()) return std::nullopt;
405 return candidates.front(); // first valid set (may itself be empty)
406 }
407
408 // ===============================
409 // DOT export
410 // ===============================
411 template < GUM_Numeric GUM_SCALAR >
413 return toDot(dot::DotOptions());
415
416 template < GUM_Numeric GUM_SCALAR >
417 std::string CausalModel< GUM_SCALAR >::toDot(const dot::DotOptions& options) const {
418 return _toDot_(dot::CausalDotStyle::read(), options);
419 }
420
421 template < GUM_Numeric GUM_SCALAR >
422 std::string CausalModel< GUM_SCALAR >::toDot(const bool SHOW_LATENT_NAMES,
423 const char* NODE_BG,
424 const char* NODE_FG,
425 const char* EDGE_COL) const {
426 auto style = dot::CausalDotStyle::read();
427 style.showLatentNames = SHOW_LATENT_NAMES;
428 if (EDGE_COL != nullptr) style.arcColor = EDGE_COL;
429 if (NODE_BG != nullptr) {
430 style.nodeBgColor = NODE_BG;
431 style.latentBgColor = NODE_BG;
432 }
433 if (NODE_FG != nullptr) {
434 style.nodeFgColor = NODE_FG;
435 style.latentFgColor = NODE_FG;
436 }
437 return _toDot_(style, dot::DotOptions());
438 }
439
440 template < GUM_Numeric GUM_SCALAR >
442 const dot::DotOptions& options) const {
443 dot::DotWriter writer(true);
444 dot::writePreamble(writer, style, options);
445
446 // the name of a node of the causal DAG
447 const auto label = [this](NodeId n) {
448 try {
449 return nameFromId(n);
450 } catch (gum::NotFound&) { return "L" + std::to_string(n); }
451 };
452
453 // a font color which is adaptive is resolved against the background of the kind of node
454 auto latentStyle = static_cast< dot::DotStyle >(style);
455 latentStyle.nodeBgColor = style.latentBgColor;
456
457 // First, latent nodes
458 for (const auto n: _causalDAG_.nodes()) {
459 if (!latentVariablesIds().contains(n)) continue;
460 auto attributes = dot::nodeAttributes(latentStyle,
461 options.node(n),
462 "",
463 "(" + std::to_string(n) + ") " + label(n));
464 dot::addDefaultAttribute(attributes, "shape", style.showLatentNames ? "ellipse" : "point");
465 dot::addDefaultAttribute(attributes, "fontcolor", style.latentFgColor);
466 dot::addDefaultAttribute(attributes, "fillcolor", style.latentBgColor);
467 writer.node(label(n), attributes);
468 }
469
470 // Then, observed nodes
471 for (const auto n: _causalDAG_.nodes()) {
472 if (latentVariablesIds().contains(n)) continue;
473 writer.node(label(n),
475 options.node(n),
476 "",
477 "(" + std::to_string(n) + ") " + label(n)));
478 }
479
480 // Edges
481 for (const auto& arc: _causalDAG_.arcs()) {
482 auto attributes = dot::arcAttributes(options.arc(arc.tail(), arc.head()));
483 if (latentVariablesIds().contains(arc.tail()) || latentVariablesIds().contains(arc.head())) {
484 dot::addDefaultAttribute(attributes, "style", "dashed");
485 } else {
486 // two lines of the same color for an arc of the observed variables
487 dot::addDefaultAttribute(attributes, "color", style.arcColor + ":" + style.arcColor);
488 }
489 writer.edge(label(arc.tail()), label(arc.head()), attributes);
490 }
491
492 return writer.str();
493 }
494
495 // ===============================
496 // Names and IDs Getters
497 // ===============================
498
499 template < GUM_Numeric GUM_SCALAR >
502 for (const auto n: _observedDAG_.nodes()) {
503 names.insert(_observedDAG_.nameFromId(n));
504 }
505 // latent names (from bijection)
506 for (auto it = _ids_names_.begin(); it != _ids_names_.end(); ++it) {
507 names.insert(it.second());
508 }
509 return names;
510 }
511
512 template < GUM_Numeric GUM_SCALAR >
513 NodeId CausalModel< GUM_SCALAR >::idFromName(std::string_view name) const {
514 if (_ids_names_.existsSecond(std::string(name))) {
515 return _ids_names_.first(std::string(name));
516 } else {
517 const auto opt = _observedDAG_.idFromName(std::string(name));
518 if (!opt.has_value()) {
520 "CausalModel::idFromName: no observed or latent variable named '"
521 + std::string(name) + "'.");
522 }
523 return *opt;
524 }
525 }
526
527 template < GUM_Numeric GUM_SCALAR >
529 if (_ids_names_.existsFirst(id)) {
530 return _ids_names_.second(id);
531 } else {
532 return _observedDAG_.nameFromId(id);
533 }
534 }
535
536 template < GUM_Numeric GUM_SCALAR >
538 CausalModel< GUM_SCALAR >::id2name(bool includeLatentVariables) const {
540
541 // Add observed variables
542 for (const auto n: _observedDAG_.nodes()) {
543 result.insert(n, _observedDAG_.nameFromId(n));
544 }
545
546 // Add latent variables if requested
547 if (includeLatentVariables) {
548 for (auto iter = _ids_names_.beginSafe(); iter != _ids_names_.endSafe(); ++iter) {
549 result.insert(iter.first(), iter.second());
550 }
551 }
552
553 return result;
554 }
555
556 // ===============================
557 // Inline accessors
558 // ===============================
559
560 template < GUM_Numeric GUM_SCALAR >
562 return _observationalBN_.has_value();
563 }
564
565 template < GUM_Numeric GUM_SCALAR >
566 const BayesNet< GUM_SCALAR >& CausalModel< GUM_SCALAR >::observationalBN() const {
567 if (!_observationalBN_.has_value()) {
569 "CausalModel::observationalBN: this model has no observational BayesNet "
570 "(built from a DAG).");
571 }
572 return *_observationalBN_;
573 }
574
575 template < GUM_Numeric GUM_SCALAR >
577 DAG g = _causalDAG_;
578 for (auto id: g)
579 g.setName(id, nameFromId(id));
580 return g;
581 }
582
583 template < GUM_Numeric GUM_SCALAR >
584 std::optional< NodeSet > CausalModel< GUM_SCALAR >::backDoor(std::string_view cause,
585 std::string_view effect) const {
586 return backDoor(idFromName(cause), idFromName(effect));
587 }
588
589 template < GUM_Numeric GUM_SCALAR >
590 std::optional< NodeSet > CausalModel< GUM_SCALAR >::frontDoor(std::string_view cause,
591 std::string_view effect) const {
592 return frontDoor(idFromName(cause), idFromName(effect));
593 }
594
595 template < GUM_Numeric GUM_SCALAR >
597 return observationalBN().variable(id);
598 }
599
600 template < GUM_Numeric GUM_SCALAR >
601 const DiscreteVariable& CausalModel< GUM_SCALAR >::variable(std::string_view name) const {
602 return observationalBN().variable(idFromName(name));
603 }
604
605} // namespace gum
The base class for all directed edges.
Class representing a Bayesian network.
Definition BayesNet.h:99
A causal model pairing a causal DAG with an optional observational BayesNet.
Definition causalModel.h:96
DAG causalDAG() const
Causal DAG (observed + latent variables), with node names set.
DAG _observedDAG_
Frozen copy of the observed DAG (never mutated after construction); names are set on it directly (set...
std::string _toDot_(const dot::CausalDotStyle &style, const dot::DotOptions &options) const
the dot of the causal DAG, with a look and options
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.
NodeProperty< NodeId > connectedComponents() const
Weakly connected components of the causal DAG.
bool hasObservationalBN() const noexcept
Whether this model was built from a BayesNet (CPTs available).
~CausalModel()
Destructor.
NodeSet latentVariablesIds() const
Node ids of all latent variables.
DAG _causalDAG_
The underlying DAG representing the causal structure (observed + latent).
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 (latents only).
NodeId idFromName(std::string_view name) const
Node id from variable name (observed or latent).
std::string toDot() const
DOT representation of the causal DAG (observed + latent), with the look of the configuration.
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).
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).
std::optional< BayesNet< GUM_SCALAR > > _observationalBN_
The observed Bayesian network, when the model was built with CPTs. Absent when built from a plain DAG...
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
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition DAG_inl.h:75
Base class for discrete random variable.
static NodeSetVec enumerateBackdoorSets(const DAG &dag, NodeId X, NodeId Y, const NodeSet &excluded_nodes=NodeSet(), std::size_t max_cardinality=0, bool only_minimal=true, bool stopAtFirst=false)
Enumerate valid backdoor adjustment sets.
static NodeSetVec enumerateFrontdoorSets(const DAG &dag, NodeId X, NodeId Y, const NodeSet &excluded_nodes=NodeSet(), std::size_t max_cardinality=0, bool only_minimal=true, bool stopAtFirst=false)
Enumerate valid frontdoor adjustment sets.
Exception: at least one argument passed to a function is not what was expected.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
void setName(NodeId id, const std::string &name)
sets the name of node id
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:468
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
const DotArcOptions * arc(Size tail, Size head) const
the options of the arc from tail to head, or nullptr if none was given for it
const DotNodeOptions * node(Size node) const
the options of a node, or nullptr if none was given for it
Writes a graph in the dot language, statement after statement, in the order of the calls.
Definition dotWriter.h:100
void edge(std::string_view from, std::string_view to, const DotAttributes &attributes={})
an edge from from to to (-> in a directed graph, -- in an undirected one).
std::string str() const
The graph written so far, closed. It can be called at any moment, and the writer can be used after.
void node(std::string_view id, const DotAttributes &attributes={})
a node, whose identifier is id (quoted).
Door criteria (backdoor/frontdoor) utilities for a DAG.
The statements common to the graphs drawn with graphviz : preamble, attributes of nodes and arcs.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
GUM_SHARED_PUBLIC bool contains(std::string_view s, std::string_view needle)
true if needle in s
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
NodeProperty< NodeId > connectedComponents(const G &g)
Returns a node-to-component-id mapping for the (weakly) connected components of g.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::vector< LatentDescriptorIds > LatentDescriptorVector
Collection of latent descriptors (see LatentDescriptorIds).
Definition causalModel.h:71
Generic node-reachability algorithms for aGrUM graphs.
The style of a causal model (section causal): nodeBgColor and nodeFgColor are the ones of the observe...
Definition dotStyle.h:153
std::string latentBgColor
Definition dotStyle.h:154
std::string latentFgColor
Definition dotStyle.h:155
static CausalDotStyle read()
Definition dotStyle.cpp:155
bool showLatentNames
show the names of the latent variables
Definition dotStyle.h:157
The look shared by all the graphs: size, direction, layout, colors of the arcs and of the nodes.
Definition dotStyle.h:76
std::string nodeBgColor
background color of the nodes
Definition dotStyle.h:86
std::string arcColor
color of the arcs and edges, and of the border of the nodes
Definition dotStyle.h:84