MCPcopy Create free account
hub / github.com/CyberPoint/libpgm / HyBayesianNetwork

Class HyBayesianNetwork

libpgm/hybayesiannetwork.py:37–152  ·  view source on GitHub ↗

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/CP

Source from the content-addressed store, hash-verified

35from orderedskeleton import OrderedSkeleton
36
37class HyBayesianNetwork(OrderedSkeleton):
38 '''
39 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.
40
41 '''
42
43 def __init__(self, orderedskeleton=None, nodedata=None):
44 '''
45 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):
46
47 1. *orderedskeleton* -- An instance of the :doc:`OrderedSkeleton <orderedskeleton>` or :doc:`GraphSkeleton <graphskeleton>` (as long as it&#x27;s ordered) class.
48 2. *nodedata* -- An instance of the :doc:`NodeData <nodedata>` class.
49
50 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.
51
52 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.
53
54 Upon loading, the class will also check that the keys of *Vdata* correspond to the vertices in *V*.
55
56 ''&#x27;
57 if (orderedskeleton != None and nodedata != None):
58 try:
59 self.V = orderedskeleton.V
60 '''A list of the names of the vertices.'''
61 self.E = orderedskeleton.E
62 '''A list of [origin, destination] pairs of vertices that make edges.'''
63 self.Vdata = nodedata.Vdata
64 '''A dictionary containing CPD data for the nodes.'''
65
66 # specific to hybrid Bayesian network
67 self.nodes = nodedata.nodes
68 '''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.'''
69 except:
70 raise Exception, "Inputs were malformed; first arg must contain V and E attributes and second arg must contain Vdata and nodes attributes."
71
72 # check that inputs match up
73 assert sorted(self.V) == sorted(self.Vdata.keys()), "Node data did not match graph skeleton nodes."
74
75 def randomsample(self, n, evidence=None):
76 ''&#x27;
77 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.
78
79 This function takes the following arguments:
80
81 1. *n* -- The number of random samples to produce.
82 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.
83
84 And returns:
85 A list of *n* independent random samples, each element of which is a dict containing (vertex: value) pairs.
86
87 Usage example: this would generate a sequence of 10 random samples::
88
89 import json
90
91 from libpgm.nodedata import NodeData
92 from libpgm.graphskeleton import GraphSkeleton
93 from libpgm.hybayesiannetwork import HyBayesianNetwork
94

Callers 2

setUpMethod · 0.90
examples.pyFile · 0.90

Calls

no outgoing calls

Tested by 1

setUpMethod · 0.72