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

Skip to content

Commit c686131

Browse files
committed
Add doc comments.
1 parent 64a5d00 commit c686131

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

probability.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def enumerate_all(vars, e, bn):
288288
#______________________________________________________________________________
289289

290290
def elimination_ask(X, e, bn):
291-
"""[Fig. 14.11]
291+
"""Compute bn's P(X|e) by variable elimination. [Fig. 14.11]
292292
>>> elimination_ask('Burglary', dict(JohnCalls=T, MaryCalls=T), burglary
293293
... ).show_approx()
294294
'False: 0.716, True: 0.284'"""
@@ -300,9 +300,13 @@ def elimination_ask(X, e, bn):
300300
return pointwise_product(factors, bn).normalize()
301301

302302
def is_hidden(var, X, e):
303+
"Is var a hidden variable when querying P(X|e)?"
303304
return var != X and var not in e
304305

305306
def make_factor(var, e, bn):
307+
"""Return the factor for var in bn's joint distribution given e.
308+
That is, bn's full joint distribution, projected to accord with e,
309+
is the pointwise product of these factors for bn's variables."""
306310
node = bn.variable_node(var)
307311
vars = [X for X in [var] + node.parents if X not in e]
308312
cpt = dict((event_values(e1, vars), node.p(e1[var], e1))
@@ -313,24 +317,28 @@ def pointwise_product(factors, bn):
313317
return reduce(lambda f, g: f.pointwise_product(g, bn), factors)
314318

315319
def sum_out(var, factors, bn):
320+
"Eliminate var from all factors by summing over its values."
316321
result, var_factors = [], []
317322
for f in factors:
318323
(var_factors if var in f.vars else result).append(f)
319324
result.append(pointwise_product(var_factors, bn).sum_out(var, bn))
320325
return result
321326

322327
class Factor:
328+
"A factor in a joint distribution."
323329

324330
def __init__(self, vars, cpt):
325331
update(self, vars=vars, cpt=cpt)
326332

327333
def pointwise_product(self, other, bn):
334+
"Multiply two factors, combining their variables."
328335
vars = list(set(self.vars) | set(other.vars))
329336
cpt = dict((event_values(e, vars), self.p(e) * other.p(e))
330337
for e in all_events(vars, bn, {}))
331338
return Factor(vars, cpt)
332339

333340
def sum_out(self, var, bn):
341+
"Make a factor eliminating var by summing over its values."
334342
vars = [X for X in self.vars if X != var]
335343
cpt = dict((event_values(e, vars),
336344
sum(self.p(extend(e, var, val))
@@ -339,21 +347,24 @@ def sum_out(self, var, bn):
339347
return Factor(vars, cpt)
340348

341349
def normalize(self):
350+
"Return my probabilities; must be down to one variable."
342351
assert len(self.vars) == 1
343352
return ProbDist(self.vars[0],
344353
dict((k, v) for ((k,), v) in self.cpt.items()))
345354

346355
def p(self, e):
356+
"Look up my value tabulated for e."
347357
return self.cpt[event_values(e, self.vars)]
348358

349-
def all_events(vars, bn, e1):
359+
def all_events(vars, bn, e):
360+
"Yield every way of extending e with values for all vars."
350361
if not vars:
351-
yield e1
362+
yield e
352363
else:
353364
X, rest = vars[0], vars[1:]
354-
for e in all_events(rest, bn, e1):
365+
for e1 in all_events(rest, bn, e):
355366
for x in bn.variable_values(X):
356-
yield extend(e, X, x)
367+
yield extend(e1, X, x)
357368

358369
#______________________________________________________________________________
359370

0 commit comments

Comments
 (0)