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

Class DiscreteBayesianNetwork

libpgm/discretebayesiannetwork.py:38–228  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

36from tablecpdfactorization import TableCPDFactorization
37
38class DiscreteBayesianNetwork(OrderedSkeleton):
39 '''
40 This class represents a Bayesian network with discrete CPD tables. It contains the attributes *V*, *E*, and *Vdata*, as well as the method *randomsample*.
41
42 '''
43
44
45 def __init__(self, orderedskeleton=None, nodedata=None, path=None):
46 '''
47 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. Note that the user must specify EITHER *nodedata* and *orderedskeleton* OR *path*.
48
49 1. *orderedskeleton* -- An instance of the :doc:`OrderedSkeleton <orderedskeleton>` or :doc:`GraphSkeleton <graphskeleton>` (as long as it&#x27;s ordered) class.
50 2. *nodedata* -- An instance of the :doc:`NodeData <nodedata>` class.
51 3. *path* -- The path to a file containing complete, properly formatted json for a discrete Bayesian network. See :doc:`unittestdict` for an example.
52
53 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.
54
55 This class requires that the *Vdata* attribute gets loaded with a dictionary with node data of the following fomat::
56
57
58 Note that additional keys besides the ones listed are possible in the dict of each vertex. For a full example see :doc:`unittestdict`.
59
60 Upon loading, the class will also check that the keys of *Vdata* correspond to the vertices in *V*.
61 ''&#x27;
62 assert not (orderedskeleton and nodedata and path), "specify nodedata and orderedskeleton OR path"
63 if (orderedskeleton != None and nodedata != None):
64 try:
65 self.V = orderedskeleton.V
66 '''A list of the names of the vertices.'''
67 self.E = orderedskeleton.E
68 '''A list of [origin, destination] pairs of vertices that make edges.'''
69 self.Vdata = nodedata.Vdata
70 ''&#x27;
71 A dictionary containing CPD data for the nodes of the format::
72
73 "vertex": {
74 "numoutcomes": <number of possible outcome values>,
75 "vals": ["<name of value 1>", ... , "<name of value n>"],
76 "parents": ["<name of parent 1>", ... , "<name of parent n>"],
77 "children": ["<name of child 1>", ... , "<name of child n>"],
78 "cprob": {
79 "['<parent 1, value 1>',...,'<parent n, value 1>']": [<probability of vals[0]>, ... , <probability of vals[n-1]>],
80 ...
81 "['<parent 1, value j>',...,'<parent n, value k>']": [<probability of vals[0]>, ... , <probability of vals[n-1]>],
82 }
83 }
84
85 ''&#x27;
86 except:
87 raise Exception, "Inputs were malformed; first arg must contain V and E attributes and second arg must contain Vdata attribute."
88
89 # check that inputs match up
90 assert (sorted(self.V) == sorted(self.Vdata.keys())), ("Vertices did not match vertex data:", self.V, self.Vdata.keys())
91
92 if (path):
93
94 # validate
95 with open(path) as f:

Callers 9

timerFunction · 0.90
timerFunction · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
examples.pyFile · 0.90

Calls

no outgoing calls

Tested by 5

setUpMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72