# Bayesian Network¶

The Bayesian Network is the main object of pyAgrum. A Bayesian network is a probabilistic graphical model. It represents a joint distribution over a set of random variables. In pyAgrum, the variables are (for now) only discrete. A Bayesian network uses a directed acyclic graph (DAG) to represent conditional indepencies in the joint distribution. These conditional indepencies allow to factorize the joint distribution, thereby allowing to compactly represent very large ones. Moreover, inference algorithms can also use this graph to speed up the computations. Finally, the Bayesian networks can be learnt from data.

## Model¶

pyAgrum.BayesNet
class pyAgrum.pyAgrum.BayesNet_double(*args)

BayesNet represents a Bayesian Network.

Available ructors:

BayesNet(name='') -> BayesNet

BayesNet(source) -> BayesNet

Parameters: name (str) – the name of the Bayes Net (optional) source (pyAgrum.BayesNet) – the Bayesian network to copy
add(self, var)

Add a variable to the pyAgrum.BayesNet.

Parameters: variable (pyAgrum.DiscreteVariable) – the variable added name (str) – the variable name nbrmod (int) – the numbre of modalities for the new variable id (int) – the variable forced id in the pyAgrum.BayesNet the id of the new node int gum.DuplicateLabel – If variable.name() is already used in this pyAgrum.BayesNet. gum.NotAllowed – If nbrmod<2 gum.DuplicateElement – If id is already used.
addAMPLITUDE(self, var)

Others aggregators

Parameters: variable (pyAgrum.DiscreteVariable) – the variable to be added the id of the added value int
addAND(self, var)

Add a variable, it’s associate node and an AND implementation.

The id of the new variable is automatically generated.

Parameters: variable (pyAgrum.DiscreteVariable) – The variable added by copy. the id of the added variable. int gum.SizeError – If variable.domainSize()>2
addArc(self, tail, head)

Parameters: head – a variable’s id (int) tail – a variable’s id (int) head – a variable’s name (str) tail – a variable’s name (str) gum.InvalidEdge – If arc.tail and/or arc.head are not in the BN.
addCOUNT(self, var, value=1)

Others aggregators

Parameters: variable (pyAgrum.DiscreteVariable) – the variable to be added the id of the added value int
addEXISTS(self, var, value=1)

Others aggregators

Parameters: variable (pyAgrum.DiscreteVariable) – the variable to be added the id of the added value int
addFORALL(self, var, value=1)

Others aggregators

Parameters: variable (pyAgrum.DiscreteVariable) – the variable to be added the id of the added variable. int
addLogit(self, var, external_weight, id)

Add a variable, its associate node and a Logit implementation.

(The id of the new variable can be automatically generated.)

Parameters: variable (pyAgrum.DiscreteVariable) – The variable added by copy externalWeight (double) – the added external weight id (int) – The proposed id for the variable. the id of the added variable. int gum.DuplicateElement – If id is already used
addMAX(self, var)

Others aggregators

Parameters: variable (pyAgrum.DiscreteVariable) – the variable to be added the id of the added value int
addMEDIAN(self, var)

Others aggregators

Parameters: variable (pyAgrum.DiscreteVariable) – the variable to be added the id of the added value int
addMIN(self, var)

Others aggregators

Parameters: variable (pyAgrum.DiscreteVariable) – the variable to be added the id of the added value int
addNoisyAND(self, var, external_weight, id)

Add a variable, its associate node and a noisyAND implementation.

(The id of the new variable can be automatically generated.)

Parameters: variable (pyAgrum.DiscreteVariable) – The variable added by copy externalWeight (double) – the added external weight id (int) – The proposed id for the variable. the id of the added variable. int gum.DuplicateElement – If id is already used
addNoisyOR(self, var, external_weight)

addNoisyOR(self, var, external_weight, id) -> int

Add a variable, it’s associate node and a noisyOR implementation.

Since it seems that the ‘classical’ noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

(The id of the new variable can be automatically generated.)

Parameters: variable (pyAgrum.DiscreteVariable) – The variable added by copy externalWeight (double) – the added external weight id (int) – The proposed id for the variable. the id of the added variable. int gum.DuplicateElement – If id is already used
addNoisyORCompound(self, var, external_weight)

