Skip to content

Inference in Markov random fields

Inference is the process that consists in computing new probabilistc information from a Markov random field and some evidence. aGrUM/pyAgrum mainly focus and the computation of (joint) posterior for some variables of the Markov random fields 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 random field.

Shafer-Shenoy Inference in Markov random field

Section titled “Shafer-Shenoy Inference in Markov random field”

class pyagrum.ShaferShenoyMRFInference(MN, use_binary_join_tree=True)

Section titled “class pyagrum.ShaferShenoyMRFInference(MN, use_binary_join_tree=True)”

Class used for Shafer-Shenoy inferences for Markov random field.

ShaferShenoyMRFInference(bn) -> ShaferShenoyMRFInference : Parameters: : - mrf (pyagrum.MarkovRandomField) – a Markov random field

  • Parameters:
    • MN (IMarkovRandomField)
    • use_binary_join_tree (bool)
  • 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: float

Add all the nodes as targets.

  • Return type: None

Adds a new evidence on a node (might be soft or hard).

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
    • targets (object)
  • Raises: pyagrum.UndefinedElement – If some node(s) do not belong to the Bayesian network
  • Return type: None

Add a marginal target to the list of targets.

  • Parameters:
    • target (int) – a node Id
    • nodeName (str) – a node name
  • Raises: pyagrum.UndefinedElement – If target is not a NodeId in the Bayes net
  • Return type: None

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 (intstr) – a node value or the label of the node value
    • vals (List [**float ]) – a list of values
  • Raises:
  • Return type: None

Removes all the evidence entered into the network.

  • Return type: None

Clear all previously defined joint targets.

  • Return type: None

Clear all the previously defined marginal targets.

  • Return type: None

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).

  • Return type: None

Remove the evidence, if any, corresponding to the node Id or name.

  • Parameters:
    • id (int) – a node Id
    • nodeName (int) – a node name
  • Raises: pyagrum.IndexError – If the node does not belong to the Bayesian network
  • Return type: None

Remove, if existing, the joint target.

  • Parameters:
    • list – a list of names or Ids of nodes
    • targets (object)
  • Raises:
    • pyagrum.IndexError – If one of the node does not belong to the Bayesian network
    • pyagrum.UndefinedElement – If node Id is not in the Bayesian network
  • Return type: None

Remove, if existing, the marginal target.

  • Parameters:
    • target (int) – a node Id
    • nodeName (int) – a node name
  • Raises:
    • pyagrum.IndexError – If one of the node does not belong to the Bayesian network
    • pyagrum.UndefinedElement – If node Id is not in the Bayesian network
  • Return type: None

Create a pyagrum.Tensor 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 Tensor.

Create a pyagrum.Tensor for P(joint targets|evs) (for all instanciation of targets and evs)

  • Parameters:
    • targets (List [**intstr ]) – a list of node Ids or node names
    • evs (Set [**intstr ]) – a set of nodes ids or names.
  • Returns: a Tensor for P(target|evs)
  • Return type: pyagrum.Tensor
  • Raises: pyagrum.Exception – If some evidene entered into the Bayes net are incompatible (their joint proba = 0)
  • Returns: the probability of evidence
  • Return type: float

returns the number of threads used by LazyPropagation during inferences.

  • Returns: the number of threads used by LazyPropagation during inferences
  • Return type: int
  • Returns: the set of nodes with hard evidence
  • Return type: set
  • 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: pyagrum.IndexError – If the node does not belong to the Bayesian network
  • Parameters:
    • id (int) – a node Id
    • nodeName (str) – a node name
  • Returns: True if node has received a hard evidence
  • Return type: bool
  • Raises: pyagrum.IndexError – If the node does not belong to the Bayesian network
  • Parameters:
    • id (int) – a node Id
    • nodeName (str) – a node name
  • Returns: True if node has received a soft evidence
  • Return type: bool
  • Raises: pyagrum.IndexError – If the node does not belong to the Bayesian network

Indicates whether LazyPropagation currently overrides aGrUM’s default number of threads (see method setNumberOfThreads).

  • Returns: A Boolean indicating whether LazyPropagation currently overrides aGrUM’s default number of threads
  • Return type: bool
  • Parameters:
    • list – a list of nodes ids or names.
    • targets (object)
  • Returns: True if target is a joint target.
  • Return type: bool
  • Raises:
    • pyagrum.IndexError – If the node does not belong to the Bayesian network
    • pyagrum.UndefinedElement – If node Id is not in the Bayesian network
  • Parameters:
    • variable (int) – a node Id
    • nodeName (str) – a node name
  • Returns: True if variable is a (marginal) target
  • Return type: bool
  • Raises:
    • pyagrum.IndexError – If the node does not belong to the Bayesian network
    • pyagrum.UndefinedElement – If node Id is not in the Bayesian network
  • Parameters: targets (object)
  • Return type: float

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 by the Tensor.

  • Returns: a const ref to the posterior joint probability of the set of nodes.
  • Return type: pyagrum.Tensor
  • Raises: pyagrum.UndefinedElement – If an element of nodes is not in targets
  • Parameters: targets (object)
  • Returns: the list of target sets
  • Return type: list

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.

  • Return type: None
  • Returns: the number of evidence entered into the Bayesian network
  • Return type: int
  • Returns: the number of hard evidence entered into the Bayesian network
  • Return type: int
  • Returns: the number of joint targets
  • Return type: int
  • Returns: the number of soft evidence entered into the Bayesian network
  • Return type: int
  • Returns: the number of marginal targets
  • Return type: int

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 const ref to the posterior probability of the node
  • Return type: pyagrum.Tensor
  • Raises: pyagrum.UndefinedElement – If an element of nodes is not in targets

Erase all the evidences and apply addEvidence(key,value) for every pairs in evidces.

sets an upper bound on the memory consumption admissible

  • Parameters: gigabytes (float) – this upper bound in gigabytes.
  • Return type: None

If the argument nb is different from 0, this number of threads will be used during inferences, hence overriding aGrUM’s default number of threads. If, on the contrary, nb is equal to 0, the parallelized inference engine will comply with aGrUM’s default number of threads.

  • Parameters: nb (int) – the number of threads to be used by ShaferShenoyMRFInference
  • Return type: None

Remove all the targets and add the ones in parameter.

  • Parameters: targets (set) – a set of targets
  • Raises: pyagrum.UndefinedElement – If one target is not in the Bayes net
  • Returns: the set of nodes with soft evidence
  • Return type: set
  • Returns: the list of marginal targets
  • Return type: list

The membership flag

Apply chgEvidence(key,value) for every pairs in evidces (or addEvidence).