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)
Section titled “class pyagrum.BayesNetFragment(bn)”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
- Parameters:
bn (
IBayesNet)
addArcs(listArcs)
Section titled “addArcs(listArcs)”add a list of arcs in te model.
- Parameters: listArcs (List [**Tuple [**intstr ,**intstr ] ]) – the list of arcs
addStructureListener(whenNodeAdded=None, whenNodeDeleted=None, whenArcAdded=None, whenArcDeleted=None)
Section titled “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
addVariables(listFastVariables, default_nbr_mod=2)
Section titled “addVariables(listFastVariables, default_nbr_mod=2)”Add a list of variable in the form of ‘fast’ syntax.
- Parameters:
- listFastVariables (List [**str ]) – the list of variables in ‘fast’ syntax.
- default_nbr_mod (int) – the number of modalities for the variable if not specified following fast syntax. Note that default_nbr_mod=1 is mandatory to create variables with only one modality (for utility for instance).
- Returns: the list of created ids.
- Return type: List[int]
adjacencyMatrix()
Section titled “adjacencyMatrix()”adjacency matrix from a graph/graphical models
Compute the adjacency matrix of a pyAgrum’s graph or graphical models (more generally an object that has nodes, children/parents or neighbours methods)
- Returns: adjacency matrix (as numpy.ndarray) with nodeId as key.
- Return type: numpy.ndarray
ancestors(norid)
Section titled “ancestors(norid)”give the set of nodeid of ancestors of a node
- Parameters: norid (str |**int) – the name or the id of the node
- Returns: the set of ids of the ancestors of node norid.
- Return type: Set[int]
arcs()
Section titled “arcs()”- Returns: The lisf of arcs in the IBayesNet
- Return type: list
check()
Section titled “check()”Check if the BayesNet is consistent (variables, CPT, …)
- Returns: list of found issues
- Return type: List[str]
checkConsistency(*args)
Section titled “checkConsistency(*args)”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:
- pyagrum.NotFound –
- if the node is not found. –
children(norid)
Section titled “children(norid)”- Parameters:
- id (int) – the id of the parent
- norid (
object)
- Returns: the set of all the children
- Return type: Set
completeInstantiation()
Section titled “completeInstantiation()”Give an instantiation over all the variables of the model
- Returns: a complete Instantiation for the model
- Return type: pyagrum.Instantiation
connectedComponents()
Section titled “connectedComponents()”connected components from a graph/graphical models
Compute the connected components of a pyAgrum’s graph or graphical models (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(*args)
Section titled “cpt(*args)”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: pyagrum.Tensor
- Raises: pyagrum.NotFound – If no variable’s id matches varId.
- Returns: a constant reference to the dag of this BayesNet.
- Return type: pyagrum.DAG
descendants(norid)
Section titled “descendants(norid)”give the set of nodeid of descendants of a node
- Parameters: norid (str |**int) – the name or the id of the node
- Returns: the set of ids of the descendants of node norid.
- Return type: Set[int]
Returns the dimension (the number of free parameters) in this BayesNet.
- Returns: the dimension of the BayesNet
- Return type: int
empty()
Section titled “empty()”Check if there are some variables in the model.
- Returns: True if there is no variable in the model.
- Return type: bool
evEq(name, value)
Section titled “evEq(name, value)”This method is used to set an observation on a quasi-continuous variables (pyagrum .DiscretizedVariable with a large number of ticks) that the variable is equal to a given value.
Note
- see also
Tensor.evEq() - see also
BayesNet.evGt(),BayesNet.evLt(),BayesNet.evIn()
Examples
>>> import pyagrum as gum>>> bn = pyagrum.fastBN('A[0:10:20]'') # DiscretizedVariable from 0 to 10 in 20 steps>>> print(bn.evEqu('A',5) A |[0;0.5[ |[0.5;1[ |[1;1.5[ |[1.5;2[ |[2;2.5[ |[2.5;3[ |[3;3.5[ |[3.5;4[ |[4;4.5[ |[4.5;5[ |[5;5.5[ |[5.5;6[ |[6;6.5[ |[6.5;7[ |[7;7.5[ |[7.5;8[ |[8;8.5[ |[8.5;9[ |[9;9.5[ |[9.5;10] |---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 1.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |- Parameters:
- var (Union [**int ,**str ]) – the current name or the id of the variable
- value (float)
- name (
str)
- Returns:
The representation of the evidence as a
Tensor. - Return type: pyAgrum.Tensor
evGt(name, value)
Section titled “evGt(name, value)”This method is used to set an obvervation on a quasi-continuous variables (pyagrum .DiscretizedVariable with a large number of ticks) that the variable greater than a given value.
Note
- see also
Tensor.evGt() - see also
BayesNet.evEq(),BayesNet.evLt(),BayesNet.evIn()
Examples
>>> import pyagrum as gum>>> bn = pyagrum.fastBN('A[0:10:20]'') # DiscretizedVariable from 0 to 10 in 20 steps>>> print(bn.eGt('A',5) A |[0;0.5[ |[0.5;1[ |[1;1.5[ |[1.5;2[ |[2;2.5[ |[2.5;3[ |[3;3.5[ |[3.5;4[ |[4;4.5[ |[4.5;5[ |[5;5.5[ |[5.5;6[ |[6;6.5[ |[6.5;7[ |[7;7.5[ |[7.5;8[ |[8;8.5[ |[8.5;9[ |[9;9.5[ |[9.5;10] |---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 | 1.0000 |- Parameters:
- var (Union [**int ,**str ]) – the current name or the id of the variable
- value (float)
- name (
str)
- Returns:
The representation of the evidence as a
Tensor. - Return type: pyAgrum.Tensor
evIn(name, val1, val2)
Section titled “evIn(name, val1, val2)”This method is used to set an obvervation on a quasi-continuous variables (pyagrum .DiscretizedVariable with a large number of ticks) that the variable is less than a given value.
Note
- see also
Tensor.evIn() - see also
BayesNet.evEq(),BayesNet.evGt(),BayesNet.evLt()
- Parameters:
- var (Union [**int ,**str ]) – the current name or the id of the variable
- valueMin (float) – the minimum value
- valueMax (float) – the maximum value
- name (
str) - val1 (
float) - val2 (
float)
- Returns:
The representation of the evidence as a
Tensor. - Return type: pyagrum.Tensor
evLt(name, value)
Section titled “evLt(name, value)”This method is used to set an obvervation on a quasi-continuous variables (pyagrum .DiscretizedVariable with a large number of ticks) that the variable is less than a given value.
Note
- see also
Tensor.evLt() - see also
BayesNet.evEq(),BayesNet.evGt(),BayesNet.evIn()
- Parameters:
- var (Union [**int ,**str ]) – the current name or the id of the variable
- value (float)
- name (
str)
- Returns:
The representation of the evidence as a
Tensor. - Return type: pyAgrum.Tensor
exists(*args)
Section titled “exists(*args)”Check if a node with this name or id exists
- Parameters: norid (str |**int) – name or id of the searched node
- Returns: True if there is a node with such a name or id
- Return type: bool
existsArc(*args)
Section titled “existsArc(*args)”Check if an arc exists
- Parameters:
- tail (str |**int) – the name or id of the tail of the arc
- head (str |**int) – the name or the id of the head of the arc
- Returns: True if tail->head is an arc.
- Return type: bool
family(norid)
Section titled “family(norid)”give the set of parents of a node and the node
- Parameters: norid (str |**int) – the node
- Returns: the set of nodeId of the family of the node norid
- Return type: Set[int]
hasSameStructure(other)
Section titled “hasSameStructure(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(name)
Section titled “idFromName(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.
Notes
A convenient shortcut for g.variableFromName(name) is g[name].
- Returns: The variable’s node id.
- Return type: int
- Raises: pyagrum.NotFound – If name does not match a variable in the graph
ids(names)
Section titled “ids(names)”List of ids for a list of names of variables in the model
- Parameters:
- lov (List [**str ]) – List of variable names
- names (
list[str])
- Returns: The ids for the list of names of the graph variables
- Return type: List[int]
installAscendants(*args)
Section titled “installAscendants(*args)”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:
- pyagrum.NotFound –
- if the node is not found. –
- Return type:
None
installCPT(*args)
Section titled “installCPT(*args)”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 (Tensor) – the Tensor to install
- Raises: pyagrum.NotFound – if the node is not found.
- Return type:
None
installMarginal(*args)
Section titled “installMarginal(*args)”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 (Tensor) – the Tensor (marginal) to install
- Raises: pyagrum.NotFound – if the node is not found.
- Return type:
None
installNode(*args)
Section titled “installNode(*args)”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: pyagrum.NotFound – if the node is not found.
- Return type:
None
isIndependent(*args)
Section titled “isIndependent(*args)”check if nodes X and nodes Y are independent given nodes Z
- Parameters:
- X (str |**intList [**str |**int ]) – a list of of nodeIds or names
- Y (str |**intList [**str |**int ]) – a list of of nodeIds or names
- Z (str |**intList [**str |**int ]) – a list of of nodeIds or names
- Raises: InvalidArgument – if X and Y share variables
- Returns: True if X and Y are independent given Z in the model
- Return type: bool
isInstalledNode(*args)
Section titled “isInstalledNode(*args)”Check if a node is in the fragment
- Parameters: n (int , str) – the id or the name of the variable.
- Return type:
bool
jointProbability(i)
Section titled “jointProbability(i)”- Parameters: i (pyagrum.instantiation) – an instantiation of the variables
- Returns: a parameter of the joint probability for the BayesNet
- Return type: float
Warning
a variable not present in the instantiation is assumed to be instantiated to 0
log10DomainSize()
Section titled “log10DomainSize()”returns the log10 of the domain size of the model defined as the product of the domain sizes of the variables in the model.
- Returns: the log10 domain size.
- Return type: float
log2JointProbability(i)
Section titled “log2JointProbability(i)”- Parameters: i (pyagrum.instantiation) – an instantiation of the variables
- Returns: a parameter of the log joint probability for the BayesNet
- Return type: float
Warning
a variable not present in the instantiation is assumed to be instantiated to 0
maxNonOneParam()
Section titled “maxNonOneParam()”- Returns: The biggest value (not equal to 1) in the CPTs of the BayesNet
- Return type: float
maxParam()
Section titled “maxParam()”- Returns: the biggest value in the CPTs of the BayesNet
- Return type: float
maxVarDomainSize()
Section titled “maxVarDomainSize()”- Returns: the biggest domain size among the variables of the BayesNet
- Return type: int
memoryFootprint()
Section titled “memoryFootprint()”get the size (in byte) of the (main footprint) of the BayesNet
- Returns: the size in byte of the representation (of the parameters) of the BayesNet
- Return type: int
minNonZeroParam()
Section titled “minNonZeroParam()”- Returns: the smallest value (not equal to 0) in the CPTs of the IBayesNet
- Return type: float
minParam()
Section titled “minParam()”- Returns: the smallest value in the CPTs of the IBayesNet
- Return type: float
minimalCondSet(*args)
Section titled “minimalCondSet(*args)”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 [**int ]) – The ids of the targets
- list (List [**int ]) – The list of available variables
- Returns: The minimal set of variables
- Return type: Set[int]
moralGraph()
Section titled “moralGraph()”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
moralizedAncestralGraph(nodes)
Section titled “moralizedAncestralGraph(nodes)”build a UndiGraph by moralizing the Ancestral Graph of a list of nodes
- Parameters: nodes (str |**intList [**str |**int ]) – the list of of nodeIds or names
Warning
pyagrum.UndiGraph only knows NodeId. Hence the moralized ancestral graph does not include the names of the variables.graph
- Returns: the moralized ancestral graph of the nodes
- Return type: pyagrum.UndiGraph
names()
Section titled “names()”Set of names of variables in the model
- Returns: The names of the graph variables
- Return type: Set[str]
nodeId(var)
Section titled “nodeId(var)”- Parameters: var (pyagrum.DiscreteVariable) – a variable
- Returns: the id of the variable
- Return type: int
- Raises: pyagrum.IndexError – If the graph does not contain the variable
nodes()
Section titled “nodes()”- Returns: the set of ids
- Return type: Set[int]
nodeset(names)
Section titled “nodeset(names)”Set of ids for a list of names of variables in the model
- Parameters:
- lov (List [**str ]) – List of variable names
- names (
list[str])
- Returns: The set of ids for the list of names of the graph variables
- Return type: Set[int]
parents(norid)
Section titled “parents(norid)”- Parameters:
- id – The id of the child node
- norid (
object)
- Returns: the set of the parents ids.
- Return type: Set
properties()
Section titled “properties()”- Return type:
list[str]
property(name)
Section titled “property(name)”Returns the value associated to this property.
Properties are a way to keep some (name,value) together with de model.
- Parameters: name (str) – the name of the property
- Raises: NotFound – if no name property is found
- Returns: The value associated to this name
- Return type: str
propertyWithDefault(name, byDefault)
Section titled “propertyWithDefault(name, byDefault)”Returns the value associated to this property or the default value if there is no such property.
Properties are a way to keep some information (name,value) together with de model.
- Parameters:
- name (str) – the name of the property
- byDefault (str) – the value by default if no property has been found.
- Returns: The value associated to this name or the value by default.
- Return type: str
setProperty(name, value)
Section titled “setProperty(name, value)”Create or change the couple (name,value) in the properties.
Properties are a way to keep some information (name,value) together with de model.
- Parameters:
- name (str) – the name of the property
- value (str) – the value of the property.
- Return type:
None
size()
Section titled “size()”- Returns: the number of nodes in the graph
- Return type: int
sizeArcs()
Section titled “sizeArcs()”- Returns: the number of arcs in the graph
- Return type: int
toBN()
Section titled “toBN()”Create a BayesNet from a fragment.
- Raises: pyagrum.OperationNotAllowed – if the fragment is not consistent.
- Return type:
BayesNet
toDot()
Section titled “toDot()”- Returns: a friendly display of the graph in DOT format
- Return type: str
topologicalOrder()
Section titled “topologicalOrder()”- Returns: the list of the nodes Ids in a topological order
- Return type: List
- Raises: pyagrum.InvalidDirectedCycle – If this graph contains cycles
uninstallCPT(*args)
Section titled “uninstallCPT(*args)”Remove a local CPT. The fragment can become inconsistant.
- Parameters: n (int , str) – the id or the name of the variable.
- Raises: pyagrum.NotFound – if the node is not found.
- Return type:
None
uninstallNode(*args)
Section titled “uninstallNode(*args)”Remove a node from the fragment. The fragment can become inconsistant.
- Parameters: n (int , str) – the id or the name of the variable.
- Raises: pyagrum.NotFound – if the node is not found.
- Return type:
None
variable(*args)
Section titled “variable(*args)”- Parameters:
- id (int) – a variable’s id
- name (str) – a variable’s name
- Returns: the variable
- Return type: pyagrum.DiscreteVariable
- Raises: pyagrum.IndexError – If the graph does not contain the variable
variableFromName(name)
Section titled “variableFromName(name)”- Parameters: name (str) – a variable’s name
- Returns: the variable
- Return type: pyagrum.DiscreteVariable
- Raises: pyagrum.IndexError – If the graph does not contain the variable
variableNodeMap()
Section titled “variableNodeMap()”- Returns: the variable node map
- Return type: pyagrum.variableNodeMap
variables(*args)
Section titled “variables(*args)”whenArcAdded(src, _from, to)
Section titled “whenArcAdded(src, _from, to)”- Parameters:
- src (
object) - _from (
int) - to (
int)
- src (
- Return type:
None
whenArcDeleted(src, _from, to)
Section titled “whenArcDeleted(src, _from, to)”- Parameters:
- src (
object) - _from (
int) - to (
int)
- src (
- Return type:
None
whenNodeAdded(src, id)
Section titled “whenNodeAdded(src, id)”- Parameters:
- src (
object) - id (
int)
- src (
- Return type:
None
whenNodeDeleted(src, id)
Section titled “whenNodeDeleted(src, id)”- Parameters:
- src (
object) - id (
int)
- src (
- Return type:
None