Model for Bayesian Network

a Bayesian network (alarm)

The Bayesian network is the main graphical model of pyagrum. A Bayesian network is a directed probabilistic graphical model based on a DAG. 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 independence in the joint distribution. These conditional independence allow to factorize the joint distribution, thereby allowing to compactly represent very large ones.

\[P(X_1,\cdots,X_n)=\prod_{i=1}^n P(X_i | Parents(X_i))\]

Moreover, inference algorithms can also use this graph to speed up the computations. Finally, the Bayesian networks can be learnt from data.

See also

Input/Output Formats for Bayesian Networks

Supported file formats for saving and loading Bayesian networks.

Inference

Exact and approximated inference algorithms.

Learning

Learning Bayesian network structure and parameters from data.

Tools for Bayesian networks

Utility tools: comparison, information theory, random generation, etc.

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:

Scientific notes

A Bayesian network represents a joint distribution by a DAG and the conditional probability table (CPT) of each variable given its parents [1]: \(P(x_1, \dots, x_n) = \prod_i P(x_i \mid \mathrm{pa}(x_i))\).

Arc reversal. reverseArc(X, Y) keeps the joint distribution, as in [2]: both nodes receive the union of their parents (\(X\) keeping \(Y\) as a new parent), and the new CPTs are

\[P(y \mid \mathrm{pa}) = \sum_x P(x \mid \mathrm{pa}(x))\, P(y \mid \mathrm{pa}(y)), \qquad P(x \mid y, \mathrm{pa}) = \frac{P(x \mid \mathrm{pa}(x))\, P(y \mid \mathrm{pa}(y))}{P(y \mid \mathrm{pa})}.\]

The reversal is refused when another directed path from \(X\) to \(Y\) would make a cycle.

Canonical tables. The noisy-OR and noisy-AND are canonical models with independence of causal influence [3], for a binary variable \(C\) with binary parents \(X_k\), a leak \(\ell\) (external_weight) and a weight \(p_k\) for each parent. With compound parameters (addNoisyOR, alias of addNoisyORCompound), \(p_k\) is the probability of \(C=1\) when only \(X_k\) is present, leak included, as in the leaky noisy-OR of [4]; with net parameters (addNoisyORNet), \(p_k\) excludes the leak:

\[P(C = 0 \mid x) = (1-\ell) \prod_{k : x_k = 1} \frac{1-p_k}{1-\ell} \quad \text{(compound)}, \qquad P(C = 0 \mid x) = (1-\ell) \prod_{k : x_k = 1} (1-p_k) \quad \text{(net)}.\]

pyAgrum’s noisy-AND is \(P(C = 1 \mid x) = \ell \prod_{k : x_k = 1} p_k \prod_{k : x_k = 0} (1-p_k)\), and the logit table is the logistic regression \(P(C=1 \mid x) = 1/(1 + e^{-(\ell + \sum_k p_k v_k)})\), \(v_k\) being the numerical value of \(x_k\). The aggregators (addOR, addMAX, addCOUNT…) are deterministic tables: a function of the values of the parents.

Methods

add()

Add a variable to the pyagrum.BayesNet.

addAMPLITUDE()

Add a deterministic variable whose value is the amplitude (maximum minus minimum) of the values of its parents.

addAND()

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

addArc()

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

addArcs()

add a list of arcs in te model.

addCOUNT()

Others aggregators

addEXISTS()

Others aggregators

addFORALL()

Others aggregators

addLogit()

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

addMAX()

Add a deterministic variable whose value is the maximum of the values of its parents.

addMEDIAN()

Add a deterministic variable whose value is the median of the values of its parents.

addMIN()

Add a deterministic variable whose value is the minimum of the values of its parents.

addNoisyAND()

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

addNoisyOR()

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

addNoisyORCompound()

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

addNoisyORNet()

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

addOR()

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

addSUM()

Add a deterministic variable whose value is the sum of the values of its parents.

addStructureListener()

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

addVariables()

Add a list of variable in the form of ‘fast’ syntax.

addWeightedArc()

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

adjacencyMatrix()

adjacency matrix from a graph/graphical models

ancestors()

give the set of nodeid of ancestors of a node

arcs()

beginTopologyTransformation()

When inserting/removing arcs, node CPTs change their dimension with a cost in time.

