Sample problems from Peter Komar; after trying to analytically solve everything, Monte Carlo and see if I'm right.
In [53]:
def compare(analytic,N,f):
errval = err(f,N)
successes = sum(f)
print "Analytic prediction: {:.0f}%.".format(analytic*100.)
print "Monte Carlo: {:.0f} +- {:.0f}%.".format(successes/float(N)*100.,errval*100.)
In [161]:
def err(fx,N):
# http://www.northeastern.edu/afeiguin/phys5870/phys5870/node71.html
f2 = [x*x for x in fx]
return np.sqrt((1./N * sum(f2) - (1./N * sum(fx))**2)/float(N))
Question 1
Q1: What is the probability of light rain on both days?
In [39]:
import numpy as np
from numpy.random import binomial
In [77]:
# Default is 1000 trials each
N = 1000
In [54]:
p_rain_sat = 0.5
p_rain_sun = 0.2
p_light_sat = 0.9
p_heavy_sat = 0.1
p_light_sun = 1.0
p_heavy_sun = 0.0
f = []
for i in range(N):
# Light rain on Saturday?
rain_sat = binomial(1,p_rain_sat)
if rain_sat:
light_sat = binomial(1,p_light_sat)
else:
light_sat = 0
# Light rain on Sunday?
rain_sun = binomial(1,p_rain_sun)
if rain_sun:
light_sun = binomial(1,p_light_sun)
else:
light_sun = 0
if light_sat and light_sun:
f.append(1)
else:
f.append(0)
compare(9/100.,N,f)
Q2: What is the probability of rain during the weekend?
In [55]:
f = []
for i in range(N):
# Light rain on either day?
rain_sat = binomial(1,p_rain_sat)
rain_sun = binomial(1,p_rain_sun)
if rain_sat or rain_sun:
f.append(1)
else:
f.append(0)
compare(60/100.,N,f)
Question 2
Q1: With what probability are the two drawn pieces of candy different?
In [13]:
from random import randint
In [56]:
f = []
for i in range(N):
# Draw candy from bag 1
r1 = randint(0,6)
if r1 < 3:
candy1 = "taffy"
else:
candy1 = "caramel"
# Draw candy from bag 2
r2 = randint(0,5)
if r2 == 0:
candy2 = "taffy"
else:
candy2 = "caramel"
if candy1 is not candy2:
f.append(1)
else:
f.append(0)
compare(19/42.,N,f)
Q2: With what probability are the two drawn pieces of candy different if they are drawn from the same (but randomly chosen) bag?
In [58]:
f = []
for i in range(N):
# Choose the bag
bag = binomial(1,0.5)
if bag:
# Bag 1
# First draw
r1 = randint(0,6)
if r1 < 3:
candy1 = "taffy"
else:
candy1 = "caramel"
# Second draw
r2 = randint(0,5)
if candy1 is "taffy":
if r2 < 2:
candy2 = "taffy"
else:
candy2 = "caramel"
else:
if r2 < 3:
candy2 = "taffy"
else:
candy2 = "caramel"
else:
# Bag 2
# First draw
r1 = randint(0,5)
if r1 < 2:
candy1 = "taffy"
else:
candy1 = "caramel"
# Second draw
r2 = randint(0,4)
if candy1 is "caramel":
if r2 < 4:
candy2 = "caramel"
else:
candy2 = "taffy"
else:
candy2 = "caramel"
if candy1 is not candy2:
f.append(1)
else:
f.append(0)
compare(23/42.,N,f)
Question 3
Q: What is the expectation value and standard deviation of the reward?
In [78]:
p_H = 0.5
f = []
for i in range(N):
# Flip coin 1
c1 = binomial(1,p_H)
# Flip coin 2
c2 = binomial(1,p_H)
# Flip coin 3
c3 = binomial(1,p_H)
total_heads = c1 + c2 + c3
# Three heads
if total_heads == 3:
reward = 100
if total_heads == 2:
reward = 40
if total_heads == 1:
reward = 0
if total_heads == 0:
reward = -200
f.append(reward)
print "Analytic: {:.2f} +- {:.0f}".format(20/8.,82)
print "Monte Carlo: {:.2f} +- {:.0f}".format(np.mean(f),np.std(f))
Question 4
Q1: What is the probability that Potter, Granger, and Weasley are standing next to each other?
In [85]:
n = 10
f = []
for i in range(N):
line = range(n)
np.random.shuffle(line)
# Assume Potter, Granger, Weasley correspond to 0, 1, and 2
indices = [line.index(person) for person in (0,1,2)]
if max(indices) - min(indices) == 2:
f.append(1)
compare(1/15.,N,f)
Q2: What is the probability that Potter, Granger, and Weasley are standing next to each other if the line is a circle?
In [91]:
f = []
for i in range(N):
line = range(n)
np.random.shuffle(line)
# Assume Potter, Granger, Weasley correspond to 0, 1, and 2
indices = [line.index(person) for person in (0,1,2)]
if max(indices) - min(indices) == 2:
f.append(1)
else:
# Shift line halfway around and check again
line = list(np.roll(line,n//2))
indices = [line.index(person) for person in (0,1,2)]
if max(indices) - min(indices) == 2:
f.append(1)
compare(1/12.,N,f)
Question 5
Q: What is the probability that c dances with gamma?
In [93]:
f = []
for i in range(N):
guys = ['a','b','c','d','e']
gals = ['alpha','beta','gamma','delta','epsilon']
np.random.shuffle(guys)
np.random.shuffle(gals)
if guys.index('c') == gals.index('gamma'):
f.append(1)
compare(1./5,N,f)
Question 6
Q: What is the probability that Derrick and Gaurav end up in the same group?
In [97]:
f = []
for i in range(N):
fellows = range(21)
np.random.shuffle(fellows)
# Derrick = 0, Gaurav = 1
group_derrick = fellows.index(0)//7
group_gaurav = fellows.index(1)//7
if group_derrick == group_gaurav:
f.append(1)
compare(0.30,N,f)
Question 7
Q: What is the probability that stocking A gets no candy?
In [112]:
f = []
for i in range(N):
a,b,c,d = 0,0,0,0
for candy in range(10):
selection = randint(0,3)
if selection == 0:
a += 1
if selection == 1:
b += 1
if selection == 2:
c += 1
if selection == 3:
d += 1
if a == 0:
f.append(1)
compare(0.75**10,N,f)
Question 8
Q1: What is the probability that we get two 1s in the first twenty throws?
In [139]:
n = 20
f = []
for i in range(N):
throws = np.random.randint(1,11,n)
counts = np.bincount(throws)
if counts[1] == 2:
f.append(1)
analytic = 10**(np.log10(190) + 18*np.log10(9) - 20)
compare(analytic,N,f)
Q2: What is the probability that we get the first 1 in the tenth throw?
In [145]:
n = 10
f = []
for i in range(N):
throws = np.random.randint(1,11,n)
counts = np.bincount(throws)
if counts[1] == 1 and throws[-1] == 1:
f.append(1)
analytic = 0.9**9 * 0.1
compare(analytic,N,f)
Q3: What is the probability that we get the third 1 on the thirtieth throw?
In [166]:
n = 30
f = []
for i in range(N):
throws = np.random.randint(1,11,n)
counts = np.bincount(throws)
if counts[1] == 3 and throws[-1] == 1:
f.append(1)
analytic = (29*28/2. * 0.9**27 * 0.1**2) * 0.1
compare(analytic,N,f)
In [ ]: