-
Notifications
You must be signed in to change notification settings - Fork 0
/
BN.py
75 lines (54 loc) · 1.74 KB
/
BN.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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 15 15:51:49 2018
@author: mlopes
"""
class Node():
def __init__(self, prob, parents = []):
self.parents = parents
self.prob = prob
def computeProb(self, evid):
if (self.parents == []):
return [1 - self.prob[0], self.prob[0]]
else:
prob = self.prob
for i in self.parents:
prob = prob[evid[i]]
return [1 - prob, prob]
class BN():
def __init__(self, gra, prob):
self.prob = prob
self.gra = gra
def aux(self, evid, post, value, post_value):
flag = 0
for i in range(0, len(evid)):
if evid[i] == [] :
flag = 1
for j in range(0, 2):
evid_copy = evid.copy()
evid_copy[i] = j
value = self.aux(evid_copy, post, value, post_value)
break
if flag:
return value
else:
evid[post] = post_value
return value + self.computeJointProb(evid)
def computePostProb(self, evid):
post = -1
for i in range(0, len(evid)):
if(evid[i] == -1):
post = i
break
#marginalizar
izero = self.aux( list(evid), post, 0, 0)
ione = self.aux( list(evid), post, 0, 1)
#multiplicar pelo alpha
res = (1/(izero + ione)) * ione
return res
def computeJointProb(self, evid):
result = 1;
#aplicar teorema de Bayes
for i in range(0, len(evid)):
result *= self.prob[i].computeProb(evid)[evid[i]]
return result