46 lines
2.2 KiB
Python
46 lines
2.2 KiB
Python
|
|
|
|
# Defining the local conditional probability distributions
|
|
P_F = {1: 0.01, 0: 0.99}
|
|
P_E = {1: 0.02, 0: 0.98}
|
|
|
|
# Probability of alarm given Fire, Earthquake, and Neighbor calls
|
|
P_A_given_FBE = {
|
|
(1, 1, 1): 0.99, # Alarm likely if fire, earthquake, and neighbor B calls
|
|
(1, 1, 0): 0.98, # Alarm likely if fire, earthquake, but no call from B
|
|
(1, 0, 1): 0.95, # Alarm likely if fire, no earthquake, but neighbor B calls
|
|
(1, 0, 0): 0.90, # Alarm likely if fire, no earthquake, and no call from B
|
|
(0, 1, 1): 0.70, # Alarm possible if earthquake and neighbor B calls, but no fire
|
|
(0, 1, 0): 0.60, # Alarm possible if earthquake, no fire, and no call from B
|
|
(0, 0, 1): 0.20, # Alarm less likely if no fire, no earthquake, but neighbor B calls
|
|
(0, 0, 0): 0.01 # Alarm very unlikely if no fire, no earthquake, and no call from B
|
|
}
|
|
|
|
P_B_given_A = {1: 0.90, 0: 0.05} # Probability of neighbor B calling given alarm state
|
|
P_C_given_A = {1: 0.70, 0.01: 0.01} # Probability of neighbor C calling given alarm state
|
|
|
|
|
|
def joint_probability(f, e, a, b, c):
|
|
return (P_F[f] * P_E[e] * P_A_given_FBE[(f, e, b)] *
|
|
P_B_given_A[a if b else 1-a] *
|
|
P_C_given_A.get(a if c else 1-a, 0.5))
|
|
|
|
|
|
# Calculating total probabilities where B=1 and C=1 respectively
|
|
total_P_B_1 = sum([joint_probability(f, e, a, 1, c) for f in [True, False] for e in [True, False]
|
|
for a in [True, False] for c in [True, True]]) # Only consider B=1
|
|
total_P_C_1 = sum([joint_probability(f, e, a, b, 1) for f in [True, False] for e in [True, False]
|
|
for a in [True, False] for b in [True, True]]) # Only consider C=1
|
|
|
|
# Calculate P(F=1 | B=1)
|
|
P_F_given_B = total_P_B_1 / sum([joint_probability(f, e, a, 1, c) for f in [True, False] for e in [True, False]
|
|
for a in [True, False] for c in [True, True]])
|
|
|
|
# Calculate P(F=1 | C=1)
|
|
P_F_given_C = total_P_C_1 / sum([joint_probability(f, e, a, b, 1) for f in [True, False] for e in [True, False]
|
|
for a in [True, False] for b in [True, True]])
|
|
|
|
# Print the results
|
|
print(f"P(F=1 | B=1): {P_F_given_B:.6f}")
|
|
print(f"P(F=1 | C=1): {P_F_given_C:.6f}")
|