```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
```


```python
n_params = [1, 2, 4]  # Number of trials
p_params = [0.25, 0.5, 0.75]  # Probability of success

x = np.arange(0, max(n_params)+1)
f,ax = plt.subplots(len(n_params), len(p_params), sharex=True, 
                    sharey=True,
                    figsize=(8, 7), constrained_layout=True)

for i in range(len(n_params)):
    for j in range(len(p_params)):
        n = n_params[i]
        p = p_params[j]

        y = stats.binom(n=n, p=p).pmf(x)

        ax[i,j].vlines(x, 0, y, colors='C0', lw=5)
        ax[i,j].set_ylim(0, 1)
        ax[i,j].plot(0, 0, label="N = {:3.2f}\nθ = {:3.2f}".format(n,p), alpha=0)
        ax[i,j].legend()

        ax[2,1].set_xlabel('y')
        ax[1,0].set_ylabel('p(y | θ, N)')
        ax[0,0].set_xticks(x)

```

### Choosing the prior

As a prior we will use a beta distribution, which is a very common distribution:

\begin{equation}
p\left(\theta\right) = \frac{\Gamma\left(\alpha+\beta\right)}{\Gamma\left(\alpha\right)\Gamma\left(\beta\right)}\theta^(\alpha - 1)\left(1-\theta\right)^{\beta -1}
\end{equation}

The beta distribution is a common choice of prior in Bayesian statistics:


```python
params = [0.5, 1, 2, 3]
x = np.linspace(0, 1, 100)
f, ax = plt.subplots(len(params), len(params), sharex=True, 
                     sharey=True,
                     figsize=(8, 7), constrained_layout=True)
for i in range(4):
    for j in range(4):
        a = params[i]
        b = params[j]
        y = stats.beta(a, b).pdf(x)
        ax[i,j].plot(x, y)
        ax[i,j].plot(0, 0, label="α = {:2.1f}\nβ = {:2.1f}".format(a, 
                     b), alpha=0)
        ax[i,j].legend()
ax[1,0].set_yticks([])
ax[1,0].set_xticks([0, 0.5, 1])
f.text(0.5, 0.05, 'θ', ha='center')
f.text(0.07, 0.5, 'p(θ)', va='center', rotation=0)
```


```python

```

d0ab0457ddff44f0847c277d51024527e639b785
ipynb
Jupyter Notebook
Chapter1/bayesian_introduction.ipynb
nathdipankar/Bayesian-Statistics
444d0476e61e2ce61ae39bf0bb30c6d0e1b3d2c9
MIT
Chapter1/bayesian_introduction.ipynb
nathdipankar/Bayesian-Statistics
444d0476e61e2ce61ae39bf0bb30c6d0e1b3d2c9
MIT
Chapter1/bayesian_introduction.ipynb
nathdipankar/Bayesian-Statistics
444d0476e61e2ce61ae39bf0bb30c6d0e1b3d2c9
MIT
Qwen/Qwen-72B
1. YES
2. YES