In [1]:
from pgmpy.models import BayesianModel
from pgmpy.factors import TabularCPD
In [2]:
student = BayesianModel()
# instantiates a new Bayesian Model called 'student'
student.add_nodes_from(['diff', 'intel', 'grade'])
# adds nodes labelled 'diff', 'intel', 'grade' to student
student.add_edges_from([('diff', 'grade'), ('intel', 'grade')])
# adds directed edges from 'diff' to 'grade' and 'intel' to 'grade'
diff cpd:
diff: | |
---|---|
easy | 0.2 |
hard | 0.8 |
In [3]:
diff_cpd = TabularCPD('diff', 2, [[0.2], [0.8]])
intel cpd:
intel: | |
---|---|
dumb | 0.5 |
avg | 0.3 |
smart | 0.2 |
In [4]:
intel_cpd = TabularCPD('intel', 3, [[0.5], [0.3], [0.2]])
grade cpd:
diff: | easy | easy | easy | hard | hard | hard |
---|---|---|---|---|---|---|
intel: | dumb | avg | smart | dumb | avg | smart |
gradeA | 0.1 | 0.1 | 0.1 | 0.1 | 0.1 | 0.1 |
gradeB | 0.1 | 0.1 | 0.1 | 0.1 | 0.1 | 0.1 |
gradeC | 0.8 | 0.8 | 0.8 | 0.8 | 0.8 | 0.8 |
In [5]:
grade_cpd = TabularCPD('grade', 3,
[[0.1,0.1,0.1,0.1,0.1,0.1],
[0.1,0.1,0.1,0.1,0.1,0.1],
[0.8,0.8,0.8,0.8,0.8,0.8]],
evidence=['intel', 'diff'],
evidence_card=[3, 2])
In [6]:
student.add_cpds(diff_cpd, intel_cpd, grade_cpd)
# Finding active trail
student.active_trail_nodes('diff')
{'diff', 'grade'}
# Finding active trail with observation
student.active_trail_nodes('diff', observed='grade')
{'diff', 'intel'}
Out[6]: