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

Skip to content

Commit 137802a

Browse files
committed
Squeezed out some of the vertical whitespace.
1 parent 2d711ad commit 137802a

File tree

1 file changed

+4
-29
lines changed

1 file changed

+4
-29
lines changed

probability.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ def __setitem__(self, val, p):
5858

5959
def normalize(self):
6060
"""Make sure the probabilities of all values sum to 1.
61-
6261
Returns the normalized distribution.
63-
6462
Raises a ZeroDivisionError if the sum of the values is 0.
65-
6663
>>> P = ProbDist('Flip'); P['H'], P['T'] = 35, 65
6764
>>> P = P.normalize()
6865
>>> print '%5.3f %5.3f' % (P.prob['H'], P.prob['T'])
@@ -89,8 +86,7 @@ class JointProbDist(ProbDist):
8986
0.25
9087
>>> P[dict(X=0, Y=1)] = 0.5
9188
>>> P[dict(X=0, Y=1)]
92-
0.5
93-
"""
89+
0.5"""
9490
def __init__(self, variables):
9591
update(self, prob={}, variables=variables, vals=DefaultDict([]))
9692

@@ -124,7 +120,6 @@ class BoolCpt:
124120
random variable conditioned on its parents."""
125121

126122
def __init__(self, table_data):
127-
128123
"""Initialize the table.
129124
130125
table_data may have one of three forms, depending on the
@@ -148,12 +143,10 @@ def __init__(self, table_data):
148143
>>> cpt = BoolCpt({T: 0.2, F: 0.7})
149144
>>> cpt = BoolCpt({(T, T): 0.2, (T, F): 0.3, (F, T): 0.5, (F, F): 0.7})
150145
"""
151-
152146
# A little work here makes looking up values MUCH simpler
153147
# later on. We transform table_data into the standard form
154148
# of a dictionary {(value, ...): number, ...} even if
155149
# the tuple has just 0 or 1 value.
156-
157150
if type(table_data) == float: # no parents, 0-tuple
158151
self.table_data = {(): table_data}
159152
elif type(table_data) == dict:
@@ -171,7 +164,6 @@ def __init__(self, table_data):
171164
raise Exception("wrong table_data type: %s" % table_data)
172165

173166
def p(self, value, parent_vars, event):
174-
175167
"""Return the conditional probability P(value | parent_vars =
176168
parent_values), where parent_values are the values of
177169
parent_vars in event.
@@ -197,15 +189,12 @@ def p(self, value, parent_vars, event):
197189
0.375
198190
>>> BoolCpt(0.75).p(False, [], {})
199191
0.25
200-
201192
"""
202-
203193
return self.p_values(value, event_values(event, parent_vars))
204194

205195
def p_values(self, xvalue, parent_values):
206196
"""Return P(X = xvalue | parents = parent_values),
207197
where parent_values is a tuple, even if of only 0 or 1 element.
208-
209198
>>> cpt = BoolCpt(0.25)
210199
>>> cpt.p_values(F, ())
211200
0.75
@@ -221,7 +210,6 @@ def p_values(self, xvalue, parent_values):
221210
>>> cpt.p_values(F, (F, F))
222211
0.38
223212
"""
224-
225213
ptrue = self.table_data[parent_values] # True or False
226214
if xvalue:
227215
return ptrue
@@ -244,12 +232,10 @@ def rand(self, parents, event):
244232
>>> cpt.rand(['A', 'B'], {'A': True, 'B': False}) in [True, False]
245233
True
246234
"""
247-
248235
return (random() <= self.p(True, parents, event))
249236

250237
def event_values(event, vars):
251238
"""Return a tuple of the values of variables vars in event.
252-
253239
>>> event_values ({'A': 10, 'B': 9, 'C': 8}, ['C', 'A'])
254240
(8, 10)
255241
>>> event_values ((1, 2), ['C', 'A'])
@@ -289,7 +275,6 @@ def enumerate_joint(vars, values, P):
289275
#______________________________________________________________________________
290276

291277
class BayesNet:
292-
293278
"""Bayesian network containing only boolean variable nodes."""
294279

295280
def __init__(self, nodes=[]):
@@ -306,23 +291,17 @@ def observe(self, var, val):
306291

307292
def variable_node(self, var):
308293
"""Returns the node for the variable named var.
309-
310294
>>> burglary.variable_node('Burglary').variable
311-
'Burglary'
312-
"""
313-
295+
'Burglary'"""
314296
for n in self.nodes:
315297
if n.variable == var:
316298
return n
317299
raise Exception("No such variable: %s" % var)
318300

319301
def variables(self):
320302
"""Returns the list of names of the variables.
321-
322303
>>> burglary.variables()
323-
['Burglary', 'Earthquake', 'Alarm', 'JohnCalls', 'MaryCalls']
324-
"""
325-
304+
['Burglary', 'Earthquake', 'Alarm', 'JohnCalls', 'MaryCalls']"""
326305
return [n.variable for n in self.nodes]
327306

328307
def variable_values(self, var):
@@ -366,9 +345,7 @@ def enumeration_ask (X, e, bn):
366345
>>> p = enumeration_ask('Burglary',
367346
... {'JohnCalls': True, 'MaryCalls': True}, burglary)
368347
>>> p.show_approx()
369-
'False: 0.716, True: 0.284'
370-
"""
371-
348+
'False: 0.716, True: 0.284'"""
372349
Q = ProbDist(X) # empty probability distribution for X
373350
for xi in bn.variable_values(X):
374351
Q[xi] = enumerate_all(bn.variables(), extend(e, X, xi), bn)
@@ -487,7 +464,6 @@ def rejection_sampling (X, e, bn, N):
487464
>>> p.show_approx()
488465
'False: 0.7, True: 0.3'
489466
"""
490-
491467
counts = {True: 0, False: 0} # boldface N in Fig. 14.13
492468

493469
for j in xrange(N):
@@ -542,7 +518,6 @@ def likelihood_weighting (X, e, bn, N):
542518
>>> p.show_approx()
543519
'False: 0.702, True: 0.298'
544520
"""
545-
546521
weights = {True: 0.0, False: 0.0} # boldface W in Fig. 14.14
547522

548523
for j in xrange(N):

0 commit comments

Comments
 (0)