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

class pyAgrum.BayesNet(*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(self, name, nbrmod) -> int add(self, var, aContent) -> int add(self, var, id) -> int add(self, var, aContent, id) -> int

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
Returns:

the id of the new node

Return type:

int

Raises:
  • 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
Returns:the id of the added value
Return type: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.
Returns:the id of the added variable.
Return type:int
Raises:gum.SizeError – If variable.domainSize()>2
addArc(self, tail, head)

addArc(self, tail, head)

Add an arc in the BN, and update arc.head’s CPT.

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

gum.InvalidEdge – If arc.tail and/or arc.head are not in the BN.

addCOUNT(self, var, value=1)

addCOUNT(self, var) -> int

Others aggregators

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

addEXISTS(self, var) -> int

Others aggregators

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

addFORALL(self, var) -> int

Others aggregators

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

addLogit(self, var, external_weight) -> int

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.
Returns:

the id of the added variable.

Return type:

int

Raises:

gum.DuplicateElement – If id is already used

addMAX(self, var)

Others aggregators

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

Others aggregators

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

Others aggregators

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

addNoisyAND(self, var, external_weight) -> int

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.
Returns:

the id of the added variable.

Return type:

int

Raises:

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.
Returns:

the id of the added variable.

Return type:

int

Raises:

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.
Returns:

the id of the added variable.

Return type:

int

Raises:

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.
Returns:

the id of the added variable.

Return type:

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
Returns:the id of the added variable.
Return type:int
Raises: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)

addWeightedArc(self, tail, head, causalWeight)

Add an arc in the BN, and update arc.head’s CPT.

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
Raises:
  • 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
Return type: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
Raises:

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
Raises:

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
Raises:
  • 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
Returns:the set of all the children
Return type:Set
completeInstantiation(self)

Get an instantiation over all the variables of the model.

Returns:the complete instantiation
Return type: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.
Returns:

The variable’s CPT.

Return type:

pyAgrum.Potential

Raises:

gum.NotFound – If no variable’s id matches varId.

dag(self)
Returns:a ant reference to the dag of this BayesNet.
Return type:pyAgrum.DAG
dim(self)

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

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

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

Returns:
Return type: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)

eraseArc(self, tail, head) eraseArc(self, tail, head)

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
Returns:

the resulting bayesian network

Return type:

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
Returns:True if all the named node are the same and all the named arcs are the same
Return type: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.
Returns:The variable’s node id.
Return type:int
Raises: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
Returns:a parameter of the joint probability for the BayesNet
Return type:double

Warning

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

loadBIF(self, name, l)

loadBIF(self, name) -> str

Load a BIF file.

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

loadBIFXML(self, name) -> str

Load a BIFXML file.

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

loadDSL(self, name) -> str

Load a DSL file.

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

loadNET(self, name) -> str

Load a NET file.

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

loadO3PRM(self, name, system, classpath) -> str loadO3PRM(self, name, system) -> str loadO3PRM(self, name) -> str

Load 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
  • system (str) – the system’s name
  • classpath (str) – the classpath
  • l (list) – list of functions to execute
Raises:
  • gum.IOError – If file not found
  • gum.FatalError – If file is not valid
loadUAI(self, name, l)

loadUAI(self, name) -> str

Load an UAI file.

Parameters:
  • name (str) – the name’s file
  • l (list) – list of functions to execute
Raises:
  • 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.
Return type:double
log2JointProbability(self, i)
Parameters:i (pyAgrum.instantiation) – an instantiation of the variables
Returns:a parameter of the log joint probability for the BayesNet
Return type: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
Return type:double
maxParam(self)
Returns:the biggest value in the CPTs of the BayesNet
Return type:double
maxVarDomainSize(self)
Returns:the biggest domain size among the variables of the BayesNet
Return type:int
minNonZeroParam(self)
Returns:the smallest value (not equal to 0) in the CPTs of the IBayesNet
Return type:double
minParam(self)
Returns:the smallest value in the CPTs of the IBayesNet
Return type: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
Returns:

The minimal set of variables