addNoisyORCompound(self, var, external_weight, id) -> int

Add a variable, it’s associate node and a noisyOR implementation.

Since it seems that the ‘classical’ noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

(The id of the new variable can be automatically generated.)

Parameters: variable (pyAgrum.DiscreteVariable) – The variable added by copy externalWeight (double) – the added external weight id (int) – The proposed id for the variable. the id of the added variable. int gum.DuplicateElement – If id is already used
addNoisyORNet(self, var, external_weight)

addNoisyORNet(self, var, external_weight, id) -> int

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the ‘classical’ noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

(The id of the new variable can be automatically generated.)

Parameters: variable (pyAgrum.DiscreteVariable) – The variable added by copy externalWeight (double) – the added external weight id (int) – The proposed id for the variable. the id of the added variable. int
addOR(self, var)

Add a variable, it’s associate node and an OR implementation.

The id of the new variable is automatically generated.

Warning

If parents are not boolean, all value>1 is True

Parameters: variable (pyAgrum.DiscreteVariable) – The variable added by copy the id of the added variable. int gum.SizeError – If variable.domainSize()>2
addStructureListener(whenNodeAdded=None, whenNodeDeleted=None, whenArcAdded=None, whenArcDeleted=None)

Add the listeners in parameters to the list of existing ones.

Parameters: whenNodeAdded (lambda expression) – a function for when a node is added whenNodeDeleted (lambda expression) – a function for when a node is removed whenArcAdded (lambda expression) – a function for when an arc is added whenArcDeleted (lambda expression) – a function for when an arc is removed
addWeightedArc(self, tail, head, causalWeight)

Parameters: head – a variable’s id (int) tail – a variable’s id (int) head – a variable’s name (str) tail – a variable’s name (str) causalWeight (double) – the added causal weight gum.InvalidArc – If arc.tail and/or arc.head are not in the BN. gum.InvalidArc – If variable in arc.head is not a NoisyOR variable.
arcs(self)
Returns: The lisf of arcs in the IBayesNet list
beginTopologyTransformation(self)

When inserting/removing arcs, node CPTs change their dimension with a cost in time. begin Multiple Change for all CPTs These functions delay the CPTs change to be done just once at the end of a sequence of topology modification, begins a sequence of insertions/deletions of arcs without changing the dimensions of the CPTs.

changePotential(self, id, newPot)

changePotential(self, name, newPot)

change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

Parameters: newPot (pyAgrum.Potential) – the new potential NodeId (int) – the id of the node name (str) – the name of the variable gum.NotAllowed – If newPot has not the same signature as __probaMap[NodeId]
changeVariableLabel(self, id, old_label, new_label)

changeVariableLabel(self, name, old_label, new_label)

change the label of the variable associated to nodeId to the new value.

Parameters: id (int) – the id of the node name (str) – the name of the variable old_label (str) – the new label new_label (str) – the new label gum.NotFound – if id/name is not a variable or if old_label does not exist.
changeVariableName(self, id, new_name)

changeVariableName(self, name, new_name)

Changes a variable’s name in the pyAgrum.BayesNet.

This will change the pyAgrum.DiscreteVariable names in the pyAgrum.BayesNet.

Parameters: new_name (str) – the new name of the variable NodeId (int) – the id of the node name (str) – the name of the variable gum.DuplicateLabel – If new_name is already used in this BayesNet. gum.NotFound – If no variable matches id.
children(self, id)
Parameters: id (int) – the id of the parent the set of all the children Set
completeInstantiation(self)

Get an instantiation over all the variables of the model.

Returns: the complete instantiation pyAgrum.instantiation
cpt(self, varId)

cpt(self, name) -> Potential

Returns the CPT of a variable.

Parameters: VarId (int) – A variable’s id in the pyAgrum.BayesNet. name (str) – A variable’s name in the pyAgrum.BayesNet. The variable’s CPT. pyAgrum.Potential gum.NotFound – If no variable’s id matches varId.
dag(self)
Returns: a ant reference to the dag of this BayesNet. pyAgrum.DAG
dim(self)

Returns the dimension (the number of free parameters) in this BayesNet.

