-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlikelyhood4.py
244 lines (226 loc) · 6.76 KB
/
likelyhood4.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
"""
def calcProb_X_Leaf(tree,X,ret1 = 1, ret0 = 1):
if (X[tree.name]):
ret1 *= (tree.Pz*tree.Px) + ((1-tree.Pz)*tree.Ex)
ret0 *= (tree.Ez*tree.Px) + ((1-tree.Ez)*tree.Ex)
else:
ret1 *= tree.Pz*(1-tree.Px) + ((1-tree.Pz)*(1-tree.Ex))
ret0 *= tree.Ez*(1-tree.Px) + ((1-tree.Ez)*(1-tree.Ex))
return ret1, ret0
"""
def calcProb_X_Node(tree,X,ret1 = 1, ret0 = 1):
nRet1, nRet0 = 0,0
if (X[tree.name]):
nRet1 = ret1*(tree.Pz*tree.Px) + ret0*((1-tree.Pz)*tree.Ex)
nRet0 = ret1*(tree.Ez*tree.Px) + ret0*((1-tree.Ez)*tree.Ex)
else:
nRet1 = ret1*tree.Pz*(1-tree.Px) + ret0*((1-tree.Pz)*(1-tree.Ex))
nRet0 = ret1*tree.Ez*(1-tree.Px) + ret0*((1-tree.Ez)*(1-tree.Ex))
return nRet1, nRet0
def calcProbForDataPoint(tree,X): # X is a datapoint
ret1, ret0 = 1, 1
if(tree.is_leaf()):
return calcProb_X_Node(tree,X)
for node in tree.children:
tmpRet1, tmpRet0 = calcProbForDataPoint(node,X)
ret1 *= tmpRet1
ret0 *= tmpRet0
return calcProb_X_Node(tree,X,ret1,ret0)
def calcProb_X(tree,X):
#print "="*20," SLASK ","="*20
ret1,ret0 = calcProbForDataPoint(tree,X)
return ret1+ret0
def calcProb_X_Z_Node(tree,X,ZuName,ZuValue,ret1=1,ret0=1):
nRet1, nRet0 = 0,0
if (tree.name == ZuName):
if ZuValue:
if (X[tree.name]):
nRet1 = ret1 * tree.Pz*tree.Px
nRet0 = ret1 * tree.Ez*tree.Px
else:
nRet1 = ret1 * tree.Pz*(1-tree.Px)
nRet0 = ret1 * tree.Ez*(1-tree.Px)
else:
if (X[tree.name]):
nRet1 = ret0 * (1-tree.Pz)*tree.Ex
nRet0 = ret0 * (1-tree.Ez)*tree.Ex
else:
nRet1 = ret0 * (1-tree.Pz)*(1-tree.Ex)
nRet0 = ret0 * (1-tree.Ez)*(1-tree.Ex)
else:
return calcProb_X_Node(tree,X,ret1,ret0)
return nRet1, nRet0
def calcProb_X_Z_Help(tree,X,ZuName,ZuValue):
ret1, ret0 = 1, 1
if(tree.is_leaf()):
return calcProb_X_Z_Node(tree, X, ZuName, ZuValue)
for node in tree.children:
tmpRet1, tmpRet0 = calcProb_X_Z_Help(node, X, ZuName, ZuValue)
ret1 *= tmpRet1
ret0 *= tmpRet0
return calcProb_X_Z_Node(tree, X, ZuName, ZuValue, ret1, ret0)
def calcProb_X_Z(tree,X,ZuName,ZuValue):
ret1,ret0 = calcProb_X_Z_Help(tree,X,ZuName,ZuValue)
return ret1+ret0
"""
def calcProbNode_X_Z_Zp(tree,X,ZuName,ZuValue,ZuPName,ZuPValue,ret1=1,ret0=1):
if (tree.name == ZuPName):
if ZuPValue:
if (X[tree.name]):
nRet1 = ret1 * tree.Pz*tree.Px
nRet0 = ret1 * tree.Ez*tree.Px
else:
nRet1 = ret1 * tree.Pz*(1-tree.Px)
nRet0 = ret1 * tree.Ez*(1-tree.Px)
else:
if (X[tree.name]):
nRet1 = ret0 * (1-tree.Pz)*tree.Ex
nRet0 = ret0 * (1-tree.Ez)*tree.Ex
else:
nRet1 = ret0 * (1-tree.Pz)*(1-tree.Ex)
nRet0 = ret0 * (1-tree.Ez)*(1-tree.Ex)
elif (tree.name == ZuName):
if ZuValue:
if (X[tree.name]):
nRet1 = ret1 * tree.Pz*tree.Px
nRet0 = ret1 * tree.Ez*tree.Px
else:
nRet1 = ret1 * tree.Pz*(1-tree.Px)
nRet0 = ret1 * tree.Ez*(1-tree.Px)
else:
if (X[tree.name]):
nRet1 = ret0 * (1-tree.Pz)*tree.Ex
nRet0 = ret0 * (1-tree.Ez)*tree.Ex
else:
nRet1 = ret0 * (1-tree.Pz)*(1-tree.Ex)
nRet0 = ret0 * (1-tree.Ez)*(1-tree.Ex)
else:
return calcProb_X_Node(tree,X,ret1,ret0)
return nRet1, nRet0
"""
def calcProbNode_X_Z_Zp(tree,X,ZuName,ZuValue,ZuPName,ZuPValue,ret1=1,ret0=1):
nRet1, nRet0 = 0,0
if not (tree.is_root()): # Om tree inte r root
if (tree.up.name == ZuPName and tree.name == ZuName):
if ZuPValue:
if ZuValue:
if (X[tree.name]):
nRet1 = ret1 * tree.Pz*tree.Px
#nRet0 = ret1 * tree.Ez*tree.Px
else:
nRet1 = ret1 * tree.Pz*(1-tree.Px)
#nRet0 = ret1 * tree.Ez*(1-tree.Px)
else:
if (X[tree.name]):
nRet1 = ret0 * (1-tree.Pz)*tree.Ex
#nRet0 = ret0 * (1-tree.Ez)*tree.Ex
else:
nRet1 = ret0 * (1-tree.Pz)*(1-tree.Ex)
#nRet0 = ret0 * (1-tree.Ez)*(1-tree.Ex)
else:
if ZuValue:
if (X[tree.name]):
#nRet1 = ret1 * tree.Pz*tree.Px
nRet0 = ret1 * tree.Ez*tree.Px
else:
#nRet1 = ret1 * tree.Pz*(1-tree.Px)
nRet0 = ret1 * tree.Ez*(1-tree.Px)
else:
if (X[tree.name]):
#nRet1 = ret0 * (1-tree.Pz)*tree.Ex
nRet0 = ret0 * (1-tree.Ez)*tree.Ex
else:
#nRet1 = ret0 * (1-tree.Pz)*(1-tree.Ex)
nRet0 = ret0 * (1-tree.Ez)*(1-tree.Ex)
elif(tree.name == ZuPName):
return calcProb_X_Z_Node(tree,X,ZuPName,ZuPValue,ret1,ret0)
elif(tree.name == ZuName):
return calcProb_X_Z_Node(tree,X,ZuName,ZuValue,ret1,ret0)
else:
return calcProb_X_Node(tree,X,ret1,ret0)
elif(tree.name == ZuPName):
return calcProb_X_Z_Node(tree,X,ZuPName,ZuPValue,ret1,ret0)
elif(tree.name == ZuName):
return calcProb_X_Z_Node(tree,X,ZuName,ZuValue,ret1,ret0)
else:
return calcProb_X_Node(tree,X,ret1,ret0)
#print "HEJSAN"
#raw_input("continue")
return nRet1, nRet0
"""
def calcProbNode_X_Z_Zp(tree,X,ZuName,ZuValue,ZuPName,ZuPValue,ret1=1,ret0=1):
#print tree.name, tree.up.name
eB = True
print not tree.is_root()
if not tree.is_root():
print "Hej"
print tree.up.name, ZuPName
if (tree.up.name == ZuPName):
print "Here"
if (tree.name == ZuName):
print "There"
if ZuPValue:
if ZuValue:
if (X[tree.name]):
if(eB): print "E1"
ret1 *= tree.Pz*tree.Px
ret0 *= 0
#ret0 *= tree.Ez*tree.Px
else:
if(eB): print "E2"
ret1 *= tree.Pz*(1-tree.Px)
ret0 *= 0
#ret0 *= tree.Ez*(1-tree.Px)
else:
if (X[tree.name]):
if(eB): print "E3"
ret1 *= (1-tree.Pz)*tree.Ex
ret0 *= 0
#ret0 *= (1-tree.Ez)*tree.Ex
else:
if(eB): print "E4"
ret1 *= (1-tree.Pz)*(1-tree.Ex)
ret0 *= 0
#ret0 *= (1-tree.Ez)*(1-tree.Ex)
else:
if ZuValue:
if (X[tree.name]):
#ret1 *= tree.Pz*tree.Px
if(eB): print "E5"
ret1 *= 0
ret0 *= tree.Ez*tree.Px
else:
#ret1 *= tree.Pz*(1-tree.Px)
if(eB): print "E6"
ret1 *= 0
ret0 *= tree.Ez*(1-tree.Px)
else:
if (X[tree.name]):
#ret1 *= (1-tree.Pz)*tree.Ex
if(eB): print "E7"
ret1 *= 0
ret0 *= (1-tree.Ez)*tree.Ex
else:
#ret1 *= (1-tree.Pz)*(1-tree.Ex)
if(eB): print "E8"
ret1 *= 0
ret0 *= (1-tree.Ez)*(1-tree.Ex)
else:
raw_input("ERROR")
else:
return calcProbForNode(tree,X,ret1,ret0)
print ret1,ret0
return ret1, ret0
"""
def calcProb_X_Z_Zp_Help(tree,X,ZuName,ZuValue,ZuPName,ZuPValue):
ret1, ret0 = 1, 1
if(tree.is_leaf()):
return calcProbNode_X_Z_Zp(tree, X, ZuName, ZuValue,ZuPName,ZuPValue)
for node in tree.children:
tmpRet1, tmpRet0 = calcProb_X_Z_Zp_Help(node, X, ZuName, ZuValue,ZuPName,ZuPValue)
ret1 *= tmpRet1
ret0 *= tmpRet0
return calcProbNode_X_Z_Zp(tree, X, ZuName, ZuValue,ZuPName,ZuPValue, ret1, ret0)
def calcProb_X_Z_Zp(tree,X,ZuName,ZuValue,ZuPName,ZuPValue):
ret1,ret0 = calcProb_X_Z_Zp_Help(tree,X,ZuName,ZuValue,ZuPName,ZuPValue)
return ret1+ret0