11. Two Meanings of Probability#

11.1. Overview#

This lecture illustrates two distinct interpretations of a probability distribution

  • A frequentist interpretation as relative frequencies anticipated to occur in a large IID. sample

  • A Bayesian interpretation as a personal opinion (about a parameter or list of parameters) after seeing a collection of observations

We recommend watching this video about hypothesis testing within the frequentist approach

After you watch that video, please watch the following video on the Bayesian approach to constructing coverage intervals

After you are familiar with the material in these videos, this lecture uses the Socratic method to help consolidate your understanding of the different questions that are answered by

  • a frequentist confidence interval

  • a Bayesian coverage interval

We do this by inviting you to write some Python code.

It would be especially useful if you tried doing this after each question that we pose for you, before proceeding to read the rest of the lecture.

We provide our own answers as the lecture unfolds, but you’ll learn more if you try writing your own code before reading and running ours.

Code for answering questions:

To answer our coding questions, we’ll start with some imports

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import binom
import scipy.stats as st
Matplotlib is building the font cache; this may take a moment.

Empowered with these Python tools, we’ll now explore the two meanings described above.

11.2. Frequentist Interpretation#

Consider the following classic example.

The random variable \(X \) takes on possible values \(k = 0, 1, 2, \ldots, n\) with probabilities

\[ \mathbb{P}\{X = k \mid \theta\} = \left(\frac{n!}{k! (n-k)!} \right) \theta^k (1-\theta)^{n-k} \]

where the fixed parameter \(\theta \in (0,1)\).

This is called the binomial distribution.

Here

  • \(\theta\) is the probability that one toss of a coin will be a head, an outcome that we encode as \(Y = 1\).

  • \(1 -\theta\) is the probability that one toss of the coin will be a tail, an outcome that we denote \(Y = 0\).

  • \(X\) is the total number of heads that came up after flipping the coin \(n\) times.

Consider the following experiment:

Take \(I\) independent sequences of \(n\) independent flips of the coin

Notice the repeated use of the adjective independent:

  • we use it once to describe that we are drawing \(n\) independent times from a Bernoulli distribution with parameter \(\theta\) to arrive at one draw from a Binomial distribution with parameters \(\theta,n\).

  • we use it again to describe that we are then drawing \(I\) sequences of \(n\) coin draws.

Let \(y_h^i \in \{0, 1\}\) be the realized value of \(Y\) on the \(h\)th flip during the \(i\)th sequence of flips.

Let \(\sum_{h=1}^n y_h^i\) denote the total number of times heads come up during the \(i\)th sequence of \(n\) independent coin flips.

Let \(f_k\) record the fraction of samples of length \(n\) for which \(\sum_{h=1}^n y_h^i = k\):

\[ f_k^I = \frac{\textrm{number of samples of length n for which } \sum_{h=1}^n y_h^i = k}{ I} \]

The probability \(\mathbb{P}\{X = k \mid \theta\}\) answers the following question:

  • As \(I\) becomes large, in what fraction of \(I\) independent draws of \(n\) coin flips should we anticipate \(k\) heads to occur?

As usual, a law of large numbers justifies this answer.

Exercise 11.1

  1. Please write a Python class to compute \(f_k^I\)

  2. Please use your code to compute \(f_k^I, k = 0, \ldots , n\) and compare them to \(\mathbb{P}\{X = k \mid \theta\}\) for various values of \(\theta, n\) and \(I\)

  3. With the Law of Large Numbers in mind, use your code to describe the relationship between \(f_k^I\) and \(\mathbb{P}\{X = k \mid \theta\}\) as \(I\) grows

Let’s do some more calculations.

Comparison with different \(\theta\)

Now we fix

\[ n=20, k=10, I=1,000,000 \]

We’ll vary \(\theta\) from \(0.01\) to \(0.99\) and plot outcomes against \(\theta\).

rng = np.random.default_rng(234)
θ_low, θ_high, n_thetas = 0.01, 0.99, 50
thetas = np.linspace(θ_low, θ_high, n_thetas)
P = []
f_kI = []
for i in range(n_thetas):
    freq = Frequentist(thetas[i], n, I, rng=rng)
    freq.binomial(k)
    freq.draw()
    freq.compute_fk(k)
    P.append(freq.P)
    f_kI.append(freq.f_kI)
fig, ax = plt.subplots(figsize=(8, 6))
ax.grid()
ax.plot(thetas, P, 'k-.', label='Theoretical')
ax.plot(thetas, f_kI, 'r--', label='Fraction')
ax.set_title(r'Comparison with different $\theta$',
             fontsize=16)
ax.set_xlabel(r'$\theta$', fontsize=15)
ax.set_ylabel('Fraction', fontsize=15)
ax.tick_params(labelsize=13)
ax.legend()
plt.show()
_images/ac2b33e3d6017b3c87cfe8e8d519f7f56e86d7911c1434c6894e48c2cb6d81b2.png

Comparison with different \(n\)

Now we fix \(\theta=0.7, k=10, I=1,000,000\) and vary \(n\) from \(1\) to \(100\).

