Inference

Inference is the process that consists in computing new probabilistc information from a Bayesian network and some evidence. aGrUM/pyAgrum mainly focus and the computation of (joint) posterior for some variables of the Bayesian networks given soft or hard evidence that are the form of likelihoods on some variables. Inference is a hard task (NP-complete). aGrUM/pyAgrum implements exact inference but also approximated inference that can converge slowly and (even) not exactly but thant can in many cases be useful for applications.

Exact Inference

Lazy Propagation

Lazy Propagation is the main exact inference for classical Bayesian networks in aGrUM/pyAgrum.

class pyAgrum.LazyPropagation(*args)

Class used for Lazy Propagation

LazyPropagation(bn) -> LazyPropagation
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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(self, X, Y)

I(self, X, Y) -> double

Parameters:
  • X (int or str) – a node Id or a node name
  • Y (int or str) –

    another node Id or node name

    Returns

  • -------
  • double – the Mutual Information of X and Y given the observation
VI(self, X, Y)

VI(self, X, Y) -> double

Parameters:
  • X (int or str) – a node Id or a node name
  • Y (int or str) –

    another node Id or node name

    Returns

  • -------
  • double – variation of information between X and Y
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addJointTarget(self, 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(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllJointTargets(self)

Clear all previously defined joint targets.

eraseAllMarginalTargets(self)

Clear all the previously defined marginal targets.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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(self, targets, evs)

evidenceJointImpact(self, targets, 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:

pyAgrum.Potential

Raises:

gum.Exception – If some evidene entered into the Bayes net are incompatible (their joint proba = 0)

evidenceProbability(self)
Returns:the probability of evidence
Return type:double
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
joinTree(self)
Returns:the current join tree used
Return type:pyAgrum.CliqueGraph
jointMutualInformation(self, targets)
jointPosterior(self, 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(self)
Returns:the list of target sets
Return type:list
junctionTree(self)
Returns:the current junction tree
Return type:pyAgrum.CliqueGraph
makeInference(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(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrJointTargets(self)
Returns:the number of joint targets
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
posterior(self, var)

posterior(self, nodeName) -> Potential posterior(self, 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:

pyAgrum.Potential

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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setFindBarrenNodesType(self, type)

sets how we determine barren nodes

Barren nodes are unnecessary for probability inference, so they can be safely discarded in this case (type = FIND_BARREN_NODES). This speeds-up inference. However, there are some cases in which we do not want to remove barren nodes, typically when we want to answer queries such as Most Probable Explanations (MPE).

0 = FIND_NO_BARREN_NODES 1 = FIND_BARREN_NODES

Parameters:type (int) – the finder type
Raises:gum.InvalidArgument – If type is not implemented
setRelevantPotentialsFinderType(self, type)

sets how we determine the relevant potentials to combine

When a clique sends a message to a separator, it first constitute the set of the potentials it contains and of the potentials contained in the messages it received. If RelevantPotentialsFinderType = FIND_ALL, all these potentials are combined and projected to produce the message sent to the separator. If RelevantPotentialsFinderType = DSEP_BAYESBALL_NODES, then only the set of potentials d-connected to the variables of the separator are kept for combination and projection.

0 = FIND_ALL 1 = DSEP_BAYESBALL_NODES 2 = DSEP_BAYESBALL_POTENTIALS 3 = DSEP_KOLLER_FRIEDMAN_2009

Parameters:type (int) – the finder type
Raises:gum.InvalidArgument – If type is not implemented
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(self, new_triangulation)
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network

Shafer Shenoy Inference

class pyAgrum.ShaferShenoyInference(*args)

Class used for Shafer-Shenoy inferences.

ShaferShenoyInference(bn) -> ShaferShenoyInference
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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(self, X, Y)

I(self, X, Y) -> double

Parameters:
  • X (int or str) – a node Id or a node name
  • Y (int or str) –

    another node Id or node name

    Returns

  • -------
  • double – the Mutual Information of X and Y given the observation
VI(self, X, Y)

VI(self, X, Y) -> double

Parameters:
  • X (int or str) – a node Id or a node name
  • Y (int or str) –

    another node Id or node name

    Returns

  • -------
  • double – variation of information between X and Y
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addJointTarget(self, 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(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllJointTargets(self)

Clear all previously defined joint targets.

eraseAllMarginalTargets(self)

Clear all the previously defined marginal targets.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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(self, targets, evs)

evidenceJointImpact(self, targets, 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:

pyAgrum.Potential

Raises:

gum.Exception – If some evidene entered into the Bayes net are incompatible (their joint proba = 0)

evidenceProbability(self)
Returns:the probability of evidence
Return type:double
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
joinTree(self)
Returns:the current join tree used
Return type:pyAgrum.CliqueGraph
jointMutualInformation(self, targets)
jointPosterior(self, 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(self)
Returns:the list of target sets
Return type:list
junctionTree(self)
Returns:the current junction tree
Return type:pyAgrum.CliqueGraph
makeInference(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(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrJointTargets(self)
Returns:the number of joint targets
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
posterior(self, var)

posterior(self, nodeName) -> Potential posterior(self, 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:

pyAgrum.Potential

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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setFindBarrenNodesType(self, type)

sets how we determine barren nodes

Barren nodes are unnecessary for probability inference, so they can be safely discarded in this case (type = FIND_BARREN_NODES). This speeds-up inference. However, there are some cases in which we do not want to remove barren nodes, typically when we want to answer queries such as Most Probable Explanations (MPE).

0 = FIND_NO_BARREN_NODES 1 = FIND_BARREN_NODES

Parameters:type (int) – the finder type
Raises:gum.InvalidArgument – If type is not implemented
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(self, new_triangulation)
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network

Variable Elimination

class pyAgrum.VariableElimination(*args)

Class used for Variable Elimination inference algorithm.

VariableElimination(bn) -> VariableElimination
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addJointTarget(self, 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(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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(self, targets, evs)

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:

pyAgrum.Potential

Raises:

gum.Exception – If some evidene entered into the Bayes net are incompatible (their joint proba = 0)

hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
jointMutualInformation(self, targets)
jointPosterior(self, 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(self)
Returns:the list of target sets
Return type:list
junctionTree(self, id)
Returns:the current junction tree
Return type:pyAgrum.CliqueGraph
makeInference(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(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setFindBarrenNodesType(self, type)

sets how we determine barren nodes

Barren nodes are unnecessary for probability inference, so they can be safely discarded in this case (type = FIND_BARREN_NODES). This speeds-up inference. However, there are some cases in which we do not want to remove barren nodes, typically when we want to answer queries such as Most Probable Explanations (MPE).

0 = FIND_NO_BARREN_NODES 1 = FIND_BARREN_NODES

Parameters:type (int) – the finder type
Raises:gum.InvalidArgument – If type is not implemented
setRelevantPotentialsFinderType(self, type)

sets how we determine the relevant potentials to combine

When a clique sends a message to a separator, it first constitute the set of the potentials it contains and of the potentials contained in the messages it received. If RelevantPotentialsFinderType = FIND_ALL, all these potentials are combined and projected to produce the message sent to the separator. If RelevantPotentialsFinderType = DSEP_BAYESBALL_NODES, then only the set of potentials d-connected to the variables of the separator are kept for combination and projection.

0 = FIND_ALL 1 = DSEP_BAYESBALL_NODES 2 = DSEP_BAYESBALL_POTENTIALS 3 = DSEP_KOLLER_FRIEDMAN_2009

Parameters:type (int) – the finder type
Raises:gum.InvalidArgument – If type is not implemented
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(self, new_triangulation)
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network

Approximated Inference

Loopy Belief Propagation

class pyAgrum.LoopyBeliefPropagation(bn: pyAgrum.IBayesNet)

Class used for inferences using loopy belief propagation algorithm.

LoopyBeliefPropagation(bn) -> LoopyBeliefPropagation
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Sampling

Gibbs Sampling

class pyAgrum.GibbsSampling(bn: pyAgrum.IBayesNet)

Class for making Gibbs sampling inference in Bayesian networks.

GibbsSampling(bn) -> GibbsSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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

burnIn(self)
Returns:size of burn in on number of iteration
Return type:int
chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isDrawnAtRandom(self)
Returns:True if variables are drawn at random
Return type:bool
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrDrawnVar(self)
Returns:the number of variable drawn at each iteration
Return type:int
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setBurnIn(self, b)
Parameters:b (int) – size of burn in on number of iteration
setDrawnAtRandom(self, _atRandom)
Parameters:_atRandom (bool) – indicates if variables should be drawn at random
setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setNbrDrawnVar(self, _nbr)
Parameters:_nbr (int) – the number of variables to be drawn at each iteration
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Monte Carlo Sampling

class pyAgrum.MonteCarloSampling(bn: pyAgrum.IBayesNet)

Class used for Monte Carlo sampling inference algorithm.

MonteCarloSampling(bn) -> MonteCarloSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Weighted Sampling

class pyAgrum.WeightedSampling(bn: pyAgrum.IBayesNet)

Class used for Weighted sampling inference algorithm.

WeightedSampling(bn) -> WeightedSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Importance Sampling

class pyAgrum.ImportanceSampling(bn: pyAgrum.IBayesNet)

Class used for inferences using the Importance Sampling algorithm.

ImportanceSampling(bn) -> ImportanceSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy sampling

Loopy Gibbs Sampling

class pyAgrum.LoopyGibbsSampling(bn: pyAgrum.IBayesNet)

Class used for inferences using a loopy version of Gibbs sampling.

LoopyGibbsSampling(bn) -> LoopyGibbsSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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

burnIn(self)
Returns:size of burn in on number of iteration
Return type:int
chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isDrawnAtRandom(self)
Returns:True if variables are drawn at random
Return type:bool
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

makeInference_(self)
maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrDrawnVar(self)
Returns:the number of variable drawn at each iteration
Return type:int
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setBurnIn(self, b)
Parameters:b (int) – size of burn in on number of iteration
setDrawnAtRandom(self, _atRandom)
Parameters:_atRandom (bool) – indicates if variables should be drawn at random
setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setNbrDrawnVar(self, _nbr)
Parameters:_nbr (int) – the number of variables to be drawn at each iteration
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy Monte Carlo Sampling

class pyAgrum.LoopyMonteCarloSampling(bn: pyAgrum.IBayesNet)

Class used for inferences using a loopy version of Monte Carlo sampling.

LoopyMonteCarloSampling(bn) -> LoopyMonteCarloSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

makeInference_(self)
maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy Weighted Sampling

class pyAgrum.LoopyWeightedSampling(bn: pyAgrum.IBayesNet)

Class used for inferences using a loopy version of weighted sampling.

LoopyWeightedSampling(bn) -> LoopyWeightedSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

makeInference_(self)
maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy Importance Sampling

class pyAgrum.LoopyImportanceSampling(bn: pyAgrum.IBayesNet)

Class used for inferences using a loopy version of importance sampling.

LoopyImportanceSampling(bn) -> LoopyImportanceSampling
Parameters:
  • bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns:A constant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, 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

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, 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(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, 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 evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current 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 current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(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(self, id)

eraseEvidence(self, 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

eraseTarget(self, target)

eraseTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, 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
hardEvidenceNodes(self)
Returns:the set of nodes with hard evidence
Return type:set
hasEvidence(self, id)

hasEvidence(self, 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(self, 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(self, id)

hasSoftEvidence(self, 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

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
isTarget(self, variable)

isTarget(self, 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 network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(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.

makeInference_(self)
maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfBounds – If p<1
posterior(self, var)

posterior(self, 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:

pyAgrum.Potential

Raises:

gum.UndefinedElement – If an element of nodes is not in targets

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfBounds – If eps<0
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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfBounds – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises:gum.OutOfBounds – If timeout<=0.0
setMinEpsilonRate(self, rate)
Parameters:rate (double) – the minimal epsilon rate
setPeriodSize(self, p)
Parameters:p (int) – number of samples between 2 stopping
Raises:gum.OutOfBounds – If p<1
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
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(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 node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool