-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
53 lines (41 loc) · 2.5 KB
/
main.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
from Network import *
# This file contains boilerplate code to explain how to use the inference algorithms implemented in order to run custom
# queries on the ALARM Bayesian Network.
# The CPDs are stored in Pandas DataFrames. Here is an example for LVEDVOLUME node.
cpd = bn.cpd['LVEDVOLUME']
print(cpd)
# With these DataFrames, one for each node, all the knowledge is encoded in the Python Object instantiated with bn.
# We can obtain the probability for any node in the following methods:
p = bn.get_probability('LVEDVOLUME')
print(p)
# This statement can be modified to perform more meaningful analysis.
p = bn.get_probability('LVEDVOLUME', evidence={'HYPOVOLEMIA': 'TRUE'})
print(p)
p = bn.get_probability('LVEDVOLUME', evidence={'HYPOVOLEMIA': 'TRUE', 'LVFAILURE': 'TRUE'})
print(p)
p = bn.get_probability('LVEDVOLUME', evidence={'HYPOVOLEMIA': 'TRUE', 'LVFAILURE': 'TRUE'}, value='HIGH')
print(p)
# Any node can be sampled using the following step. The node needs to be sampled in descending order
# i.e. parents need to be sampled first. This is achieved using recursion. The parameter r is provided for testability
# to be able to reproduce results as sampling is a uniformly random step.
sample = bn.sample('LVEDVOLUME', given={'HYPOVOLEMIA': 'TRUE', 'LVFAILURE': 'TRUE'}, r=0.011)
print(sample)
# With the help of the above method, a complete sample can be generated from be generated using a simple statement.
sample = bn.compute_sample()
print(sample)
# Another method of generating a completely random sample, without any consideration of the CPDs is shown below.
random_sample = bn.random_sample()
print(random_sample)
# Finally, using all the above-mentioned methods, we are able to perform some approximate sampling-based inference
# algorithms as described below.
# Firstly, the Rejection Sampling Inference Algorithm.
prob = bn.rejection_sample(predict={'HRSAT': 'HIGH'}, given={'PCWP': 'NORMAL', 'VENTLUNG': 'LOW'}, n=1000)
print(prob)
# Secondly, the Likelihood Weighting Sampling Inference Algorithm
prob = bn.likelihood_weighting(predict={'HRSAT': 'HIGH'}, given={'PCWP': 'NORMAL', 'VENTLUNG': 'LOW'}, n=1000)
print(prob)
# Thirdly, the MCMC Gibbs Sampling Inference Algorithm
prob = bn.gibbs_sampling(predict={'HRSAT': 'HIGH'}, given={'PCWP': 'NORMAL', 'VENTLUNG': 'LOW'}, n=10000, skip=50)
print(prob)
# Lastly, this statement returns the likely value of the given node with its posterior probability.
print(bn.predict(nodes=['HRSAT'], algorithm='gibbs', given={'PCWP': 'NORMAL', 'VENTLUNG': 'LOW'}, n=10000, skip=50))