Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb8b235

Browse files
committed
Delete BayesNet.variables(); add check that X is distinct from evidence.
1 parent c05e846 commit bb8b235

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

probability.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def add(self, node):
157157
"""Add a node to the net. Its parents must already be in the
158158
net, and node itself must not."""
159159
assert node not in self.nodes
160-
assert every(lambda parent: parent in self.variables(), node.parents)
160+
assert every(lambda parent: parent in self.vars, node.parents)
161161
self.nodes.append(node)
162162
self.vars.append(node.variable)
163163
for parent in node.parents:
@@ -172,12 +172,6 @@ def variable_node(self, var):
172172
return n
173173
raise Exception("No such variable: %s" % var)
174174

175-
def variables(self):
176-
"""List all of the net's variables, parents before children.
177-
>>> burglary.variables()
178-
['Burglary', 'Earthquake', 'Alarm', 'JohnCalls', 'MaryCalls']"""
179-
return [n.variable for n in self.nodes]
180-
181175
def variable_values(self, var):
182176
"Return the domain of var."
183177
return [True, False]
@@ -270,9 +264,10 @@ def enumeration_ask(X, e, bn):
270264
>>> enumeration_ask('Burglary', dict(JohnCalls=T, MaryCalls=T), burglary
271265
... ).show_approx()
272266
'False: 0.716, True: 0.284'"""
267+
assert X not in e, "Query variable must be distinct from evidence"
273268
Q = ProbDist(X)
274269
for xi in bn.variable_values(X):
275-
Q[xi] = enumerate_all(bn.variables(), extend(e, X, xi), bn)
270+
Q[xi] = enumerate_all(bn.vars, extend(e, X, xi), bn)
276271
return Q.normalize()
277272

278273
def enumerate_all(vars, e, bn):
@@ -398,7 +393,7 @@ def gibbs_ask(X, e, bn, N):
398393
'False: 0.738, True: 0.262'
399394
"""
400395
counts = dict((x, 0) for x in bn.variable_values(X)) # bold N in Fig. 14.16
401-
Z = [var for var in bn.variables() if var not in e]
396+
Z = [var for var in bn.vars if var not in e]
402397
state = dict(e) # boldface x in Fig. 14.16
403398
for Zi in Z:
404399
state[Zi] = choice(bn.variable_values(Zi))

0 commit comments

Comments
 (0)