Returns: the dimension of the BayesNet int
empty(self)
Returns: True if the model is empty bool
endTopologyTransformation(self)

Terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions. End Multiple Change for all CPTs.

Returns: pyAgrum.BayesNet
erase(self, varId)

erase(self, name) erase(self, var)

Remove a variable from the pyAgrum.BayesNet.

Removes the corresponding variable from the pyAgrum.BayesNet and from all of it’s children pyAgrum.Potential.

If no variable matches the given id, then nothing is done.

Parameters: id (int) – The variable’s id to remove. name (str) – The variable’s name to remove. var (pyAgrum.DiscreteVariable) – A reference on the variable to remove.
eraseArc(self, arc)

Removes an arc in the BN, and update head’s CTP.

If (tail, head) doesn’t exist, the nothing happens.

Parameters: arc (pyAgrum.Arc) – The arc to be removed. head – a variable’s id (int) tail – a variable’s id (int) head – a variable’s name (str) tail – a variable’s name (str)
static fastPrototype(dotlike, domainSize=2)

fastPrototype(dotlike) -> BayesNet

Create a bn with a dotlike syntax : ‘a->b->c;b->d;’.

The domain size maybe specified using ‘a[10]’.

Note that if the dotlike string contains such a specification for an already defined variable, the first specification will be used.

Parameters: dotlike (str) – the string containing the specification domainSize (int) – the default domain size for variables the resulting bayesian network pyAgrum.BayesNet
generateCPT(self, node)

generateCPT(self, name)

Randomly generate CPT for a given node in a given structure.

Parameters: node (int) – The variable’s id. name (str) – The variable’s name.
generateCPTs(self)

Randomly generates CPTs for a given structure.

hasSameStructure(self, other)
Parameters: pyAgrum.DAGmodel – a direct acyclic model True if all the named node are the same and all the named arcs are the same bool
idFromName(self, name)

Returns a variable’s id given its name in the graph.

Parameters: name (str) – The variable’s name from which the id is returned. The variable’s node id. int gum.NotFound – If name does not match a variable in the graph
ids()

Deprecated method in pyAgrum>0.12.0. See nodes instead.

jointProbability(self, i)
Parameters: i (pyAgrum.instantiation) – an instantiation of the variables a parameter of the joint probability for the BayesNet double

Warning

a variable not present in the instantiation is assumed to be instantiated to 0

loadBIF(self, name, l)

Parameters: name (str) – the file’s name l (list) – list of functions to execute gum.IOError – If file not found gum.FatalError – If file is not valid
loadBIFXML(self, name, l)

Parameters: name (str) – the name’s file l (list) – list of functions to execute gum.IOError – If file not found gum.FatalError – If file is not valid
loadDSL(self, name, l)

Parameters: name (str) – the file’s name l (list) – list of functions to execute gum.IOError – If file not found gum.FatalError – If file is not valid
loadNET(self, name, l)

Parameters: name (str) – the name’s file l (list) – list of functions to execute gum.IOError – If file not found gum.FatalError – If file is not valid
loadO3PRM(self, name, system, classpath, l)

Warning

The O3PRM language is the only language allowing to manipulate not only DiscretizedVariable but also RangeVariable and LabelizedVariable.

Parameters: name (str) – the file’s name system (str) – the system’s name classpath (str) – the classpath l (list) – list of functions to execute gum.IOError – If file not found gum.FatalError – If file is not valid
loadUAI(self, name, l)

Parameters: name (str) – the name’s file l (list) – list of functions to execute gum.IOError – If file not found gum.FatalError – If file is not valid
log10DomainSize(self)
Returns: The log10 domain size of the joint probability for the model. double
log2JointProbability(self, i)
Parameters: i (pyAgrum.instantiation) – an instantiation of the variables a parameter of the log joint probability for the BayesNet double

Warning

a variable not present in the instantiation is assumed to be instantiated to 0

maxNonOneParam(self)
Returns: The biggest value (not equal to 1) in the CPTs of the BayesNet double
maxParam(self)
Returns: the biggest value in the CPTs of the BayesNet double
maxVarDomainSize(self)
Returns: the biggest domain size among the variables of the BayesNet int
minNonZeroParam(self)
Returns: the smallest value (not equal to 0) in the CPTs of the IBayesNet double
minParam(self)
Returns: the smallest value in the CPTs of the IBayesNet double
minimalCondSet(self, target, soids)