Then we’ll plot outcomes.

rng = np.random.default_rng(345)
n_low, n_high, n_ns = 1, 100, 50
ns = np.linspace(n_low, n_high, n_ns, dtype='int')
P = []
f_kI = []
for i in range(n_ns):
    freq = Frequentist(θ, ns[i], I, rng=rng)
    freq.binomial(k)
    freq.draw()
    freq.compute_fk(k)
    P.append(freq.P)
    f_kI.append(freq.f_kI)
fig, ax = plt.subplots(figsize=(8, 6))
ax.grid()
ax.plot(ns, P, 'k-.', label='Theoretical')
ax.plot(ns, f_kI, 'r--', label='Frequentist')
ax.set_title(r'Comparison with different $n$',
             fontsize=16)
ax.set_xlabel(r'$n$', fontsize=15)
ax.set_ylabel('Fraction', fontsize=15)
ax.tick_params(labelsize=13)
ax.legend()
plt.show()
_images/2a8ce35f8537883bfd27ab6121df4a65664c07ea6da9c4dc18b3e62abce91262.png

Comparison with different \(I\)

Now we fix \(\theta=0.7, n=20, k=10\) and vary \(\log(I)\) from \(2\) to \(6\).

rng = np.random.default_rng(456)
I_log_low, I_log_high, n_Is = 2, 6, 200
log_Is = np.linspace(I_log_low, I_log_high, n_Is)
Is = np.power(10, log_Is).astype(int)
P = []
f_kI = []
for i in range(n_Is):
    freq = Frequentist(θ, n, Is[i], rng=rng)
    freq.binomial(k)
    freq.draw()
    freq.compute_fk(k)
    P.append(freq.P)
    f_kI.append(freq.f_kI)
fig, ax = plt.subplots(figsize=(8, 6))
ax.grid()
ax.plot(Is, P, 'k-.', label='Theoretical')
ax.plot(Is, f_kI, 'r--', label='Fraction')
ax.set_title(r'Comparison with different $I$',
             fontsize=16)
ax.set_xlabel(r'$I$', fontsize=15)
ax.set_ylabel('Fraction', fontsize=15)
ax.tick_params(labelsize=13)
ax.legend()
plt.show()
_images/d51813fe6228e08556ca7bdfa8bc4d3e2e8e137727c078fd8bc668def5451dcb.png

From the above graphs, we can see that \(I\), the number of independent sequences, plays an important role.

When \(I\) becomes larger, the difference between theoretical probability and frequentist estimate becomes smaller.

Also, as long as \(I\) is large enough, changing \(\theta\) or \(n\) does not substantially change the accuracy of the observed fraction as an approximation of \(\mathbb{P}\{X = k \mid \theta\}\).

The Law of Large Numbers is at work here.

For each draw of an independent sequence, \(\mathbb{P}\{X_i = k \mid \theta\}\) is the same, so aggregating all draws forms an IID sequence of a binary random variable \(\rho_{k,i},i=1,2,...I\), with a mean of \(\mathbb{P}\{X = k \mid \theta\}\) and a variance of

\[ \mathbb{P}\{X = k \mid \theta\} \cdot (1-\mathbb{P}\{X = k \mid \theta\}). \]

So, by the LLN, the average of \(\rho_{k,i}\) converges to:

\[ \mathbb{E}[\rho_{k,i}] = \mathbb{P}\{X = k \mid \theta\} = \left(\frac{n!}{k! (n-k)!} \right) \theta^k (1-\theta)^{n-k} \]

as \(I\) goes to infinity.

11.3. Bayesian Interpretation#

We again use a binomial distribution.

But now we don’t regard \(\theta\) as being a fixed number.

Instead, we think of it as a random variable.

\(\theta\) is described by a probability distribution.

But now this probability distribution means something different than a relative frequency that we can anticipate to occur in a large IID. sample.

Instead, the probability distribution of \(\theta\) is now a summary of our views about likely values of \(\theta\) either

  • before we have seen any data at all, or

  • before we have seen more data, after we have seen some data

Thus, suppose that, before seeing any data, you have a personal prior probability distribution with density

\[ p(\theta) = \frac{\theta^{\alpha-1}(1-\theta)^{\beta -1}}{B(\alpha, \beta)} \]

where \(B(\alpha, \beta)\) is a beta function , so that \(p(\theta)\) is the density of a beta distribution with parameters \(\alpha, \beta\).

We can update this prior after observing data using Bayes’ Law (see Probability with Matrices for an introduction).

For a sample of \(n\) coin flips that yields \(k\) heads, the likelihood function is the binomial probability

\[ L(k \mid \theta) = {n \choose k} \theta^k (1-\theta)^{n-k} \]

Applying Bayes’ Law with our beta prior, the posterior density is

\[ p(\theta \mid k) = \frac{L(k \mid \theta) \cdot p(\theta)}{\int_0^1 L(k \mid \theta) \cdot p(\theta) \, d\theta} = \textrm{Beta}(\alpha + k, \, \beta + n - k) \]

