Belief propagation using pgmpy lib - algorithm understanding

618 views Asked by At

I am now starting to use pgmpy lib for probabailistic graphical model implementation. The probability that I get using this lib differs from the one I get manually (e.g. using SamIam). Here is a screenshot of the very small graphical model made in SamIam to check the concept idea: Student example

The code I have using pgmpy.

from pgmpy.models import BayesianModel
from pgmpy.factors import TabularCPD
from pgmpy.inference import BeliefPropagation

student_model = BayesianModel([('D', 'G'), ('I', 'G')])

difficulty_cpd = TabularCPD(variable='D', variable_card=2, values=[[0.6, 0.4]])
intel_cpd = TabularCPD(variable='I', variable_card=2, values=[[0.7, 0.3]])
grade_cpd = TabularCPD(variable='G',variable_card=3, values=[[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.02, 0.2]], evidence=['I', 'D'], evidence_card=[2, 2])

student_model.add_cpds(grade_cpd, difficulty_cpd, intel_cpd)

print (student_model.nodes())
print (student_model.get_cpds('D'))
print (student_model.get_cpds('I'))
print (student_model.get_cpds('G'))

belief_propagation = BeliefPropagation(student_model)
res = belief_propagation.query(variables=["G"])
print (res['G'])

I get following results code output

+-----+----------+
| G   |   phi(G) |
|-----+----------|
| G_0 |   0.4470 |
| G_1 |   0.2714 |
| G_2 |   0.2816 |
+-----+----------+

The values of phi(G) are not the same as in Samiam.

According the algorithm used in Samiam we should get for G_0:

P(G_0) = P(G_0|I_0,D_0) + P(G_0|I_0,D_1) + P(G_0|I_1,D_0) + P(G_0|I_1,D_1)
P(G_0) = 0.3*0.7*0.6 + 0.05*0.7*0.4 + 0.9*0.3*0.6 + 0.5*0.3*0.4 = 0.3620

Could someone please give me a tip on how these phi(G) values have been counted (which algorithm is really used), and how I could get the same values as in SamIam.

0

There are 0 answers