minimalCondSet(self, targets, soids) -> Set minimalCondSet(self, target, list) -> PyObject minimalCondSet(self, targets, list) -> PyObject *

Returns, given one or many targets and a list of variables, the minimal set of those needed to calculate the target/targets.

Parameters: target (int) – The id of the target targets (list) – The ids of the targets list (list) – The list of available variables The minimal set of variables Set
moralGraph(self, clear=True)

moralGraph(self) -> UndiGraph

Returns the moral graph of the BayesNet, formed by adding edges between all pairs of nodes that have a common child, and then making all edges in the graph undirected.

Returns: The moral graph pyAgrum.UndiGraph
names(self)
Returns: The names of the graph variables list
nodeId(self, var)
Parameters: var (pyAgrum.DiscreteVariable) – a variable the id of the variable int gum.IndexError – If the graph does not contain the variable
nodes(self)
Returns: the set of ids set
parents(self, id)
Parameters: id – The id of the child node the set of the parents ids. Set
reverseArc(self, tail, head)

Reverses an arc while preserving the same joint distribution.

Parameters: tail – (int) the id of the tail variable head – (int) the id of the head variable tail – (str) the name of the tail variable head – (str) the name of the head variable arc (pyAgrum.Arc) – an arc gum.InvalidArc – If the arc does not exsit or if its reversal would induce a directed cycle.
saveBIF(self, name)

Save the BayesNet in a BIF file.

Parameters: name (str) – the file’s name
saveBIFXML(self, name)

Save the BayesNet in a BIFXML file.

Parameters: name (str) – the file’s name
saveDSL(self, name)

Save the BayesNet in a DSL file.

Parameters: name (str) – the file’s name
saveNET(self, name)

Save the BayesNet in a NET file.

Parameters: name (str) – the file’s name
saveO3PRM(self, name)

Save the BayesNet in an O3PRM file.

Warning

The O3PRM language is the only language allowing to manipulate not only DiscretizedVariable but also RangeVariable and LabelizedVariable.

Parameters: name (str) – the file’s name
saveUAI(self, name)

Save the BayesNet in an UAI file.

Parameters: name (str) – the file’s name
size(self)
Returns: the number of nodes in the graph int
sizeArcs(self)
Returns: the number of arcs in the graph int
toDot(self)
Returns: a friendly display of the graph in DOT format str
topologicalOrder(self, clear=True)

topologicalOrder(self) -> pyAgrum.Sequence< int >

Returns: the list of the nodes Ids in a topological order List gum.InvalidDirectedCycle – If this graph contains cycles
variable(self, id)

variable(self, name) -> DiscreteVariable

Parameters: id (int) – a variable’s id name (str) – a variable’s name the variable pyAgrum.DiscreteVariable gum.IndexError – If the graph does not contain the variable
variableFromName(self, name)
Parameters: name (str) – a variable’s name the variable pyAgrum.DiscreteVariable gum.IndexError – If the graph does not contain the variable
variableNodeMap(self)
Returns: the variable node map pyAgrum.variableNodeMap

## Exact Inference¶

### Lazy Propagation¶

pyAgrum.LazyPropagation
class pyAgrum.pyAgrum.LazyPropagation_double(*args)

Class used for Lazy Propagation

Available ructors:
LazyPropagation(bn) -> LazyPropagation
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
I(self, X, Y)
Parameters: X (int) – a node Id Y (int) – another node Id the computed Shanon’s entropy of a node given the observation double
VI(self, X, Y)
Parameters: X (int) – a node Id Y (int) – another node Id variation of information between X and Y double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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 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, list)

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 gum.UndefinedElement – If some node(s) do not belong to the Bayesian network
addTarget(self, target)

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 gum.IndexError – If the node does not belong to the Bayesian network
eraseJointTarget(self, list)

Remove, if existing, the joint target.

Parameters: list – a list of names or Ids of nodes 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 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)

evidenceImpact(self, target, evs) -> Potential

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) 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. a Potential for P(target|evs) pyAgrum.Potential gum.Exception – If some evidene entered into the Bayes net are incompatible (their joint proba = 0)
evidenceProbability(self)
Returns: the probability of evidence double
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
isJointTarget(self, list)
Parameters: list – a list of nodes ids or names. True if target is a joint target. bool 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 True if variable is a (marginal) target bool 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 pyAgrum.CliqueGraph
jointPosterior(self, list)

Compute the joint posterior of a set of nodes.

Parameters: list – the list of nodes whose posterior joint probability is wanted a ref to the posterior joint probability of the set of nodes. pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
jointTargets(self)
Returns: the list of target sets list
junctionTree(self)
Returns: the current junction tree 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 int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrJointTargets(self)
Returns: the number of joint targets int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets 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 a ref to the posterior probability of the node pyAgrum.Potential 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 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
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets gum.UndefinedElement – If one target is not in the Bayes net
softEvidenceNodes(self)
Returns: the set of nodes with soft evidence set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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¶

pyAgrum.ShaferShenoyInference
class pyAgrum.pyAgrum.ShaferShenoyInference_double(*args)

Class used for Shafer-Shenoy inferences.

Available ructors:
ShaferShenoyInference(bn) -> ShaferShenoyInference
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
I(self, X, Y)
Parameters: X (int) – a node Id Y (int) – another node Id the computed Shanon’s entropy of a node given the observation double
VI(self, X, Y)
Parameters: X (int) – a node Id Y (int) – another node Id variation of information between X and Y double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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 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, list)

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 gum.UndefinedElement – If some node(s) do not belong to the Bayesian network
addTarget(self, target)

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 gum.IndexError – If the node does not belong to the Bayesian network
eraseJointTarget(self, list)

Remove, if existing, the joint target.

Parameters: list – a list of names or Ids of nodes 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 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)

evidenceImpact(self, target, evs) -> Potential

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) 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. a Potential for P(target|evs) pyAgrum.Potential gum.Exception – If some evidene entered into the Bayes net are incompatible (their joint proba = 0)
evidenceProbability(self)
Returns: the probability of evidence double
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
isJointTarget(self, list)
Parameters: list – a list of nodes ids or names. True if target is a joint target. bool 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 True if variable is a (marginal) target bool 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 pyAgrum.CliqueGraph
jointPosterior(self, list)

Compute the joint posterior of a set of nodes.

Parameters: list – the list of nodes whose posterior joint probability is wanted a ref to the posterior joint probability of the set of nodes. pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
jointTargets(self)
Returns: the list of target sets list
junctionTree(self)
Returns: the current junction tree 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 int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrJointTargets(self)
Returns: the number of joint targets int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets 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 a ref to the posterior probability of the node pyAgrum.Potential 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 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
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets gum.UndefinedElement – If one target is not in the Bayes net
softEvidenceNodes(self)
Returns: the set of nodes with soft evidence set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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¶

pyAgrum.VariableElimination
class pyAgrum.pyAgrum.VariableElimination_double(*args)

Class used for Variable Elimination inference algorithm.

Available ructors:
VariableElimination(bn) -> VariableElimination
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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 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, list)

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 gum.UndefinedElement – If some node(s) do not belong to the Bayesian network
addTarget(self, target)

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 gum.IndexError – If the node does not belong to the Bayesian network
eraseJointTarget(self, list)

Remove, if existing, the joint target.

Parameters: list – a list of names or Ids of nodes 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
isJointTarget(self, list)
Parameters: list – a list of nodes ids or names. True if target is a joint target. bool 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 True if variable is a (marginal) target bool gum.IndexError – If the node does not belong to the Bayesian network gum.UndefinedElement – If node Id is not in the Bayesian network
jointPosterior(self, list)

Compute the joint posterior of a set of nodes.

Parameters: list – the list of nodes whose posterior joint probability is wanted a ref to the posterior joint probability of the set of nodes. pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
jointTargets(self)
Returns: the list of target sets list
junctionTree(self, id)
Returns: the current junction tree 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 int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets 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 a ref to the posterior probability of the node pyAgrum.Potential 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 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
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets gum.UndefinedElement – If one target is not in the Bayes net
softEvidenceNodes(self)
Returns: the set of nodes with soft evidence set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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

