20 Free throw binomial probability distribution#

%%html
<iframe width="700" height="400" src="https://www.youtube.com/embed/eL965_Lscb8/" frameborder="0" allowfullscreen></iframe>
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats, special

khanacademy

Free throw binomial probability distribution fig 1Free throw binomial probability distribution fig 2

\[ P(X) = \frac{N!}{x!(N - x)!} \pi^{x} 1-\pi^{N-x} \]
n = 6
f = 0.7
x = range(7)
y = [special.comb(n, k) * f ** k * (1-f)**(n - k) for k in x]
plt.bar(x, y)
<BarContainer object of 7 artists>
../_images/20 Free throw binomial probability distribution_9_1.png
binom_data =stats.binom.rvs(n=6, p=0.7, size=1000)
sns.distplot(binom_data, kde=True, hist_kws={'alpha':1})
plt.xlabel('Binomial')
plt.ylabel('Frequency')
/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
Text(0, 0.5, 'Frequency')
../_images/20 Free throw binomial probability distribution_11_2.png