@@ -176,6 +176,8 @@ def variable_values(self, var):
176
176
"Return the domain of var."
177
177
return [True , False ]
178
178
179
+ def __repr__ (self ):
180
+ return 'BayesNet(%r)' % self .nodes
179
181
180
182
class BayesNode :
181
183
"""A conditional probability distribution for a boolean variable,
@@ -241,6 +243,9 @@ def sample(self, event):
241
243
parents."""
242
244
return probability (self .p (True , event ))
243
245
246
+ def __repr__ (self ):
247
+ return 'node(%r, %r)' % (self .variable , ' ' .join (self .parents ))
248
+
244
249
node = BayesNode
245
250
246
251
# Burglary example [Fig. 14.2]
@@ -292,6 +297,7 @@ def elimination_ask(X, e, bn):
292
297
>>> elimination_ask('Burglary', dict(JohnCalls=T, MaryCalls=T), burglary
293
298
... ).show_approx()
294
299
'False: 0.716, True: 0.284'"""
300
+ assert X not in e , "Query variable must be distinct from evidence"
295
301
factors = []
296
302
for var in reversed (bn .vars ):
297
303
factors .append (make_factor (var , e , bn ))
@@ -492,19 +498,19 @@ def particle_filtering(e, N, dbn):
492
498
493
499
#_______________________________________________________________________________
494
500
__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):
496
502
>>> P = ProbDist()
497
503
>>> P['sunny'] = 0.7
498
504
>>> P['rain'] = 0.2
499
505
>>> P['cloudy'] = 0.08
500
506
>>> P['snow'] = 0.02
501
507
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.)
504
510
>>> P['rain'] #doctest:+ELLIPSIS
505
511
0.2...
506
512
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):
508
514
>>> P = JointProbDist(['Toothache', 'Cavity', 'Catch'])
509
515
>>> T, F = True, False
510
516
>>> 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):
513
519
>>> P[T, T, T]
514
520
0.108
515
521
516
- ## Ask for P(Cavity|Toothache=T)
522
+ # Ask for P(Cavity|Toothache=T)
517
523
>>> PC = enumerate_joint_ask('Cavity', {'Toothache': T}, P)
518
524
>>> PC.show_approx()
519
525
'False: 0.4, True: 0.6'
0 commit comments