-
Notifications
You must be signed in to change notification settings - Fork 0
/
likelyhood.py
355 lines (308 loc) · 8.82 KB
/
likelyhood.py
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
from __future__ import division
import likelyhood3 as lh3
import math as math
#epsilonZ = 0.01
#epsilonX = 0.01
def allChildrenAreLeaves(tree):
if (tree.is_leaf()):
return False
for node in tree.children:
if not (node.is_leaf()):
return False
return True
def calcProbForLeaf(tree,X):
if (X[tree.name]):
return (tree.dist * tree.Px), True # + ((1-tree.dist) * tree.Ex)), True
else:
return ((1-tree.dist) + (tree.dist*(1-tree.Px))), False
#print "leaf: ",tree.name, ret, observed
def calcProbForDataPoint(tree,X): # X is a datapoint
""" Should probably use reverse order traversal for this. The use dynamic
programming to speed up process. """
ret = 1
observed = False
if(tree.is_leaf()):
return calcProbForLeaf(tree,X)
for node in tree.children:
tmp, tmpObs = calcProbForDataPoint(node,X)
ret *= tmp
observed = observed or tmpObs
if (X[tree.name]):
ret *= tree.dist*tree.Px
else:
if (observed):
ret *= tree.dist*(1-tree.Px)
else:
ret *= (tree.dist*(1-tree.Px))
ret += (1-tree.dist) # Change made here
#print "inner node: ", tree.name, ret, observed
return ret, observed
def calcProbForLeaf2(tree,X):
if (X[tree.name]):
#print tree.dist, tree.Ex
return ((tree.dist * tree.Px) + ((1-tree.dist) * tree.Ex)), True
else:
return ((1-tree.dist) + (tree.dist*(1-tree.Px))), False
#print "leaf: ",tree.name, ret, observed
def calcProbForDataPoint2(tree,X): # X is a datapoint
""" Should probably use reverse order traversal for this. The use dynamic
programming to speed up process. """
ret = 1
observed = False
if(tree.is_leaf()):
return calcProbForLeaf2(tree,X)
for node in tree.children:
tmp, tmpObs = calcProbForDataPoint2(node,X)
ret *= tmp
observed = observed or tmpObs
if (X[tree.name]):
ret *= (tree.dist*tree.Px)+((1-tree.dist) * tree.Ex)
else:
if (observed):
ret *= tree.dist*(1-tree.Px)
else:
ret *= (tree.dist*(1-tree.Px))
ret += (1-tree.dist) # Change made here
#print "inner node: ", tree.name, ret, observed
return ret, observed
def calcProbForLeaf3(tree,X,ret = 1, observed = False):
if (X[tree.name]):
#print tree.dist, tree.Ex
ret *= ((tree.dist * tree.Px) + ((1-tree.dist) * tree.Ex))
observed = True
else:
if (observed):
ret *= tree.dist*(1-tree.Px)
else:
ret *= (tree.dist*(1-tree.Px))
ret += (1-tree.dist) # Change made here
#print "leaf: ",tree.name, ret, observed
return ret, observed
def calcProbForDataPoint3(tree,X): # X is a datapoint
""" Should probably use reverse order traversal for this. The use dynamic
programming to speed up process. """
ret = 1
observed = False
if(tree.is_leaf()):
return calcProbForLeaf3(tree,X)
#print "Tree: ",tree
for node in tree.children:
#print "Node: ",node
tmp, tmpObs = calcProbForDataPoint3(node,X)
ret *= tmp
observed = observed or tmpObs
return calcProbForLeaf3(tree,X,ret, observed)
def likelyhoodOfX(tree,X):
tmp, observed = calcProbForDataPoint(tree,X)
return tmp
def likelyhoodOfX2(tree,X):
tmp, observed = calcProbForDataPoint2(tree,X)
return tmp
def likelyhoodOfX3(tree,X):
tmp, observed = calcProbForDataPoint3(tree,X)
return tmp
def logLikelyhood(tree,data):
ret = 0
for i in range(len(data)):
ret += math.log(lh3.calcProbForX(tree,data[i]))
return ret
def calcProbForLeafAndZ(tree,X,ZuName,Zvalue):
ret = 0
observed = False
if (X[tree.name]): # If observed
if (tree.name == ZuName):
if(Zvalue):
ret = tree.dist * tree.Px
else:
ret = tree.Ex # constant tree.dist?
else:
ret = tree.dist * tree.Px
observed = True
else:
if (tree.name == ZuName):
if(Zvalue):
ret = tree.dist* (1-tree.Px)
#observed = True # Check
else:
ret = 1-tree.dist
else:
ret = ((1-tree.dist) + (tree.dist*(1-tree.Px)))
#print tree, observed, " is this likley ", ret
return ret, observed
def calcProbForDataPointAndZ(tree,X,ZuName,Zvalue):
ret = 1
observed = False
if(tree.is_leaf()):
return calcProbForLeafAndZ(tree,X,ZuName,Zvalue)
for node in tree.children:
tmp, tmpObs = calcProbForDataPointAndZ(node,X,ZuName,Zvalue)
ret *= tmp
observed = observed or tmpObs
if X[tree.name]:
if (tree.name == ZuName and Zvalue):
ret *= tree.dist * tree.Px
elif (tree.name == ZuName and not Zvalue):
ret *= tree.Ex # Check
else:
ret *= tree.dist * tree.Px
else:
if (observed):
if (tree.name == ZuName and Zvalue):
ret *= tree.dist*(1-tree.Px)
elif (tree.name == ZuName and not Zvalue):
ret *= tree.Ex # Check ASK JENS
else:
ret *= tree.dist*(1-tree.Px)
else:
if (tree.name == ZuName and Zvalue):
ret *= tree.dist*(1-tree.Px)
elif (tree.name == ZuName and not Zvalue):
ret = 1-tree.dist # Change made for test 3 ret *= 1-tree.dist
else:
ret *= tree.dist*(1-tree.Px)
ret += 1-tree.dist
#if tree.name == "[M91]":
# print "S5: ",ret
if (ret == 0):
raw_input("ERROR")
return ret, observed
def calcProbForLeafZandZp(tree,X,Zu,Zp,Zvalue,Zpvalue):
Zswitch = False
ret = 0
#global epsilonZ
#global epsilonX
observed = False
if (X[tree.name]): # If observed
#print tree.name, Zu, Zvalue
if (tree.name == Zu):
if(tree.up.name == Zp):
if(Zvalue and not Zpvalue):
ret = tree.Ez
elif(Zvalue and Zpvalue):
ret = tree.dist * tree.Px
elif(not Zvalue and Zpvalue):
ret = tree.Ex
elif(not Zvalue and not Zpvalue):
ret = tree.Ex
else:
raw_input("ERROR")
else:
ret *= tree.dist * tree.Px
observed = True
else:
if (tree.name == Zu):
if(tree.up.name == Zp):
if(Zvalue and not Zpvalue):
ret = 1-tree.Px
observed = True
elif(Zvalue and Zpvalue):
ret = 1-tree.Px
observed = True
elif(not Zvalue and Zpvalue):
ret = 1-tree.dist
elif(not Zvalue and not Zpvalue):
ret = 1-tree.Ez # Check
else:
raw_input("ERROR")
else:
ret *= ((1-tree.dist) + (tree.dist*(1-tree.Px)))
#print tree, observed, " is this likley ", ret
return ret, observed
def calcProbForDataPointZandZp(tree,X,Zu,Zp,Zvalue,Zpvalue):
ret = 1
observed = False
if(tree.is_leaf()):
return calcProbForLeafZandZp(tree,X,Zu,Zvalue,Zp,Zpvalue)
for node in tree.children:
tmp, tmpObs = calcProbForDataPointAndZ(node,X,Zu,Zvalue)
ret *= tmp
observed = observed or tmpObs
if X[tree.name]:
if (tree.name == Zu):
if(tree.up.name == Zp):
if(Zvalue and not Zpvalue):
ret = tree.Ez
elif(Zvalue and Zpvalue):
ret = tree.dist * tree.Px
elif(not Zvalue and Zpvalue):
ret = tree.Ex
elif(not Zvalue and not Zpvalue):
ret = tree.Ex
else:
raw_input("ERROR")
else:
ret *= tree.dist*tree.Px
observed = True
else:
#if (observed):
if (tree.name == Zu):
if(tree.up.name == Zp):
if(Zvalue and not Zpvalue):
ret = 1-tree.Px
observed = True
elif(Zvalue and Zpvalue):
ret = 1-tree.Px
observed = True
elif(not Zvalue and Zpvalue):
ret = 1-tree.dist
elif(not Zvalue and not Zpvalue):
ret = 1-tree.Ez # Check
else:
ret *= tree.dist*(1-tree.Px)
ret += 1-tree.dist # Change made here
"""
else:
if (tree.name == Zu and Zvalue):
ret *= tree.dist*(1-tree.Px)
elif (tree.name == Zu and not Zvalue):
ret *= 1-tree.dist
else:
ret *= ((1-tree.dist) + (tree.dist*(1-tree.Px)))
"""
return ret, observed
def likelyhoodOfXandZ(tree,X,Z,Zvalue): # P[Z(u) = a, X|T]
tmp, observed = calcProbForDataPointAndZ(tree,X,Z,Zvalue)
#print "Obeservation ", X, " in tree ", tree, " is this likley ", tmp
return tmp
def likelyhoodOfXZandZp(tree,X,Zu,Zvalue,Zpvalue): # P[Z(u) = a, Z(p(u)) = b, X|T]
#print tree, Zu, tree.search_nodes(name = Zu) # FAULT, check for correct search function.
Zp = tree.search_nodes(name = Zu)[0].up.name
tmp, observed = calcProbForDataPointZandZp(tree,X,Zu,Zp,Zvalue,Zpvalue)
#print "Obeservation ", X, " in tree ", tree, " is this likley ", tmp
return tmp
def nodeProb(node, X, Zu, ZValue, ret = 1):
if (node.name == Zu):
if (Zvalue): # P[z(node) = 1,X|T]
if X[node.name]:
ret *= node.Pz*node.Px
else:
ret *= node.Pz*(1-node.Px)
else: # P[z(node) = 0,X|T]
if X[node.name]:
ret *= (1-node.Pz)*node.Ex
else:
ret *= 1-node.Pz
elif (node.up.name == Zu):
if(Zvalue):
return #NOT DONE
else:
return #NOT DONE
else:
if X[node.name]:
ret *= node.Pz*node.Px+((1-node.Pz)*node.Ex)
else:
ret *= (1-node.Pz)+node.Pz*(1-node.Px)
return ret, observed
def calcProbwithZ(tree,X,Zu,Zvalue):
ret = 1
observed = False
if(tree.is_leaf()):
return nodeProb(tree,X,ZuName,Zvalue)
for node in tree.children:
tmpRet, tmpObserved = calcProbForDataPointAndZ(node,X,ZuName,Zvalue)
ret *= tmpRet
observed = observed or tmpObserved
return nodeProb(tree,X,Zu,Zvalue,ret)
# -------------------------------------------------------------------------------------------------
# SCRAP
# -------------------------------------------------------------------------------------------------