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

Skip to content

Commit d43e072

Browse files
committed
Assorted minor cleanup, mostly probability.py.
1 parent c89b517 commit d43e072

File tree

2 files changed

+56
-62
lines changed

2 files changed

+56
-62
lines changed

logic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ def occur_check(var, x, s):
821821
(or in subst(s, x), if s has a binding for x)."""
822822
if var == x:
823823
return True
824-
elif is_variable(x) and s.has_key(x):
824+
elif is_variable(x) and x in s:
825825
return occur_check(var, s[x], s)
826826
elif isinstance(x, Expr):
827827
return (occur_check(var, x.op, s) or

probability.py

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ def values(self, var):
111111
def __repr__(self):
112112
return "P(%s)" % self.variables
113113

114+
#______________________________________________________________________________
115+
116+
def enumerate_joint_ask(X, e, P):
117+
"""Return a probability distribution over the values of the variable X,
118+
given the {var:val} observations e, in the JointProbDist P.
119+
Works for Boolean variables only. [Fig. 13.4].
120+
121+
X is a string (variable name).
122+
e is a dictionary of variable-name value pairs.
123+
P is an instance of JointProbDist."""
124+
125+
Q = ProbDist(X) # probability distribution for X, initially empty
126+
Y = [v for v in P.variables if v != X and v not in e] # hidden vars.
127+
for xi in P.values(X):
128+
Q[xi] = enumerate_joint(Y, extend(e, X, xi), P)
129+
return Q.normalize()
130+
131+
def enumerate_joint(vars, values, P):
132+
"As in Fig 13.4, except x and e are already incorporated in values."
133+
if not vars:
134+
return P[values]
135+
Y, rest = vars[0], vars[1:]
136+
return sum([enumerate_joint(rest, extend(values, Y, y), P)
137+
for y in P.values(Y)])
114138

115139
#______________________________________________________________________________
116140

@@ -119,20 +143,20 @@ class BoolCpt:
119143
"""Conditional probability table for a boolean (True/False)
120144
random variable conditioned on its parents."""
121145

122-
def __init__(self, table_data):
146+
def __init__(self, table):
123147
"""Initialize the table.
124148
125-
table_data may have one of three forms, depending on the
149+
table may have one of three forms, depending on the
126150
number of parents:
127151
128-
1. If the variable has no parents, table_data MAY be
152+
1. If the variable has no parents, table MAY be
129153
a single number (float), representing P(X = True).
130154
131-
2. If the variable has one parent, table_data MAY be
155+
2. If the variable has one parent, table MAY be
132156
a dictionary containing items of the form v: p,
133157
where p is P(X = True | parent = v).
134158
135-
3. If the variable has n parents, n > 1, table_data MUST be
159+
3. If the variable has n parents, n > 1, table MUST be
136160
a dictionary containing items (v1, ..., vn): p,
137161
where p is P(P = True | parent1 = v1, ..., parentn = vn).
138162
@@ -144,24 +168,21 @@ def __init__(self, table_data):
144168
>>> cpt = BoolCpt({(T, T): 0.2, (T, F): 0.3, (F, T): 0.5, (F, F): 0.7})
145169
"""
146170
# A little work here makes looking up values MUCH simpler
147-
# later on. We transform table_data into the standard form
171+
# later on. We transform table into the standard form
148172
# of a dictionary {(value, ...): number, ...} even if
149173
# the tuple has just 0 or 1 value.
150-
if type(table_data) == float: # no parents, 0-tuple
151-
self.table_data = {(): table_data}
152-
elif type(table_data) == dict:
153-
keys = table_data.keys()
154-
if type(keys[0]) == bool: # one parent, 1-tuple
155-
d = {}
156-
for k in keys:
157-
d[(k,)] = table_data[k]
158-
self.table_data = d
159-
elif type(keys[0]) == tuple: # normal case, n-tuple
160-
self.table_data = table_data
174+
if isinstance(table, (float, int)): # no parents, 0-tuple
175+
self.table = {(): table}
176+
elif isinstance(table, dict):
177+
key = table.keys()[0] if table else None
178+
if isinstance(key, bool): # one parent, 1-tuple
179+
self.table = dict(((k,), v) for k, v in table.items())
180+
elif isinstance(key, tuple): # normal case, n-tuple
181+
self.table = table
161182
else:
162-
raise Exception("wrong key type: %s" % table_data)
183+
raise Exception("wrong key type: %s" % table)
163184
else:
164-
raise Exception("wrong table_data type: %s" % table_data)
185+
raise Exception("wrong table type: %s" % table)
165186

166187
def p(self, value, parent_vars, event):
167188
"""Return the conditional probability P(value | parent_vars =
@@ -210,7 +231,7 @@ def p_values(self, xvalue, parent_values):
210231
>>> cpt.p_values(F, (F, F))
211232
0.38
212233
"""
213-
ptrue = self.table_data[parent_values] # True or False
234+
ptrue = self.table[parent_values] # True or False
214235
if xvalue:
215236
return ptrue
216237
else:
@@ -243,34 +264,9 @@ def event_values(event, vars):
243264
"""
244265
if isinstance(event, tuple) and len(event) == len(vars):
245266
return event
246-
return tuple([event[parent] for parent in vars])
247-
248-
249-
#______________________________________________________________________________
250-
251-
def enumerate_joint_ask(X, e, P):
252-
"""Return a probability distribution over the values of the variable X,
253-
given the {var:val} observations e, in the JointProbDist P.
254-
Works for Boolean variables only. [Fig. 13.4].
255-
256-
X is a string (variable name).
257-
e is a dictionary of variable-name value pairs.
258-
P is an instance of JointProbDist."""
259-
260-
Q = ProbDist(X) # probability distribution for X, initially empty
261-
Y = [v for v in P.variables if v != X and v not in e] # hidden vars.
262-
for xi in P.values(X):
263-
ext = extend(e, X, xi) # copies e and adds X: xi
264-
Q[xi] = enumerate_joint(Y, ext, P)
265-
return Q.normalize()
267+
else:
268+
return tuple([event[var] for var in vars])
266269

267-
def enumerate_joint(vars, values, P):
268-
"As in Fig 13.4, except x and e are already incorporated in values."
269-
if not vars:
270-
return P[values]
271-
Y = vars[0]; rest = vars[1:]
272-
return sum([enumerate_joint(rest, extend(values, Y, y), P)
273-
for y in P.values(Y)])
274270

275271
#______________________________________________________________________________
276272

@@ -290,7 +286,7 @@ def observe(self, var, val):
290286
self.evidence[var] = val
291287

292288
def variable_node(self, var):
293-
"""Returns the node for the variable named var.
289+
"""Return the node for the variable named var.
294290
>>> burglary.variable_node('Burglary').variable
295291
'Burglary'"""
296292
for n in self.nodes:
@@ -299,7 +295,7 @@ def variable_node(self, var):
299295
raise Exception("No such variable: %s" % var)
300296

301297
def variables(self):
302-
"""Returns the list of names of the variables.
298+
"""Return the list of names of the variables.
303299
>>> burglary.variables()
304300
['Burglary', 'Earthquake', 'Alarm', 'JohnCalls', 'MaryCalls']"""
305301
return [n.variable for n in self.nodes]
@@ -332,7 +328,7 @@ def __init__(self, variable, parents, cpt):
332328

333329
#______________________________________________________________________________
334330

335-
def enumeration_ask (X, e, bn):
331+
def enumeration_ask(X, e, bn):
336332
"""Returns a distribution of X given e from bayes net bn. [Fig. 14.9]
337333
338334
X is a string (variable name).
@@ -354,7 +350,7 @@ def enumeration_ask (X, e, bn):
354350
# may be unspecified.
355351
return Q.normalize()
356352

357-
def enumerate_all (vars, e, bn):
353+
def enumerate_all(vars, e, bn):
358354
"""Returns the probability that X = xi given e.
359355
360356
vars is a list of variables, the parents of X in bn.
@@ -365,24 +361,22 @@ def enumerate_all (vars, e, bn):
365361
if vars == []:
366362
return 1.0
367363
else:
368-
Y = vars[0]
369-
rest = vars[1:]
364+
Y, rest = vars[0], vars[1:]
370365

371366
Ynode = bn.variable_node(Y)
372367
parents = Ynode.parents
373368
cpt = Ynode.cpt
374369

375-
if e.has_key(Y):
370+
if Y in e:
376371
y = e[Y]
377372
cp = cpt.p(y, parents, e) # P(y | parents(Y))
378-
result = cp * enumerate_all(rest, e, bn)
373+
return cp * enumerate_all(rest, e, bn)
379374
else:
380375
result = 0
381376
for y in bn.variable_values(Y):
382377
cp = cpt.p(y, parents, e) # P(y | parents(Y))
383378
result += cp * enumerate_all(rest, extend(e, Y, y), bn)
384-
385-
return result
379+
return result
386380

387381
#______________________________________________________________________________
388382

@@ -440,7 +434,7 @@ def prior_sample(bn):
440434

441435
#_______________________________________________________________________________
442436

443-
def rejection_sampling (X, e, bn, N):
437+
def rejection_sampling(X, e, bn, N):
444438
"""Estimates probability distribution of X given evidence e
445439
in BayesNet bn, using N samples. [Fig. 14.13]
446440
@@ -473,7 +467,7 @@ def rejection_sampling (X, e, bn, N):
473467

474468
return ProbDist(X, counts)
475469

476-
def consistent_with (sample, evidence):
470+
def consistent_with(sample, evidence):
477471
"""Returns True if sample is consistent with evidence, False otherwise.
478472
479473
sample is a dictionary of variable-name: value pairs.
@@ -498,7 +492,7 @@ def consistent_with (sample, evidence):
498492

499493
#_______________________________________________________________________________
500494

501-
def likelihood_weighting (X, e, bn, N):
495+
def likelihood_weighting(X, e, bn, N):
502496
"""Returns an estimate of P(X | e). [Fig. 14.14]
503497
504498
Arguments:
@@ -527,7 +521,7 @@ def likelihood_weighting (X, e, bn, N):
527521

528522
return ProbDist(X, weights)
529523

530-
def weighted_sample (bn, e):
524+
def weighted_sample(bn, e):
531525
"""Returns an event (a sample) and a weight."""
532526

533527
event = {} # boldface x in Fig. 14.14

0 commit comments

Comments
 (0)