Fragment of Bayesian networks¶
This class proposes a shallow copy of a part of Bayesian Network. It can be used as a Bayesian Network for inference algorithms (for instance).

class
pyAgrum.
BayesNetFragment
(bn: pyAgrum.IBayesNet)¶ BayesNetFragment represents a part of a Bayesian Network (subset of nodes). By default, the arcs and the CPTs are the same as the BN but local CPTs can be build to express different local dependencies. All the non local CPTs are not copied. Therefore a BayesNetFragment is a light object.
 BayesNetFragment(BayesNet bn) > BayesNetFragment
 Parameters:
 bn (pyAgrum.BayesNet) – the bn refered by the fragment

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

arcs
(BayesNetFragment self)¶ Returns: The lisf of arcs in the IBayesNet Return type: list

checkConsistency
(BayesNetFragment self, int id)¶ checkConsistency(BayesNetFragment self, str name) > bool checkConsistency(BayesNetFragment self) > bool
If a variable is added to the fragment but not its parents, there is no CPT consistant for this variable. This function checks the consistency for a variable of for all.
Parameters: n (int, str (optional)) – the id or the name of the variable. If no argument, the function checks all the variables. Returns: True if the variable(s) is consistant. Return type: boolean Raises: gum.NotFound
– if the node is not found.

children
(BayesNetFragment self, PyObject * norid)¶ Parameters: id (int) – the id of the parent Returns: the set of all the children Return type: Set

completeInstantiation
(GraphicalModel self)¶

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.
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
(BayesNetFragment self, int varId)¶ cpt(BayesNetFragment self, str name) > Potential
Returns the CPT of a variable.
Parameters:  VarId (int) – A variable’s id in the pyAgrum.IBayesNet.
 name (str) – A variable’s name in the pyAgrum.IBayesNet.
Returns: The variable’s CPT.
Return type: Raises: gum.NotFound
– If no variable’s id matches varId.

dag
(BayesNetFragment 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
(GraphicalModel self)¶

existsArc
(DAGmodel self, int tail, int head)¶ existsArc(DAGmodel self, str nametail, str namehead) > bool

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
(BayesNetFragment 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

installAscendants
(BayesNetFragment self, int id)¶ installAscendants(BayesNetFragment self, str name)
Add the variable and all its ascendants in the fragment. No inconsistant node are created.
Parameters: n (int, str) – the id or the name of the variable. Raises: gum.NotFound
– if the node is not found.

installCPT
(BayesNetFragment self, int id, Potential pot)¶ installCPT(BayesNetFragment self, str name, Potential pot)
Install a local CPT for a node. Doing so, it changes the parents of the node in the fragment.
Parameters:  n (int, str) – the id or the name of the variable.
 pot (Potential) – the Potential to install
Raises: gum.NotFound
– if the node is not found.

installMarginal
(BayesNetFragment self, int id, Potential pot)¶ installMarginal(BayesNetFragment self, str name, Potential pot)
Install a local marginal for a node. Doing so, it removes the parents of the node in the fragment.
Parameters:  n (int, str) – the id or the name of the variable.
 pot (Potential) – the Potential (marginal) to install
Raises: gum.NotFound
– if the node is not found.

installNode
(BayesNetFragment self, int id)¶ installNode(BayesNetFragment self, str name)
Add a node to the fragment. The arcs that can be added between installed nodes are created. No specific CPT are created. Then either the parents of the node are already in the fragment and the node is consistant, or the parents are not in the fragment and the node is not consistant.
Parameters: n (int, str) – the id or the name of the variable. Raises: gum.NotFound
– if the node is not found.

isInstalledNode
(BayesNetFragment self, int id)¶ isInstalledNode(BayesNetFragment self, str name) > bool
Check if a node is in the fragment
Parameters: n (int, str) – the id or the name of the variable.

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

log10DomainSize
(GraphicalModel self)¶

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
(BayesNetFragment self, int target, PyObject * list)¶ minimalCondSet(BayesNetFragment 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
(BayesNetFragment self)¶ Returns: The names of the graph variables Return type: list

nodeId
(BayesNetFragment 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
(BayesNetFragment self)¶ Returns: the set of ids Return type: set

parents
(BayesNetFragment self, PyObject * norid)¶ Parameters: id – The id of the child node Returns: the set of the parents ids. Return type: Set

property
(GraphicalModel self, str name)¶

propertyWithDefault
(GraphicalModel self, str name, str byDefault)¶

setProperty
(GraphicalModel self, str name, str value)¶

size
(DAGmodel 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

toBN
(BayesNetFragment self)¶ Create a BayesNet from a fragment.
Raises: gum.OperationNotAllowed
– if the fragment is not consistent.

toDot
(BayesNetFragment 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

uninstallCPT
(BayesNetFragment self, int id)¶ uninstallCPT(BayesNetFragment self, str name)
Remove a local CPT. The fragment can become inconsistant.
Parameters: n (int, str) – the id or the name of the variable. Raises: gum.NotFound
– if the node is not found.

uninstallNode
(BayesNetFragment self, int id)¶ uninstallNode(BayesNetFragment self, str name)
Remove a node from the fragment. The fragment can become inconsistant.
Parameters: n (int, str) – the id or the name of the variable. Raises: gum.NotFound
– if the node is not found.

variable
(BayesNetFragment self, int id)¶ variable(BayesNetFragment self, str name) > DiscreteVariable
Parameters:  id (int) – a variable’s id
 name (str) – a variable’s name
Returns: the variable
Return type: Raises: gum.IndexError
– If the graph does not contain the variable

variableFromName
(BayesNetFragment 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
(BayesNetFragment self)¶ Returns: the variable node map Return type: pyAgrum.variableNodeMap

whenArcAdded
(BayesNetFragment self, void * src, int _from, int to)¶

whenArcDeleted
(BayesNetFragment self, void * src, int _from, int to)¶

whenNodeAdded
(BayesNetFragment self, void * src, int id)¶

whenNodeDeleted
(BayesNetFragment self, void * src, int id)¶