导入
Python 中的共因图
深入探究常见原因图的复杂性及其对因果关系的影响。学习如何在贝叶斯网络中引入诊断测试结果。
...
Common cause graph
...
Bayesian network representing a patient being infected given the test result is positive
1import networkx as nx 2import matplotlib.pyplot as plt 3from causalnex.structure import StructureModel 4from causalnex.network import BayesianNetwork 5 6# Define the network structure 7sm = StructureModel() 8sm.add_edges_from([("Disease Presence", "Test Results")]) 9 10# Define the Bayesian Network 11bn = BayesianNetwork(sm) 12 13# Manually set the node states 14bn.node_states = { 15 "Disease Presence": ["Non Infected", "Infected"], 16 "Test Results": ["False", "True"], 17} 18 19# Manually define the CPD for 'Disease Presence' 20cpd_disease_presence = { 21 "Non Infected": 0.999, 22 "Infected": 0.001, 23} 24 25# Manually define the CPD for 'Test Results' given 'Disease Presence' 26cpd_test_results_given_disease_presence = { 27 "Non Infected": { 28 "False": 0.99, # P(Test Results=False | Disease Presence=Non Infected) 29 "True": 0.01, # P(Test Results=True | Disease Presence=Non Infected) 30 }, 31 "Infected": { 32 "False": 0.01, # P(Test Results=False | Disease Presence=Infected) 33 "True": 0.99, # P(Test Results=True | Disease Presence=Infected) 34 }, 35} 36 37# Set the CPDs for the Bayesian Network 38bn.cpds["Disease Presence"] = cpd_disease_presence 39bn.cpds["Test Results"] = cpd_test_results_given_disease_presence 40 41 42# Plot the network 43pos = nx.spring_layout(sm) 44nx.draw(sm, pos, with_labels=True, node_size=3000, node_color="skyblue", font_size=10, font_weight="bold") 45plt.show() 46 47plt.savefig('output/FirstBN_Pieter')
测试 #2 的 CPD
| States Test Result node | P(Disease) = Infected | P(Disease) = Non Infected |
|---|---|---|
| P(Test Result) = True | 0.99 | 0.01 |
| P(Test Result) = False | 0.01 | 0.99 |
...
Bayesian network for two positive test
...
Bayesian network for one positive test and one negative test
...
Bayesian network for three positive tests
...
Bayesian network for two positive tests and one negative