Skip to content

elhamabedi/bayesian-network

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Bayesian Network for Farm Security System

Scenario

A farmer is concerned about protecting his orange farm from thieves during the night while he is sleeping. He has installed a sensitive alarm system that can be triggered by intruders, sheep roaming the farm, or thunder. The farmer uses a Bayesian Network to model the situation and make informed decisions about activating the alarm to ensure the safety of his oranges. The Bayesian Network includes the following random variables:

  • IntruderPresence (I): Presence of intruders on the farm. States: True (intruders present), False (no intruders).
  • SheepPresence (S): Presence of sheep on the farm, which could trigger the alarm. States: True (sheep present), False (no sheep).
  • Thunder (T): Occurrence of thunder, which could trigger the alarm. States: True (thunder occurs), False (no thunder).
  • AlarmTriggered (A): Whether the alarm is triggered. States: True (alarm triggered), False (alarm not triggered).
  • FarmerWakeUp (F): Farmer's decision to wake up and defend the oranges. States: True (farmer wakes up), False (farmer remains asleep).
  • OrangeSafety (O): Safety of the oranges. States: 1 (oranges secure), 0 (oranges at risk).

The farmer has collected probability data in a dataset (night_farm_dataset.csv).

Task a) Model the Bayesian Network

Construct a directed acyclic graph (DAG) for the Bayesian Network. Define the nodes (variables) and edges (dependencies) based on the scenario. Provide a brief explanation of the dependencies chosen. Draw the DAG (sketch or describe in text).

Dependency Justification

Edge Reason
I → A Intruders trigger the alarm
S → A Sheep roaming can trigger the alarm
T → A Thunder can trigger the alarm
A → F Farmer wakes up only if alarm sounds
I → O Intruders directly threaten orange safety
F → O Farmer waking up protects the oranges

Result Table for Task a

Component Description
Number of Nodes 6 (I, S, T, A, F, O)
Number of Edges 6
Graph Type Directed Acyclic Graph (DAG)

Task b) Construct Conditional Probability Tables (CPTs)

Define the CPTs for each variable, listing states and probabilities conditioned on parent nodes. For variables with no parents, provide prior probabilities. Assume realistic probability values from the dataset if specific values are unavailable, ensuring probabilities sum to 1.

Prior Probabilities

Variable P(True)
I 0.04999
S 0.30099
T 0.09843

P(A | I, S, T)

I S T P(A=True) Support
True True True 0.9065 139
True True False 0.9424 1,337
True False True 0.9190 395
True False False 0.9156 3,128
False True True 0.7983 2,786
False True False 0.7478 25,837
False False True 0.5924 6,523
False False False 0.0096 59,855

P(F | A)

A P(F=True) Support
True 0.8019 30,595
False 0.0486 69,405

P(O | I, F)

I F P(O=True) Support
True True 1.0000 3,671
True False 0.0000 1,328
False True 1.0000 24,237
False False 1.0000 70,764

Result Table for Task b

CPT Number of Entries Source
P(I) 2 Dataset (100,000 rows)
P(S) 2 Dataset (100,000 rows)
P(T) 2 Dataset (100,000 rows)
P(A|I,S,T) 8 Dataset (100,000 rows)
P(F|A) 2 Dataset (100,000 rows)
P(O|I,F) 4 Dataset (100,000 rows)

Task c) Calculate Marginal Probabilities

Using the Bayesian Network and CPTs, calculate the marginal probabilities for each variable (I, S, T, A, F, O) with no prior evidence. Show calculations, including chain rule or marginalization.

P(A=True) Calculation

$$ P(A=True) = \sum_{i,s,t} P(I=i) \cdot P(S=s) \cdot P(T=t) \cdot P(A=True \mid I=i, S=s, T=t) $$

Term I S T Product
1 T T T 0.04999 × 0.30099 × 0.09843 × 0.9065 = 0.001343
2 T T F 0.04999 × 0.30099 × 0.90157 × 0.9424 = 0.012784
3 T F T 0.04999 × 0.69901 × 0.09843 × 0.9190 = 0.003161
4 T F F 0.04999 × 0.69901 × 0.90157 × 0.9156 = 0.028845
5 F T T 0.95001 × 0.30099 × 0.09843 × 0.7983 = 0.022468
6 F T F 0.95001 × 0.30099 × 0.90157 × 0.7478 = 0.192792
7 F F T 0.95001 × 0.69901 × 0.09843 × 0.5924 = 0.038719
8 F F F 0.95001 × 0.69901 × 0.90157 × 0.0096 = 0.005721
Total 0.3058

