Description
E09 Bayesian Network
17341015 Hongzheng Chen
Contents
1 Pomegranate Installation 2
2 Building Bayesian Network 2
3 Tasks 3
3.1 Burglary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Diagnosing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 Codes and Results 6
1 Pomegranate Installation
Under Linux:
1. Install python first (python 2, not python 3).
2. Run sudo apt-get install python-pip to install pip.
3. Run sudo pip install pomegranate to install pomegranate.
Under Windows
You can also run pip install pomegranate if you have installed pip. If you donโt know how to install pip, please click https://jingyan.baidu.com/article/e73e26c0d94e0524adb6a7ff.html.
For more, please click the homepage of Pomegranate – https://github.com/jmschrei/pomegranate for help.
2 Building Bayesian Network
Please refer to Tutorial 4 Bayesian Networks.pdf. I will explain it in class.
3 Tasks
3.1 Burglary
Please code to calculate:
1.
2.
3.
4.
5.
6.
3.2 Diagnosing
Variables and their domais
(1)PatientAge:[โ0-30โ,โ31-65โ,โ65+โ]
(2)CTScanResult:[โIschemic Strokeโ,โHemmorraghic Strokeโ]
(3)MRIScanResult: [โIschemic Strokeโ,โHemmorraghic Strokeโ]
(4)StrokeType: [โIschemic Strokeโ,โHemmorraghic Strokeโ, โStroke Mimicโ]
(5)Anticoagulants: [โUsedโ,โNot usedโ]
(6)Mortality:[โTrueโ, โFalseโ]
(7)Disability: [โNegligibleโ, โModerateโ, โSevereโ]
CPTs
Note: [CTScanResult, MRIScanResult,StrokeType] means:
P(StrokeType=โ…โ | CTScanResult=โ…โ โง MRIScanResult=โ…โ)
(1)
[PatientAge]
[โ0-30โ, 0.10],
[โ31-65โ, 0.30],
[โ65+โ, 0.60]
(2)
[CTScanResult]
[โIschemic Strokeโ,0.7],
[ โHemmorraghic Strokeโ,0.3]
(3)
[MRIScanResult]
[โIschemic Strokeโ,0.7],
[ โHemmorraghic Strokeโ,0.3]
(4)
[Anticoagulants]
[โUsedโ,0.5],
[โNot usedโ,0.5]
(5)
[CTScanResult, MRIScanResult,StrokeType]
[โIschemic Strokeโ,โIschemic Strokeโ,โIschemic Strokeโ,0.8],
[โIschemic Strokeโ,โHemmorraghic Strokeโ,โIschemic Strokeโ,0.5],
[ โHemmorraghic Strokeโ,โIschemic Strokeโ,โIschemic Strokeโ,0.5],
[ โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,โIschemic Strokeโ,0],
[โIschemic Strokeโ,โIschemic Strokeโ,โHemmorraghic Strokeโ,0],
[โIschemic Strokeโ,โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,0.4],
[ โHemmorraghic Strokeโ,โIschemic Strokeโ,โHemmorraghic Strokeโ,0.4],
[ โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,0.9],
[โIschemic Strokeโ,โIschemic Strokeโ,โStroke Mimicโ,0.2],
[โIschemic Strokeโ,โHemmorraghic Strokeโ,โStroke Mimicโ,0.1],
[ โHemmorraghic Strokeโ,โIschemic Strokeโ,โStroke Mimicโ,0.1],
[ โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,โStroke Mimicโ,0.1],
(6)
[StrokeType, Anticoagulants, Mortality]
[โIschemic Strokeโ, โUsedโ, โFalseโ,0.28],
[โHemmorraghic Strokeโ, โUsedโ, โFalseโ,0.99],
[โStroke Mimicโ, โUsedโ, โFalseโ,0.1],
[โIschemic Strokeโ,โNot usedโ, โFalseโ,0.56],
[โHemmorraghic Strokeโ, โNot usedโ, โFalseโ,0.58],
[โStroke Mimicโ, โNot usedโ, โFalseโ,0.05],
[โIschemic Strokeโ, โUsedโ ,โTrueโ,0.72],
[โHemmorraghic Strokeโ, โUsedโ, โTrueโ,0.01],
[โStroke Mimicโ, โUsedโ, โTrueโ,0.9],
[โIschemic Strokeโ, โNot usedโ ,โTrueโ,0.44],
[โHemmorraghic Strokeโ, โNot usedโ, โTrueโ,0.42 ],
[โStroke Mimicโ, โNot usedโ, โTrueโ,0.95]
(7)
[StrokeType, PatientAge, Disability]
[โIschemic Strokeโ, โ0-30โ,โNegligibleโ, 0.80],
[โHemmorraghic Strokeโ, โ0-30โ,โNegligibleโ, 0.70],
[โStroke Mimicโ, โ0-30โ, โNegligibleโ,0.9],
[โIschemic Strokeโ, โ31-65โ,โNegligibleโ, 0.60],
[โHemmorraghic Strokeโ, โ31-65โ,โNegligibleโ, 0.50],
[โStroke Mimicโ, โ31-65โ, โNegligibleโ,0.4],
[โIschemic Strokeโ, โ65+โ , โNegligibleโ,0.30],
[โHemmorraghic Strokeโ, โ65+โ , โNegligibleโ,0.20], [โStroke Mimicโ, โ65+โ , โNegligibleโ,0.1],
[โIschemic Strokeโ, โ0-30โ ,โModerateโ,0.1],
[โHemmorraghic Strokeโ, โ0-30โ ,โModerateโ,0.2],
[โStroke Mimicโ, โ0-30โ ,โModerateโ,0.05],
[โIschemic Strokeโ, โ31-65โ,โModerateโ,0.3],
[โHemmorraghic Strokeโ, โ31-65โ,โModerateโ,0.4],
[โStroke Mimicโ, โ31-65โ,โModerateโ,0.3],
[โIschemic Strokeโ, โ65+โ ,โModerateโ,0.4],
[โHemmorraghic Strokeโ, โ65+โ ,โModerateโ,0.2], [โStroke Mimicโ, โ65+โ ,โModerateโ,0.1],
[โIschemic Strokeโ, โ0-30โ ,โSevereโ,0.1],
[โHemmorraghic Strokeโ, โ0-30โ ,โSevereโ,0.1],
[โStroke Mimicโ, โ0-30โ ,โSevereโ,0.05],
[โIschemic Strokeโ, โ31-65โ,โSevereโ,0.1],
[โHemmorraghic Strokeโ, โ31-65โ,โSevereโ,0.1],
[โStroke Mimicโ, โ31-65โ,โSevereโ,0.3],
[โIschemic Strokeโ, โ65+โ ,โSevereโ,0.3],
[โHemmorraghic Strokeโ, โ65+โ ,โSevereโ,0.6], [โStroke Mimicโ, โ65+โ ,โSevereโ,0.8]
Calculation
Please code to calculate the following probability value:
p1 = P(Mortality=โTrueโ | PatientAge=โ31-65โ โง CTScanResult=โIschemic Strokeโ) p2 = P(Disability=โModerateโ | PatientAge=โ65+โ โง MRIScanResult=โHemmorraghic Strokeโ) p3 = P(StrokeType=โStroke Mimicโ | PatientAge=โ65+โ โง CTScanResult=โHemmorraghic Strokeโ โง MRIScanResult=โIschemic Strokeโ)
p4 = P(Anticoagulants=โNot usedโ | PatientAge=โ0-30โ)
Please solve the 2 tasks and hand in a file named E09 YourNumber.pdf, and send it to ai 201901@foxmail.com
4 Codes and Results
The codes use Python 3!
burglary.py
from pomegranate import *
burglary = DiscreteDistribution( {โTโ:0.001, โFโ:0.999} ) earthquake = DiscreteDistribution( {โTโ:0.002, โFโ:0.998} ) alarm = ConditionalProbabilityTable(
[[โTโ,โTโ,โTโ,0.95],
[โTโ,โFโ,โTโ,0.94],
[โFโ,โTโ,โTโ,0.29],
[โFโ,โFโ,โTโ,0.001],
[โTโ,โTโ,โFโ,0.05],
[โTโ,โFโ,โFโ,0.06],
[โFโ,โTโ,โFโ,0.71],
[โFโ,โFโ,โFโ,0.999]], [burglary, earthquake]) johncalls = ConditionalProbabilityTable(
[[โTโ,โTโ,0.90],
[โFโ,โTโ,0.05],
[โTโ,โFโ,0.10],
[โFโ,โFโ,0.95]], [alarm]) marycalls = ConditionalProbabilityTable(
[[โTโ,โTโ,0.70],
[โFโ,โTโ,0.01],
[โTโ,โFโ,0.30],
[โFโ,โFโ,0.99]], [alarm])
s1 = State(burglary, name=”burglary”) s2 = State(earthquake, name=”earthquake”) s3 = State(alarm, name=”alarm”) s4 = State(johncalls, name=”johncalls”) s5 = State(marycalls, name=”marycalls”)
model = BayesianNetwork(“Burglary”) model.add_states(s1,s2,s3,s4,s5)
model.add_transition(s1,s3) model.add_transition(s2,s3) model.add_transition(s3,s4) model.add_transition(s3,s5) model.bake()
marginals = model.predict_proba({})
# P(A) print(“P(A) = {}”.format(marginals[2].parameters[0][“T”]))
# P(J&&~M) = P(J|~M)P(~M) j_nm = model.predict_proba({โmarycallsโ:โFโ})[3].parameters[0][“T”] * marginals[4].
,โ parameters[0][“F”] print(“P(J && ~M) = {}”.format(j_nm))
# P(A|J&&~M) print(“P(A | J && ~M) = {}”.format(model.predict_proba({โjohncallsโ:โTโ,โmarycallsโ:โFโ}) ,โ [2].parameters[0][“T”]))
# P(B|A) print(“P(B | A) = {}”.format(model.predict_proba({โalarmโ:โTโ})[0].parameters[0][“T”]))
# P(B|J&&~M) b_c_j_nm = model.predict_proba({โjohncallsโ:โTโ,โmarycallsโ:โFโ})[0].parameters[0][“T”] print(“P(B | J && ~M) = {}”.format(b_c_j_nm)) # P(J&&~M|~B) = P(~B && J && ~M) / P(~B)
# = P(~B | J && ~M) P(J && ~M) / P(~B)
# = (1- P(B | J && ~M)) P(J && ~M) / P(~B)
print(“P(J && ~M | ~B) = {}”.format((1-b_c_j_nm) * j_nm / marginals[0].parameters[0][“F”]))
diagnosing.py
from pomegranate import *
PatientAge = DiscreteDistribution({โ0-30โ:0.10, โ31-65โ:0.30, โ65+โ:0.60})
CTScanResult = DiscreteDistribution({โIschemic Strokeโ:0.7, โHemmorraghic Strokeโ:0.3})
MRIScanResult = DiscreteDistribution({โIschemic Strokeโ:0.7, โHemmorraghic Strokeโ:0.3})
Anticoagulants = DiscreteDistribution({โUsedโ:0.5 ,โNot usedโ:0.5})
StrokeType = ConditionalProbabilityTable(
[[โIschemic Strokeโ,โIschemic Strokeโ,โIschemic Strokeโ,0.8],
[โIschemic Strokeโ,โHemmorraghic Strokeโ,โIschemic Strokeโ,0.5],
[โHemmorraghic Strokeโ,โIschemic Strokeโ,โIschemic Strokeโ,0.5],
[โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,โIschemic Strokeโ,0],
[โIschemic Strokeโ,โIschemic Strokeโ,โHemmorraghic Strokeโ,0],
[โIschemic Strokeโ,โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,0.4],
[โHemmorraghic Strokeโ,โIschemic Strokeโ,โHemmorraghic Strokeโ,0.4],
[โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,0.9],
[โIschemic Strokeโ,โIschemic Strokeโ,โStroke Mimicโ,0.2],
[โIschemic Strokeโ,โHemmorraghic Strokeโ,โStroke Mimicโ,0.1],
[โHemmorraghic Strokeโ,โIschemic Strokeโ,โStroke Mimicโ,0.1],
[โHemmorraghic Strokeโ,โHemmorraghic Strokeโ,โStroke Mimicโ,0.1]],[CTScanResult, ,โ MRIScanResult])
Mortality = ConditionalProbabilityTable(
[[โIschemic Strokeโ,โUsedโ,โFalseโ,0.28],
[โHemmorraghic Strokeโ,โUsedโ,โFalseโ,0.99], [โStroke Mimicโ,โUsedโ,โFalseโ,0.1],
[โIschemic Strokeโ,โNot usedโ,โFalseโ,0.56],
[โHemmorraghic Strokeโ,โNot usedโ,โFalseโ,0.58],
[โStroke Mimicโ,โNot usedโ,โFalseโ,0.05],
[โIschemic Strokeโ,โUsedโ,โTrueโ,0.72],
[โHemmorraghic Strokeโ,โUsedโ,โTrueโ,0.01], [โStroke Mimicโ,โUsedโ,โTrueโ,0.9],
[โIschemic Strokeโ,โNot usedโ,โTrueโ,0.44],
[โHemmorraghic Strokeโ,โNot usedโ,โTrueโ,0.42],
[โStroke Mimicโ,โNot usedโ,โTrueโ,0.95]],[StrokeType, Anticoagulants]) Disability = ConditionalProbabilityTable(
[[โIschemic Strokeโ,โ0-30โ,โNegligibleโ,0.80],
[โHemmorraghic Strokeโ,โ0-30โ,โNegligibleโ,0.70],
[โStroke Mimicโ,โ0-30โ,โNegligibleโ,0.9],
[โIschemic Strokeโ,โ31-65โ,โNegligibleโ,0.60],
[โHemmorraghic Strokeโ,โ31-65โ,โNegligibleโ,0.50],
[โStroke Mimicโ,โ31-65โ,โNegligibleโ,0.4],
[โIschemic Strokeโ,โ65+โ,โNegligibleโ,0.30],
[โHemmorraghic Strokeโ,โ65+โ,โNegligibleโ,0.20],
[โStroke Mimicโ,โ65+โ,โNegligibleโ,0.1],
[โIschemic Strokeโ,โ0-30โ,โModerateโ,0.1],
[โHemmorraghic Strokeโ,โ0-30โ,โModerateโ,0.2],
[โStroke Mimicโ,โ0-30โ,โModerateโ,0.05],
[โIschemic Strokeโ,โ31-65โ,โModerateโ,0.3],
[โHemmorraghic Strokeโ,โ31-65โ,โModerateโ,0.4],
[โStroke Mimicโ,โ31-65โ,โModerateโ,0.3],
[โIschemic Strokeโ,โ65+โ,โModerateโ,0.4],
[โHemmorraghic Strokeโ,โ65+โ,โModerateโ,0.2],
[โStroke Mimicโ,โ65+โ,โModerateโ,0.1],
[โIschemic Strokeโ,โ0-30โ,โSevereโ,0.1],
[โHemmorraghic Strokeโ,โ0-30โ,โSevereโ,0.1],
[โStroke Mimicโ,โ0-30โ,โSevereโ,0.05],
[โIschemic Strokeโ,โ31-65โ,โSevereโ,0.1],
[โHemmorraghic Strokeโ,โ31-65โ,โSevereโ,0.1],
[โStroke Mimicโ,โ31-65โ,โSevereโ,0.3],
[โIschemic Strokeโ,โ65+โ,โSevereโ,0.3],
[โHemmorraghic Strokeโ,โ65+โ,โSevereโ,0.6],
[โStroke Mimicโ,โ65+โ,โSevereโ,0.8]],[StrokeType,PatientAge])
s1 = State(PatientAge, name=”PatientAge”) s2 = State(CTScanResult, name=”CTScanResult”) s3 = State(MRIScanResult, name=”MRIScanResult”) s4 = State(StrokeType, name=”StrokeType”) s5 = State(Anticoagulants, name=”Anticoagulants”) s6 = State(Mortality, name=”Mortality”) s7 = State(Disability, name=”Disability”) model = BayesianNetwork(“Diagnosing”)
model.add_states(s1,s2,s3,s4,s5,s6,s7)
model.add_transition(s2,s4) model.add_transition(s3,s4)
model.add_transition(s4,s6) model.add_transition(s5,s6)
model.add_transition(s1,s7) model.add_transition(s4,s7) model.bake() marginals = model.predict_proba({})
p1 = model.predict_proba({โPatientAgeโ:โ31-65โ,โCTScanResultโ:โIschemic Strokeโ})[5].
,โ parameters[0][“True”] p2 = model.predict_proba({โPatientAgeโ:โ65+โ,โMRIScanResultโ:โHemmorraghic Strokeโ})[6].
,โ parameters[0][“Moderate”] p3 = model.predict_proba({โPatientAgeโ:โ65+โ,โCTScanResultโ:โHemmorraghic Strokeโ,โ
,โ MRIScanResultโ:โIschemic Strokeโ})[3].parameters[0][“Stroke Mimic”] p4 = model.predict_proba({โPatientAgeโ:โ0-30โ})[4].parameters[0][“Not used”] print(p1) print(p2) print(p3) print(p4)
Reviews
There are no reviews yet.