Skip to content

Commit 2966301

Browse files
committed
Add repr methods and assertion.
1 parent c686131 commit 2966301

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

probability.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ def variable_values(self, var):
176176
"Return the domain of var."
177177
return [True, False]
178178

179+
def __repr__(self):
180+
return 'BayesNet(%r)' % self.nodes
179181

180182
class BayesNode:
181183
"""A conditional probability distribution for a boolean variable,
@@ -241,6 +243,9 @@ def sample(self, event):
241243
parents."""
242244
return probability(self.p(True, event))
243245

246+
def __repr__(self):
247+
return 'node(%r, %r)' % (self.variable, ' '.join(self.parents))
248+
244249
node = BayesNode
245250

246251
# Burglary example [Fig. 14.2]
@@ -292,6 +297,7 @@ def elimination_ask(X, e, bn):
292297
>>> elimination_ask('Burglary', dict(JohnCalls=T, MaryCalls=T), burglary
293298
... ).show_approx()
294299
'False: 0.716, True: 0.284'"""
300+
assert X not in e, "Query variable must be distinct from evidence"
295301
factors = []
296302
for var in reversed(bn.vars):
297303
factors.append(make_factor(var, e, bn))
@@ -492,19 +498,19 @@ def particle_filtering(e, N, dbn):
492498

493499
#_______________________________________________________________________________
494500
__doc__ += """
495-
## We can build up a probability distribution like this (p. 469):
501+
# We can build up a probability distribution like this (p. 469):
496502
>>> P = ProbDist()
497503
>>> P['sunny'] = 0.7
498504
>>> P['rain'] = 0.2
499505
>>> P['cloudy'] = 0.08
500506
>>> P['snow'] = 0.02
501507
502-
## and query it like this: (Never mind this ELLIPSIS option
503-
## added to make the doctest portable.)
508+
# and query it like this: (Never mind this ELLIPSIS option
509+
# added to make the doctest portable.)
504510
>>> P['rain'] #doctest:+ELLIPSIS
505511
0.2...
506512
507-
## A Joint Probability Distribution is dealt with like this (Fig. 13.3):
513+
# A Joint Probability Distribution is dealt with like this (Fig. 13.3):
508514
>>> P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
509515
>>> T, F = True, False
510516
>>> P[T, T, T] = 0.108; P[T, T, F] = 0.012; P[F, T, T] = 0.072; P[F, T, F] = 0.008
@@ -513,7 +519,7 @@ def particle_filtering(e, N, dbn):
513519
>>> P[T, T, T]
514520
0.108
515521
516-
## Ask for P(Cavity|Toothache=T)
522+
# Ask for P(Cavity|Toothache=T)
517523
>>> PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
518524
>>> PC.show_approx()
519525
'False: 0.4, True: 0.6'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy