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

Method specificquery

libpgm/tablecpdfactorization.py:206–308  ·  view source on GitHub ↗

Eliminate all variables except for the ones specified by *query*. Adjust all distributions to reflect *evidence*. Return the entry that matches the exact probability of a specific event, as specified by *query*. Arguments: 1. *query* -- A dict containing (key: v

(self, query, evidence)

Source from the content-addressed store, hash-verified

204 return self.factorlist
205
206 def specificquery(self, query, evidence):
207 '''
208 Eliminate all variables except for the ones specified by *query*. Adjust all distributions to reflect *evidence*. Return the entry that matches the exact probability of a specific event, as specified by *query*.
209
210 Arguments:
211 1. *query* -- A dict containing (key: value) pairs reflecting (variable: value) that represents what outcome to calculate the probability of. The value must be a list of values (for ordinary queries do a list of length one).
212 2. *evidence* -- A dict containing (key: value) pairs reflecting (variable: value) evidence that is known about the system.
213
214 Attributes modified:
215 1. *factorlist* -- Modified as in *condprobve*.
216
217 The function then chooses the entries of *factorlist* that match the queried event or events. It then operates on them to return the probability that the event (or events) specified will occur, represented as a float between 0 and 1.
218
219 Note that in this function, queries of the type P((x=A or x=B) and (y=C or y=D)) are permitted. They are executed by formatting the *query* dictionary like so::
220
221 {
222 "x": ["A", "B"],
223 "y": ["C", "D"]
224 }
225
226 Usage example: this code would answer the specific query that vertex ``Grade`` gets outcome ``A`` given that ``Letter`` has outcome ``weak``, in :doc:`this Bayesian network <unittestdict>`::
227
228 import json
229
230 from libpgm.graphskeleton import GraphSkeleton
231 from libpgm.nodedata import NodeData
232 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
233 from libpgm.tablecpdfactorization import TableCPDFactorization
234
235 # load nodedata and graphskeleton
236 nd = NodeData()
237 skel = GraphSkeleton()
238 nd.load("../tests/unittestdict.txt")
239 skel.load("../tests/unittestdict.txt")
240
241 # toporder graph skeleton
242 skel.toporder()
243
244 # load evidence
245 evidence = dict(Letter='weak')
246 query = dict(Grade=['A'])
247
248 # load bayesian network
249 bn = DiscreteBayesianNetwork(skel, nd)
250
251 # load factorization
252 fn = TableCPDFactorization(bn)
253
254 # calculate probability distribution
255 result = fn.specificquery(query, evidence)
256
257 # output
258 print result
259
260 ''&#x27;
261 assert (isinstance(query, dict) and isinstance(evidence, dict)), "First and second args must be dicts."
262 assert query, "Query must be non-empty."
263

Callers 4

specificqueryMethod · 0.95
test_refreshMethod · 0.45
test_specificqueryMethod · 0.45
examples.pyFile · 0.45

Calls 1

condprobveMethod · 0.95

Tested by 2

test_refreshMethod · 0.36
test_specificqueryMethod · 0.36