-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing.py
69 lines (58 loc) · 1.8 KB
/
testing.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
from BayesNetCreacion import BayesNetCreacion, Node
#Burglar Alarm System example
#Reference: https://www.youtube.com/watch?v=hEZjPZ-Ze0A&t=35s
#Creating nodes and network
burglary = Node('B')
earthquake = Node('E')
alarm = Node('A')
johncalls = Node('J')
marycalls = Node('M')
alarm.set_parents([str(burglary),str(earthquake)])
johncalls.set_parents([str(alarm)])
marycalls.set_parents([str(alarm)])
bnc = BayesNetCreacion()
bnc.add_node(burglary)
bnc.add_node(earthquake)
bnc.add_node(alarm)
bnc.add_node(johncalls)
bnc.add_node(marycalls)
bnc.add_prob({
('B', True): 0.001,
('B', False): 0.999,
('E', True): 0.002,
('E', False): 0.998,
('A', True, 'B', True, 'E', True): 0.95,
('A', True, 'B', False, 'E', True): 0.29,
('A', True, 'B', True, 'E', False): 0.94,
('A', True, 'B', False, 'E', False): 0.001,
('A', False, 'B', True, 'E', True): 0.05,
('A', False, 'B', False, 'E', True): 0.71,
('A', False, 'B', True, 'E', False): 0.06,
('A', False, 'B', False, 'E', False): 0.999,
('J', True, 'A', True): 0.9,
('J', True, 'A', False): 0.05,
('J', False, 'A', True): 0.1,
('J', False, 'A', False): 0.95,
('M', True, 'A', True): 0.7,
('M', True, 'A', False): 0.01,
('M', False, 'A', True): 0.3,
('M', False, 'A', False): 0.99,
})
# print(bnc.get_network())
#Functions
#Probabilistic Inference
evidence = {'J': True, 'M': True}
query = 'B'
result = bnc.probabilistic_inference(query, evidence)
print(result) #0.28417
#Compact check
print(bnc.get_compact()) #P(B), P(E), P(A|B,E), P(J|A), P(M|A)
#Factores
print(bnc.get_factors()) #Outputs Conditional Probability Table
#Definition check
print(bnc.defined_check())
#Enumeration
evidence = {'B': True, 'E':False}
query = 'A'
result = bnc.pre_enum(query, evidence)
print(result) #{True: 0.94, False: 0.06}