## Approximate Inference¶

### Loopy Belief Propagation¶

pyAgrum.LoopyBeliefPropagation
class pyAgrum.pyAgrum.LoopyBeliefPropagation_double(bn)

Class used for inferences using loopy belief propagation algorithm.

Available ructors:
LoopyBeliefPropagation(bn) -> LoopyBeliefPropagation
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

### Sampling¶

#### Gibbs Sampling¶

pyAgrum.GibbsSampling
class pyAgrum.pyAgrum.GibbsSampling_double(bn)

Class for making Gibbs sampling inference in bayesian networks.

Available ructors:
GibbsSampling(bn) -> GibbsSampling
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name gum.UndefinedElement – If target is not a NodeId in the Bayes net
burnIn(self)
Returns: size of burn in on number of iteration 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 bool
isTarget(self, variable)

isTarget(self, nodeName) -> bool

Parameters: variable (int) – a node Id nodeName (str) – a node name True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrDrawnVar(self)
Returns: the number of variable drawn at each iteration int
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential 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 gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

#### Monte Carlo Sampling¶

pyAgrum.MonteCarloSampling
class pyAgrum.pyAgrum.MonteCarloSampling_double(bn)

Class used for Monte Carlo sampling inference algorithm.

Available ructors:
MonteCarloSampling(bn) -> MonteCarloSampling
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

#### Weighted Sampling¶

pyAgrum.WeightedSampling
class pyAgrum.pyAgrum.WeightedSampling_double(bn)

Class used for Weighted sampling inference algorithm.

Available ructors:
WeightedSampling(bn) -> WeightedSampling
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

#### Importance Sampling¶

pyAgrum.ImportanceSampling
class pyAgrum.pyAgrum.ImportanceSampling_double(bn)

Class used for inferences using the Importance Sampling algorithm.

Available ructors:
ImportanceSampling(bn) -> ImportanceSampling
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

### Loopy sampling¶

#### Loopy Gibbs Sampling¶

pyAgrum.LoopyGibbsSampling
class pyAgrum.pyAgrum.LoopyGibbsSampling_double(bn)

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

Available ructors:
LoopyGibbsSampling(bn) -> LoopyGibbsSampling
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name gum.UndefinedElement – If target is not a NodeId in the Bayes net
burnIn(self)
Returns: size of burn in on number of iteration 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 bool
isTarget(self, variable)

isTarget(self, nodeName) -> bool

Parameters: variable (int) – a node Id nodeName (str) – a node name True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrDrawnVar(self)
Returns: the number of variable drawn at each iteration int
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential 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 gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

#### Loopy Monte Carlo Sampling¶

pyAgrum.LoopyMonteCarloSampling
class pyAgrum.pyAgrum.LoopyMonteCarloSampling_double(bn)

Proxy of C++ pyAgrum.LoopySamplingInference<(double,pyAgrum.MonteCarloSampling)> class.

BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

#### Loopy Weighted Sampling¶

pyAgrum.LoopyWeightedSampling
class pyAgrum.pyAgrum.LoopyWeightedSampling_double(bn)

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

Available ructors:
LoopyWeightedSampling(bn) -> LoopyWeightedSampling
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

#### Loopy Importance Sampling¶

pyAgrum.LoopyImportanceSampling
class pyAgrum.pyAgrum.LoopyImportanceSampling_double(bn)

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

Available ructors:
LoopyImportanceSampling(bn) -> LoopyImportanceSampling
Parameters: bn (pyAgrum.BayesNet) – a Bayesian network
BN(self)
Returns: A ant reference over the IBayesNet referenced by this class. pyAgrum.IBayesNet 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 the computed Shanon’s entropy of a node given the observation double
addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

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

Add a marginal target to the list of targets.

Parameters: target (int) – a node Id nodeName (str) – a node name 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 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 a ref to the current posterior probability of the node pyAgrum.Potential UndefinedElement – If an element of nodes is not in targets
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon 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 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 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)

