# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

#Import packages
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
sns.set()

# Draw 1,000 samples from uniform & plot results
x = np.random.rand(1000)
plt.hist(x);

# +
# Bootstrap WRONG

proportions = []
for i in range(1000):                       # Modded to 1000 for testing
    subset = np.random.choice(x, size=200)  # Wrong size for question
    clicks = subset <= .5
    n_clicks = sum(clicks)
    prop = n_clicks/len(clicks)
    proportions.append(prop)           

print(np.mean(proportions), np.std(proportions)) # Backwards wrong table method!
bootm = np.mean(proportions)
boots = np.std(proportions)
print(bootm - 1.96*boots, bootm + 1.96*boots)   # Using true critical t's TOO WIDE

# +
# Bootstrap RIGHT

n = len(x)   # Sample size for each bootstrap resample (must equal original!)
m = 10000    # Number of bootstrap samples
alpha = 0.05 # Interval type (classical)

proportions = []
for i in range(m):                       
    bootsample = np.random.choice(x, size = n, replace = True)
    n_clicks   = sum(bootsample <= .5)
    prop = n_clicks/n
    proportions.append(prop)

sp = np.sort(proportions)     # Sort!

# For alpha/2, 1-alpha/2 interval, need to multiply against m the number of bootstrap samples:

print(sp[int(alpha/2*m)], sp[int((1-alpha/2)*m)])

# +
m = 1000
alpha = 0.05

int((alpha/2)*m)
# -

len(clicks)



StarcoderdataJupyterScriptsDedupFiltered