Inference

After you learned the structure and parameters of the BBN, then you can use Py-BBN to perform inference. First, you have to create an instance of a BBN, and then use that BBN instance to create an instance of a Junction Tree (JT). Py-BBN is opened source may be installed on PyPi. This library already lists Py-BBN as a requirement, and by installing this library, you will also install Py-BBN. The methods that you need to pay attention to are as follows.

  • to_bbn(d, g, p) : uses the domain information d, structure g and parameters p to create a Bayesian Belief Network (BBN)

  • to_join_tree(bbn) : converts a BBN to a Join Tree (JT)

  • posters_to_df(jt) : gets the posterior information as a data frame

 1# Step 3. Get the BBN
 2bbn = to_bbn(d, g, p)
 3
 4# Step 4. Get the Join Tree
 5jt = to_join_tree(bbn)
 6
 7print('bbn')
 8print(bbn)
 9print('-' * 15)
10# 0|d!b|0,1
11# 1|e|0,1
12# 2|d|0,1
13# 3|b|0,1
14# 4|b!a|0,1
15# 5|a|0,1
16# 0->1
17# 2->0
18# 3->0
19# 3->4
20# 4->2
21# 5->4
22
23print('join tree')
24print(jt)
25print('-' * 15)
26# (d!b,e)
27# (b,d,d!b)
28# (b,b!a,d)
29# (a,b,b!a)
30# |(b,d,d!b) -- d,b -- (b,b!a,d)|
31# |(b,b!a,d) -- b,b!a -- (a,b,b!a)|
32# |(d!b,e) -- d!b -- (b,d,d!b)|
33# (b,d,d!b)--|(b,d,d!b) -- d,b -- (b,b!a,d)|--(b,b!a,d)
34# (b,b!a,d)--|(b,b!a,d) -- b,b!a -- (a,b,b!a)|--(a,b,b!a)
35# (d!b,e)--|(d!b,e) -- d!b -- (b,d,d!b)|--(b,d,d!b)
36
37# Get posteriors
38print('posteriors')
39mdf = posteriors_to_df(jt)
40print(mdf)
41
42# should print
43#              0         1
44# name
45# d!b   0.960997  0.039003
46# e     0.740779  0.259221
47# d     0.795200  0.204800
48# b     0.802900  0.197100
49# b!a   0.840211  0.159789
50# a     0.189300  0.810700