evidenceImpact(self, target, evs) -> Potential

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) pyAgrum.Potential
hardEvidenceNodes(self)
Returns: the set of nodes with hard evidence set
hasEvidence(self, id)

hasEvidence(self, nodeName) -> bool

Parameters: id (int) – a node Id nodeName (str) – a node name True if some node(s) (or the one in parameters) have received evidence bool 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 True if node has received a hard evidence bool 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 True if node has received a soft evidence bool gum.IndexError – If the node does not belong to the Bayesian network
history(self)
Returns: the scheme history tuple 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 True if variable is a (marginal) target bool 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 int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
nbrEvidence(self)
Returns: the number of evidence entered into the Bayesian network int
nbrHardEvidence(self)
Returns: the number of hard evidence entered into the Bayesian network int
nbrIterations(self)
Returns: the number of iterations int
nbrSoftEvidence(self)
Returns: the number of soft evidence entered into the Bayesian network int
nbrTargets(self)
Returns: the number of marginal targets int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – 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 a ref to the posterior probability of the node pyAgrum.Potential gum.UndefinedElement – If an element of nodes is not in targets
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – 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 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 gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters: targets (set) – a set of targets 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 set
targets(self)
Returns: the list of marginal targets list
updateEvidence(evidces)

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

Parameters: evidces (dict) – a dict of evidences 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 bool

## Learning¶

pyAgrum.BNLearner
class pyAgrum.pyAgrum.BNLearner_double(*args)
Available ructors:

BNLearner(filename) -> BNLearner

BNLearner(filename,src,parse_database=false) -> BNLearner

BNLearner(learner) -> BNLearner

Parameters: filename (str) – the file to learn from src (pyAgrum.BayesNet) – the Bayesian network used to find thoes modalities parse_database (bool) – if true, the modalities specified by the user will be considered as a superset of the modalities of the variables. learner (pyAgrum.BNLearner) – the BNLearner to copy
addForbiddenArc(self, arc)

The arc in parameters won’t be added.

Parameters: arc (pyAgrum) – an arc head – a variable’s id (int) tail – a variable’s id (int) head – a variable’s name (str) tail – a variable’s name (str)
addMandatoryArc(self, arc)

Allow to add prior structural knowledge.

Parameters: arc (pyAgrum) – an arc head – a variable’s id (int) tail – a variable’s id (int) head – a variable’s name (str) tail – a variable’s name (str) gum.InvalidDirectedCycle – If the added arc creates a directed cycle in the DAG
currentTime(self)
Returns: get the current running time in second (double) double
epsilon(self)
Returns: the value of epsilon double
eraseForbiddenArc(self, arc)

Allow the arc in parameter to be added if necessary.

Parameters: arc (pyAgrum) – an arc head – a variable’s id (int) tail – a variable’s id (int) head – a variable’s name (str) tail – a variable’s name (str)
eraseMandatoryArc(self, arc)

Parameters: arc (pyAgrum) – an arc head – a variable’s id (int) tail – a variable’s id (int) head – a variable’s name (str) tail – a variable’s name (str)
history(self)
Returns: the scheme history tuple gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
idFromName(self, var_name)
Parameters: var_names (str) – a variable’s name the node id corresponding to a variable name int gum.MissingVariableInDatabase – If a variable of the BN is not found in the database.
latentVariables(self)

latentVariables(self) -> vector< pyAgrum.Arc,allocator< pyAgrum.Arc > >

Warning

learner must be using 3off2 or MIIC algorithm

Returns: the list of latent variables list
learnBN(self)

learn a BayesNet from a file (must have read the db before)

Returns: the learned BayesNet pyAgrum.BayesNet
learnDAG(self)

learn a structure from a file (must have read the db before)

Returns: the learned DAG pyAgrum.DAG
learnMixedStructure(self)

Warning

learner must be using 3off2 or MIIC algorithm

Returns: the learned structure as an EssentialGraph pyAgrum.EssentialGraph
learnParameters(self, dag, take_into_account_score=True)

learnParameters(self, dag) -> BayesNet learnParameters(self, bn, take_into_account_score=True) -> BayesNet learnParameters(self, bn) -> BayesNet

learns a BN (its parameters) when its structure is known.

