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

Method condprobve

libpgm/tablecpdfactorization.py:124–204  ·  view source on GitHub ↗

Eliminate all variables in *factorlist* except for the ones queried. Adjust all distributions for the evidence given. Return the probability distribution over a set of variables given by the keys of *query* given *evidence*. Arguments: 1. *query* -- A dict cont

(self, query, evidence)

Source from the content-addressed store, hash-verified

122 self.factorlist = self.factorlist[0]
123
124 def condprobve(self, query, evidence):
125 '''
126 Eliminate all variables in *factorlist* except for the ones queried. Adjust all distributions for the evidence given. Return the probability distribution over a set of variables given by the keys of *query* given *evidence*.
127
128 Arguments:
129 1. *query* -- A dict containing (key: value) pairs reflecting (variable: value) that represents what outcome to calculate the probability of.
130 2. *evidence* -- A dict containing (key: value) pairs reflecting (variable: value) that represents what is known about the system.
131
132 Attributes modified:
133 1. *factorlist* -- Modified to be one factor representing the probability distribution of the query variables given the evidence.
134
135 The function returns *factorlist* after it has been modified as above.
136
137 Usage example: this code would return the distribution over a queried node, given evidence::
138
139 import json
140
141 from libpgm.graphskeleton import GraphSkeleton
142 from libpgm.nodedata import NodeData
143 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
144 from libpgm.tablecpdfactorization import TableCPDFactorization
145
146 # load nodedata and graphskeleton
147 nd = NodeData()
148 skel = GraphSkeleton()
149 nd.load("../tests/unittestdict.txt")
150 skel.load("../tests/unittestdict.txt")
151
152 # toporder graph skeleton
153 skel.toporder()
154
155 # load evidence
156 evidence = dict(Letter='weak')
157 query = dict(Grade='A')
158
159 # load bayesian network
160 bn = DiscreteBayesianNetwork(skel, nd)
161
162 # load factorization
163 fn = TableCPDFactorization(bn)
164
165 # calculate probability distribution
166 result = fn.condprobve(query, evidence)
167
168 # output
169 print json.dumps(result.vals, indent=2)
170 print json.dumps(result.scope, indent=2)
171 print json.dumps(result.card, indent=2)
172 print json.dumps(result.stride, indent=2)
173
174 '''
175 assert (isinstance(query, dict) and isinstance(evidence, dict)), "First and second args must be dicts."
176
177 eliminate = self.bn.V[:]
178 for key in query.keys():
179 eliminate.remove(key)
180 for key in evidence.keys():
181 eliminate.remove(key)

Callers 3

specificqueryMethod · 0.95
test_condprobveMethod · 0.80
examples.pyFile · 0.80

Calls 2

sumproductveMethod · 0.95
reducefactorMethod · 0.80

Tested by 1

test_condprobveMethod · 0.64