Inference¶
Inference is the process that consists in computing new probabilistc information from a Markov network and some evidence. aGrUM/pyAgrum mainly focus and the computation of (joint) posterior for some variables of the Markov networks given soft or hard evidence that are the form of likelihoods on some variables. Inference is a hard task (NP-complete). For now, aGrUM/pyAgrum implements only one exact inference for Markov Network.
Shafer Shenoy Inference¶
-
class
pyAgrum.
ShaferShenoyMNInference
(MN: pyAgrum.IMarkovNet, use_binary_join_tree: bool = True)¶ Class used for Shafer-Shenoy inferences for Markov network.
- ShaferShenoyInference(bn) -> ShaferShenoyInference
- Parameters:
- mn (pyAgrum.MarkovNet) – a Markov network
-
H
(ShaferShenoyMNInference self, int X)¶ H(ShaferShenoyMNInference self, str nodeName) -> double
Parameters: - X (int) – a node Id
- nodeName (str) – a node name
Returns: the computed Shanon’s entropy of a node given the observation
Return type: double
-
I
(ShaferShenoyMNInference self, int X, int Y)¶ Parameters: - X (int) – a node Id
- Y (int) – another node Id
Returns: the computed Shanon’s entropy of a node given the observation
Return type: double
-
MN
(ShaferShenoyMNInference self)¶
-
VI
(ShaferShenoyMNInference self, int X, int Y)¶ Parameters: - X (int) – a node Id
- Y (int) – another node Id
Returns: variation of information between X and Y
Return type: double
-
addAllTargets
(ShaferShenoyMNInference self)¶ Add all the nodes as targets.
-
addEvidence
(ShaferShenoyMNInference self, int id, int val)¶ addEvidence(ShaferShenoyMNInference self, str nodeName, int val) addEvidence(ShaferShenoyMNInference self, int id, str val) addEvidence(ShaferShenoyMNInference self, str nodeName, str val) addEvidence(ShaferShenoyMNInference self, int id, Vector vals) addEvidence(ShaferShenoyMNInference self, str nodeName, Vector vals)
Adds a new evidence on a node (might be soft or hard).
Parameters: - id (int) – a node Id
- nodeName (int) – a node name
- val – (int) a node value
- val – (str) the label of the node value
- vals (list) – a list of values
Raises: gum.InvalidArgument
– If the node already has an evidencegum.InvalidArgument
– If val is not a value for the nodegum.InvalidArgument
– If the size of vals is different from the domain side of the nodegum.FatalError
– If vals is a vector of 0sgum.UndefinedElement
– If the node does not belong to the Bayesian network
-
addJointTarget
(ShaferShenoyMNInference self, PyObject * targets)¶ Add a list of nodes as a new joint target. As a collateral effect, every node is added as a marginal target.
Parameters: list – a list of names of nodes Raises: gum.UndefinedElement
– If some node(s) do not belong to the Bayesian network
-
addTarget
(ShaferShenoyMNInference self, int target)¶ addTarget(ShaferShenoyMNInference self, str nodeName)
Add a marginal target to the list of targets.
Parameters: - target (int) – a node Id
- nodeName (str) – a node name
Raises: gum.UndefinedElement
– If target is not a NodeId in the Bayes net
-
chgEvidence
(ShaferShenoyMNInference self, int id, int val)¶ chgEvidence(ShaferShenoyMNInference self, str nodeName, int val) chgEvidence(ShaferShenoyMNInference self, int id, str val) chgEvidence(ShaferShenoyMNInference self, str nodeName, str val) chgEvidence(ShaferShenoyMNInference self, int id, Vector vals) chgEvidence(ShaferShenoyMNInference self, str nodeName, Vector vals)
Change the value of an already existing evidence on a node (might be soft or hard).
Parameters: - id (int) – a node Id
- nodeName (int) – a node name
- val – (int) a node value
- val – (str) the label of the node value
- vals (list) – a list of values
Raises: gum.InvalidArgument
– If the node does not already have an evidencegum.InvalidArgument
– If val is not a value for the nodegum.InvalidArgument
– If the size of vals is different from the domain side of the nodegum.FatalError
– If vals is a vector of 0sgum.UndefinedElement
– If the node does not belong to the Bayesian network
-
eraseAllEvidence
(ShaferShenoyMNInference self)¶ Removes all the evidence entered into the network.
-
eraseAllJointTargets
(ShaferShenoyMNInference self)¶ Clear all previously defined joint targets.
-
eraseAllMarginalTargets
(ShaferShenoyMNInference self)¶ Clear all the previously defined marginal targets.
-
eraseAllTargets
(ShaferShenoyMNInference self)¶ Clear all previously defined targets (marginal and joint targets).
As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).
-
eraseEvidence
(ShaferShenoyMNInference self, int id)¶ eraseEvidence(ShaferShenoyMNInference self, str nodeName)
Remove the evidence, if any, corresponding to the node Id or name.
Parameters: - id (int) – a node Id
- nodeName (int) – a node name
Raises: gum.IndexError
– If the node does not belong to the Bayesian network
-
eraseJointTarget
(ShaferShenoyMNInference self, PyObject * targets)¶ Remove, if existing, the joint target.
Parameters: list – a list of names or Ids of nodes
Raises: gum.IndexError
– If one of the node does not belong to the Bayesian networkgum.UndefinedElement
– If node Id is not in the Bayesian network
-
eraseTarget
(ShaferShenoyMNInference self, int target)¶ eraseTarget(ShaferShenoyMNInference self, str nodeName)
Remove, if existing, the marginal target.
Parameters: - target (int) – a node Id
- nodeName (int) – a node name
Raises: gum.IndexError
– If one of the node does not belong to the Bayesian networkgum.UndefinedElement
– If node Id is not in the Bayesian network
-
evidenceImpact
(ShaferShenoyMNInference self, PyObject * target, PyObject * evs)¶ Create a pyAgrum.Potential for P(target|evs) (for all instanciation of target and evs)
Parameters: - target (set) – a set of targets ids or names.
- evs (set) – a set of nodes ids or names.
Warning
if some evs are d-separated, they are not included in the Potential.
Returns: a Potential for P(targets|evs) Return type: pyAgrum.Potential
-
evidenceJointImpact
(ShaferShenoyMNInference self, PyObject * targets, PyObject * evs)¶ evidenceJointImpact(ShaferShenoyMNInference self, Vector_string targets, Vector_string evs) -> Potential
Create a pyAgrum.Potential for P(joint targets|evs) (for all instanciation of targets and evs)
Parameters: - targets – (int) a node Id
- targets – (str) a node name
- evs (set) – a set of nodes ids or names.
Returns: a Potential for P(target|evs)
Return type: Raises: gum.Exception
– If some evidene entered into the Bayes net are incompatible (their joint proba = 0)
-
evidenceProbability
(ShaferShenoyMNInference self)¶ Returns: the probability of evidence Return type: double
-
hardEvidenceNodes
(ShaferShenoyMNInference self)¶ Returns: the set of nodes with hard evidence Return type: set
-
hasEvidence
(ShaferShenoyMNInference self, int id)¶ hasEvidence(ShaferShenoyMNInference self, str nodeName) -> bool
Parameters: - id (int) – a node Id
- nodeName (str) – a node name
Returns: True if some node(s) (or the one in parameters) have received evidence
Return type: bool
Raises: gum.IndexError
– If the node does not belong to the Bayesian network
-
hasHardEvidence
(ShaferShenoyMNInference self, str nodeName)¶ Parameters: - id (int) – a node Id
- nodeName (str) – a node name
Returns: True if node has received a hard evidence
Return type: bool
Raises: gum.IndexError
– If the node does not belong to the Bayesian network
-
hasSoftEvidence
(ShaferShenoyMNInference self, int id)¶ hasSoftEvidence(ShaferShenoyMNInference self, str nodeName) -> bool
Parameters: - id (int) – a node Id
- nodeName (str) – a node name
Returns: True if node has received a soft evidence
Return type: bool
Raises: gum.IndexError
– If the node does not belong to the Bayesian network
-
isJointTarget
(ShaferShenoyMNInference self, PyObject * targets)¶ Parameters: list – a list of nodes ids or names.
Returns: True if target is a joint target.
Return type: bool
Raises: gum.IndexError
– If the node does not belong to the Bayesian networkgum.UndefinedElement
– If node Id is not in the Bayesian network
-
isTarget
(ShaferShenoyMNInference self, int variable)¶ isTarget(ShaferShenoyMNInference self, str nodeName) -> bool
Parameters: - variable (int) – a node Id
- nodeName (str) – a node name
Returns: True if variable is a (marginal) target
Return type: bool
Raises: gum.IndexError
– If the node does not belong to the Bayesian networkgum.UndefinedElement
– If node Id is not in the Bayesian network
-
joinTree
(ShaferShenoyMNInference self)¶ Returns: the current join tree used Return type: pyAgrum.CliqueGraph
-
jointMutualInformation
(ShaferShenoyMNInference self, PyObject * targets)¶
-
jointPosterior
(ShaferShenoyMNInference self, PyObject * targets)¶ Compute the joint posterior of a set of nodes.
Parameters: list – the list of nodes whose posterior joint probability is wanted Warning
The order of the variables given by the list here or when the jointTarget is declared can not be assumed to be used bu the Potential.
Returns: a ref to the posterior joint probability of the set of nodes. Return type: pyAgrum.Potential Raises: gum.UndefinedElement
– If an element of nodes is not in targets
-
jointTargets
(ShaferShenoyMNInference self)¶ Returns: the list of target sets Return type: list
-
junctionTree
(ShaferShenoyMNInference self)¶ Returns: the current junction tree Return type: pyAgrum.CliqueGraph
-
makeInference
(ShaferShenoyMNInference self)¶ Perform the heavy computations needed to compute the targets’ posteriors
In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.
-
nbrEvidence
(ShaferShenoyMNInference self)¶ Returns: the number of evidence entered into the Bayesian network Return type: int
-
nbrHardEvidence
(ShaferShenoyMNInference self)¶ Returns: the number of hard evidence entered into the Bayesian network Return type: int
-
nbrJointTargets
(ShaferShenoyMNInference self)¶ Returns: the number of joint targets Return type: int
-
nbrSoftEvidence
(ShaferShenoyMNInference self)¶ Returns: the number of soft evidence entered into the Bayesian network Return type: int
-
nbrTargets
(ShaferShenoyMNInference self)¶ Returns: the number of marginal targets Return type: int
-
posterior
(ShaferShenoyMNInference self, int var)¶ posterior(ShaferShenoyMNInference self, str nodeName) -> Potential
Computes and returns the posterior of a node.
Parameters: - var (int) – the node Id of the node for which we need a posterior probability
- nodeName (str) – the node name of the node for which we need a posterior probability
Returns: a ref to the posterior probability of the node
Return type: Raises: gum.UndefinedElement
– If an element of nodes is not in targets
-
setEvidence
(evidces)¶ Erase all the evidences and apply addEvidence(key,value) for every pairs in evidces.
Parameters: evidces (dict) – a dict of evidences
Raises: gum.InvalidArgument
– If one value is not a value for the nodegum.InvalidArgument
– If the size of a value is different from the domain side of the nodegum.FatalError
– If one value is a vector of 0sgum.UndefinedElement
– If one node does not belong to the Bayesian network
-
setTargets
(targets)¶ Remove all the targets and add the ones in parameter.
Parameters: targets (set) – a set of targets Raises: gum.UndefinedElement
– If one target is not in the Bayes net
-
setTriangulation
(ShaferShenoyMNInference self, Triangulation new_triangulation)¶
-
softEvidenceNodes
(ShaferShenoyMNInference self)¶ Returns: the set of nodes with soft evidence Return type: set
-
targets
(ShaferShenoyMNInference self)¶ Returns: the list of marginal targets Return type: list
-
updateEvidence
(evidces)¶ Apply chgEvidence(key,value) for every pairs in evidces (or addEvidence).
Parameters: evidces (dict) – a dict of evidences
Raises: gum.InvalidArgument
– If one value is not a value for the nodegum.InvalidArgument
– If the size of a value is different from the domain side of the nodegum.FatalError
– If one value is a vector of 0sgum.UndefinedElement
– If one node does not belong to the Bayesian network