pyAgrum-nightly 2.3.1.9.dev202512261765915415__cp310-abi3-macosx_10_15_x86_64.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.
- pyagrum/__init__.py +165 -0
- pyagrum/_pyagrum.so +0 -0
- pyagrum/bnmixture/BNMInference.py +268 -0
- pyagrum/bnmixture/BNMLearning.py +376 -0
- pyagrum/bnmixture/BNMixture.py +464 -0
- pyagrum/bnmixture/__init__.py +60 -0
- pyagrum/bnmixture/notebook.py +1058 -0
- pyagrum/causal/_CausalFormula.py +280 -0
- pyagrum/causal/_CausalModel.py +436 -0
- pyagrum/causal/__init__.py +81 -0
- pyagrum/causal/_causalImpact.py +356 -0
- pyagrum/causal/_dSeparation.py +598 -0
- pyagrum/causal/_doAST.py +761 -0
- pyagrum/causal/_doCalculus.py +361 -0
- pyagrum/causal/_doorCriteria.py +374 -0
- pyagrum/causal/_exceptions.py +95 -0
- pyagrum/causal/_types.py +61 -0
- pyagrum/causal/causalEffectEstimation/_CausalEffectEstimation.py +1175 -0
- pyagrum/causal/causalEffectEstimation/_IVEstimators.py +718 -0
- pyagrum/causal/causalEffectEstimation/_RCTEstimators.py +132 -0
- pyagrum/causal/causalEffectEstimation/__init__.py +46 -0
- pyagrum/causal/causalEffectEstimation/_backdoorEstimators.py +774 -0
- pyagrum/causal/causalEffectEstimation/_causalBNEstimator.py +324 -0
- pyagrum/causal/causalEffectEstimation/_frontdoorEstimators.py +396 -0
- pyagrum/causal/causalEffectEstimation/_learners.py +118 -0
- pyagrum/causal/causalEffectEstimation/_utils.py +466 -0
- pyagrum/causal/notebook.py +172 -0
- pyagrum/clg/CLG.py +658 -0
- pyagrum/clg/GaussianVariable.py +111 -0
- pyagrum/clg/SEM.py +312 -0
- pyagrum/clg/__init__.py +63 -0
- pyagrum/clg/canonicalForm.py +408 -0
- pyagrum/clg/constants.py +54 -0
- pyagrum/clg/forwardSampling.py +202 -0
- pyagrum/clg/learning.py +776 -0
- pyagrum/clg/notebook.py +480 -0
- pyagrum/clg/variableElimination.py +271 -0
- pyagrum/common.py +60 -0
- pyagrum/config.py +319 -0
- pyagrum/ctbn/CIM.py +513 -0
- pyagrum/ctbn/CTBN.py +573 -0
- pyagrum/ctbn/CTBNGenerator.py +216 -0
- pyagrum/ctbn/CTBNInference.py +459 -0
- pyagrum/ctbn/CTBNLearner.py +161 -0
- pyagrum/ctbn/SamplesStats.py +671 -0
- pyagrum/ctbn/StatsIndepTest.py +355 -0
- pyagrum/ctbn/__init__.py +79 -0
- pyagrum/ctbn/constants.py +54 -0
- pyagrum/ctbn/notebook.py +264 -0
- pyagrum/defaults.ini +199 -0
- pyagrum/deprecated.py +95 -0
- pyagrum/explain/_ComputationCausal.py +75 -0
- pyagrum/explain/_ComputationConditional.py +48 -0
- pyagrum/explain/_ComputationMarginal.py +48 -0
- pyagrum/explain/_CustomShapleyCache.py +110 -0
- pyagrum/explain/_Explainer.py +176 -0
- pyagrum/explain/_Explanation.py +70 -0
- pyagrum/explain/_FIFOCache.py +54 -0
- pyagrum/explain/_ShallCausalValues.py +204 -0
- pyagrum/explain/_ShallConditionalValues.py +155 -0
- pyagrum/explain/_ShallMarginalValues.py +155 -0
- pyagrum/explain/_ShallValues.py +296 -0
- pyagrum/explain/_ShapCausalValues.py +208 -0
- pyagrum/explain/_ShapConditionalValues.py +126 -0
- pyagrum/explain/_ShapMarginalValues.py +191 -0
- pyagrum/explain/_ShapleyValues.py +298 -0
- pyagrum/explain/__init__.py +81 -0
- pyagrum/explain/_explGeneralizedMarkovBlanket.py +152 -0
- pyagrum/explain/_explIndependenceListForPairs.py +146 -0
- pyagrum/explain/_explInformationGraph.py +264 -0
- pyagrum/explain/notebook/__init__.py +54 -0
- pyagrum/explain/notebook/_bar.py +142 -0
- pyagrum/explain/notebook/_beeswarm.py +174 -0
- pyagrum/explain/notebook/_showShapValues.py +97 -0
- pyagrum/explain/notebook/_waterfall.py +220 -0
- pyagrum/explain/shapley.py +225 -0
- pyagrum/lib/__init__.py +46 -0
- pyagrum/lib/_colors.py +390 -0
- pyagrum/lib/bn2graph.py +299 -0
- pyagrum/lib/bn2roc.py +1026 -0
- pyagrum/lib/bn2scores.py +217 -0
- pyagrum/lib/bn_vs_bn.py +605 -0
- pyagrum/lib/cn2graph.py +305 -0
- pyagrum/lib/discreteTypeProcessor.py +1102 -0
- pyagrum/lib/discretizer.py +58 -0
- pyagrum/lib/dynamicBN.py +390 -0
- pyagrum/lib/explain.py +57 -0
- pyagrum/lib/export.py +84 -0
- pyagrum/lib/id2graph.py +258 -0
- pyagrum/lib/image.py +387 -0
- pyagrum/lib/ipython.py +307 -0
- pyagrum/lib/mrf2graph.py +471 -0
- pyagrum/lib/notebook.py +1821 -0
- pyagrum/lib/proba_histogram.py +552 -0
- pyagrum/lib/utils.py +138 -0
- pyagrum/pyagrum.py +31495 -0
- pyagrum/skbn/_MBCalcul.py +242 -0
- pyagrum/skbn/__init__.py +49 -0
- pyagrum/skbn/_learningMethods.py +282 -0
- pyagrum/skbn/_utils.py +297 -0
- pyagrum/skbn/bnclassifier.py +1014 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSE.md +12 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSES/LGPL-3.0-or-later.txt +304 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/LICENSES/MIT.txt +18 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/METADATA +145 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/RECORD +107 -0
- pyagrum_nightly-2.3.1.9.dev202512261765915415.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,48 @@
|
|
|
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 numpy as np
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class MarginalComputation:
|
|
45
|
+
@staticmethod
|
|
46
|
+
def _weight(evidces: dict[int, int], count: int) -> np.ndarray:
|
|
47
|
+
# The signature must be : Dict[int, int], int, **kwargs
|
|
48
|
+
return count
|
|
@@ -0,0 +1,110 @@
|
|
|
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
|
+
|
|
42
|
+
class CustomShapleyCache:
|
|
43
|
+
def __init__(self, max_capacity: int = 1000):
|
|
44
|
+
if max_capacity <= 0:
|
|
45
|
+
raise ValueError("max_capacity must be a positive integer.")
|
|
46
|
+
self._cache = {} # Structure: {longueur_str: { (entier, str): valeur }}
|
|
47
|
+
self._current_k_index = -1 # Pour suivre l'indice k actuel
|
|
48
|
+
self._max_capacity = max_capacity # Capacité maximale du cache en nombre d'éléments
|
|
49
|
+
self._current_size = 0 # Taille actuelle du cache
|
|
50
|
+
|
|
51
|
+
@staticmethod
|
|
52
|
+
def generate_keys(bn, target, feat, nodes):
|
|
53
|
+
key1 = tuple(nodes) # Key1 represents the coalition.
|
|
54
|
+
key2 = tuple(
|
|
55
|
+
n for n in nodes if n != feat
|
|
56
|
+
) # Key2 represents the coalition without the feature which is being evaluated.
|
|
57
|
+
if target is None: # For SHALL Values
|
|
58
|
+
return key1, key2, None
|
|
59
|
+
# Key3 is the coalition without ONE ! node which is not in the minimal conditional set.
|
|
60
|
+
minimal = bn.minimalCondSet(
|
|
61
|
+
target, nodes
|
|
62
|
+
) # Calculates the minimal conditional set for the target node given the nodes.
|
|
63
|
+
diff = next((n for n in nodes if n not in minimal), None)
|
|
64
|
+
if diff is not None:
|
|
65
|
+
key3 = tuple(n for n in nodes if n != diff)
|
|
66
|
+
else:
|
|
67
|
+
key3 = key1
|
|
68
|
+
return key1, key2, key3
|
|
69
|
+
|
|
70
|
+
def get(self, int_key: int, tuple_key: tuple):
|
|
71
|
+
tuple_len = len(tuple_key)
|
|
72
|
+
return self._cache[tuple_len].get((int_key, tuple_key), None)
|
|
73
|
+
|
|
74
|
+
def set(self, int_key: int, tuple_key: tuple, value):
|
|
75
|
+
tuple_len = len(tuple_key)
|
|
76
|
+
if tuple_len not in self._cache:
|
|
77
|
+
self._cache[tuple_len] = {}
|
|
78
|
+
self._current_k_index = tuple_len
|
|
79
|
+
|
|
80
|
+
# If it is the first time we add an entry of this length, we initialize the cache for this length.
|
|
81
|
+
if (int_key, tuple_key) not in self._cache[tuple_len]:
|
|
82
|
+
self._current_size += 1
|
|
83
|
+
self._cache[tuple_len][(int_key, tuple_key)] = value
|
|
84
|
+
|
|
85
|
+
# Check if we need to purge the cache
|
|
86
|
+
if self._current_size > self._max_capacity:
|
|
87
|
+
self._perform_purge()
|
|
88
|
+
|
|
89
|
+
def _perform_purge(self):
|
|
90
|
+
if self._current_k_index < 2:
|
|
91
|
+
return
|
|
92
|
+
|
|
93
|
+
str_len_to_purge = self._current_k_index - 2
|
|
94
|
+
|
|
95
|
+
# Purge all entries with length <= str_len_to_purge
|
|
96
|
+
for length in list(self._cache.keys()):
|
|
97
|
+
if length <= str_len_to_purge:
|
|
98
|
+
self._current_size -= len(self._cache[length])
|
|
99
|
+
del self._cache[length]
|
|
100
|
+
|
|
101
|
+
def __len__(self):
|
|
102
|
+
return self._current_size
|
|
103
|
+
|
|
104
|
+
def __str__(self):
|
|
105
|
+
# Affiche le cache par longueur de clé pour une meilleure visibilité
|
|
106
|
+
items_by_length = {k: len(v) for k, v in self._cache.items()}
|
|
107
|
+
return (
|
|
108
|
+
f"Cache (k={self._current_k_index}, size={self._current_size}/{self._max_capacity}): "
|
|
109
|
+
f"Contents by length: {items_by_length}"
|
|
110
|
+
)
|
|
@@ -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
|