# Example: Discrete Probability Distributions


```python
import numpy as np
import matplotlib.pyplot as plt
```

### Bernoulli Distribution
$P(X) = p$


```python
p = np.linspace(0, 1, 100)
var = p*(1-p)

plt.plot(p, var)
plt.title('variance vs p')
plt.show()
```

### Binomial Distribution
$p(X=k) = \binom{n}{k}p^kq^{n-k}$


```python
from utilities import binom_pmf
```


```python
# PMF

n = 10
k_range = np.arange(0, n+1)

for p in np.arange(.1, 1., .1):
    out = []
    for k in k_range:
        out.append(binom_pmf(k, n, p))
    
    plt.plot(k_range, out, label='p={}'.format(p.round(1)))

plt.xlabel('k')
plt.ylabel('p')
    
plt.legend()
plt.show()
```


```python
from utilities import binom_sampling
```


```python
# Sampling

for p in np.arange(.1, 1., .2):
    plt.hist(binom_sampling(10, p), bins=10, label='p={}'.format(p.round(1)))
plt.legend()
plt.show()
```

### Poisson Distribution
\begin{equation}
Y = \frac{\lambda^k}{k!}e^{-\lambda}
\end{equation}


```python
from utilities import poisson_pmf
```


```python
X = np.linspace(0, 10, 11)
```


```python
# PMF
for i, l in enumerate([1, 2, 3, 5, 7, 10]):
    Y = poisson_pmf(l, X)
    plt.plot(Y, label='lambda = {}'.format(l))
plt.legend()
plt.show()
```


```python
from utilities import poisson_sampling
```


```python
# sampling

n = 1000
for i, l in enumerate([.001, .002, .003, .005, .007, .01]):
    out = poisson_sampling(n, l)
    plt.hist(out, bins=20, label='lambda = {}'.format(round(l*n)))

plt.legend()
plt.show()
```

### Geometric Distribution


```python
from utilities import geometric_pmf, geometric_sampling
```


```python
# PMF

k = np.arange(1, 100)

for p in np.arange(.1, 1., .1):
    out = geometric_pmf(k, p)
    plt.plot(out[:5], label='p = {}'.format(round(p, 1)))

plt.legend()
plt.xlabel('k')
plt.show()
```


```python
# sampling

for p in np.arange(.1, 1., .1):
    plt.hist(geometric_sampling(p), label='p={}'.format(round(p, 1)))

plt.legend()
plt.xlabel('k')
plt.show()
```

e68690b7e3bcf77c80a28e14a0f7d42bf0a6b637
ipynb
Jupyter Notebook
Discrete+Distributions.ipynb
rkato5680/MathFromScratch
173c6c7794c632455793780e18d3692584d5b688
MIT
2021-02-04T03:34:23.000Z
2021-02-23T07:02:42.000Z
Discrete+Distributions.ipynb
rkato5680/MathFromScratch
173c6c7794c632455793780e18d3692584d5b688
MIT
Discrete+Distributions.ipynb
rkato5680/MathFromScratch
173c6c7794c632455793780e18d3692584d5b688
MIT
Qwen/Qwen-72B
1. YES
2. YES