hybayesiannetwork

This module provides tools to represent and handle Bayesian networks with conditional probability distributions that can be specified node-by-node.

This method allows for the construction of a Bayesian network with every combination of every type of CPD, provided that the user provides a method for sampling each type of node and stores this method in the proper place, namely as the choose() method of a class in libpgm.CPDtypes/.

class libpgm.hybayesiannetwork.HyBayesianNetwork(orderedskeleton=None, nodedata=None)[source]

This class represents a Bayesian network with CPDs of any type. The nodes of the Bayesian network are stored first in a dictionary, specifying their “type”, which should be descriptive (‘discrete’, ‘lg’, etc.). Furthermore, the types of each node associate them with a class found in libpgm/CPDtypes/. The nodes are then stored also as instances of classes found in this directory. The purpose of this is that each node has its own method for being sampled given the outcomes of its parents.

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.

It is required that the nodedata class instance inputted has its nodes attribute instantiated. In order for this to be the case, the instance must have run its entriestoinstances method.

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

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.

nodes = None

A dictionary of {key: value} pairs linking the node name (the key) to a class instance (the value) representing the node, its node data, and its sampling function.

randomsample(n, evidence=None)[source]

Produce n random samples from the Bayesian networki, subject to evidence, and return them in a list. This function requires the nodes attribute to be instantiated.

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.hybayesiannetwork import HyBayesianNetwork

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

# topologically order graphskeleton
skel.toporder()

# convert nodes to class instances
nd.entriestoinstances()

# load bayesian network
hybn = HyBayesianNetwork(skel, nd)

# sample 
result = hybn.randomsample(10)

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

Previous topic

discretebayesiannetwork

Next topic

lgbayesiannetwork

This Page