aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
counterfactual.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_COUNTERFACTUAL_H
42#define GUM_COUNTERFACTUAL_H
43
44#include <memory>
45#include <ostream>
46#include <string>
47
49
50namespace gum {
101 template < GUM_Numeric GUM_ELEMENT >
103 public:
104 using VarName = std::string;
105 using ValName = std::string;
106
107 // ---------- Names-based constructor ----------
126 const Set< std::string >& on,
130
131 // ---------- IDs-based constructor (NodeSet overload) ----------
147 const NodeSet& onIds,
148 const NodeSet& whatifIds,
151
152 // ---------- Twin builders (overloaded like CausalImpact) ----------
157
160 const HashTable< NodeId, Idx >& profileIds,
161 const NodeSet& whatifIds);
162
164 void run();
165
166 // ---------- Accessors ----------
168
170
179 const CausalImpact< GUM_ELEMENT >& impact() const;
180
192 const Tensor< GUM_ELEMENT >& value() const;
193
194 const Set< std::string >& on() const;
195
196 const Set< std::string >& whatif() const;
197
199
201
202 std::string toString() const;
203
204 private:
207
209
211 const NodeSet& ids);
214 const HashTable< NodeId, Idx >& idAssign);
215
216 private:
222
225
226 std::unique_ptr< CausalImpact< GUM_ELEMENT > > _ci;
227 };
228
229 // ============================================================================
230 // Standalone helpers (Python-parity)
231 // ============================================================================
232
250 template < GUM_Numeric GUM_ELEMENT >
252 const Set< std::string >& on,
253 const Set< std::string >& whatif,
258
272 template < GUM_Numeric GUM_ELEMENT >
276 const Set< std::string >& whatif);
277
278} // namespace gum
279
281
282#endif // GUM_COUNTERFACTUAL_H
A container for an identified causal query.
CausalModel< GUM_ELEMENT > _twin
const Set< std::string > & on() const
std::unique_ptr< CausalImpact< GUM_ELEMENT > > _ci
HashTable< VarName, ValName > _profile
const CausalFormula< GUM_ELEMENT > & _ciResult() const
HashTable< VarName, ValName > _values
const CausalModel< GUM_ELEMENT > & twinModel() const
const CausalModel< GUM_ELEMENT > & originalModel() const
Tensor< GUM_ELEMENT > _adaptedValue
const Set< std::string > & whatif() const
static CausalModel< GUM_ELEMENT > counterFactualModel(const CausalModel< GUM_ELEMENT > &cm, const HashTable< VarName, ValName > &profile, const Set< std::string > &whatif)
static Tensor< GUM_ELEMENT > _adaptToOriginalVariables_(const Tensor< GUM_ELEMENT > &adj, const CausalModel< GUM_ELEMENT > &cm)
static HashTable< VarName, ValName > _idAssignToNameAssign_(const CausalModel< GUM_ELEMENT > &cm, const HashTable< NodeId, Idx > &idAssign)
const Tensor< GUM_ELEMENT > & value() const
Numeric result adapted to the original model’s variables.
static Set< std::string > _idsToNames_(const CausalModel< GUM_ELEMENT > &cm, const NodeSet &ids)
const HashTable< VarName, ValName > & values() const
const CausalImpact< GUM_ELEMENT > & impact() const
Symbolic result produced on the twin model.
std::string toString() const
const CausalModel< GUM_ELEMENT > & _cm
const HashTable< VarName, ValName > & profile() const
Set< std::string > _whatif
void run()
Rebuild the internal CausalImpact (ctor already calls this).
Counterfactual(const CausalModel< GUM_ELEMENT > &cm, const Set< std::string > &on, const Set< std::string > &whatif, const HashTable< VarName, ValName > &profile=HashTable< VarName, ValName >(), const HashTable< VarName, ValName > &values=HashTable< VarName, ValName >())
Construct a counterfactual query using variable names.
Set< std::string > _on
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
CausalModel< GUM_ELEMENT > counterfactualModel(const CausalModel< GUM_ELEMENT > &cm, const HashTable< std::string, std::string > &profile, const Set< std::string > &whatif)
Build the twin causal model.
Tensor< GUM_ELEMENT > counterfactual(const CausalModel< GUM_ELEMENT > &cm, const Set< std::string > &on, const Set< std::string > &whatif, const HashTable< std::string, std::string > &profile=HashTable< std::string, std::string >(), const HashTable< std::string, std::string > &values=HashTable< std::string, std::string >())
Compute a counterfactual distribution using the high-level API.