MathOverflow is a question and answer site for professional mathematicians. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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: (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
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
        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
            tails_per_bucket += 1

        if flip_coin(draw_coin()) == Heads:
            heads_per_flip += 1
            tails_per_flip += 1

    # plot the draw per bucket results
    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',
                                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.axis([0, 1, 0, 20])

    # plot the draw per flip results
    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',
                                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.axis([0, 1, 0, 20])

share|cite|improve this question
my guess is that your two procedures are actually using two different random variables which arise from your underlying model. Is it really that surprising if different sample estimators for the same population parameter have different variances? – Yemon Choi May 2 '10 at 22:08
Yemon, I would have expected that over many draws that the two curves would look the same. Granted, I only have undergrad level familiarity with stats and probability. I'm posing this question as it basically summarizes the issue I'm seeing in a more complex simulation I am running, i.e. in the original context I'm seeing that a full simulator that introduces gaussian random vars at the bottom of a model is removing variance when compared to a monte carlo driven markov chain model that picks gaussian random vars and feeds them to the markov chain. I'd like to get my head around why. – peb May 2 '10 at 22:30
up vote 3 down vote accepted

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.

share|cite|improve this answer
Thanks. This helps me get my head around the difference in variance between the 2 experiments, and enables me to see what I need to do in the context of the actual problem that this is an abstraction of. Thanks for answering my undergrad level question :) – peb May 2 '10 at 23:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.