-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayesian networks.py
More file actions
58 lines (47 loc) · 2.1 KB
/
bayesian networks.py
File metadata and controls
58 lines (47 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pgmpy
# pip install pgmpy (This line is a comment, indicating the installation command)
from pgmpy.models import DiscreteBayesianNetwork
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination
# Define the structure of the Bayesian Network
model = DiscreteBayesianNetwork([
('Cold', 'Cough'), # 'Cold' influences 'Cough'
('Cold', 'Fever') # 'Cold' influences 'Fever'
])
# Define the Conditional Probability Distribution (CPD) for 'Cold'
cpd_cold = TabularCPD(
variable='Cold',
variable_card=2, # Binary variable: 0 for No Cold, 1 for Cold
values=[[0.9], [0.1]] # P(Cold=0) = 0.9, P(Cold=1) = 0.1 (prior probability)
)
# Define the CPD for 'Cough' given 'Cold'
cpd_cough_given_cold = TabularCPD(
variable='Cough',
variable_card=2, # Binary variable: 0 for No Cough, 1 for Cough
values=[[0.8, 0.1], # P(Cough=0 | Cold=0) = 0.8, P(Cough=0 | Cold=1) = 0.1
[0.2, 0.9]], # P(Cough=1 | Cold=0) = 0.2, P(Cough=1 | Cold=1) = 0.9
evidence=['Cold'],
evidence_card=[2]
)
# Define the CPD for 'Fever' given 'Cold'
cpd_fever_given_cold = TabularCPD(
variable='Fever',
variable_card=2, # Binary variable: 0 for No Fever, 1 for Fever
values=[[0.7, 0.2], # P(Fever=0 | Cold=0) = 0.7, P(Fever=0 | Cold=1) = 0.2
[0.3, 0.8]], # P(Fever=1 | Cold=0) = 0.3, P(Fever=1 | Cold=1) = 0.8
evidence=['Cold'],
evidence_card=[2]
)
# Add the CPDs to the model
model.add_cpds(cpd_cold, cpd_cough_given_cold, cpd_fever_given_cold)
# Validate the model
assert model.check_model()
print("Bayesian Network structure and CPDs have been defined and validated.")
# Create an inference object
infer = VariableElimination(model)
# Query 1: Calculate the probability of 'Cold' given 'Cough' (evidence = 1 for Cough)
query_result_1 = infer.query(variables=['Cold'], evidence={'Cough': 1})
print("P(Cold | Cough) =\n", query_result_1)
# Query 2: Calculate the probability of 'Cold' given 'Cough' and 'Fever'
query_result_2 = infer.query(variables=['Cold'], evidence={'Cough': 1, 'Fever': 1})
print("P(Cold | Cough, Fever) =\n", query_result_2)