Parameters: dag (pyAgrum.DAG) – bn (pyAgrum.BayesNet) – take_into_account_score (bool) – The dag passed in argument may have been learnt from a structure learning. In this case, if the score used to learn the structure has an implicit apriori (like K2 which has a 1-smoothing apriori), it is important to also take into account this implicit apriori for parameter learning. By default, if a score exists, we will learn parameters by taking into account the apriori specified by methods useAprioriXXX () + the implicit apriori of the score, else we just take into account the apriori specified by useAprioriXXX () the learned BayesNet pyAgrum.BayesNet gum.MissingVariableInDatabase – If a variable of the BN is not found in the database gum.UnknownLabelInDatabase – If a label is found in the database that do not correspond to the variable
maxIter(self)
Returns: the criterion on number of iterations int
maxTime(self)
Returns: the timeout(in seconds) double
messageApproximationScheme(self)
Returns: the approximation scheme message str
minEpsilonRate(self)
Returns: the value of the minimal epsilon rate double
modalities(self)
Returns: the number of modalities of the database’s variables. vector
nameFromId(self, id)
Parameters: id – a node id the variable’s name str
names(self)
Returns: the names of the variables in the database str
nbrIterations(self)
Returns: the number of iterations int
periodSize(self)
Returns: the number of samples between 2 stopping int gum.OutOfLowerBound – If p<1
setAprioriWeight(self, weight)
Parameters: weight (double) – the apriori weight
setEpsilon(self, eps)
Parameters: eps (double) – the epsilon we want to use gum.OutOfLowerBound – If eps<0
setInitialDAG(self, g)
Parameters: dag (pyAgrum.DAG) – an initial DAG structure
setMaxIndegree(self, max_indegree)
Parameters: max_indegree (int) – the limit number of parents
setMaxIter(self, max)
Parameters: max (int) – the maximum number of iteration gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters: tiemout (double) – stopping criterion on timeout (in seconds) gum.OutOfLowerBound – 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 gum.OutOfLowerBound – If p<1
setSliceOrder(self, l)

setSliceOrder(self, slice_order)

Set a partial order on the nodes.

Parameters: l (list) – a list of sequences (composed of ids)
setVerbosity(self, v)
Parameters: v (bool) – verbosity
use3off2(self)

Indicate that we wish to use 3off2.

useAprioriDirichlet(self, filename)

Use the Dirichlet apriori.

Parameters: filename (str) – the Dirichlet related database
useAprioriSmoothing(self, weight=-1)

useAprioriSmoothing(self)

Use the apriori smoothing.

Parameters: weight (double) – pass in argument a weight if you wish to assign a weight to the smoothing, else the current weight of the learner will be used.
useGreedyHillClimbing(self)

Indicate that we wish to use a greedy hill climbing algorithm.

useK2(self, l)

useK2(self, order) useK2(self, order)

Indicate that we wish to use K2.

Parameters: order (list) – a list of ids
useLocalSearchWithTabuList(self, tabu_size=100, nb_decrease=2)

useLocalSearchWithTabuList(self, tabu_size=100) useLocalSearchWithTabuList(self)

Indicate that we wish to use a local search with tabu list

Parameters: tabu_size (int) – The size of the tabu list nb_decrease (int) – The max number of changes decreasing the score consecutively that we allow to apply
useMDL(self)

Indicate that we wish to use the MDL correction for 3off2 or MIIC

useMIIC(self)

Indicate that we wish to use MIIC.

useNML(self)

Indicate that we wish to use the NML correction for 3off2 or MIIC

useNoApriori(self)

Use no apriori.

useNoCorr(self)

Indicate that we wish to use the NoCorr correction for 3off2 or MIIC

useScoreAIC(self)

Indicate that we wish to use an AIC score.

useScoreBD(self)

Indicate that we wish to use a BD score.

useScoreBDeu(self)

Indicate that we wish to use a BDeu score.

useScoreBIC(self)

Indicate that we wish to use a BIC score.

useScoreK2(self)

Indicate that we wish to use a K2 score.

useScoreLog2Likelihood(self)

Indicate that we wish to use a Log2Likelihood score.

verbosity(self)
Returns: True if the verbosity is enabled bool