How many samples?

In my Monte Carlo solution of the Two Child Problem, I did 10,000 simulations using Python’s random module. Was that enough? If I didn’t already know the answers (the usual case when doing Monte Carlo simulation), how could I estimate the accuracy of the results? Not surprisingly, the answers to these results can be found in the field of statistics.

When the process being simulated consists of a series of independent trials, each trial having one of two results (which we’ll call passes and fails, although any two mutually exclusive results could be used), it’s said to be a Bernoulli sequence, and the count of passes follows a binomial distribution

P(xtextrmpassesinntextrmtrials)=n!x!(nx)!p x(1p) nx

where p is the probability of a passing a single trial.1 You may recognize the term with the factorials as the binomial coefficient.

An estimate of p is easily calculated:

p^=xn

where we put the hat over the p to distinguish the estimate from the true (albeit typically unknown) value.

Because we could get a different value of x every time we run a set of n trials, p^ is a random variable. If the number of trials is large enough, p^ will have a normal distribution with a mean of

μ p^=p

and a standard deviation of

σ p^=p(1p)n

That the mean of p^ is equal to p signifies that p^ is an unbiased estimate of p: as we increase n, p^ will tend toward p.

Because we know the distribution of p^ and its parameters, we can calculate the probability that p^ will fall within some neighborhood, ±δ, around p,

P(pδp^p+δ)=Φ(δσ p^)Φ(δσ p^)=12Φ(δσ p^)

where Φ is the standard normal cumulative distribution function,

Φ(z)= z12πe ζ 2/2dζ

The Φ function can’t be integrated analytically, but many numerical analysis programs include a function for it. Octave has a function called normcdf that does the trick. Back in the not-so-good old days, you’d have to look up it up in a table; every statistics textbook had a table of Φ in its appendix.

This is all very nice, but you can only do these calculations if you know p, and the typical reason you’re doing the Monte Carlo simulation is that you don’t know p. What you have is a particular value of p^ and you want to know how good it is.

Enter the concept of the confidence interval. By flipping the terms around and using p^ to estimate the standard deviation,

σ^ p^=p^(1p^)n

we can say with a confidence2 of

12Φ(δσ^ p^)

that the true value of p is in the interval

p^±δ

A more convenient way to work is to introduce a new variable,

α=Φ(δσ^ p^)

and choose a confidence level of 12α. Then

δ=Φ 1(α)σ^ p^=Φ 1(1α)σ^ p^

where Φ 1 is the inverse of the standard normal CDF (Octave function norminv).

The confidence interval at this level is

p^±Φ 1(1α)σ^ p^

or, expanding out the σ^ p^,

p^±Φ 1(1α)p^(1p^)n

That was a longish derivation, but the result is simple to use. Let’s say we’ve done a Monte Carlo simulation and we want to get the 95% confidence interval for p. Here’s what we do:

  1. Note that for 95% confidence, α=0.025.
  2. Use whatever tools available to determine that Φ 1(0.975)=1.96.
  3. Take the values of n and p^ from the simulation and calculate the interval

    p^±1.96p^(1p^)n

Now let’s say we’re planning a simulation and want to choose a value of n to get a decent estimate of p.

  1. For 95% confidence, use Φ 1(0.975)=1.96, as before.
  2. Get a preliminary value of p^, perhaps by doing a small Monte Carlo run.
  3. Decide how tight we want the confidence interval, ±δ, to be.
  4. Calculate n via

    n=p^(1p^)(1.96δ) 2

Note that if we want a very tight confidence interval, δ will be small and n will have to be large. This makes sense: to get a close estimate, we need a lot of samples.

Although it’s a common confidence level, there’s nothing magical about 95%. We could just as easily use another value. Here are the Φ 1 values for a few others.

Confidence α Φ 1(1α)
99% 0.005 2.576
95% 0.025 1.960
90% 0.050 1.645
75% 0.125 1.150
50% 0.250 0.675

As expected, to get higher confidence, we need more samples.

Let’s calculate the confidence interval for the Monte Carlo simulation of the first example of the Two Child Problem. The result was

If we restrict ourselves to families that have at least one son,
the probability of having two sons is 2520/7535 = 0.334

So n is 75353, and p^ is 0.334. The 99% confidence interval is

0.334±2.5760.3340.6667535=0.334±0.014

So if we didn’t already know from earlier work that the true probability was 1/3, we could use this result to say with 99% confidence that the true probability was between 0.320 and 0.348.

I should mention that the formulas in this post aren’t restricted to Monte Carlo simulation. They’re valid any time you’re sampling from a pass/fail type of population.


  1. The count of fails also follows a binomial distribution; just substitute 1p for p and nx for x

  2. We use the word confidence instead of probability because we’re really not calculating a probability here. The true value of p isn’t a random variable, it’s a number that we just don’t happen to know. 

  3. Why isn’t it 10,000? In this simulation we filtered out about a quarter of the sample families because they didn’t have sons, leaving 7535.