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
pyagrum/__init__.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
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
|
+
`pyAgrum <http://agrum.org>`_ a scientific C++ and Python library dedicated to Bayesian Networks and other Probabilistic Graphical Models. Based on the C++ `aGrUM <https://agrum.lip6.fr>`_ library, it provides a high-level interface to the C++ part of aGrUM allowing to create, manage and perform efficient computations with Bayesian networks and others probabilsitic graphical models (Markov random fields, influence diagrams and LIMIDs, dynamic BN, probabilistic relational models).
|
|
43
|
+
|
|
44
|
+
The module is generated using the `SWIG <https://www.swig.org>`_ interface generator. Custom-written code was added to make the interface more friendly.
|
|
45
|
+
|
|
46
|
+
pyAgrum includes :
|
|
47
|
+
* API documentation <https://pyagrum.readthedocs.io>,
|
|
48
|
+
* examples as `notebooks <https://pyagrum.readthedocs.io/en/latest/notebooks/01-Tutorial.html>`_,
|
|
49
|
+
* a `website <http://agrum.org>`_.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
# selection of imports extracted from dir(.pyagrum)
|
|
53
|
+
|
|
54
|
+
# submodule BASE
|
|
55
|
+
from .pyagrum import log2
|
|
56
|
+
from .pyagrum import statsObj
|
|
57
|
+
from .pyagrum import fastGraph
|
|
58
|
+
from .pyagrum import config
|
|
59
|
+
from .pyagrum import (
|
|
60
|
+
DiscretizedVariable,
|
|
61
|
+
LabelizedVariable,
|
|
62
|
+
RangeVariable,
|
|
63
|
+
DiscreteVariable,
|
|
64
|
+
IntegerVariable,
|
|
65
|
+
NumericalDiscreteVariable,
|
|
66
|
+
)
|
|
67
|
+
from .pyagrum import Tensor, Instantiation, Tensor
|
|
68
|
+
from .pyagrum import Arc, Edge, DiGraph, UndiGraph, MixedGraph, DAG, PDAG, CliqueGraph
|
|
69
|
+
from .pyagrum import JunctionTreeGenerator
|
|
70
|
+
from .pyagrum import MeekRules
|
|
71
|
+
from .pyagrum import ApproximationScheme
|
|
72
|
+
from .pyagrum import fastVariable
|
|
73
|
+
from .pyagrum import initRandom, randomProba, randomDistribution, randomGeneratorSeed, randomValue
|
|
74
|
+
from .pyagrum import isOMP, getNumberOfThreads, getMaxNumberOfThreads, getNumberOfLogicalProcessors, setNumberOfThreads
|
|
75
|
+
from .pyagrum import VarType_DISCRETIZED, VarType_LABELIZED, VarType_RANGE, VarType_INTEGER, VarType_NUMERICAL
|
|
76
|
+
from .pyagrum import (
|
|
77
|
+
DefaultInLabel,
|
|
78
|
+
DuplicateElement,
|
|
79
|
+
DuplicateLabel,
|
|
80
|
+
GumException,
|
|
81
|
+
FatalError,
|
|
82
|
+
FormatNotFound,
|
|
83
|
+
GraphError,
|
|
84
|
+
IOError,
|
|
85
|
+
InvalidArc,
|
|
86
|
+
InvalidArgument,
|
|
87
|
+
InvalidArgumentsNumber,
|
|
88
|
+
InvalidDirectedCycle,
|
|
89
|
+
InvalidEdge,
|
|
90
|
+
InvalidNode,
|
|
91
|
+
DatabaseError,
|
|
92
|
+
MissingValueInDatabase,
|
|
93
|
+
MissingVariableInDatabase,
|
|
94
|
+
NoChild,
|
|
95
|
+
NoNeighbour,
|
|
96
|
+
NoParent,
|
|
97
|
+
NotFound,
|
|
98
|
+
NullElement,
|
|
99
|
+
OperationNotAllowed,
|
|
100
|
+
OutOfBounds,
|
|
101
|
+
ArgumentError,
|
|
102
|
+
SizeError,
|
|
103
|
+
SyntaxError,
|
|
104
|
+
UndefinedElement,
|
|
105
|
+
UndefinedIteratorKey,
|
|
106
|
+
UndefinedIteratorValue,
|
|
107
|
+
UnknownLabelInDatabase,
|
|
108
|
+
CPTError,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# submodule BN
|
|
112
|
+
from .pyagrum import fastBN
|
|
113
|
+
from .pyagrum import availableBNExts, loadBN, saveBN
|
|
114
|
+
from .pyagrum import randomBN, generateSample
|
|
115
|
+
from .pyagrum import mutilateBN
|
|
116
|
+
from .pyagrum import BayesNet, BayesNetFragment, EssentialGraph, MarkovBlanket
|
|
117
|
+
from .pyagrum import ExactBNdistance, GibbsBNdistance, StructuralComparator
|
|
118
|
+
from .pyagrum import LazyPropagation, ShaferShenoyInference, VariableElimination
|
|
119
|
+
from .pyagrum import LoopyBeliefPropagation, GibbsSampling, MonteCarloSampling, ImportanceSampling, WeightedSampling
|
|
120
|
+
from .pyagrum import LoopyImportanceSampling, LoopyGibbsSampling, LoopyWeightedSampling, LoopyMonteCarloSampling
|
|
121
|
+
from .pyagrum import PythonApproximationListener, PythonBNListener, PythonLoadListener, PythonDatabaseGeneratorListener
|
|
122
|
+
from .pyagrum import BNGenerator
|
|
123
|
+
from .pyagrum import BNDatabaseGenerator, InformationTheory
|
|
124
|
+
from .pyagrum import BNLearner
|
|
125
|
+
from .pyagrum import PRMexplorer
|
|
126
|
+
|
|
127
|
+
import sys
|
|
128
|
+
|
|
129
|
+
# sublodule MRF
|
|
130
|
+
try:
|
|
131
|
+
from .pyagrum import getPosterior
|
|
132
|
+
from .pyagrum import availableMRFExts, loadMRF, saveMRF
|
|
133
|
+
from .pyagrum import fastMRF
|
|
134
|
+
from .pyagrum import MarkovRandomField, ShaferShenoyMRFInference
|
|
135
|
+
except ImportError:
|
|
136
|
+
sys.stderr.write("Could not load Markov Random Field submodule")
|
|
137
|
+
|
|
138
|
+
# submodule CN
|
|
139
|
+
try:
|
|
140
|
+
from .pyagrum import CredalNet, CNMonteCarloSampling, CNLoopyPropagation
|
|
141
|
+
except ImportError:
|
|
142
|
+
sys.stderr.write("Could not load Credal Net submodule")
|
|
143
|
+
|
|
144
|
+
# submodule ID
|
|
145
|
+
try:
|
|
146
|
+
from .pyagrum import IDGenerator
|
|
147
|
+
from .pyagrum import availableIDExts, loadID, saveID
|
|
148
|
+
from .pyagrum import fastID
|
|
149
|
+
from .pyagrum import InfluenceDiagram, ShaferShenoyLIMIDInference
|
|
150
|
+
except ImportError:
|
|
151
|
+
sys.stderr.write("Could not load Influence Diagram submodule")
|
|
152
|
+
|
|
153
|
+
from .common import (
|
|
154
|
+
__version__,
|
|
155
|
+
__license__,
|
|
156
|
+
__project_url__,
|
|
157
|
+
__project_name__,
|
|
158
|
+
__project_description__,
|
|
159
|
+
__project__,
|
|
160
|
+
about,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
# deprecated
|
|
164
|
+
from .deprecated import deprecated_arg
|
|
165
|
+
from .pyagrum import Potential
|
pyagrum/_pyagrum.so
ADDED
|
Binary file
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
from typing import Tuple
|
|
2
|
+
from numpy import quantile, array
|
|
3
|
+
|
|
4
|
+
############################################################################
|
|
5
|
+
# This file is part of the aGrUM/pyAgrum library. #
|
|
6
|
+
# #
|
|
7
|
+
# Copyright (c) 2005-2025 by #
|
|
8
|
+
# - Pierre-Henri WUILLEMIN(_at_LIP6) #
|
|
9
|
+
# - Christophe GONZALES(_at_AMU) #
|
|
10
|
+
# #
|
|
11
|
+
# The aGrUM/pyAgrum library is free software; you can redistribute it #
|
|
12
|
+
# and/or modify it under the terms of either : #
|
|
13
|
+
# #
|
|
14
|
+
# - the GNU Lesser General Public License as published by #
|
|
15
|
+
# the Free Software Foundation, either version 3 of the License, #
|
|
16
|
+
# or (at your option) any later version, #
|
|
17
|
+
# - the MIT license (MIT), #
|
|
18
|
+
# - or both in dual license, as here. #
|
|
19
|
+
# #
|
|
20
|
+
# (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) #
|
|
21
|
+
# #
|
|
22
|
+
# This aGrUM/pyAgrum library is distributed in the hope that it will be #
|
|
23
|
+
# useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
|
|
24
|
+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS #
|
|
25
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
|
|
26
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
|
|
27
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, #
|
|
28
|
+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
|
|
29
|
+
# OTHER DEALINGS IN THE SOFTWARE. #
|
|
30
|
+
# #
|
|
31
|
+
# See LICENCES for more details. #
|
|
32
|
+
# #
|
|
33
|
+
# SPDX-FileCopyrightText: Copyright 2005-2025 #
|
|
34
|
+
# - Pierre-Henri WUILLEMIN(_at_LIP6) #
|
|
35
|
+
# - Christophe GONZALES(_at_AMU) #
|
|
36
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later OR MIT #
|
|
37
|
+
# #
|
|
38
|
+
# Contact : info_at_agrum_dot_org #
|
|
39
|
+
# homepage : http://agrum.gitlab.io #
|
|
40
|
+
# gitlab : https://gitlab.com/agrumery/agrum #
|
|
41
|
+
# #
|
|
42
|
+
############################################################################
|
|
43
|
+
|
|
44
|
+
import pyagrum
|
|
45
|
+
|
|
46
|
+
import pyagrum.bnmixture as BNM
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def tensor2ref(ref, tens) -> "pyagrum.Tensor":
|
|
50
|
+
"""
|
|
51
|
+
Returns a copy of ``tens`` but with a reference to ``ref`` 's variables instead. Allow to sum tensors that have the same variables
|
|
52
|
+
but with different instantiations of them.
|
|
53
|
+
|
|
54
|
+
Parameters
|
|
55
|
+
----------
|
|
56
|
+
ref : pyagrum.Tensor
|
|
57
|
+
Tensor containing variables of reference.
|
|
58
|
+
tens : pyagrum.Tensor
|
|
59
|
+
Tensor to convert.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
pyagrum.Tensor
|
|
64
|
+
The converted tensor with values of ``tens`` and variable references of ``ref``.
|
|
65
|
+
"""
|
|
66
|
+
res = pyagrum.Tensor()
|
|
67
|
+
for v in tens.names:
|
|
68
|
+
res.add(ref.variable(v))
|
|
69
|
+
return res.fillWith(tens)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class IMixtureInference:
|
|
73
|
+
"""
|
|
74
|
+
Interface class for inference on Mixture models.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(self):
|
|
78
|
+
self._inferences = []
|
|
79
|
+
raise NotImplementedError("Interface should not be initialized !")
|
|
80
|
+
|
|
81
|
+
def setEvidence(self, evs):
|
|
82
|
+
"""
|
|
83
|
+
Erases all the evidences and apply addEvidence(key,value) for every pairs in ``evs``. Does this for every BN in the model, excluding reference BN.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
evs : dict
|
|
88
|
+
A dict of evidences.
|
|
89
|
+
"""
|
|
90
|
+
for ie in self._inferences:
|
|
91
|
+
ie.setEvidence(evs)
|
|
92
|
+
|
|
93
|
+
def makeInference(self):
|
|
94
|
+
"""
|
|
95
|
+
Compute inference for all BNs in the model, excluding reference BN.
|
|
96
|
+
"""
|
|
97
|
+
for ie in self._inferences:
|
|
98
|
+
ie.makeInference()
|
|
99
|
+
|
|
100
|
+
def _posteriors(self, name: str):
|
|
101
|
+
"""
|
|
102
|
+
Parameters
|
|
103
|
+
----------
|
|
104
|
+
name : str
|
|
105
|
+
Name of the variable.
|
|
106
|
+
|
|
107
|
+
Returns
|
|
108
|
+
-------
|
|
109
|
+
List[pyagrum.Tensor]
|
|
110
|
+
List of (not normalized) posteriors of variable ``name`` for each BN in the model, excluding reference BN.
|
|
111
|
+
"""
|
|
112
|
+
return {ie._model.property("name"): pyagrum.Tensor(ie.posterior(name)) for ie in self._inferences}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class BNMixtureInference(IMixtureInference):
|
|
116
|
+
"""
|
|
117
|
+
Class used for inference on BNMixtures.
|
|
118
|
+
|
|
119
|
+
Parameters
|
|
120
|
+
----------
|
|
121
|
+
bnm : BNM.BNMixture
|
|
122
|
+
Model used.
|
|
123
|
+
engine : Optional
|
|
124
|
+
Inference tool used to compute inference. You shuold give the class and not an initialized object. Example : engine=pyagrum.LazyPropagation is ok
|
|
125
|
+
but not engine=pyagrum.LazyPropagation(bnm).
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
def __init__(self, bnm: BNM.BNMixture, engine=pyagrum.LazyPropagation):
|
|
129
|
+
"""
|
|
130
|
+
Parameters
|
|
131
|
+
----------
|
|
132
|
+
bnm : BNM.BNMixture
|
|
133
|
+
Model used.
|
|
134
|
+
engine : Optional
|
|
135
|
+
Inference tool used to compute inference. You shuold give the class and not an initialized object. Example : engine=pyagrum.LazyPropagation is ok
|
|
136
|
+
but not engine=pyagrum.LazyPropagation(bnm).
|
|
137
|
+
"""
|
|
138
|
+
self._bnm = bnm
|
|
139
|
+
self._engine = engine
|
|
140
|
+
self._inferences = [self._engine(bn) for bn in self._bnm.BNs()]
|
|
141
|
+
|
|
142
|
+
def posterior(self, name: str) -> "pyagrum.Tensor":
|
|
143
|
+
"""
|
|
144
|
+
Parameters
|
|
145
|
+
----------
|
|
146
|
+
name : str
|
|
147
|
+
Variable to compute posterior for.
|
|
148
|
+
|
|
149
|
+
Returns
|
|
150
|
+
-------
|
|
151
|
+
pyagrum.Tensor
|
|
152
|
+
The weighted mean (over all the BNs in the model) of the posterior of a variable.
|
|
153
|
+
"""
|
|
154
|
+
posts = self._posteriors(name)
|
|
155
|
+
|
|
156
|
+
ret = pyagrum.Tensor()
|
|
157
|
+
for bn_name in posts:
|
|
158
|
+
ret += posts[bn_name].toVarsIn(self._bnm) * self._bnm.weight(bn_name)
|
|
159
|
+
|
|
160
|
+
return ret.normalize()
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class BootstrapMixtureInference(IMixtureInference):
|
|
164
|
+
"""
|
|
165
|
+
Class used for inference on BootstrapMixture objects. The main difference with BNMixtureInference is that the returned posterior is not a mean of all posteriors in the BNs but only
|
|
166
|
+
the one of the reference BN. The other BNs are used to estimate parameters such as max, min and quantile values.
|
|
167
|
+
|
|
168
|
+
Parameters
|
|
169
|
+
----------
|
|
170
|
+
bnm : BNM.BootstrapMixture
|
|
171
|
+
Model used.
|
|
172
|
+
engine : Optional
|
|
173
|
+
Inference tool used to compute inference. You shold give the class and not an initialized object. Example : engine=pyagrum.LazyPropagation is ok
|
|
174
|
+
but not engine=pyagrum.LazyPropagation(bnm).
|
|
175
|
+
"""
|
|
176
|
+
|
|
177
|
+
def __init__(self, bnm: BNM.BootstrapMixture, engine=pyagrum.LazyPropagation):
|
|
178
|
+
self._bnm = bnm
|
|
179
|
+
self._engine = engine
|
|
180
|
+
self._ref_inference = self._engine(pyagrum.BayesNet(self._bnm._refBN))
|
|
181
|
+
self._inferences = [self._engine(bn) for bn in self._bnm.BNs()]
|
|
182
|
+
|
|
183
|
+
def setEvidence(self, evs):
|
|
184
|
+
"""
|
|
185
|
+
Erases all the evidences and apply addEvidence(key,value) for every pairs in ``evs``. Does this for every BN in the model, including reference BN.
|
|
186
|
+
|
|
187
|
+
Parameters
|
|
188
|
+
----------
|
|
189
|
+
evs : dict
|
|
190
|
+
A dict of evidences.
|
|
191
|
+
"""
|
|
192
|
+
self._ref_inference.setEvidence(evs)
|
|
193
|
+
super().setEvidence(evs)
|
|
194
|
+
|
|
195
|
+
def makeInference(self):
|
|
196
|
+
"""
|
|
197
|
+
Compute inference for all BNs (including reference BN) in the model.
|
|
198
|
+
"""
|
|
199
|
+
self._ref_inference.makeInference()
|
|
200
|
+
super().makeInference()
|
|
201
|
+
|
|
202
|
+
def posterior(self, name: str) -> "pyagrum.Tensor":
|
|
203
|
+
"""
|
|
204
|
+
Parameters
|
|
205
|
+
----------
|
|
206
|
+
name : str
|
|
207
|
+
Variable to compute posterior for.
|
|
208
|
+
|
|
209
|
+
Returns
|
|
210
|
+
-------
|
|
211
|
+
pyagrum.Tensor
|
|
212
|
+
The posterior of variable ``name`` in the reference BN.
|
|
213
|
+
"""
|
|
214
|
+
ref_post = self._ref_inference.posterior(name)
|
|
215
|
+
return ref_post.normalize()
|
|
216
|
+
|
|
217
|
+
def quantiles(self, name: str) -> Tuple[pyagrum.Tensor, pyagrum.Tensor]:
|
|
218
|
+
"""
|
|
219
|
+
Parameters
|
|
220
|
+
----------
|
|
221
|
+
name : str
|
|
222
|
+
Variable to compute quantiles for.
|
|
223
|
+
|
|
224
|
+
Returns
|
|
225
|
+
-------
|
|
226
|
+
Tuple[pyagrum.Tensor, pyagrum.Tensor]
|
|
227
|
+
Two tensors to store the quantile values. Each tensor stores the quantile for each state of the variable.
|
|
228
|
+
The values considered are those of the BNs in the Mixture (excluding reference BN).
|
|
229
|
+
|
|
230
|
+
Raises
|
|
231
|
+
------
|
|
232
|
+
pyagrum.SizeError
|
|
233
|
+
If the mixture doesn't contain any BN.
|
|
234
|
+
"""
|
|
235
|
+
if self._bnm.size() == 0:
|
|
236
|
+
raise pyagrum.SizeError("The mixture doesn't contain any BN, can't compute quantiles")
|
|
237
|
+
q1 = float(pyagrum.config["bnmixture", "left_quantile"])
|
|
238
|
+
q2 = float(pyagrum.config["bnmixture", "right_quantile"])
|
|
239
|
+
var = self._bnm.variable(name)
|
|
240
|
+
|
|
241
|
+
# init tensors to return
|
|
242
|
+
ret1 = pyagrum.Tensor()
|
|
243
|
+
ret1.add(var)
|
|
244
|
+
ret1.fillWith(0)
|
|
245
|
+
ret2 = pyagrum.Tensor()
|
|
246
|
+
ret2.add(var)
|
|
247
|
+
ret2.fillWith(0)
|
|
248
|
+
I = pyagrum.Instantiation()
|
|
249
|
+
I.add(var)
|
|
250
|
+
|
|
251
|
+
# normalize posteriors
|
|
252
|
+
posteriors = self._posteriors(name)
|
|
253
|
+
for nam in posteriors:
|
|
254
|
+
posteriors[nam] = posteriors[nam].toVarsIn(self._bnm).normalize()
|
|
255
|
+
|
|
256
|
+
# set quantile values
|
|
257
|
+
I.setFirst()
|
|
258
|
+
while not I.end():
|
|
259
|
+
values = []
|
|
260
|
+
for nam in posteriors:
|
|
261
|
+
values.append(posteriors[nam].get(I))
|
|
262
|
+
v1 = quantile(array(values), q1)
|
|
263
|
+
v2 = quantile(array(values), q2)
|
|
264
|
+
ret1.set(I, v1)
|
|
265
|
+
ret2.set(I, v2)
|
|
266
|
+
I.inc()
|
|
267
|
+
|
|
268
|
+
return (ret1, ret2)
|