changeVariableLabel()

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

changeVariableName()

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

check()

Check if the BayesNet is consistent (variables, CPT, …)

children()

give the set of nodeid of the children of a node (or of a set of nodes)

clear()

Clear the whole BayesNet

completeInstantiation()

Give an instantiation over all the variables of the model

connectedComponents()

Return the weakly connected components of the graph.

connectedComponentsCount()

number of connected components

connectedComponentsList()

connected components as a dict of sets

contextualize()

Return a new Bayesian network conditioned on observations and interventions.

cpt()

Returns the CPT of a variable.

dag()

descendants()

give the set of nodeid of descendants of a node

dim()

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

empty()

Check if there are some variables in the model.

endTopologyTransformation()

Ends a sequence of insertions/deletions of arcs started with pyagrum.BayesNet.beginTopologyTransformation: the dimensions of all the CPTs are adjusted.

erase()

Remove a variable from the pyagrum.BayesNet.

eraseArc()

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

evEq()

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.

evGt()

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 greater than a given value.

evIn()

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 in a given range.

evLt()

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 less than a given value.

exists()

Check if a node with this name or id exists

existsArc()

Check if an arc exists

existsProperty()

Check whether a property key exists in the model’s metadata.

family()

give the set of parents of a node and the node

fastPrototype()

Create a Bayesian network with a dot-like syntax which specifies:

generateCPT()

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

generateCPTs()

Randomly generates CPTs for a given structure.

hasSameStructure()

idFromName()

Return the node id of a variable given its name.

ids()

List of ids for a list of names of variables in the model

idsFromNames()

The ids of nodes given by their names, in a container of the same type.

isIndependent()

check if nodes X and nodes Y are independent given nodes Z

jointProbability()

loadBIF()

Load a BIF file.

loadBIFXML()

Load a BIFXML file.

loadDSL()

Load a DSL file.

loadGUM()

Load a jgum (JSON) or bgum (binary/msgpack) file.

loadGUMstring()

Deserialize a BayesNet from a jgum JSON string.

loadNET()

Load a NET file.

loadO3PRM()

Load an O3PRM file.

loadUAI()

Load an UAI file.

loadXDSL()

Load a XDSL file.

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.

log2JointProbability()

maxNonOneParam()

maxParam()

maxVarDomainSize()

memoryFootprint()

Returns the memory footprint in bytes of the Bayesian network.

minNonZeroParam()

minParam()

minimalCondSet()

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

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.

moralizedAncestralGraph()

build a UndiGraph by moralizing the Ancestral Graph of a list of nodes

names()

Set of names of variables in the model

namesFromIds()

The names of nodes given by their ids, in a container of the same type.

nodeId()

Return the node id of a variable.

nodes()

nodeset()

Set of ids for a list of names of variables in the model

parents()

give the set of nodeid of the parents of a node (or of a set of nodes)

properties()

Return the keys of all metadata properties of the model.

property()

Returns the value associated to this property.

propertyWithDefault()

Returns the value associated to this property or the default value if there is no such property.

reverseArc()

Reverses an arc while preserving the same joint distribution.

saveBIF()

Save the BayesNet in a BIF file.

saveBIFXML()

Save the BayesNet in a BIFXML file.

saveDSL()

Save the BayesNet in a DSL file.

saveGUM()

Save the Bayesian network as a .jgum (text) or .bgum (binary) file.

saveGUMstring()

Save the Bayesian network as a jgum string.

saveNET()

Save the BayesNet in a NET file.

saveO3PRM()

Save the BayesNet in an O3PRM file.

saveUAI()

Save the BayesNet in an UAI file.

saveXDSL()

Save the BayesNet in a XDSL file.

setProperty()

Create or change the couple (name,value) in the properties.

size()

sizeArcs()

spaceCplxToString()

Return a human-readable string summarising the space complexity of a graphical model.

toDot()

toFast()

Export the Bayesian network in fast syntax (the structure and the variables, not the parameters), read back by pyagrum.fastBN.

topologicalOrder()

updateMetaData()

Update the model’s built-in metadata (version, creation date, last modification date).

variable()

variableFromName()

Return the variable with the given name.

variables()

Return the variables of the model: all of them, or those corresponding to a list of names or to a set of node ids.