Return type:

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
Return type:pyAgrum.UndiGraph
names(self)
Returns:The names of the graph variables
Return type:list
nodeId(self, var)
Parameters:var (pyAgrum.DiscreteVariable) – a variable
Returns:the id of the variable
Return type:int
Raises:gum.IndexError – If the graph does not contain the variable
nodes(self)
Returns:the set of ids
Return type:set
parents(self, id)
Parameters:id – The id of the child node
Returns:the set of the parents ids.
Return type:Set
reverseArc(self, tail, head)

reverseArc(self, tail, head) reverseArc(self, arc)

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
Raises:

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
Return type:int
sizeArcs(self)
Returns:the number of arcs in the graph
Return type:int
thisown

The membership flag

toDot(self)
Returns:a friendly display of the graph in DOT format
Return type:str
topologicalOrder(self, clear=True)

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

Returns:the list of the nodes Ids in a topological order
Return type:List
Raises: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
Returns:

the variable

Return type:

pyAgrum.DiscreteVariable

Raises:

gum.IndexError – If the graph does not contain the variable

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

Exact Inference

Lazy Propagation

class pyAgrum.LazyPropagation(*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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

I(self, X, Y)
Parameters:
  • X (int) – a node Id
  • Y (int) – another node Id
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

VI(self, X, Y)
Parameters:
  • X (int) – a node Id
  • Y (int) – another node Id
Returns:

variation of information between X and Y

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addJointTarget(self, 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
Raises:gum.UndefinedElement – If some node(s) do not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllJointTargets(self)

Clear all previously defined joint targets.

eraseAllMarginalTargets(self)

Clear all the previously defined marginal targets.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseJointTarget(self, list)

Remove, if existing, the joint target.

Parameters:

list – a list of names or Ids of nodes

Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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)
Return type:pyAgrum.Potential
evidenceJointImpact(self, targets, evs)

evidenceJointImpact(self, targets, evs) -> Potential

Create a pyAgrum.Potential for P(joint targets|evs) (for all instanciation of targets and evs)

Parameters:
  • targets – (int) a node Id
  • targets – (str) a node name
  • evs (set) – a set of nodes ids or names.
Returns:

a Potential for P(target|evs)

Return type:

pyAgrum.Potential

Raises:

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

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

isJointTarget(self, list)
Parameters:

list – a list of nodes ids or names.

Returns:

True if target is a joint target.

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
isTarget(self, variable)

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
joinTree(self)
Returns:the current join tree used
Return type:pyAgrum.CliqueGraph
jointPosterior(self, list)

Compute the joint posterior of a set of nodes.

Parameters:list – the list of nodes whose posterior joint probability is wanted

Warning

The order of the variables given by the list here or when the jointTarget is declared can not be assumed to be used bu the Potential.

Returns:a ref to the posterior joint probability of the set of nodes.
Return type:pyAgrum.Potential
Raises:gum.UndefinedElement – If an element of nodes is not in targets
jointTargets(self)
Returns:the list of target sets
Return type:list
junctionTree(self)
Returns:the current junction tree
Return type:pyAgrum.CliqueGraph
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrJointTargets(self)
Returns:the number of joint targets
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
posterior(self, var)

posterior(self, nodeName) -> Potential

Computes and returns the posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setTargets(targets)

Remove all the targets and add the ones in parameter.

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

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network

Shafer Shenoy Inference

class pyAgrum.ShaferShenoyInference(*args)

Class used for Shafer-Shenoy inferences.

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

I(self, X, Y)
Parameters:
  • X (int) – a node Id
  • Y (int) – another node Id
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

VI(self, X, Y)
Parameters:
  • X (int) – a node Id
  • Y (int) – another node Id
Returns:

variation of information between X and Y

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addJointTarget(self, 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
Raises:gum.UndefinedElement – If some node(s) do not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllJointTargets(self)

Clear all previously defined joint targets.

eraseAllMarginalTargets(self)

Clear all the previously defined marginal targets.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseJointTarget(self, list)

Remove, if existing, the joint target.

Parameters:

list – a list of names or Ids of nodes

Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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)
Return type:pyAgrum.Potential
evidenceJointImpact(self, targets, evs)

evidenceJointImpact(self, targets, evs) -> Potential

Create a pyAgrum.Potential for P(joint targets|evs) (for all instanciation of targets and evs)

Parameters:
  • targets – (int) a node Id
  • targets – (str) a node name
  • evs (set) – a set of nodes ids or names.
Returns:

a Potential for P(target|evs)

Return type:

pyAgrum.Potential

Raises:

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

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

isJointTarget(self, list)
Parameters:

list – a list of nodes ids or names.

Returns:

True if target is a joint target.

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
isTarget(self, variable)

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
joinTree(self)
Returns:the current join tree used
Return type:pyAgrum.CliqueGraph
jointPosterior(self, list)

Compute the joint posterior of a set of nodes.

Parameters:list – the list of nodes whose posterior joint probability is wanted

Warning

The order of the variables given by the list here or when the jointTarget is declared can not be assumed to be used bu the Potential.

Returns:a ref to the posterior joint probability of the set of nodes.
Return type:pyAgrum.Potential
Raises:gum.UndefinedElement – If an element of nodes is not in targets
jointTargets(self)
Returns:the list of target sets
Return type:list
junctionTree(self)
Returns:the current junction tree
Return type:pyAgrum.CliqueGraph
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrJointTargets(self)
Returns:the number of joint targets
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
posterior(self, var)

posterior(self, nodeName) -> Potential

Computes and returns the posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setTargets(targets)

Remove all the targets and add the ones in parameter.

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

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network

Variable Elimination

class pyAgrum.VariableElimination(*args)

Class used for Variable Elimination inference algorithm.

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addJointTarget(self, 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
Raises:gum.UndefinedElement – If some node(s) do not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseJointTarget(self, list)

Remove, if existing, the joint target.

Parameters:

list – a list of names or Ids of nodes

Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

isJointTarget(self, list)
Parameters:

list – a list of nodes ids or names.

Returns:

True if target is a joint target.

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
isTarget(self, variable)

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
jointPosterior(self, list)

Compute the joint posterior of a set of nodes.

Parameters:list – the list of nodes whose posterior joint probability is wanted

Warning

The order of the variables given by the list here or when the jointTarget is declared can not be assumed to be used bu the Potential.

Returns:a ref to the posterior joint probability of the set of nodes.
Return type:pyAgrum.Potential
Raises:gum.UndefinedElement – If an element of nodes is not in targets
jointTargets(self)
Returns:the list of target sets
Return type:list
junctionTree(self, id)
Returns:the current junction tree
Return type:pyAgrum.CliqueGraph
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
posterior(self, var)

posterior(self, nodeName) -> Potential

Computes and returns the posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setTargets(targets)

Remove all the targets and add the ones in parameter.

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

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network

Approximate Inference

Loopy Belief Propagation

class pyAgrum.LoopyBeliefPropagation(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentTime(self)
Returns:get the current running time in second (double)
Return type:double
epsilon(self)
Returns:the value of epsilon
Return type:double
eraseAllEvidence(self)

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Sampling

Gibbs Sampling

class pyAgrum.GibbsSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

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

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrDrawnVar(self)
Returns:the number of variable drawn at each iteration
Return type:int
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setBurnIn(self, b)
Parameters:b (int) – size of burn in on number of iteration
setDrawnAtRandom(self, _atRandom)
Parameters:_atRandom (bool) – indicates if variables should be drawn at random
setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Monte Carlo Sampling

class pyAgrum.MonteCarloSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Weighted Sampling

class pyAgrum.WeightedSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Importance Sampling

class pyAgrum.ImportanceSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy sampling

Loopy Gibbs Sampling

class pyAgrum.LoopyGibbsSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

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

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrDrawnVar(self)
Returns:the number of variable drawn at each iteration
Return type:int
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setBurnIn(self, b)
Parameters:b (int) – size of burn in on number of iteration
setDrawnAtRandom(self, _atRandom)
Parameters:_atRandom (bool) – indicates if variables should be drawn at random
setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy Monte Carlo Sampling

class pyAgrum.LoopyMonteCarloSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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

BN(self)
Returns:A ant reference over the IBayesNet referenced by this class.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy Weighted Sampling

class pyAgrum.LoopyWeightedSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Loopy Importance Sampling

class pyAgrum.LoopyImportanceSampling(bn: pyAgrum.pyAgrum.IBayesNet)

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.
Return type:pyAgrum.IBayesNet
Raises:gum.UndefinedElement – If no Bayes net has been assigned to the inference.
H(self, X)

H(self, nodeName) -> double

Parameters:
  • X (int) – a node Id
  • nodeName (str) – a node name
Returns:

the computed Shanon’s entropy of a node given the observation

Return type:

double

addAllTargets(self)

Add all the nodes as targets.

addEvidence(self, id, val)

addEvidence(self, nodeName, val) addEvidence(self, id, val) addEvidence(self, nodeName, val) addEvidence(self, id, vals) addEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node already has an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
addTarget(self, target)

addTarget(self, nodeName)

Add a marginal target to the list of targets.

Parameters:
  • target (int) – a node Id
  • nodeName (str) – a node name
Raises:

gum.UndefinedElement – If target is not a NodeId in the Bayes net

chgEvidence(self, id, val)

chgEvidence(self, nodeName, val) chgEvidence(self, id, val) chgEvidence(self, nodeName, val) chgEvidence(self, id, vals) chgEvidence(self, nodeName, vals)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
  • val – (int) a node value
  • val – (str) the label of the node value
  • vals (list) – a list of values
Raises:
  • gum.InvalidArgument – If the node does not already have an evidence
  • gum.InvalidArgument – If val is not a value for the node
  • gum.InvalidArgument – If the size of vals is different from the domain side of the node
  • gum.FatalError – If vals is a vector of 0s
  • gum.UndefinedElement – If the node does not belong to the Bayesian network
currentPosterior(self, id)

currentPosterior(self, name) -> Potential

Computes and returns the current posterior of a node.

Parameters:
  • var (int) – the node Id of the node for which we need a posterior probability
  • nodeName (str) – the node name of the node for which we need a posterior probability
Returns:

a ref to the current posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

UndefinedElement – If an element of nodes is not in targets

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

Removes all the evidence entered into the network.

eraseAllTargets(self)

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

As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

eraseEvidence(self, id)

eraseEvidence(self, nodeName)

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

Parameters:
  • id (int) – a node Id
  • nodeName (int) – a node name
Raises:

gum.IndexError – If the node does not belong to the Bayesian network

eraseTarget(self, target)

eraseTarget(self, nodeName)

Remove, if existing, the marginal target.

Parameters:
  • target (int) – a node Id
  • nodeName (int) – a node name
Raises:
  • gum.IndexError – If one of the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
evidenceImpact(self, target, evs)

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

hasEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if some node(s) (or the one in parameters) have received evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasHardEvidence(self, nodeName)
Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a hard evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

hasSoftEvidence(self, id)

hasSoftEvidence(self, nodeName) -> bool

Parameters:
  • id (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if node has received a soft evidence

Return type:

bool

Raises:

gum.IndexError – If the node does not belong to the Bayesian network

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

isTarget(self, nodeName) -> bool

Parameters:
  • variable (int) – a node Id
  • nodeName (str) – a node name
Returns:

True if variable is a (marginal) target

Return type:

bool

Raises:
  • gum.IndexError – If the node does not belong to the Bayesian network
  • gum.UndefinedElement – If node Id is not in the Bayesian network
makeInference(self)

Perform the heavy computations needed to compute the targets’ posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done ‘lightly’ by multiplying and projecting those messages.

maxIter(self)
Returns:the criterion on number of iterations
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
nbrEvidence(self)
Returns:the number of evidence entered into the Bayesian network
Return type:int
nbrHardEvidence(self)
Returns:the number of hard evidence entered into the Bayesian network
Return type:int
nbrIterations(self)
Returns:the number of iterations
Return type:int
nbrSoftEvidence(self)
Returns:the number of soft evidence entered into the Bayesian network
Return type:int
nbrTargets(self)
Returns:the number of marginal targets
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.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
Returns:

a ref to the posterior probability of the node

Return type:

pyAgrum.Potential

Raises:

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

setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.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

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setTargets(targets)

Remove all the targets and add the ones in parameter.

Parameters:targets (set) – a set of targets
Raises:gum.UndefinedElement – If one target is not in the Bayes net
setVerbosity(self, v)
Parameters:v (bool) – verbosity
setVirtualLBPSize(self, vlbpsize)
Parameters:vlbpsize (double) – the size of the virtual LBP
softEvidenceNodes(self)
Returns:the set of nodes with soft evidence
Return type:set
targets(self)
Returns:the list of marginal targets
Return type:list
thisown

The membership flag

updateEvidence(evidces)

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

Parameters:

evidces (dict) – a dict of evidences

Raises:
  • gum.InvalidArgument – If one value is not a value for the node
  • gum.InvalidArgument – If the size of a value is different from the domain side of the node
  • gum.FatalError – If one value is a vector of 0s
  • gum.UndefinedElement – If one node does not belong to the Bayesian network
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool

Learning

class pyAgrum.BNLearner(*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)

addForbiddenArc(self, tail, head) addForbiddenArc(self, tail, head)

addMandatoryArc(self, arc)

addMandatoryArc(self, tail, head) addMandatoryArc(self, tail, head)

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

eraseForbiddenArc(self, tail, head) eraseForbiddenArc(self, tail, head)

eraseMandatoryArc(self, arc)

eraseMandatoryArc(self, tail, head) eraseMandatoryArc(self, tail, head)

history(self)
Returns:the scheme history
Return type:tuple
Raises:gum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false
idFromName(self, var_name)
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
Return type:list
learnBN(self)

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

Returns:the learned BayesNet
Return type:pyAgrum.BayesNet
learnDAG(self)
learnMixedStructure(self)

Warning

learner must be using 3off2 or MIIC algorithm

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

learnParameters(self, dag) -> BayesNet learnParameters(self, take_into_account_score=True) -> BayesNet learnParameters(self) -> 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 ()
Returns:

the learned BayesNet

Return type:

pyAgrum.BayesNet

Raises:
  • 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
Return type:int
maxTime(self)
Returns:the timeout(in seconds)
Return type:double
messageApproximationScheme(self)
Returns:the approximation scheme message
Return type:str
minEpsilonRate(self)
Returns:the value of the minimal epsilon rate
Return type:double
modalities(self)
nameFromId(self, id)
names(self)
nbrIterations(self)
Returns:the number of iterations
Return type:int
periodSize(self)
Returns:the number of samples between 2 stopping
Return type:int
Raises:gum.OutOfLowerBound – If p<1
setAprioriWeight(self, weight)
setEpsilon(self, eps)
Parameters:eps (double) – the epsilon we want to use
Raises:gum.OutOfLowerBound – If eps<0
setInitialDAG(self, g)
Parameters:dag (pyAgrum.DAG) – an initial DAG structure
setMaxIndegree(self, max_indegree)
setMaxIter(self, max)
Parameters:max (int) – the maximum number of iteration
Raises:gum.OutOfLowerBound – If max <= 1
setMaxTime(self, timeout)
Parameters:tiemout (double) – stopping criterion on timeout (in seconds)
Raises: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
Raises:gum.OutOfLowerBound – If p<1
setSliceOrder(self, l)

setSliceOrder(self, slice_order) setSliceOrder(self, slices)

setVerbosity(self, v)
Parameters:v (bool) – verbosity
thisown

The membership flag

use3off2(self)

Indicate that we wish to use 3off2.

useAprioriDirichlet(self, filename)
useAprioriSmoothing(self, weight=-1)

useAprioriSmoothing(self)

useGreedyHillClimbing(self)
useK2(self, l)

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

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

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

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)
useNoCorr(self)

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

useScoreAIC(self)
useScoreBD(self)
useScoreBDeu(self)
useScoreBIC(self)
useScoreK2(self)
useScoreLog2Likelihood(self)
verbosity(self)
Returns:True if the verbosity is enabled
Return type:bool