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

Class LGBayesianNetwork

libpgm/lgbayesiannetwork.py:44–177  ·  view source on GitHub ↗

This class represents a Bayesian network with linear Gaussian CPDs. It contains the attributes *V*, *E*, and *Vdata*, as well as the method *randomsample*.

Source from the content-addressed store, hash-verified

42from orderedskeleton import OrderedSkeleton
43
44class LGBayesianNetwork(OrderedSkeleton):
45 '''
46 This class represents a Bayesian network with linear Gaussian CPDs. It contains the attributes *V*, *E*, and *Vdata*, as well as the method *randomsample*.
47
48 '''
49
50 def __init__(self, orderedskeleton=None, nodedata=None):
51 '''
52 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):
53
54 1. *orderedskeleton* -- An instance of the :doc:`OrderedSkeleton <orderedskeleton>` or :doc:`GraphSkeleton <graphskeleton>` (as long as it&#x27;s ordered) class.
55 2. *nodedata* -- An instance of the :doc:`NodeData <nodedata>` class.
56
57 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.
58
59 This class requires that the *Vdata* attribute gets loaded with a dictionary with node data of the following fomat::
60
61 "vertex": {
62 "parents": ["<name of parent 1>", ... , "<name of parent n>"],
63 "children": ["<name of child 1>", ... , "<name of child n>"],
64 "mean_base": <the base mean of the Gaussian distribution>,
65 "mean_scal": [<scalar for parent 1 outcome>, ... , <scalar for parent n outcome>],
66 "variance": <variance of the Gaussian distibution>
67 }
68
69 Note that additional keys are possible in the dict of each vertex.
70
71 Upon loading, the class will also check that the keys of *Vdata* correspond to the vertices in *V*.
72
73 ''&#x27;
74 if (orderedskeleton != None and nodedata != None):
75 try:
76 self.V = orderedskeleton.V
77 '''A list of the names of the vertices.'''
78 self.E = orderedskeleton.E
79 '''A list of [origin, destination] pairs of vertices that make edges.'''
80 self.Vdata = nodedata.Vdata
81 '''A dictionary containing CPD data for the nodes.'''
82 except:
83 raise Exception, "Inputs were malformed; first arg must contain V and E attributes and second arg must contain Vdata attribute."
84
85 assert sorted(self.V) == sorted(self.Vdata.keys()), "Vertices did not match node data"
86
87 def randomsample(self, n, evidence=None, mode="normal"):
88 ''&#x27;
89 Produce *n* random samples from the Bayesian Network and return them in a list.
90
91 See above for how the means of linear Gaussians are calculated during sampling.
92
93 This function takes the following arguments:
94
95 1. *n* -- The number of random samples to produce.
96 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.
97 3. *mode* -- (Optional) Can be set to "verbose", whereupon the method will return a [value, mean, variance] list for each node rather than just the actual value.
98
99 And returns:
100 A list of *n* independent random samples, each element of which is a dict containing (vertex: value) pairs.
101

Callers 4

lg_mle_estimateparamsMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
examples.pyFile · 0.90

Calls

no outgoing calls

Tested by 2

setUpMethod · 0.72
setUpMethod · 0.72