@@ -121,17 +121,17 @@ def __repr__(self):
121121 return "P(%s)" % self .variables
122122
123123
124- def event_values (event , vars ):
125- """Return a tuple of the values of variables vars in event.
124+ def event_values (event , variables ):
125+ """Return a tuple of the values of variables variables in event.
126126 >>> event_values ({'A': 10, 'B': 9, 'C': 8}, ['C', 'A'])
127127 (8, 10)
128128 >>> event_values ((1, 2), ['C', 'A'])
129129 (1, 2)
130130 """
131- if isinstance (event , tuple ) and len (event ) == len (vars ):
131+ if isinstance (event , tuple ) and len (event ) == len (variables ):
132132 return event
133133 else :
134- return tuple ([event [var ] for var in vars ])
134+ return tuple ([event [var ] for var in variables ])
135135
136136# ______________________________________________________________________________
137137
@@ -146,18 +146,18 @@ def enumerate_joint_ask(X, e, P):
146146 """
147147 assert X not in e , "Query variable must be distinct from evidence"
148148 Q = ProbDist (X ) # probability distribution for X, initially empty
149- Y = [v for v in P .variables if v != X and v not in e ] # hidden vars .
149+ Y = [v for v in P .variables if v != X and v not in e ] # hidden variables .
150150 for xi in P .values (X ):
151151 Q [xi ] = enumerate_joint (Y , extend (e , X , xi ), P )
152152 return Q .normalize ()
153153
154154
155- def enumerate_joint (vars , e , P ):
155+ def enumerate_joint (variables , e , P ):
156156 """Return the sum of those entries in P consistent with e,
157- provided vars is P's remaining variables (the ones not in e)."""
158- if not vars :
157+ provided variables is P's remaining variables (the ones not in e)."""
158+ if not variables :
159159 return P [e ]
160- Y , rest = vars [0 ], vars [1 :]
160+ Y , rest = variables [0 ], variables [1 :]
161161 return sum ([enumerate_joint (rest , extend (e , Y , y ), P )
162162 for y in P .values (Y )])
163163
@@ -171,18 +171,18 @@ class BayesNet:
171171 def __init__ (self , node_specs = []):
172172 "nodes must be ordered with parents before children."
173173 self .nodes = []
174- self .vars = []
174+ self .variables = []
175175 for node_spec in node_specs :
176176 self .add (node_spec )
177177
178178 def add (self , node_spec ):
179179 """Add a node to the net. Its parents must already be in the
180180 net, and its variable must not."""
181181 node = BayesNode (* node_spec )
182- assert node .variable not in self .vars
183- assert every (lambda parent : parent in self .vars , node .parents )
182+ assert node .variable not in self .variables
183+ assert every (lambda parent : parent in self .variables , node .parents )
184184 self .nodes .append (node )
185- self .vars .append (node .variable )
185+ self .variables .append (node .variable )
186186 for parent in node .parents :
187187 self .variable_node (parent ).children .append (node )
188188
@@ -268,7 +268,7 @@ def p(self, value, event):
268268
269269 def sample (self , event ):
270270 """Sample from the distribution for this variable conditioned
271- on event's values for parent_vars . That is, return True/False
271+ on event's values for parent_variables . That is, return True/False
272272 at random according with the conditional probability given the
273273 parents."""
274274 return probability (self .p (True , event ))
@@ -301,18 +301,18 @@ def enumeration_ask(X, e, bn):
301301 assert X not in e , "Query variable must be distinct from evidence"
302302 Q = ProbDist (X )
303303 for xi in bn .variable_values (X ):
304- Q [xi ] = enumerate_all (bn .vars , extend (e , X , xi ), bn )
304+ Q [xi ] = enumerate_all (bn .variables , extend (e , X , xi ), bn )
305305 return Q .normalize ()
306306
307307
308- def enumerate_all (vars , e , bn ):
309- """Return the sum of those entries in P(vars | e{others})
308+ def enumerate_all (variables , e , bn ):
309+ """Return the sum of those entries in P(variables | e{others})
310310 consistent with e, where P is the joint distribution represented
311311 by bn, and e{others} means e restricted to bn's other variables
312- (the ones other than vars ). Parents must precede children in vars ."""
313- if not vars :
312+ (the ones other than variables ). Parents must precede children in variables ."""
313+ if not variables :
314314 return 1.0
315- Y , rest = vars [0 ], vars [1 :]
315+ Y , rest = variables [0 ], variables [1 :]
316316 Ynode = bn .variable_node (Y )
317317 if Y in e :
318318 return Ynode .p (e [Y ], e ) * enumerate_all (rest , e , bn )
@@ -330,7 +330,7 @@ def elimination_ask(X, e, bn):
330330 'False: 0.716, True: 0.284'"""
331331 assert X not in e , "Query variable must be distinct from evidence"
332332 factors = []
333- for var in reversed (bn .vars ):
333+ for var in reversed (bn .variables ):
334334 factors .append (make_factor (var , e , bn ))
335335 if is_hidden (var , X , e ):
336336 factors = sum_out (var , factors , bn )
@@ -347,10 +347,10 @@ def make_factor(var, e, bn):
347347 That is, bn's full joint distribution, projected to accord with e,
348348 is the pointwise product of these factors for bn's variables."""
349349 node = bn .variable_node (var )
350- vars = [X for X in [var ] + node .parents if X not in e ]
351- cpt = dict ((event_values (e1 , vars ), node .p (e1 [var ], e1 ))
352- for e1 in all_events (vars , bn , e ))
353- return Factor (vars , cpt )
350+ variables = [X for X in [var ] + node .parents if X not in e ]
351+ cpt = dict ((event_values (e1 , variables ), node .p (e1 [var ], e1 ))
352+ for e1 in all_events (variables , bn , e ))
353+ return Factor (variables , cpt )
354354
355355
356356def pointwise_product (factors , bn ):
@@ -361,7 +361,7 @@ def sum_out(var, factors, bn):
361361 "Eliminate var from all factors by summing over its values."
362362 result , var_factors = [], []
363363 for f in factors :
364- (var_factors if var in f .vars else result ).append (f )
364+ (var_factors if var in f .variables else result ).append (f )
365365 result .append (pointwise_product (var_factors , bn ).sum_out (var , bn ))
366366 return result
367367
@@ -370,43 +370,43 @@ class Factor:
370370
371371 "A factor in a joint distribution."
372372
373- def __init__ (self , vars , cpt ):
374- self .vars = vars
373+ def __init__ (self , variables , cpt ):
374+ self .variables = variables
375375 self .cpt = cpt
376376
377377 def pointwise_product (self , other , bn ):
378378 "Multiply two factors, combining their variables."
379- vars = list (set (self .vars ) | set (other .vars ))
380- cpt = dict ((event_values (e , vars ), self .p (e ) * other .p (e ))
381- for e in all_events (vars , bn , {}))
382- return Factor (vars , cpt )
379+ variables = list (set (self .variables ) | set (other .variables ))
380+ cpt = dict ((event_values (e , variables ), self .p (e ) * other .p (e ))
381+ for e in all_events (variables , bn , {}))
382+ return Factor (variables , cpt )
383383
384384 def sum_out (self , var , bn ):
385385 "Make a factor eliminating var by summing over its values."
386- vars = [X for X in self .vars if X != var ]
387- cpt = dict ((event_values (e , vars ),
386+ variables = [X for X in self .variables if X != var ]
387+ cpt = dict ((event_values (e , variables ),
388388 sum (self .p (extend (e , var , val ))
389389 for val in bn .variable_values (var )))
390- for e in all_events (vars , bn , {}))
391- return Factor (vars , cpt )
390+ for e in all_events (variables , bn , {}))
391+ return Factor (variables , cpt )
392392
393393 def normalize (self ):
394394 "Return my probabilities; must be down to one variable."
395- assert len (self .vars ) == 1
396- return ProbDist (self .vars [0 ],
395+ assert len (self .variables ) == 1
396+ return ProbDist (self .variables [0 ],
397397 dict ((k , v ) for ((k ,), v ) in list (self .cpt .items ())))
398398
399399 def p (self , e ):
400400 "Look up my value tabulated for e."
401- return self .cpt [event_values (e , self .vars )]
401+ return self .cpt [event_values (e , self .variables )]
402402
403403
404- def all_events (vars , bn , e ):
405- "Yield every way of extending e with values for all vars ."
406- if not vars :
404+ def all_events (variables , bn , e ):
405+ "Yield every way of extending e with values for all variables ."
406+ if not variables :
407407 yield e
408408 else :
409- X , rest = vars [0 ], vars [1 :]
409+ X , rest = variables [0 ], variables [1 :]
410410 for e1 in all_events (rest , bn , e ):
411411 for x in bn .variable_values (X ):
412412 yield extend (e1 , X , x )
@@ -505,7 +505,7 @@ def gibbs_ask(X, e, bn, N):
505505 assert X not in e , "Query variable must be distinct from evidence"
506506 counts = dict ((x , 0 )
507507 for x in bn .variable_values (X )) # bold N in Fig. 14.16
508- Z = [var for var in bn .vars if var not in e ]
508+ Z = [var for var in bn .variables if var not in e ]
509509 state = dict (e ) # boldface x in Fig. 14.16
510510 for Zi in Z :
511511 state [Zi ] = random .choice (bn .variable_values (Zi ))
0 commit comments