Long Run Proportion of Heads

On this page

Fix $p$ for now, and consider a sequence of i.i.d. Bernoulli $(p)$ trials $I_1, I_2, \ldots$. By the Weak Law of Large Numbers, the proportion of successes is likely to be close to $p$ in the number of trials is large. But something even stronger is true, and is known as the Strong Law: with probability 1, the sample proportion converges to $p$.

Establishing the Strong Law is beyond the scope of this course, but we can easily visualize the result by simulation.

We will start with the function binomial_proportion that takes $p$ and $n$ as its arguments and returns an array consisting of the sequence ${ \frac{S_k}{k}: k = 1, 2, \ldots, n}$ where $S_k = I_1 + I_2 + \cdots + I_k$. That’s the sequence of the proportions of heads in trials 1 through $n$.

The array above is the sequence of proportions of heads in 4 tosses of a fair coin. See if you can work out the corresponding sequence of heads and tails.

The function plot_binomial_proportions plots the proportions. Its first argument is an array of values of $p$ and its second is the number of trials. For each value of $p$ in the array, the function runs $n$ i.i.d. Bernoulli $(p)$ trials, computes the proportions of successes using binomial_proportions, and plots them againt the trial numbers $k = 1, 2, \ldots , n$.

We will call each of the plots a path of the observed proportions.

defplot_binomial_proportions(p_array,n):forpinp_array:plt.plot(np.arange(1,n+1),binomial_proportions(p,n),lw=2)plt.ylim(-0.05,1.05)plt.xlabel('$k$')plt.title('Proportion of Successes in $k$ Trials');

Let’s use plot_binomial_proportions to simulate two sets of 1000 fair coin tosses and keep track of the proportion of heads.

The paths fluctuate when the number of trials is low, but settle down to very near 0.5 as the number of trials increases.

If you are wondering why $p$ has to be entered as an array instead of just one number, be patient for a bit. Generate 10 paths of the proportions of sixes in 1000 rolls of a die, by first creating an array of 10 values of $p$, each of which is 1/6.

ten_dice=(1/6)*np.ones(10)plot_binomial_proportions(ten_dice,1000)

These paths level off at $1/6$.

In general, the proportions of heads converge to the fixed value of $p$.

Randomizing $p$

Now let’s choose the initial $p$ at random. We’ll start with a simple prior and pick $p$ to be either 0.2 or 0.7 with chance 1/2 each.

The process then becomes:

Choose a value of $p$ uniformly from 0.2 and 0.7.

With that chosen value of $p$, toss a $p$-coin and keep track of the proportion of heads.

To generate paths of this process, we have to start with a random choice of $p$. This is why we have been specifying the values of $p$ as an array. The length of the array is the number of paths to plot.

Here are 10 paths. Notice how np.random.choice is being used to pick $p$. You have used this method in your labs.

Now you can see about half the paths settling down at 0.2 and the other half at 0.7. This makes sense: about half the time, the chosen value of $p$ will be 0.2, and those paths will settle at 0.2 as we saw in the earlier examples. The other half of the time, the chosen $p$ will be 0.7 and the corresponding paths will settle at 0.7

What you have observed is that if $n$ is large, the distribution of the observed proportion at time $n$ is close to the prior distribution of $p$.

Beta Prior

Suppose we start with the uniform prior. As in the previous section, we let $X$ be uniform on $(0, 1)$, and given $X = p$ we run i.i.d. Bernoulli $(p)$ trials. Here are 10 paths of this process, consisting of 1000 trials each. Remember that the uniform $(0, 1)$ distribution is the same as beta $(1, 1)$.

Look at the right edge of the graph. That’s a sample of 10 points from the distribution of the sample proportion of successes in 1000 trials. Run the cell a few times and you should see that the distribution looks roughly uniform.

Indeed, from bottom to top you are essentially seeing the order statistics of 10 i.i.d. uniform $(0, 1)$ random variables.

Here are 15 paths of the proportion of successes when the probability of success is picked according to the beta $(2, 8)$ distribution.