In [1]:
import numpy as np
import pandas as pd
import bplot as bp
from scipy.stats import norm as normal
In [16]:
X = np.random.normal(np.pi, 2, size=99)
N = X.size
np.median(X)
R = 1001
def bootstrap(data, R, fun):
    N = data.size
    thetas = np.full(R, np.nan)
    for r in range(R):
        idx = np.random.choice(N, N, replace=True)
        thetas[r] = fun(data[idx])
    return np.percentile(thetas, [25, 75])
bootstrap(X, R, np.median)
Out[16]:
array([2.72628071, 3.24028006])
In [18]:
S = 100
CI =np.full((S, 2), np.nan)
count = 0
bp.line_v(x=np.pi, ymin=0, ymax=S, color="black")
for s in range(S):
    X = np.random.normal(np.pi, 2, size=99)
    CI[s, :] = bootstrap(X, R, np.median)
    if CI[s, 0]< np.pi and CI[s, 1] >np.pi:
        count += 1
        bp.line_h(y=s, xmin=CI[s, 0], xmax=CI[s, 1], alpha=0.5)
    else:
        bp.line_h(y=s, xmin=CI[s, 0], xmax=CI[s, 1], color="orange")
np.round(count / S, 2)
Out[18]:
0.39