In [1]:
import numpy as np
rng = np.random.default_rng()
num_repetitions = 20000
L = []
for i in np.arange(num_repetitions):
L.append(rng.choice([0, 1], size=100, replace=True, p=[0.74, 0.26]).sum())
from matplotlib.pyplot import subplots
fig, ax = subplots(figsize = (8, 8))
ax.hist(L, bins = np.arange(0, 50, 1));
ax.scatter(8, 0, color='red', s=100);
In [2]:
import pandas as pd
data = [['Asian/PI', 0.15, 0.26],
['Black/AA', 0.18, 0.08],
['Caucasian', 0.54, 0.54],
['Hispanic', 0.12, 0.08],
['Other',0.01, 0.04]]
df = pd.DataFrame(data, columns=['Ethnicity', 'Eligible', 'Panels'])
print(df)
df.plot.barh('Ethnicity');
Ethnicity Eligible Panels 0 Asian/PI 0.15 0.26 1 Black/AA 0.18 0.08 2 Caucasian 0.54 0.54 3 Hispanic 0.12 0.08 4 Other 0.01 0.04
In [3]:
x, y = np.unique(rng.choice(df['Ethnicity'], size=1453, replace=True, p=df['Eligible']), return_counts=True)
y = y/1453
print(y)
df.insert(loc=3, column='Random', value=y)
print(df)
df.plot.barh('Ethnicity')
[0.15485203 0.16311081 0.54439092 0.12594632 0.01169993] Ethnicity Eligible Panels Random 0 Asian/PI 0.15 0.26 0.154852 1 Black/AA 0.18 0.08 0.163111 2 Caucasian 0.54 0.54 0.544391 3 Hispanic 0.12 0.08 0.125946 4 Other 0.01 0.04 0.011700
Out[3]:
<Axes: ylabel='Ethnicity'>
In [4]:
def calculate_tvd(X, Y):
A = np.abs(X - Y).sum()/2
return A
num_repetitions = 10000
L=[]
for i in range(num_repetitions):
x, y = np.unique(rng.choice(df['Ethnicity'], size=1453, replace=True, p=df['Eligible']), return_counts=True)
y = y/1453
L.append(calculate_tvd(df['Eligible'], y))
fig, ax = subplots(figsize = (8, 8))
ax.hist(L, bins=np.arange(0.0, 0.2, 0.001));
ax.scatter(0.14, 0, color='red')
Out[4]:
<matplotlib.collections.PathCollection at 0x104e43cd0>
In [ ]: