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

class pyagrum.ShaferShenoyMRFInference(MN, use_binary_join_tree=True)

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

ShaferShenoyMRFInference(bn) -> ShaferShenoyMRFInference
Parameters:
Parameters:
  • MN (IMarkovRandomField)

  • use_binary_join_tree (bool)

Scientific notes

ShaferShenoyMRFInference is the propagation of [1], described in pyagrum.ShaferShenoyInference and pyagrum.LazyPropagation, applied to the factors of a Markov random field: since the messages only multiply and sum out factors, they need no conditional probability.

What differs from the Bayesian networks: the junction tree is a triangulation of the undirected graph of the Markov random field itself (with the joint targets made cliques and the observed variables removed); there is no barren node and no d-separation pruning, each factor goes to a clique containing its variables, and the factors of a clique are multiplied into one table as in pyagrum.ShaferShenoyInference. The posteriors are normalized, so that the partition function \(Z = \sum_x \prod_f \phi_f(x_f)\) is not needed for them; evidenceProbability returns \(Z_e / Z\), where \(Z_e\) is the same sum restricted by the evidence, \(Z\) being computed once by a propagation without evidence.

Unlike CPTs, factors are not normalized: on a large or strongly weighted Markov random field, the products of factors and messages leave the range of floating-point numbers (overflow to infinity, or underflow to 0 that would pass for incompatible evidence). Two kinds of tables are therefore multiplied by a power of 2, which is exact in floating point and leaves the normalized posteriors unchanged:

  • every message, whose maximum is brought into \([0.5, 1)\);

  • the table of a clique, the product of its factors, only when the exponent of its maximum exceeds half the range of exponents (otherwise the next products could leave the range); if this product has already overflowed or underflowed, the factors of the clique are multiplied again, the result of each product being scaled.

Usual clique tables are thus exactly the products of their factors, and the cost of this scaling is negligible.

\(Z_e\) itself is computed as a logarithm from the calibrated junction tree. Let \(B_c\) be the mass (sum of all the entries) of the table of clique \(c\), multiplied back by its own scale, times the messages that \(c\) receives, and \(M_s\) the mass of the product of the two messages through separator \(s\). Then

\[\ln Z_e = \sum_c \ln B_c - \sum_s \ln M_s + \sum_f \ln \phi_f(e_f) ,\]

the last sum running over the factors whose variables are all observed (a consequence of the clique tree invariant, [2], section 10.2.3, Proposition 10.3, p. 361). Indeed, without scaling, in a connected component of the junction tree, every \(B_c\) and every \(M_s\) would be equal to the mass of this component, and a tree has one clique more than separators. The scale of the message from \(c\) to \(d\) appears once in \(B_d\) and once in the \(M_s\) of their separator, hence the scales of the messages cancel out. The masses are themselves computed with the same scaling when needed. evidenceProbability returns \(\exp(\ln Z_e - \ln Z)\), which is representable whenever the probability of the evidence is, even if \(Z_e\) and \(Z\) are not.

Limitation of the implementation: every factor must be finite; a clique whose table is exactly 0 for every configuration compatible with the evidence raises IncompatibleEvidence.

Methods

H()

MRF()

Return the Markov random field associated with this inference engine.

addAllTargets()

Add all the nodes as targets.

addEvidence()

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

addJointTarget()

Add a set of nodes as a new joint target.

addTarget()

Add a marginal target to the list of targets.

chgEvidence()

Change the value of an already existing evidence on a node (might be soft or hard).

eraseAllEvidence()

Removes all the evidence entered into the network.

eraseAllJointTargets()

Clear all previously defined joint targets.

eraseAllMarginalTargets()

Clear all the previously defined marginal targets.

eraseAllTargets()

Clear all previously defined targets (marginal and joint targets).

eraseEvidence()

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

eraseJointTarget()

Remove, if existing, the joint target.

eraseTarget()

Remove, if existing, the marginal target.

evidenceImpact()

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

evidenceJointImpact()

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

evidenceProbability()

getNumberOfThreads()

returns the number of threads used by LazyPropagation during inferences.

hardEvidenceNodes()

hasEvidence()

Indicates whether a node has received evidence or, without argument, whether at least one node has.

hasHardEvidence()

hasSoftEvidence()

invalidateTensors()

Tells the inference that the values of tensors of its model changed in place.

isGumNumberOfThreadsOverriden()

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

isJointTarget()

isTarget()

joinTree()

jointMutualInformation()

Compute the joint mutual information (interaction information) among a set of target variables.

jointPosterior()

Compute the joint posterior of a set of nodes.

jointTargets()

junctionTree()

makeInference()

Perform the heavy computations needed to compute the targets’ posteriors

maxMemory()

Gives the upper bound on the memory used by the inference (see method setMaxMemory).

nbrEvidence()

nbrHardEvidence()

nbrJointTargets()

nbrSoftEvidence()

nbrTargets()

posterior()

Computes and returns the posterior of a node.

setEvidence()

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

setMaxMemory()

Sets an upper bound on the memory used by the inference.

setNumberOfThreads()

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.

setTargets()

Remove all the targets and add the ones in parameter.

softEvidenceNodes()

targets()

updateEvidence()

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