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()

```


simulating chances of success when drawing from a bag of biased coins
You seem to answer the question yourself in the last sentence of your first paragraph. If you draw a new coin every flip, then the experiment is equivalent to a sequence of coin tosses with a single coin (with appropriately chosen probability).


Maybe helpful to think about independence. If you draw a new coin every flip, then the results of the different flips are all independent. So all that matters is the probability of a head on any given flip (and that is just a single number - 0.4 in your example). 


If you reuse the same coin for several flips, then the results of these flips are no longer independent, and the form of the dependence between them is important. 


Put another way - there are two contributions to the variance of the total number of heads. One comes from the variance of individual flips. The other comes from the covariance between pairs of flips. This comes from a simple formula like $Var (\sum\_{i=1}^n X\_i)
= \sum\_{i=1}^n Var (X\_i) + 2\sum\_{1\leq i<j\leq n} Cov(X\_i,X\_j)$.


The variance of any individual flip is the same in both your experiments. When you draw a new coin every time, the covariance between any pair of flips is 0. However, when you reuse the same coin for different flips, the covariance between two flips which use the same coin is positive. So the total variance is higher. 


Try thinking about an extreme example. Suppose the bag contains two coins, one with probability 0 of heads and the other with probability 1 of heads. If you choose a coin from the bag and flip it N times, you will see either 0 or N heads. On the other hand, if you make N different draws and one flip after each of them, you are likely to see approximately N/2 heads. The variance is $N^2/4$ in the first case and $N/4$ in the second. 


