# Beta distribution

When trying to to Bayesian interference to estimate probability, we can use Beta distribution as a prior. Below are some steps to calculate it.

From the Data Science from Scratch book.

```
import math as m
```

$ f(x, \alpha, \beta) = x^{\alpha - 1} (1 - x)^{\beta - 1} \frac {1} {\text{B}(\alpha, \beta)} $

where

$ \text{B} = \Gamma(\alpha) \Gamma(\beta) \frac{1} {\Gamma(\alpha + \beta)} $

where

$\Gamma(n)$ is the Gamma function which, for positive integers is

$ \Gamma(n) = (n - 1)!$

```
def B(alpha: float, beta: float) -> float:
"This scales the parameters between 0 and 1"
return m.gamma(alpha) * m.gamma(beta) / m.gamma(alpha + beta)
```

```
def beta_pdf(x: float, alpha: float, beta:float) -> float:
if x <= 0 or x >=1: return 0
return x ** (alpha - 1) * (1 - x) ** (beta - 1) / B(alpha, beta)
```

```
import numpy as np
import pandas as pd
import altair as alt
```

```
df = pd.DataFrame()
Beta_combinations = [(1, 1), (10, 10), (4, 16), (16, 4), (30, 30)]
for Beta in Beta_combinations:
alpha, beta = Beta
df_B = pd.DataFrame()
df_B['x'] = pd.Series(np.arange(0.01, 1, .01))
df_B['y'] = df_B['x'].apply(lambda x: beta_pdf(x, alpha, beta))
df_B['Beta'] = f'({alpha}, {beta})'
df = pd.concat([df, df_B])
```

The distribution centers around $ \alpha \frac{1} {\alpha + \beta} $

- Beta(1, 1) is the uniform distribution in [1, 1]
- When alpha is greater than beta the distribution is skewed to the left (and respectively, in the opposite case)
- The greater alpha and beta are the 'tighter' is the distribution

```
alt.Chart(df).mark_line().encode(alt.X('x:Q'), alt.Y('y:Q'), alt.Color('Beta'), tooltip=['x', 'y', 'Beta'], strokeDash='Beta').properties(width=600)
```