dyndiscbayesiannetwork

This module provides tools to represent and handle dynamic Bayesian networks with discrete conditional probability distributions. Dynamic Bayesian networks represent systems that change over time. This means that each node in the BN has a finite number of outcomes, the distribution over which is dependent on the outcomes of the node’s parents and on the outcomes of the Bayesian network at the previous time interval. In other words, the Bayesian network changes over time according to Bayesian conditional probability rules.

class libpgm.dyndiscbayesiannetwork.DynDiscBayesianNetwork(orderedskeleton=None, nodedata=None)[source]

This class represents a dynamic Bayesian network with discrete CPD tables. It contains the attributes V, E, initial_Vdata, and twotbn_Vdata, and 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 initial_Vdata and twotbn_Vdata attributes get loaded with a dictionary with node data of the following fomat:

{
    "initial_Vdata": {
        "<vertex 1>": <dict containing vertex 1 data>,
        ...
        "<vertex n>": <dict containing vertex n data>
    }
    "twotbn_Vdata": {
        "<vertex 1>": <dict containing vertex 1 data>,
        ...
        "<vertex n>": <dict containing vertex n data>
    }
}

In particular, the "parents" attribute of "twotbn_Vdata" has the following format:

"twotbn_Vdata": {
    "vertex": {
        "parents": ["past_<vertex 1>",...,"past_<vertex n>", "vertex 1",..., "vertex m"]
        ...
    }
}

Where vertices 1 through n come from the previous time interval, and vertices 1 through m come from the current time interval. Note that additional keys besides the ones listed are possible in the dict of each vertex. For a full example see dynamic 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.

initial_Vdata = None

A dictionary containing CPD data for the Bayesian network at time interval 0.

twotbn_Vdata = None

A dictionary containing CPD data for the Bayesian network for time intervals greater than 0.

randomsample(n)[source]

This method produces a sequence of length n containing one dynamic Bayesian network sample over n time units. In other words, the first entry of the sequence is a sample from the initial Bayesian network, and each subsequent entry is sampled from the Bayesian network conditioned on the outcomes of its predecessor. This function requires a specific dictionary format in Vdata, as shown in dynamic discrete bayesian network.

This function takes the following arguments:
  1. n – The number of time units over which to sample (thus also the length of the sequence produced)
And returns:
A list of n random samples, each conditioned on its precedessor, each a dict containing (vertex: value) pairs.

Usage example: this would produce a sequence of 10 samples, one per time step, each conditioned on its predecessor:

import json

from graphskeleton import GraphSkeleton
from dyndiscbayesiannetwork import DynDiscBayesianNetwork

path = "../tests/unittestdyndict.txt" # an input file
f = open(path, 'r')
g = eval(f.read())

d = DynDiscBayesianNetwork()
skel = GraphSkeleton()
skel.V = g["V"]
skel.E = g["E"]
skel.toporder()
d.V = skel.V
d.E = skel.E
d.initial_Vdata = g["initial_Vdata"]
d.twotbn_Vdata = g["twotbn_Vdata"]

seq = d.randomsample(10)
print json.dumps(seq, indent=2)

Previous topic

lgbayesiannetwork

Next topic

tablecpdfactorization

This Page