pyAgrum-nightly 2.1.1.9.dev202506061747485979__cp310-abi3-manylinux2014_aarch64.whl → 2.3.1.9.dev202601031765915415__cp310-abi3-manylinux2014_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. pyagrum/__init__.py +6 -2
  2. pyagrum/_pyagrum.so +0 -0
  3. pyagrum/bnmixture/BNMInference.py +6 -2
  4. pyagrum/bnmixture/BNMLearning.py +12 -2
  5. pyagrum/bnmixture/BNMixture.py +6 -2
  6. pyagrum/bnmixture/__init__.py +6 -2
  7. pyagrum/bnmixture/notebook.py +6 -2
  8. pyagrum/causal/_CausalFormula.py +6 -2
  9. pyagrum/causal/_CausalModel.py +6 -2
  10. pyagrum/causal/__init__.py +6 -2
  11. pyagrum/causal/_causalImpact.py +6 -2
  12. pyagrum/causal/_dSeparation.py +6 -2
  13. pyagrum/causal/_doAST.py +6 -2
  14. pyagrum/causal/_doCalculus.py +6 -2
  15. pyagrum/causal/_doorCriteria.py +6 -2
  16. pyagrum/causal/_exceptions.py +6 -2
  17. pyagrum/causal/_types.py +6 -2
  18. pyagrum/causal/causalEffectEstimation/_CausalEffectEstimation.py +6 -2
  19. pyagrum/causal/causalEffectEstimation/_IVEstimators.py +6 -2
  20. pyagrum/causal/causalEffectEstimation/_RCTEstimators.py +6 -2
  21. pyagrum/causal/causalEffectEstimation/__init__.py +6 -2
  22. pyagrum/causal/causalEffectEstimation/_backdoorEstimators.py +6 -2
  23. pyagrum/causal/causalEffectEstimation/_causalBNEstimator.py +6 -2
  24. pyagrum/causal/causalEffectEstimation/_frontdoorEstimators.py +6 -2
  25. pyagrum/causal/causalEffectEstimation/_learners.py +6 -2
  26. pyagrum/causal/causalEffectEstimation/_utils.py +6 -2
  27. pyagrum/causal/notebook.py +8 -3
  28. pyagrum/clg/CLG.py +6 -2
  29. pyagrum/clg/GaussianVariable.py +6 -2
  30. pyagrum/clg/SEM.py +6 -2
  31. pyagrum/clg/__init__.py +6 -2
  32. pyagrum/clg/canonicalForm.py +6 -2
  33. pyagrum/clg/constants.py +6 -2
  34. pyagrum/clg/forwardSampling.py +6 -2
  35. pyagrum/clg/learning.py +6 -2
  36. pyagrum/clg/notebook.py +6 -2
  37. pyagrum/clg/variableElimination.py +6 -2
  38. pyagrum/common.py +7 -3
  39. pyagrum/config.py +7 -2
  40. pyagrum/ctbn/CIM.py +6 -2
  41. pyagrum/ctbn/CTBN.py +6 -2
  42. pyagrum/ctbn/CTBNGenerator.py +6 -2
  43. pyagrum/ctbn/CTBNInference.py +6 -2
  44. pyagrum/ctbn/CTBNLearner.py +6 -2
  45. pyagrum/ctbn/SamplesStats.py +6 -2
  46. pyagrum/ctbn/StatsIndepTest.py +6 -2
  47. pyagrum/ctbn/__init__.py +6 -2
  48. pyagrum/ctbn/constants.py +6 -2
  49. pyagrum/ctbn/notebook.py +6 -2
  50. pyagrum/deprecated.py +6 -2
  51. pyagrum/explain/_ComputationCausal.py +75 -0
  52. pyagrum/explain/_ComputationConditional.py +48 -0
  53. pyagrum/explain/_ComputationMarginal.py +48 -0
  54. pyagrum/explain/_CustomShapleyCache.py +110 -0
  55. pyagrum/explain/_Explainer.py +176 -0
  56. pyagrum/explain/_Explanation.py +70 -0
  57. pyagrum/explain/_FIFOCache.py +54 -0
  58. pyagrum/explain/_ShallCausalValues.py +204 -0
  59. pyagrum/explain/_ShallConditionalValues.py +155 -0
  60. pyagrum/explain/_ShallMarginalValues.py +155 -0
  61. pyagrum/explain/_ShallValues.py +296 -0
  62. pyagrum/explain/_ShapCausalValues.py +208 -0
  63. pyagrum/explain/_ShapConditionalValues.py +126 -0
  64. pyagrum/explain/_ShapMarginalValues.py +191 -0
  65. pyagrum/explain/_ShapleyValues.py +298 -0
  66. pyagrum/explain/__init__.py +81 -0
  67. pyagrum/explain/_explGeneralizedMarkovBlanket.py +152 -0
  68. pyagrum/explain/_explIndependenceListForPairs.py +146 -0
  69. pyagrum/explain/_explInformationGraph.py +264 -0
  70. pyagrum/explain/notebook/__init__.py +54 -0
  71. pyagrum/explain/notebook/_bar.py +142 -0
  72. pyagrum/explain/notebook/_beeswarm.py +174 -0
  73. pyagrum/explain/notebook/_showShapValues.py +97 -0
  74. pyagrum/explain/notebook/_waterfall.py +220 -0
  75. pyagrum/explain/shapley.py +225 -0
  76. pyagrum/lib/__init__.py +6 -2
  77. pyagrum/lib/_colors.py +6 -2
  78. pyagrum/lib/bn2graph.py +6 -2
  79. pyagrum/lib/bn2roc.py +6 -2
  80. pyagrum/lib/bn2scores.py +6 -2
  81. pyagrum/lib/bn_vs_bn.py +6 -2
  82. pyagrum/lib/cn2graph.py +6 -2
  83. pyagrum/lib/discreteTypeProcessor.py +99 -81
  84. pyagrum/lib/discretizer.py +6 -2
  85. pyagrum/lib/dynamicBN.py +6 -2
  86. pyagrum/lib/explain.py +17 -492
  87. pyagrum/lib/export.py +6 -2
  88. pyagrum/lib/id2graph.py +6 -2
  89. pyagrum/lib/image.py +6 -2
  90. pyagrum/lib/ipython.py +6 -2
  91. pyagrum/lib/mrf2graph.py +6 -2
  92. pyagrum/lib/notebook.py +6 -2
  93. pyagrum/lib/proba_histogram.py +6 -2
  94. pyagrum/lib/utils.py +6 -2
  95. pyagrum/pyagrum.py +976 -126
  96. pyagrum/skbn/_MBCalcul.py +6 -2
  97. pyagrum/skbn/__init__.py +6 -2
  98. pyagrum/skbn/_learningMethods.py +6 -2
  99. pyagrum/skbn/_utils.py +6 -2
  100. pyagrum/skbn/bnclassifier.py +6 -2
  101. pyagrum_nightly-2.1.1.9.dev202506061747485979.dist-info/LICENSE → pyagrum_nightly-2.3.1.9.dev202601031765915415.dist-info/LICENSE.md +3 -1
  102. pyagrum_nightly-2.3.1.9.dev202601031765915415.dist-info/LICENSES/LGPL-3.0-or-later.txt +304 -0
  103. pyagrum_nightly-2.3.1.9.dev202601031765915415.dist-info/LICENSES/MIT.txt +18 -0
  104. {pyagrum_nightly-2.1.1.9.dev202506061747485979.dist-info → pyagrum_nightly-2.3.1.9.dev202601031765915415.dist-info}/METADATA +3 -1
  105. pyagrum_nightly-2.3.1.9.dev202601031765915415.dist-info/RECORD +107 -0
  106. {pyagrum_nightly-2.1.1.9.dev202506061747485979.dist-info → pyagrum_nightly-2.3.1.9.dev202601031765915415.dist-info}/WHEEL +1 -1
  107. pyagrum/lib/shapley.py +0 -657
  108. pyagrum_nightly-2.1.1.9.dev202506061747485979.dist-info/LICENSE.LGPL +0 -165
  109. pyagrum_nightly-2.1.1.9.dev202506061747485979.dist-info/LICENSE.MIT +0 -17
  110. pyagrum_nightly-2.1.1.9.dev202506061747485979.dist-info/RECORD +0 -83
