-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBayesianNetwork.py
More file actions
380 lines (292 loc) · 13.9 KB
/
BayesianNetwork.py
File metadata and controls
380 lines (292 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'''Classes for variable elimination Routines
Note:
The following three classes have a name field. The name field is not
necessarily unique, and exists primarily for output purposes.
A) class Variable
This class allows one to define Bayes Net variables.
On initialization the variable object can be given a name and a
domain of values. This list of domain values can be added to or
deleted from in support of an incremental specification of the
variable domain.
The variable also has a set and get value method. These set a
value for the variable that can be used by the factor class.
B) class Factor
This class allows one to define a factor specified by a table
of values.
On initialization the variables the factor is over is
specified. This must be a list of variables. This list of
variables cannot be changed once the constraint object is
created.
Once created the factor can be incrementally initialized with a
list of values. To interact with the factor object one first
sets the value of each variable in its scope (using the
variable's set_value method), then one can set or get the value
of the factor (a number) on those fixed values of the variables
in its scope.
Initially, one creates a factor object for every conditional
probability table in the bayes-net. Then one initializes the
factor by iteratively setting the values of all of the factor's
variables and then adding the factor's numeric value using the
add_value method.
C) class BayesianNetwork
This class allows one to put factors and variables together to form a Bayes net.
It serves as a convient place to store all of the factors and variables associated
with a Bayes Net in one place. It also has some utility routines to, e.g,., find
all of the factors a variable is involved in.
You may call min_fill_ordering to calculate the variable elimination when
performing variable elimination
'''
class Variable:
'''Class for defining Bayes Net variables. '''
def __init__(self, name, domain=[]):
'''Create a variable object, specifying its name (a
string). Optionally specify the initial domain.
'''
self.name = name #text name for variable
self.dom = list(domain) #Make a copy of passed domain
self.evidence_index = 0 #evidence value (stored as index into self.dom)
def add_domain_values(self, values):
'''Add domain values to the domain. values should be a list.'''
for val in values: self.dom.append(val)
def value_index(self, value):
'''Domain values need not be numbers, so return the index
in the domain list of a variable value'''
return self.dom.index(value)
def domain_size(self):
'''Return the size of the domain'''
return(len(self.dom))
def domain(self):
'''return the variable domain'''
return(list(self.dom))
def set_evidence(self,val):
'''set this variable's value when it operates as evidence'''
self.evidence_index = self.value_index(val)
def get_evidence(self):
return(self.dom[self.evidence_index])
##These routines are special low-level routines used directly by the
##factor objects
def get_assignment_index(self, assignment_value):
'''return the index of a possible variable assignment'''
return(self.dom.index(assignment_value))
def __repr__(self):
'''string to return when evaluating the object'''
return("{}".format(self.name))
def __str__(self):
'''more elaborate string for printing'''
return("{}, Dom = {}".format(self.name, self.dom))
class AssignmentIterator:
''' class for iterating over possible assignments to a list of variables'''
def __init__(self, scope):
self.scope = scope
self.init = False
def __iter__(self):
self.init = False
return self
def increment(self):
for i in range (0, len(self.scope)):
self.currentindex[i] += 1
if(self.currentindex[i] >= self.scope[i].domain_size()):
if(i == len(self.scope) - 1):
raise StopIteration
self.currentindex[i] = 0
else:
break
def getassignment(self):
assignment = []
for i in range (0, len(self.scope)):
assignment.append(self.scope[i].dom[self.currentindex[i]])
return assignment
def __next__(self):
if(self.init == False):
if(len(self.scope) == 0):
raise StopIteration
self.init = True
self.currentindex = [0] * len(self.scope)
else:
self.increment()
return self.getassignment()
class Factor:
'''Class for defining factors. A factor is a function that is over
an ORDERED sequence of variables called its scope. It maps every
assignment of values to these variables to a number. In a Bayes
Net every CPT is represented as a factor. Pr(A|B,C) for example
will be represented by a factor over the variables (A,B,C). If we
assign A = a, B = b, and C = c, then the factor will map this
assignment, A=a, B=b, C=c, to a number that is equal to Pr(A=a|
B=b, C=c). During variable elimination new factors will be
generated. However, the factors computed during variable
elimination do not necessarily correspond to conditional
probabilities. Nevertheless, they still map assignments of values
to the variables in their scope to numbers.
Note that if the factor's scope is empty it is a constaint factor
that stores only one value. add_values would be passed something
like [[0.25]] to set the factor's single value. The get_value
functions will still work. E.g., get_value([]) will return the
factor's single value. Constant factors may be created when a
factor operation is performed'''
def __init__(self, name, scope):
'''create a Factor object, specify the Factor name (a string)
and its scope (an ORDERED list of variable objects).'''
self.scope = list(scope)
self.name = name
size = 1
for v in scope:
size = size * v.domain_size()
self.values = [0]*size #initialize values to be long list of zeros.
def get_scope(self):
'''returns copy of scope...you can modify this copy without affecting
the factor object'''
return list(self.scope)
def add_values(self, values):
'''This routine can be used to initialize the factor. We pass
it a list of lists. Each sublist is a ORDERED sequence of
values, one for each variable in self.scope followed by a
number that is the factor's value when its variables are
assigned these values. For example, if self.scope = [A, B, C],
and A.domain() = [1,2,3], B.domain() = ['a', 'b'], and
C.domain() = ['heavy', 'light'], then we could pass add_values the
following list of lists
[[1, 'a', 'heavy', 0.25], [1, 'a', 'light', 1.90],
[1, 'b', 'heavy', 0.50], [1, 'b', 'light', 0.80],
[2, 'a', 'heavy', 0.75], [2, 'a', 'light', 0.45],
[2, 'b', 'heavy', 0.99], [2, 'b', 'light', 2.25],
[3, 'a', 'heavy', 0.90], [3, 'a', 'light', 0.111],
[3, 'b', 'heavy', 0.01], [3, 'b', 'light', 0.1]]
This list initializes the factor so that, e.g., its value on
(A=2,B=b,C='light) is 2.25'''
for t in values:
index = 0
for v in self.scope:
index = index * v.domain_size() + v.value_index(t[0])
t = t[1:]
self.values[index] = t[0]
def add_value_at_assignment(self, value, assignment):
'''This function allows adding a value at a specific assignment of the variables. We pass a single
number and a list of values representing an assignment of the variables. It then looks at the assigned values of the variables
in its scope and initializes the factor to have value equal to
number according to the assignment of its variables.
For example, if self.scope = [A, B, C],
and A.domain() = [1,2,3], B.domain() = ['a', 'b'], and
C.domain() = ['heavy', 'light']
then we call
add_value_at_current_assignment(0.33,[1,'a','heavy'])
with the value 0.33, we would have initialized this factor to have
the value 0.33 on the assigments (A=1, B='1', C='heavy')
This has the same effect as the call
add_values([1, 'a', 'heavy', 0.33])
'''
if(assignment == []):
#deal with the constant factor case
self.values = [[value]]
index = 0
for i in range(0, len(self.scope)):
v = self.scope[i]
index = index * v.domain_size() + v.get_assignment_index(assignment[i])
self.values[index] = value
def get_value(self, variable_values):
'''This function is used to retrieve a value from the
factor. We pass it an ordered list of values, one for every
variable in self.scope. It then returns the factor's value on
that set of assignments. For example, if self.scope = [A, B,
C], and A.domain() = [1,2,3], B.domain() = ['a', 'b'], and
C.domain() = ['heavy', 'light'], and we invoke this function
on the list [1, 'b', 'heavy'] we would get a return value
equal to the value of this factor on the assignment (A=1,
B='b', C='light')'''
index = 0
for v in self.scope:
index = index * v.domain_size() + v.value_index(variable_values[0])
variable_values = variable_values[1:]
return self.values[index]
def get_assignment_iterator(self):
'''This function returns an iterator over all possible assignments
of the variables for this factor
Assignments are a list of variable values, such that the i'th
entry is a value from the domain of the i'th variable
for assignment in factor.get_assignment_iterator():
...
'''
return AssignmentIterator(self.scope)
def print_table(self):
'''print the factor's table'''
for assignment in self.get_assignment_iterator():
print("[",end="")
for i in range(0, len(self.scope)):
v = self.scope[i]
print("{} = {}, ".format(v.name, assignment[i]), end="")
print("] = {}".format(self.get_value(assignment)))
def __repr__(self):
return("{}({})".format(self.name, list(map(lambda x: x.name, self.scope))))
class BayesianNetwork:
'''Class for defining a Bayes Net.
This class is simple, it just is a wrapper for a list of factors. And it also
keeps track of all variables in the scopes of these factors'''
def __init__(self, name, Vars, Factors):
self.name = name
self.Variables = list(Vars)
self.Factors = list(Factors)
for f in self.Factors:
for v in f.get_scope():
if not v in self.Variables:
print("Bayes net initialization error")
print("Factor scope {} has variable {} that", end='')
print(" does not appear in list of variables {}.".format(list(map(lambda x: x.name, f.get_scope())), v.name, list(map(lambda x: x.name, Vars))))
def factors(self):
return list(self.Factors)
def variables(self):
return list(self.Variables)
def min_fill_ordering(Factors, QueryVar):
'''Compute a min fill ordering given a list of factors. Return a list
of variables from the scopes of the factors in Factors. The QueryVar is
NOT part of the returned ordering'''
scopes = []
for f in Factors:
scopes.append(list(f.get_scope()))
Vars = []
for s in scopes:
for v in s:
if not v in Vars and v != QueryVar:
Vars.append(v)
ordering = []
while Vars:
(var,new_scope) = min_fill_var(scopes,Vars)
ordering.append(var)
if var in Vars:
Vars.remove(var)
scopes = remove_var(var, new_scope, scopes)
return ordering
def min_fill_var(scopes, Vars):
'''Given a set of scopes (lists of lists of variables) compute and
return the variable with minimum fill in. That the variable that
generates a factor of smallest scope when eliminated from the set
of scopes. Also return the new scope generated from eliminating
that variable.'''
minv = Vars[0]
(minfill,min_new_scope) = compute_fill(scopes,Vars[0])
for v in Vars[1:]:
(fill, new_scope) = compute_fill(scopes, v)
if fill < minfill:
minv = v
minfill = fill
min_new_scope = new_scope
return (minv, min_new_scope)
def compute_fill(scopes, var):
'''Return the fill in scope generated by eliminating var from
scopes along with the size of this new scope'''
union = []
for s in scopes:
if var in s:
for v in s:
if not v in union:
union.append(v)
if var in union: union.remove(var)
return (len(union), union)
def remove_var(var, new_scope, scopes):
'''Return the new set of scopes that arise from eliminating var
from scopes'''
new_scopes = []
for s in scopes:
if not var in s:
new_scopes.append(s)
new_scopes.append(new_scope)
return new_scopes