pyAgrum-nightly 2.3.0.9.dev202512061764412981__cp310-abi3-macosx_11_0_arm64.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 +171 -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.0.9.dev202512061764412981.dist-info/LICENSE.md +12 -0
- pyagrum_nightly-2.3.0.9.dev202512061764412981.dist-info/LICENSES/LGPL-3.0-or-later.txt +304 -0
- pyagrum_nightly-2.3.0.9.dev202512061764412981.dist-info/LICENSES/MIT.txt +18 -0
- pyagrum_nightly-2.3.0.9.dev202512061764412981.dist-info/METADATA +145 -0
- pyagrum_nightly-2.3.0.9.dev202512061764412981.dist-info/RECORD +107 -0
- pyagrum_nightly-2.3.0.9.dev202512061764412981.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,161 @@
|
|
|
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 itertools import combinations
|
|
42
|
+
|
|
43
|
+
import pyagrum
|
|
44
|
+
|
|
45
|
+
from pyagrum.ctbn import CTBN
|
|
46
|
+
from pyagrum.ctbn import Trajectory
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
This file contains the learning algorithm of a CTBN (i.e of its CIMs and dependency graph).
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Learner:
|
|
54
|
+
"""
|
|
55
|
+
Class used to learn a CTBN (independence between variables and CIMs) using samples.
|
|
56
|
+
|
|
57
|
+
Parameters
|
|
58
|
+
----------
|
|
59
|
+
source : str|Dict[int, List[Tuple[float, str, str]]]
|
|
60
|
+
Path to the csv file containing the samples(trajectories). Or directly the trajectories in a python dict.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def __init__(self, source):
|
|
64
|
+
if isinstance(source, str):
|
|
65
|
+
self.data = pyagrum.ctbn.readTrajectoryCSV(source)
|
|
66
|
+
else:
|
|
67
|
+
self.data = source
|
|
68
|
+
|
|
69
|
+
def fitParameters(self, ctbn: CTBN):
|
|
70
|
+
"""
|
|
71
|
+
Learns the parameters of ``ctbn``'s CIMs.
|
|
72
|
+
|
|
73
|
+
Parameters
|
|
74
|
+
----------
|
|
75
|
+
ctbn : CTBN
|
|
76
|
+
CTBN containing the CIMs to learn.
|
|
77
|
+
"""
|
|
78
|
+
traj = Trajectory(self.data, ctbn)
|
|
79
|
+
traj.computeAllCIMs()
|
|
80
|
+
|
|
81
|
+
def learnCTBN(self, template: CTBN = None) -> CTBN:
|
|
82
|
+
"""
|
|
83
|
+
Learns a CTBN, using the CTPC(continuous-time PC) algorithm.
|
|
84
|
+
Reference : A. Bregoli, M. Scutari, F. Stella, Constraint-Based Learning for Continuous-Time Bayesian Networks,
|
|
85
|
+
arXiv:2007.03248, 2020.
|
|
86
|
+
|
|
87
|
+
Parameters
|
|
88
|
+
----------
|
|
89
|
+
template : CTBN
|
|
90
|
+
CTBN used to find variables. If not given, variables are searched inside the trajectories.
|
|
91
|
+
(if the trajectory is very short, some variables can be missed).
|
|
92
|
+
|
|
93
|
+
Returns
|
|
94
|
+
-------
|
|
95
|
+
CTBN
|
|
96
|
+
The learned ctbn.
|
|
97
|
+
"""
|
|
98
|
+
test = pyagrum.ctbn.FChi2Test(Trajectory(self.data))
|
|
99
|
+
# List[pyagrum.DiscreteVariable] : list of all the variables of our Ctbn
|
|
100
|
+
newVariableList = []
|
|
101
|
+
|
|
102
|
+
newGraph = pyagrum.DiGraph()
|
|
103
|
+
newCtbn = CTBN()
|
|
104
|
+
|
|
105
|
+
if template is None:
|
|
106
|
+
tmpCtbn = pyagrum.ctbn.CTBNFromData(self.data)
|
|
107
|
+
newVariableList = tmpCtbn.variables()
|
|
108
|
+
|
|
109
|
+
else:
|
|
110
|
+
newVariableList = [pyagrum.LabelizedVariable(var) for var in template.variables()]
|
|
111
|
+
|
|
112
|
+
for var in newVariableList:
|
|
113
|
+
newGraph.addNodeWithId(
|
|
114
|
+
newCtbn.add(var)
|
|
115
|
+
) # makes the link between the variable and its corresponding node in the DiGraph
|
|
116
|
+
|
|
117
|
+
# List[int] : list of the indexes of all the nodes
|
|
118
|
+
variableIdList = newGraph.nodes()
|
|
119
|
+
|
|
120
|
+
# We add all the possible arcs first (initialisation of the Ctbn)
|
|
121
|
+
# logging.debug(f"variables id list : {variableIdList}")
|
|
122
|
+
for id1 in variableIdList:
|
|
123
|
+
for id2 in variableIdList:
|
|
124
|
+
if id1 != id2:
|
|
125
|
+
newGraph.addArc(id1, id2) # arc from var1 to var2
|
|
126
|
+
|
|
127
|
+
for Xi in variableIdList: # (2.)
|
|
128
|
+
# List[int] : "U" in the Continuous-time PC Algorithm (2.1)
|
|
129
|
+
parentsIdList = list(newGraph.parents(Xi))
|
|
130
|
+
length = len(parentsIdList)
|
|
131
|
+
|
|
132
|
+
parents_done = {id: False for id in parentsIdList}
|
|
133
|
+
|
|
134
|
+
for b in range(length): # +1 # (2.2)
|
|
135
|
+
for Xj in parentsIdList: # (2.2.1 / 2.2.2)
|
|
136
|
+
if parents_done[Xj]:
|
|
137
|
+
continue
|
|
138
|
+
|
|
139
|
+
UXj = parentsIdList.copy()
|
|
140
|
+
UXj.remove(Xj)
|
|
141
|
+
|
|
142
|
+
if b == 0:
|
|
143
|
+
loop = [[]]
|
|
144
|
+
elif b <= len(UXj):
|
|
145
|
+
loop = combinations(UXj, b)
|
|
146
|
+
|
|
147
|
+
for SXiXj in loop:
|
|
148
|
+
SXiXj = list(SXiXj)
|
|
149
|
+
independance_test = test.testIndep(newCtbn.name(Xi), newCtbn.name(Xj), [newCtbn.name(id) for id in SXiXj])
|
|
150
|
+
|
|
151
|
+
if independance_test: # if the test is True
|
|
152
|
+
newGraph.eraseArc(Xj, Xi)
|
|
153
|
+
parents_done[Xj] = True
|
|
154
|
+
break
|
|
155
|
+
|
|
156
|
+
parentsIdList = [var for var in parents_done.keys() if not parents_done[var]]
|
|
157
|
+
|
|
158
|
+
for arc in newGraph.arcs():
|
|
159
|
+
newCtbn.addArc(arc[0], arc[1])
|
|
160
|
+
|
|
161
|
+
return newCtbn
|