Model

class pyAgrum.BayesNet(*args)

BayesNet represents a Bayesian Network.

BayesNet(name=’‘) -> BayesNet
Parameters:
  • name (str) – the name of the Bayes Net
BayesNet(source) -> BayesNet
Parameters:
  • source (pyAgrum.BayesNet) – the Bayesian network to copy
add(BayesNet self, DiscreteVariable var)

add(BayesNet self, str name, unsigned int nbrmod) -> int add(BayesNet self, DiscreteVariable var, pyAgrum.MultiDimImplementation aContent) -> int add(BayesNet self, DiscreteVariable var, int id) -> int add(BayesNet self, DiscreteVariable var, pyAgrum.MultiDimImplementation aContent, int id) -> int

Add a variable to the pyAgrum.BayesNet.

Parameters:
  • variable (pyAgrum.DiscreteVariable) – the variable added
  • name (str) – the variable name
  • nbrmod (int) – the number 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 is less than 2
  • gum.DuplicateElement – If id is already used.
addAMPLITUDE(BayesNet self, DiscreteVariable var)

Others aggregators

Parameters:variable (pyAgrum.DiscreteVariable) – the variable to be added
Returns:the id of the added value
Return type:int
addAND(BayesNet self, DiscreteVariable 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(BayesNet self, int tail, int head)

addArc(BayesNet self, str tail, str 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.
  • gum.DuplicateElement – If the arc already exists.
addCOUNT(BayesNet self, DiscreteVariable var, int value=1)

Others aggregators

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

Others aggregators

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

Others aggregators

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

addLogit(BayesNet self, DiscreteVariable var, double 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(BayesNet self, DiscreteVariable var)

Others aggregators

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

Others aggregators

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

Others aggregators

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

addNoisyAND(BayesNet self, DiscreteVariable var, double 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(BayesNet self, DiscreteVariable var, double external_weight)

addNoisyOR(BayesNet self, DiscreteVariable var, double external_weight, int 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(BayesNet self, DiscreteVariable var, double external_weight)

addNoisyORCompound(BayesNet self, DiscreteVariable var, double external_weight, int 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(BayesNet self, DiscreteVariable var, double external_weight)

addNoisyORNet(BayesNet self, DiscreteVariable var, double external_weight, int 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(BayesNet self, DiscreteVariable 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(BayesNet self, int tail, int head, double causalWeight)

addWeightedArc(BayesNet self, str tail, str head, double 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(BayesNet self)
Returns:The lisf of arcs in the IBayesNet
Return type:list
beginTopologyTransformation(BayesNet 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(BayesNet self, int id, Potential newPot)

changePotential(BayesNet self, str name, Potential 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(BayesNet self, int id, str old_label, str new_label)

changeVariableLabel(BayesNet self, str name, str old_label, str 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(BayesNet self, int id, str new_name)

changeVariableName(BayesNet self, str name, str 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(BayesNet self, PyObject * norid)
Parameters:id (int) – the id of the parent
Returns:the set of all the children
Return type:Set
clear(BayesNet self)

Clear the whole BayesNet

completeInstantiation(DAGmodel self)

Get an instantiation over all the variables of the model.

Returns:the complete instantiation
Return type:pyAgrum.instantiation
connectedComponents()

connected components from a graph/BN

Compute the connected components of a pyAgrum’s graph or Bayesian Network (more generally an object that has nodes, children/parents or neighbours methods)

The firstly visited node for each component is called a ‘root’ and is used as a key for the component. This root has been arbitrarily chosen during the algorithm.

Parameters:graph (pyAgrum's graph) – A graph, a Bayesian network, more generally an object that has nodes, children/parents or neighbours methods in which the search will take place
Returns:dict of connected components (as set of nodeIds (int)) with a nodeId (root) of each component as key.
Return type:dict(int,Set[int])
cpt(BayesNet self, int varId)

cpt(BayesNet self, str 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(BayesNet self)
Returns:a constant reference to the dag of this BayesNet.
Return type:pyAgrum.DAG
dim(IBayesNet self)

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

Returns:the dimension of the BayesNet
Return type:int
empty(DAGmodel self)
Returns:True if the model is empty
Return type:bool
endTopologyTransformation(BayesNet 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(BayesNet self, int varId)

erase(BayesNet self, str name) erase(BayesNet self, DiscreteVariable 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(BayesNet self, Arc arc)

eraseArc(BayesNet self, int tail, int head) eraseArc(BayesNet self, str tail, str 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(str dotlike, int domainSize=2)
Create a Bayesian network with a dot-like syntax which specifies:
  • the structure ‘a->b->c;b->d<-e;’.
  • the type of the variables with different syntax:
    • by default, a variable is a gum.RangeVariable using the default domain size (second argument)
    • with ‘a[10]’, the variable is a gum.RangeVariable using 10 as domain size (from 0 to 9)
    • with ‘a[3,7]’, the variable is a gum.RangeVariable using a domainSize from 3 to 7
    • with ‘a[1,3.14,5,6.2]’, the variable is a gum.DiscretizedVariable using the given ticks (at least 3 values)
    • with ‘a{top|middle|bottom}’, the variable is a gum.LabelizedVariable using the given labels.

Note

  • If the dot-like string contains such a specification more than once for a variable, the first specification will be used.
  • the CPTs are randomly generated.
  • see also pyAgrum.fastBN.

Examples

>>> import pyAgrum as gum
>>> bn=gum.BayesNet.fastPrototype('A->B[1,3]<-C{yes|No}->D[2,4]<-E[1,2.5,3.9]',6)
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(BayesNet self, int node)

generateCPT(BayesNet self, str 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(BayesNet self)

Randomly generates CPTs for a given structure.

hasSameStructure(DAGmodel self, DAGmodel 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(BayesNet self, str 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(IBayesNet self, Instantiation 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(BayesNet self, str name, PyObject * l=(PyObject *) 0)

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(BayesNet self, str name, PyObject * l=(PyObject *) 0)

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(BayesNet self, str name, PyObject * l=(PyObject *) 0)

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(BayesNet self, str name, PyObject * l=(PyObject *) 0)

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(BayesNet self, str name, str system="", str classpath="", PyObject * l=(PyObject *) 0)

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(BayesNet self, str name, PyObject * l=(PyObject *) 0)

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(BayesNet self)
Returns:The log10 domain size of the joint probability for the model.
Return type:double
log2JointProbability(IBayesNet self, Instantiation 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(IBayesNet self)
Returns:The biggest value (not equal to 1) in the CPTs of the BayesNet
Return type:double
maxParam(IBayesNet self)
Returns:the biggest value in the CPTs of the BayesNet
Return type:double
maxVarDomainSize(IBayesNet self)
Returns:the biggest domain size among the variables of the BayesNet
Return type:int
minNonZeroParam(IBayesNet self)
Returns:the smallest value (not equal to 0) in the CPTs of the IBayesNet
Return type:double
minParam(IBayesNet self)
Returns:the smallest value in the CPTs of the IBayesNet
Return type:double
minimalCondSet(BayesNet self, int target, PyObject * list)

minimalCondSet(BayesNet self, PyObject * targets, PyObject * 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(DAGmodel self, bool clear=True)

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(BayesNet self)
Returns:The names of the graph variables
Return type:list
nodeId(BayesNet self, DiscreteVariable 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(BayesNet self)
Returns:the set of ids
Return type:set
parents(BayesNet self, PyObject * norid)
Parameters:id – The id of the child node
Returns:the set of the parents ids.
Return type:Set
property(DAGmodel self, str name)

Warning

Unreferenced function

propertyWithDefault(DAGmodel self, str name, str byDefault)

Warning

Unreferenced function

reverseArc(BayesNet self, int tail, int head)

reverseArc(BayesNet self, str tail, str head) reverseArc(BayesNet self, Arc 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(BayesNet self, str name)

Save the BayesNet in a BIF file.

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

Save the BayesNet in a BIFXML file.

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

Save the BayesNet in a DSL file.

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

Save the BayesNet in a NET file.

Parameters:name (str) – the file’s name
saveO3PRM(BayesNet self, str 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(BayesNet self, str name)

Save the BayesNet in an UAI file.

Parameters:name (str) – the file’s name
setProperty(DAGmodel self, str name, str value)

Warning

Unreferenced function

size(BayesNet self)
Returns:the number of nodes in the graph
Return type:int
sizeArcs(DAGmodel self)
Returns:the number of arcs in the graph
Return type:int
toDot(IBayesNet self)
Returns:a friendly display of the graph in DOT format
Return type:str
topologicalOrder(DAGmodel self, bool clear=True)
Returns:the list of the nodes Ids in a topological order
Return type:List
Raises:gum.InvalidDirectedCycle – If this graph contains cycles
variable(BayesNet self, int id)

variable(BayesNet self, str 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(BayesNet self, str 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(BayesNet self)
Returns:the variable node map
Return type:pyAgrum.variableNodeMap