discretebayesiannetwork

This module provides tools to represent and handle Bayesian networks with discrete conditional probability distribuitions. This means that each node has a finite number of outcomes, the distribution over which is dependent on the outcomes of the node’s parents.

class libpgm.discretebayesiannetwork.DiscreteBayesianNetwork(orderedskeleton=None, nodedata=None)[source]

This class represents a Bayesian network with discrete CPD tables. It contains the attributes V, E, and Vdata, as well as the method randomsample.

This class can be called either with or without arguments. If it is called without arguments, none of its attributes are instantiated and it is left to the user to instantiate them manually. If it is called with arguments, the attributes will be loaded directly from the inputs. The arguments must be (in order):

  1. orderedskeleton – An instance of the OrderedSkeleton or GraphSkeleton (as long as it’s ordered) class.
  2. nodedata – An instance of the NodeData class.

If these arguments are present, all attributes of the class (V, E, and Vdata) will be automatically copied from the graph skeleton and node data inputs.

This class requires that the Vdata attribute gets loaded with a dictionary with node data of the following fomat:

"vertex": {
    "numoutcomes": <number of possible outcome values>,
    "vals": ["<name of value 1>", ... , "<name of value n>"],
    "parents": ["<name of parent 1>", ... , "<name of parent n>"],
    "children": ["<name of child 1>", ... , "<name of child n>"],   
    "cprob": {
        "['<parent 1, value 1>',...,'<parent n, value 1>']": [<probability of vals[0]>, ... , <probability of vals[n-1]>],
        ...
        "['<parent 1, value j>',...,'<parent n, value k>']": [<probability of vals[0]>, ... , <probability of vals[n-1]>],
    }
}

Note that additional keys besides the ones listed are possible in the dict of each vertex. For a full example see discrete bayesian network.

Upon loading, the class will also check that the keys of Vdata correspond to the vertices in V.

V = None

A list of the names of the vertices.

E = None

A list of [origin, destination] pairs of vertices that make edges.

Vdata = None

A dictionary containing CPD data for the nodes.

randomsample(n, evidence=None)[source]

Produce n random samples from the Bayesian network, subject to evidence, and return them in a list.

This function takes the following arguments:

  1. n – The number of random samples to produce.
  2. evidence – (Optional) A dict containing (vertex: value) pairs that describe the evidence. To be used carefully because it does manually overrides the nodes with evidence instead of affecting the joint probability distribution of the entire graph.
And returns:
A list of n independent random samples, each element of which is a dict containing (vertex: value) pairs.

Usage example: this would generate a sequence of 10 random samples:

import json

from libpgm.nodedata import NodeData
from libpgm.graphskeleton import GraphSkeleton
from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork

# load nodedata and graphskeleton
nd = NodeData()
skel = GraphSkeleton()
nd.load("../tests/unittestdict.txt")    # any input file
skel.load("../tests/unittestdict.txt")

# topologically order graphskeleton
skel.toporder()

# load bayesian network
bn = DiscreteBayesianNetwork(skel, nd)

# sample 
result = bn.randomsample(10)

# output
print json.dumps(result, indent=2)

Previous topic

nodedata

Next topic

hybayesiannetwork

This Page