@@ -0,0 +1,176 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 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-2025 #
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
+ from abc import ABC, abstractmethod
42
+ import pyagrum as gum
43
+ from pyagrum.explain._FIFOCache import FIFOCache
44
+
45
+ # Calculations
46
+ import numpy as np
47
+ import math
48
+ from itertools import combinations
49
+ from typing import Callable
50
+
51
+
52
+ class Explainer(ABC):
53
+ """
54
+ ___Documentation___
55
+ """
56
+
57
+ def __init__(self, bn: gum.BayesNet):
58
+ """
59
+ Parameters:
60
+ ------
61
+ bn : pyagrum.BayesNet
62
+ The Bayesian Network.
63
+
64
+ Raises:
65
+ ------
66
+ TypeError : If bn is not a gum.BayesNet.
67
+ """
68
+ super().__init__()
69
+ if not isinstance(bn, gum.BayesNet):
70
+ raise TypeError("bn must be a gum.BayesNet instance, but got {}".format(type(bn)))
71
+ self.bn = bn
72
+ self.M = len(bn.nodes()) # Total number of nodes in the Bayesian Network.
73
+ self.feat_names = np.empty(self.M, dtype=object) # Array to store feature names by their node ID.
74
+ for name in self.bn.names():
75
+ self.feat_names[self.bn.idFromName(name)] = name
76
+
77
+ @staticmethod
78
+ def _log(p: float):
79
+ # Applies the log function to the probability.
80
+ return np.log(p) if p > 0 else -np.inf
81
+
82
+ @staticmethod
83
+ def _logit(p: any):
84
+ # Applies the logit transformation to the probabilities.
85
+ p = np.asarray(p) # Guarantee p is a numpy array.
86
+ with np.errstate(divide="ignore", invalid="ignore"):
87
+ result = np.log(p / (1 - p))
88
+ result = np.where(p == 0, 0.0, result)
89
+ result = np.where(p == 1, 0.0, result)
90
+ return result
91
+
92
+ @staticmethod
93
+ def _identity(x: any):
94
+ # Returns the input as is (identity function).
95
+ return x
96
+
97
+ def _labelToPos_row(self, x: np.ndarray, elements: list[int]) -> np.ndarray:
98
+ # Converts labels to positions for a single instance.
99
+ y = np.empty(shape=x.shape, dtype=int)
100
+ for i in elements:
101
+ try:
102
+ val = self.bn.variable(i).posLabel(x[i])
103
+ except:
104
+ val = int(x[i])
105
+ y[i] = val
106
+ return y
107
+
108
+ def _labelToPos_df(self, x: np.ndarray, elements: list[int]) -> np.ndarray:
109
+ # Converts labels to positions for multiple instances.
110
+ y = np.empty(shape=x.shape, dtype=int) # Initialisation
111
+ posLabelVect = np.vectorize(lambda i, j: self.bn.variable(int(j)).posLabel(i))
112
+ for j in elements:
113
+ try:
114
+ self.bn.variable(j).posLabel(x[0, j])
115
+ y[:, j] = posLabelVect(x[:, j], j)
116
+ except NotImplementedError:
117
+ y[:, j] = x[:, j].astype(int)
118
+ return y
119
+
120
+ @staticmethod
121
+ def _extract(data: np.ndarray, tau: list[int], alpha: np.ndarray) -> np.ndarray:
122
+ # Extracts the data given the values in alpha for the nodes in tau.
123
+ mask = np.all(data[:, tau] == alpha, axis=1)
124
+ idx = np.nonzero(mask)[0]
125
+ return idx
126
+
127
+ @staticmethod
128
+ def _coalitions(elements_for_coalitions):
129
+ # Generates all possible coalitions from the given elements.
130
+ all_coalitions = []
131
+ for r in range(1, len(elements_for_coalitions) + 1):
132
+ for combo in combinations(elements_for_coalitions, r):
133
+ all_coalitions.append(list(combo))
134
+ return all_coalitions
135
+
136
+ @staticmethod
137
+ def _invcoeff_shap(m, s):
138
+ # Computes the inverse coefficient for the Shapley value formula.
139
+ return (m - s) * math.comb(m, s)
140
+
141
+ @staticmethod
142
+ def _shap_term(prob_with, prob_without, m, s):
143
+ return (prob_with - prob_without) / Explainer._invcoeff_shap(m, s)
144
+
145
+ def _value(
146
+ self, data: np.ndarray, counts: list[int], elements: list[int], sigma: list[int], cache: FIFOCache, **kwargs
147
+ ) -> float | np.ndarray:
148
+ # -- #
149
+ length: int = len(data)
150
+ func1: Callable = kwargs["func1"]
151
+ func2: Callable = kwargs["func2"]
152
+ params1: dict[str, any] = kwargs["params1"]
153
+ params2: dict[str, any] = kwargs["params2"]
154
+ # -- #
155
+
156
+ val = 0.0
157
+ norm = 0.0
158
+ for i in range(length):
159
+ evidces1 = {self.feat_names[key]: int(data[i, key]) for key in elements}
160
+ evidces2 = {self.feat_names[key]: int(data[i, key]) for key in sigma}
161
+ term1 = cache.get(tuple(data[i, elements]), None)
162
+ if term1 is None:
163
+ term1 = func1(evidces1, **params1)
164
+ cache[tuple(data[i, elements])] = term1
165
+
166
+ term2 = func2(evidces2, counts[i], **params2)
167
+ val += term1 * term2
168
+ norm += term2
169
+
170
+ return 0.0 if norm == 0.0 else val / norm
171
+
172
+ @abstractmethod
173
+ def compute(self, data: tuple | None, N=100):
174
+ # Computes the Shapley values for the target node based on the provided data.
175
+ # This method must be implemented in subclasses
176
+ raise NotImplementedError("This method must be implemented in subclasses.")
@@ -0,0 +1,70 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 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-2025 #
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
+ from collections.abc import MutableMapping
42
+
43
+
44
+ class Explanation(MutableMapping):
45
+ def __init__(self, values, importances, feature_names, data, baseline, func, values_type) -> None:
46
+ self._values = values
47
+ self.importances = importances
48
+ self.feature_names = feature_names
49
+ self.data = data
50
+ self.baseline = baseline
51
+ self.func = func
52
+ self.values_type = values_type
53
+
54
+ def __getitem__(self, key):
55
+ return self._values[key]
56
+
57
+ def __setitem__(self, key, value):
58
+ self._values[key] = value
59
+
60
+ def __delitem__(self, key):
61
+ del self._values[key]
62
+
63
+ def __iter__(self):
64
+ return iter(self._values)
65
+
66
+ def __len__(self):
67
+ return len(self._values)
68
+
69
+ def __repr__(self):
70
+ return f"Explanation(values={self._values})"
@@ -0,0 +1,54 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 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-2025 #
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
+ from collections import OrderedDict
42
+
43
+
44
+ class FIFOCache(OrderedDict):
45
+ def __init__(self, maxlen, *args, **kwargs):
46
+ self.maxlen = maxlen
47
+ super().__init__(*args, **kwargs)
48
+
49
+ def __setitem__(self, key, value):
50
+ if key in self:
51
+ del self[key] # pour mettre à jour l'ordre
52
+ elif len(self) >= self.maxlen:
53
+ self.popitem(last=False) # retire le plus ancien
54
+ super().__setitem__(key, value)
@@ -0,0 +1,204 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 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-2025 #
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
+ import pyagrum as gum
42
+ from pyagrum.explain._ShallValues import ShallValues
43
+ from pyagrum.explain._CustomShapleyCache import CustomShapleyCache
44
+ from pyagrum.explain._ComputationCausal import CausalComputation
45
+ from pyagrum.explain._FIFOCache import FIFOCache
46
+
47
+ import numpy as np
48
+
49
+
50
+ class CausalShallValues(ShallValues, CausalComputation):
51
+ """
52
+ The CausalShallValues class computes the Causal Shall values in a Bayesian Network.
53
+ """
54
+
55
+ def __init__(self, bn: gum.BayesNet, background: tuple | None, sample_size: int = 1000, log: bool = True):
56
+ """
57
+ Note 1 : All rows in the background data that contain NaN values in columns corresponding to variables in the Bayesian Network will be dropped.
58
+ Note 2 : In comparison to Marginal and Conditional Shall values it is impossible to calculate empirical probabilities 'true to the data'.
59
+ We are forced to calculate probabilités 'true to the model'.
60
+
61
+
62
+ Parameters:
63
+ ------
64
+ bn : pyagrum.BayesNet
65
+ The Bayesian Network.
66
+ background : tuple[pandas.DataFrame, bool] | None
67
+ A tuple containing a pandas DataFrame and a boolean indicating whether the DataFrame includes labels or positional values.
68
+ sample_size : int
69
+ The size of the background sample to generate if `background` is None.
70
+ log : bool
71
+ If True, applies a logarithmic transformation to the probabilities.
72
+
73
+ Raises
74
+ ------
75
+ TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
76
+ ValueError : If background data does not contain all variables present in the Bayesian Network or if
77
+ background data is empty after rows with NaNs were dropped.
78
+
79
+ Raises:
80
+ ------
81
+ TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
82
+ ValueError : If background data does not contain all variables present in the Bayesian Network or if
83
+ background data is empty after rows with NaNs were dropped.
84
+ """
85
+
86
+ super().__init__(bn, background, sample_size, log)
87
+ self.baseline = self._value(
88
+ data=self._data,
89
+ counts=self.counts,
90
+ elements=self.vars_ids,
91
+ sigma=self.vars_ids,
92
+ cache=FIFOCache(100),
93
+ func1=self._joint,
94
+ params1={},
95
+ func2=self._weight,
96
+ params2={"doLazy": gum.LazyPropagation(self.bn)},
97
+ )
98
+
99
+ def _coalition_contribution(self, posterior_prob_with, posterior_prob_without, m, s):
100
+ return (posterior_prob_with - posterior_prob_without) / self._invcoeff_shap(m, s)
101
+
102
+ def _shall_1dim(self, x):
103
+ contributions = np.zeros(self.M) # M : number of nodes in BN
104
+
105
+ # Caches
106
+ custom_cache = CustomShapleyCache(5000)
107
+ fifo_cache = FIFOCache(1000)
108
+ # Sets the baseline probability in the cache.
109
+ custom_cache.set(0, (), self.baseline)
110
+ # Compute the coalitions
111
+ coalitions = self._coalitions(self.vars_ids)
112
+
113
+ for tau in coalitions:
114
+ # self.ie.eraseAllEvidence()
115
+ doNet = self._doCalculus(self.bn, tau) # new BN
116
+ sigma = self._outOfCoalition(tau, self.vars_ids) # all nodes \ tau
117
+
118
+ doInst = gum.Instantiation()
119
+ for var in doNet.ids(self.feat_names):
120
+ doInst.add(doNet.variable(var))
121
+
122
+ # Instanciation of tau
123
+ alpha = x[tau] # extract columns in tau
124
+ if sigma != []:
125
+ self._chgCpt(doNet, tau, alpha)
126
+ doLazy = gum.LazyPropagation(doNet)
127
+ doLazy.addTarget(tau[0]) # see if target should be added for optimization
128
+ idx = self._extract(self._data, tau, alpha)
129
+ # Compute the value for this coalition.
130
+ joint_with = self._value(
131
+ data=self._data[idx],
132
+ counts=self.counts[idx],
133
+ elements=self.vars_ids,
134
+ sigma=sigma,
135
+ cache=fifo_cache,
136
+ func1=self._joint,
137
+ params1={},
138
+ func2=self._weight,
139
+ params2={"doLazy": doLazy},
140
+ )
141
+ else:
142
+ self.inst.fromdict({self.feat_names[key]: int(val) for key, val in zip(tau, alpha)})
143
+ joint = self.bn.jointProbability(self.inst)
144
+ joint_with = self.func(joint)
145
+
146
+ custom_cache.set(0, tuple(tau), joint_with)
147
+ # Contribution of each feature
148
+ for t in tau:
149
+ key = tuple((f for f in tau if f != t))
150
+ joint_without = custom_cache.get(0, key)
151
+ contributions[t] += self._coalition_contribution(joint_with, joint_without, len(self.vars_ids), len(tau) - 1)
152
+ return contributions
153
+
154
+ def _shall_ndim(self, x):
155
+ # Initialisation
156
+ contributions = np.zeros((self.M, len(x)))
157
+
158
+ # Caches
159
+ custom_cache = CustomShapleyCache(5000)
160
+ fifo_cache = FIFOCache(1000)
161
+ # Sets the baseline probability in the cache.
162
+ custom_cache.set(0, (), self.baseline)
163
+ # Compute the coalitions
164
+ coalitions = self._coalitions(self.vars_ids)
165
+
166
+ for tau in coalitions:
167
+ doNet = self._doCalculus(self.bn, tau)
168
+ sigma = self._outOfCoalition(tau, self.vars_ids)
169
+
170
+ for i in range(len(x)):
171
+ alpha = x[i, tau]
172
+ if sigma != []:
173
+ # Instanciation of tau
174
+ self._chgCpt(doNet, tau, alpha) # BN knowing alpha
175
+ doLazy = gum.LazyPropagation(doNet)
176
+ doLazy.addTarget(tau[0]) # just to speed up the calculation
177
+ idx = self._extract(self._data, tau, alpha)
178
+ # Compute the value for this coalition.
179
+ joint_with = self._value(
180
+ data=self._data[idx],
181
+ counts=self.counts[idx],
182
+ elements=self.vars_ids,
183
+ sigma=sigma,
184
+ cache=fifo_cache,
185
+ func1=self._joint,
186
+ params1={},
187
+ func2=self._weight,
188
+ params2={"doLazy": doLazy},
189
+ )
190
+
191
+ else:
192
+ self.inst.fromdict({self.feat_names[key]: int(val) for key, val in zip(tau, alpha)})
193
+ joint = self.bn.jointProbability(self.inst)
194
+ joint_with = self.func(joint)
195
+
196
+ custom_cache.set(i, tuple(tau), joint_with)
197
+ # Contribution of each feature
198
+ for t in tau:
199
+ key = tuple((f for f in tau if f != t))
200
+ joint_without = custom_cache.get(i, key) if len(key) > 0 else custom_cache.get(0, ())
201
+ contributions[t, i] += self._coalition_contribution(
202
+ joint_with, joint_without, len(self.vars_ids), len(tau) - 1
203
+ )
204
+ return contributions
@@ -0,0 +1,155 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 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-2025 #
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
+ import pyagrum as gum
42
+
43
+ from pyagrum.explain._ShallValues import ShallValues
44
+ from pyagrum.explain._CustomShapleyCache import CustomShapleyCache
45
+ from pyagrum.explain._FIFOCache import FIFOCache
46
+ from pyagrum.explain._ComputationConditional import ConditionalComputation
47
+
48
+ import numpy as np
49
+ from warnings import warn
50
+
51
+
52
+ class ConditionalShallValues(ShallValues, ConditionalComputation):
53
+ """
54
+ The ConditionalShallValues class computes the conditional Shall values in a Bayesian Network.
55
+ """
56
+
57
+ def __init__(self, bn: gum.BayesNet, background: tuple | None, sample_size: int = 1000, log: bool = True):
58
+ """
59
+ Note 1 : All rows in the background data that contain NaN values in columns corresponding to variables in the Bayesian Network will be dropped.
60
+ Note 2 : For small databases SHALL values can be incorrect.
61
+
62
+ Parameters:
63
+ ------
64
+ bn : pyagrum.BayesNet
65
+ The Bayesian Network.
66
+ background : tuple[pandas.DataFrame, bool] | None
67
+ A tuple containing a pandas DataFrame and a boolean indicating whether the DataFrame includes labels or positional values.
68
+ sample_size : int
69
+ The size of the background sample to generate if `background` is None.
70
+ log : bool
71
+ If True, applies a logarithmic transformation to the probabilities.
72
+
73
+ Raises
74
+ ------
75
+ TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
76
+ ValueError : If background data does not contain all variables present in the Bayesian Network or if
77
+ background data is empty after rows with NaNs were dropped.
78
+
79
+ Raises:
80
+ ------
81
+ TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
82
+ ValueError : If background data does not contain all variables present in the Bayesian Network or if
83
+ background data is empty after rows with NaNs were dropped.
84
+ """
85
+
86
+ super().__init__(bn, background, sample_size, log) # Initializes the ShapleyValues class.
87
+ self.baseline = self._value(
88
+ data=self._data,
89
+ counts=self.counts,
90
+ elements=self.vars_ids,
91
+ sigma=[],
92
+ cache=FIFOCache(100),
93
+ func1=self._joint,
94
+ params1={},
95
+ func2=self._weight,
96
+ params2={},
97
+ )
98
+
99
+ def _coalition_contribution(self, k, ex, feature, nodes_id, nodes_vals, cache, fifo_cache):
100
+ # key2 is always set since subsets are sorted by length
101
+ key1, key2, _ = cache.generate_keys(self.bn, None, feature, nodes_id)
102
+ # key1 : nodes_id, key2 : nodes id without feature
103
+ if k == 0:
104
+ idx = self._extract(self._data, nodes_id, nodes_vals)
105
+ # warn(f"Extracted database is empty ({self.feat_names[nodes_id]} = {nodes_vals}). Conditional SHALL values may be incorrect. ")
106
+ cache.set(
107
+ ex,
108
+ key1,
109
+ self._value(
110
+ data=self._data[idx],
111
+ counts=self.counts[idx],
112
+ elements=self.vars_ids,
113
+ sigma=[],
114
+ cache=fifo_cache,
115
+ func1=self._joint,
116
+ params1={},
117
+ func2=self._weight,
118
+ params2={},
119
+ ),
120
+ )
121
+
122
+ joint_prob_with = cache.get(ex, key1) # with feature
123
+ joint_prob_without = cache.get(ex, key2) if len(key1) > 1 else cache.get(-1, "") # without feature
124
+ return (joint_prob_with - joint_prob_without) / self._invcoeff_shap(len(self.vars_ids), len(nodes_id) - 1)
125
+
126
+ def _shall_1dim(self, x) -> np.ndarray:
127
+ contributions = np.zeros((self.M)) # Initializes contributions array.
128
+ fifo_cache = FIFOCache(2000)
129
+ cache = CustomShapleyCache(5000) # Initializes the custom cache.
130
+ cache.set(-1, "", self.baseline) # Sets the baseline probability in the cache.
131
+ coalitions = self._coalitions(self.vars_ids) # Generates coalitions.
132
+ for nodes_id in coalitions:
133
+ nodes_vals = x[nodes_id] # Gets the values of the nodes in the coalition.
134
+ for k, feature in enumerate(nodes_id):
135
+ # Accumulates the contribution for each feature.
136
+ contributions[feature] += self._coalition_contribution(
137
+ k, 0, int(feature), nodes_id, nodes_vals, cache, fifo_cache
138
+ )
139
+ return contributions
140
+
141
+ def _shall_ndim(self, x) -> np.ndarray:
142
+ contributions = np.zeros((self.M, len(x))) # Initializes contributions array.
143
+ fifo_cache = FIFOCache(2000)
144
+ cache = CustomShapleyCache(5000) # Initializes the custom cache.
145
+ cache.set(-1, "", self.baseline) # Sets the baseline probability in the cache.
146
+
147
+ coalitions = self._coalitions(self.vars_ids) # Generates coalitions.
148
+ for i, nodes_id in enumerate(coalitions):
149
+ data_vals = x[:, nodes_id] # Gets the values of the nodes in the coalition.
150
+ for ex, nodes_vals in enumerate(data_vals):
151
+ for k, feature in enumerate(nodes_id):
152
+ contributions[feature, ex] += self._coalition_contribution(
153
+ k, ex, int(feature), nodes_id, nodes_vals, cache, fifo_cache
154
+ )
155
+ return contributions