# Hungry Baby Problem, solved using Bayesian Sampling
import random
def simulation():
Hungry=False
Tired=False
Crying=False
Woken=False
if random.random()<0.3: Hungry = True
if random.random()<0.2: Tired = True
if Hungry and Tired:
if random.random()<0.9: Crying = True
elif Hungry and not Tired:
if random.random()<0.85: Crying = True
elif not Hungry and Tired:
if random.random()<0.7: Crying = True
else:
if random.random()<0.3: Crying = True
if Crying:
if random.random()<0.9: Woken = True
else:
if random.random()<0.2: Woken = True
return(Hungry, Tired, Crying, Woken)
# Return the results of our simulation
# The order is Hungry, Tired, Crying, Woken
simulation()
(False, False, True, True)
# Estimate the probability of P(Woken)
# This should be approximately 0.5668
count=0
for k in range(1000000):
result = simulation()
if result[3]:
count+=1
count/1000000
0.56704
# Estimate the probability of P(Hungry|Woken)
# This should be approximately 0.4244
wcount=0
hcount=0
for k in range(1000000):
result = simulation()
if result[3]:
wcount+=1
if result[0]:
hcount+=1
hcount/wcount
0.42498540777981353