I am trying to plot the pdf of flipping heads when drawing from a bag of biased coins. Since I am interested in the % of heads flipped, not the number, I simulate 500K flips and group the results into buckets of 100, computing the % of heads in each bucket. I do this using two different methods, in method 1 I draw a coin per bucket, in method 2 I draw a coin per flip. They come up with the same mean, but the variance of the two methods are different. In fact, method 2 basically removes the variance, i.e. if I have a bag of coins with P of heads being [0.3, 0.35, 0.4, 0.45, 0.5] method 2 has simulation results that are equivalent to just having a bag with a coin with p 0.4 of flipping heads.
Why does method 2 (i.e. a draw from the bag per flip) remove the variance associated with the bag of biased coins?
See the following histograms of the results: http://img208.imageshack.us/img208/111/biasedcoin.png (I'm a new user and the site won't let me link this inline)
And the code that generated these:
#!/usr/bin/env python
import random as rand
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
#biased_coin_bag = [0.4]
biased_coin_bag = [0.3, 0.35, 0.4, 0.45, 0.5]
Heads = 'H'
Tails = 'T'
def draw_coin():
return biased_coin_bag[rand.randint(0, len(biased_coin_bag)-1)]
def flip_coin(bc):
if bc >= rand.random():
return Heads
else:
return Tails
def histogram(iters=500000, bucket_size=100):
results_per_flip = []
heads_per_flip, tails_per_flip = 0, 0
results_per_bucket = []
heads_per_bucket, tails_per_bucket = 0, 0
bc_per_bucket = draw_coin()
for i in range(iters):
if i != 0 and i % bucket_size == 0:
#
# track the bucketized results when drawing per bucket
#
results_per_bucket.append(float(heads_per_bucket) /
float(heads_per_bucket + tails_per_bucket))
heads_per_bucket, tails_per_bucket = 0, 0
bc_per_bucket = draw_coin()
#
# track the bucketized results when drawing per flip
#
results_per_flip.append(float(heads_per_flip) /
float(heads_per_flip + tails_per_flip))
heads_per_flip, tails_per_flip = 0, 0
#
# flip the coins
#
if flip_coin(bc_per_bucket) == Heads:
heads_per_bucket += 1
else:
tails_per_bucket += 1
if flip_coin(draw_coin()) == Heads:
heads_per_flip += 1
else:
tails_per_flip += 1
#
# plot the draw per bucket results
#
plt.subplot(211)
mu_per_bucket = np.average(results_per_bucket)
sigma_per_bucket = np.std(results_per_bucket)
n, bins, patches = plt.hist(results_per_bucket, 40, normed=1, color='green',
alpha=0.75,
label=r'draw per bucket $\mathrm{\mu=%f\ \sigma=%f}$' %
(mu_per_bucket, sigma_per_bucket))
y = mlab.normpdf(bins, mu_per_bucket, sigma_per_bucket)
plt.plot(bins, y, 'r--', linewidth=1)
plt.legend()
plt.axis([0, 1, 0, 20])
#
# plot the draw per flip results
#
plt.subplot(212)
mu_per_flip = np.average(results_per_flip)
sigma_per_flip = np.std(results_per_flip)
n, bins, patches = plt.hist(results_per_flip, 40, normed=1, color='blue',
alpha=0.75,
label=r'draw per flip $\mathrm{\mu=%f\ \sigma=%f}$' %
(mu_per_flip, sigma_per_flip))
y = mlab.normpdf(bins, mu_per_flip, sigma_per_flip)
plt.plot(bins, y, 'r--', linewidth=1)
plt.legend()
plt.axis([0, 1, 0, 20])
plt.show()
histogram()