So the posterior is also a beta distribution — a consequence of the beta prior being conjugate to the binomial likelihood.

Exercise 11.2

a) Please write down the likelihood function for a single coin flip with outcome \(Y \in \{0, 1\}\).

b) Please write down the posterior distribution for \(\theta\) after observing that single flip.

c) Now pretend that the true value of \(\theta = .4\) and that someone who doesn’t know this has a beta prior distribution with parameters \(\beta = \alpha = .5\). Please write a Python class to simulate this person’s personal posterior distribution for \(\theta\) for a single sequence of \(n\) draws.

d) Please plot the posterior distribution for \(\theta\) as a function of \(\theta\) as \(n\) grows as \(1, 2, \ldots\).

e) For various \(n\)’s, please describe and compute a Bayesian coverage interval for the interval \([.45, .55]\).

f) Please tell what question a Bayesian coverage interval answers.

g) Please compute the Posterior probability that \(\theta \in [.45, .55]\) for various values of sample size \(n\).

h) Please use your Python class to study what happens to the posterior distribution as \(n \rightarrow + \infty\), again assuming that the true value of \(\theta = .4\), though it is unknown to the person doing the updating via Bayes’ Law.

How shall we interpret the patterns above?

The answer is encoded in the Bayesian updating formula derived above.

Recall that after observing \(k\) heads in \(n\) flips, the posterior is \(\textrm{Beta}(\alpha + k, \, \beta + n - k)\).

A beta distribution with parameters \(\alpha\) and \(\beta\) has

  • mean \(\frac{\alpha}{\alpha + \beta}\)

  • variance \(\frac{\alpha \beta}{(\alpha + \beta)^2 (\alpha + \beta + 1)}\)

Here \(\alpha + k\) can be viewed as the number of successes (prior pseudo-count plus observed heads) and \(\beta + n - k\) as the number of failures.

Since the data are generated with \(\theta = 0.4\), the Law of Large Numbers tells us that, as \(n\) grows, \(k/n \to 0.4\) (see Exercise 11.1).

Consequently, the posterior mean converges to \(0.4\) and the posterior variance shrinks to zero.

upper_bound = [post.ppf(0.95) for post in bayes.posterior_list]
lower_bound = [post.ppf(0.05) for post in bayes.posterior_list]

fig, ax = plt.subplots(figsize=(10, 6))
ax.scatter(np.arange(len(upper_bound)),
           upper_bound, label='95th Quantile')
ax.scatter(np.arange(len(lower_bound)),
           lower_bound, label='5th Quantile')

ax.set_xticks(np.arange(0, len(upper_bound), 2))
ax.set_xticklabels(n_obs_list[::2])
ax.set_xlabel('Number of Observations', fontsize=12)
ax.set_title('Bayesian Coverage Intervals of '
             'Posterior Distributions', fontsize=15)

ax.legend(fontsize=11)
plt.show()
_images/03cb0ff7dd949ffb3119c92470bd785d72c766e96be96b22c03a8d8261513dcb.png

After observing a large number of outcomes, the posterior distribution collapses around \(0.4\).

Thus, the Bayesian statistician comes to believe that \(\theta\) is near \(.4\).

As shown in the figure above, as the number of observations grows, the Bayesian coverage intervals (BCIs) become narrower and narrower around \(0.4\).

However, if you take a closer look, you will find that the centers of the BCIs are not exactly \(0.4\), due to the persistent influence of the prior distribution and the randomness of the simulation path.

11.4. Role of a Conjugate Prior#

We have made assumptions that link functional forms of our likelihood function and our prior in a way that has eased our calculations considerably.

In particular, our assumptions that the likelihood function is binomial and that the prior distribution is a beta distribution have the consequence that the posterior distribution implied by Bayes’ Law is also a beta distribution.

So posterior and prior are both beta distributions, albeit ones with different parameters.

When a likelihood function and prior fit together like hand and glove in this way, we can say that the prior and posterior are conjugate distributions.

In this situation, we also sometimes say that we have conjugate prior for the likelihood function \(\mathbb{P}\{X \mid \theta\}\).

Typically, the functional form of the likelihood function determines the functional form of a conjugate prior.

A natural question to ask is why should a person’s personal prior about a parameter \(\theta\) be restricted to be described by a conjugate prior?

Why not some other functional form that more sincerely describes the person’s beliefs?

To be argumentative, one could ask, why should the form of the likelihood function have anything to say about my personal beliefs about \(\theta\)?

A dignified response to that question is, well, it shouldn’t, but if you want to compute a posterior easily you’ll just be happier if your prior is conjugate to your likelihood.

Otherwise, your posterior won’t have a convenient analytical form and you’ll be in the situation of wanting to apply the Markov chain Monte Carlo techniques deployed in Non-Conjugate Priors.

We also apply these powerful methods to approximating Bayesian posteriors for non-conjugate priors in Posterior Distributions for AR(1) Parameters and Forecasting an AR(1) Process.