Show code cell source
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib_inline
matplotlib_inline.backend_inline.set_matplotlib_formats('svg')
import seaborn as sns
sns.set_context("paper")
sns.set_style("ticks");
Example: Inferring the probability of a coin toss from data#
We toss a coin with an unknown probability of heads \(\theta\) \(N\) times independently, and we observe the result:
Assume that we have coded the result so that heads correspond to a “1” and tails to a “0.” We aim to estimate the probability of heads \(\theta\) from this dataset.
Assuming that we know nothing, we set:
In terms of probability densities, this is:
Now, let’s write down the likelihood of the data. Because of the independence assumption, we have:
Then, each measurement is a Bernoulli with probability of success \(\theta\), i.e.,
In terms of probability densities, we have the likelihood:
Using a common mathematical trick, we can rewrite this as:
Work out the cases \(x_n=0\) and \(x_n=1\) to convince yourself.
Now, we can find the expression for the likelihood of the entire dataset. It is:
This intuitively means the probability of getting \(\sum_{n=1}^Nx_n\) heads and the rest \(N-\sum_{n=1}^Nx_n\) tails.
We can now find the posterior. It is:
In our problem:
And this is just the density corresponding to a Beta distribution:
Let’s try this out with some fake data. Take a fake coin which is a little bit biased:
import scipy.stats as st
theta_true = 0.8
X = st.bernoulli(theta_true)
Sample from it a number of times to generate our data = (x1, …, xN):
N = 5
data = X.rvs(size=N)
data
array([0, 1, 1, 1, 1])
Now we are ready to calculate the posterior which the Beta we have above:
alpha = 1.0 + data.sum()
beta = 1.0 + N - data.sum()
Theta_post = st.beta(alpha, beta)
And we can plot it:
fig, ax = plt.subplots()
thetas = np.linspace(0, 1, 100)
ax.plot(
[theta_true],
[0.0],
'o',
markeredgewidth=2,
markersize=10,
label='True value'
)
ax.plot(
thetas,
Theta_post.pdf(thetas),
label=r'$p(\theta|x_{1:N})$'
)
ax.set_xlabel(r'$\theta$')
ax.set_ylabel('Probability density')
ax.set_title(f'$N={N}$')
plt.legend(loc='best', frameon=False)
sns.despine(trim=True);
Questions#
Try \(N=0,5,10,100\) and see what happens.