P(F=True) Calculation

$$ P(F=\text{True}) = P(F=\text{True} \mid A=\text{True}) \times P(A=\text{True}) + P(F=\text{True} \mid A=\text{False}) \times P(A=\text{False}) $$

$$ = 0.8019 \times 0.3058 + 0.0486 \times (1 - 0.3058) = 0.245237 + 0.033756 = 0.2790 $$

P(O=True) Calculation

$$ P(O=\text{True}) = \sum_{i,f} P(O=\text{True} \mid I=i, F=f) \cdot P(I=i) \cdot P(F=f) $$

$$ = (1.0 \times 0.04999 \times 0.2790) + (0.0 \times 0.04999 \times 0.7210) + (1.0 \times 0.95001 \times 0.2790) + (1.0 \times 0.95001 \times 0.7210) $$

$$ = 0.013947 + 0 + 0.265046 + 0.684964 = 0.9640 $$

Result Table for Task c

Variable Marginal Probability
P(I=True) 0.0500
P(S=True) 0.3010
P(T=True) 0.0984
P(A=True) 0.3058
P(F=True) 0.2790
P(O=True) 0.9640

Task d) Perform Probabilistic Inference

Answer the following:

  • Compute P(A = True | S = True), the probability that the alarm is triggered due to sheep presence. Show calculations, accounting for other variables.
  • Compute P(F = True | I = True), the probability that the farmer wakes up given an intruder. Show calculations, including summations or Bayes' theorem.

Query 1: P(A=True | S=True)

$$ P(A=\text{True} \mid S=\text{True}) = \frac{P(A=\text{True}, S=\text{True})}{P(S=\text{True})} $$

$$ P(A=\text{True}, S=\text{True}) = \sum_{i,t} P(I=i) \times P(T=t) \times P(S=\text{True} \mid I=i, T=t) \times P(A=\text{True} \mid I=i, S=\text{True}, T=t) $$

I T P(S=True|I, T) Contribution
True True 0.2603 0.04999 × 0.09843 × 0.2603 × 0.9065 = 0.001161
True False 0.2994 0.04999 × 0.90157 × 0.2994 × 0.9424 = 0.012718
False True 0.2993 0.95001 × 0.09843 × 0.2993 × 0.7983 = 0.022340
False False 0.3015 0.95001 × 0.90157 × 0.3015 × 0.7478 = 0.193125

$$ P(A=\text{True}, S=\text{True}) = 0.001161 + 0.012718 + 0.022340 + 0.193125 = 0.229345 $$

$$ P(S=\text{True}) = 0.300990 $$

$$ P(A=\text{True} \mid S=\text{True}) = \frac{0.229345}{0.300990} = 0.7620 $$

Query 2: P(F=True | I=True)

$$ P(F=\text{True} \mid I=\text{True}) = \frac{P(F=\text{True}, I=\text{True})}{P(I=\text{True})} $$

$$ P(F=\text{True}, I=\text{True}) = \sum_{s,t,a} P(I=\text{True}) \cdot P(S=s) \cdot P(T=t) \cdot P(A=a \mid I=\text{True}, S=s, T=t) \cdot P(F=\text{True} \mid A=a) $$

After summing over all 8 combinations:

$$ P(F=\text{True}, I=\text{True}) = 0.037180 $$

$$ P(I=\text{True}) = 0.049990 $$

$$ P(F=\text{True} \mid I=\text{True}) = \frac{0.037180}{0.049990} = 0.7437 $$

Result Table for Task d

Query Result Interpretation
P(A=True | S=True) 0.7620 Given sheep presence, alarm triggers with 76.20% probability
P(F=True | I=True) 0.7437 Given intruder presence, farmer wakes up with 74.37% probability

Learning Objectives

  • Understand Bayesian Network structure and probabilistic reasoning.
  • Model dependencies and perform inference for marginal and conditional probabilities.
  • Interpret results in a real-world context.

Final Results Summary Table

Task Key Result
Task a DAG with 6 nodes, 6 edges
Task b 6 CPTs extracted from 100,000 rows
Task c Marginals: P(A)=0.3058, P(F)=0.2790, P(O)=0.9640
Task d P(A|S)=0.7620, P(F|I)=0.7437

This code was developed as part of a Machine Learning course assignment.

Releases

No releases published

Packages

 
 
 

Contributors