02 Confidence interval simulation#

%%html
<iframe width="700" height="400" src="https://www.youtube.com/embed/bGALoCckICI/" 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

Confidence interval simulation fig 1

def confidence_interval(n, p, level=0.95):
    sem = np.sqrt(p*(1-p)/n)
    return stats.norm.interval(level, loc=p, scale=sem)
n = 50
p = 0.6
plt.plot([0.6, 0.6], [0, 20])
for i in range(1, 20):
    p_ = np.random.choice(np.arange(0.4, 0.8, 0.1))
    p_ = round(p_, 1)
    plt.plot(confidence_interval(n, p_), [i, i])
../_images/02 Confidence interval simulation_9_0.png
# need better implementation