<jupyter_start><jupyter_text>Variance of a sample mean<jupyter_code># initialMean = np.mean(data)
# initialMean
# initialSTD = np.std(data)
# initialSTD
from sklearn.utils import resample
import numpy as np
sampleSize = 3
data = np.array(range(1, sampleSize+1))
sampleMeans = []
sampleCount = 500
for i in range(sampleCount):
    boot = resample(data, replace=True, n_samples=sampleSize, random_state=i)
    sampleMeans.append(np.mean(boot))
sampleMeanVarHat = np.var(sampleMeans)
sampleMeanVarHat
from sklearn.utils import resample
import numpy as np
sampleSize = 3
data = np.array(range(1, sampleSize+1))
sampleMeans = []
sampleMeansVarHats = []
sampleCountList = [5, 105, 205, 305, 405]
for j, k in enumerate(sampleCountList):
    sampleCount = sampleCountList[j]
    for i in range(sampleCount):
        boot = resample(data, replace=True, n_samples=sampleSize, random_state=None)
        sampleMeans.append(np.mean(boot))
    sampleMeansVarHats.append(np.var(sampleMeans))

sampleMeansVarHats


from matplotlib import pyplot as plt
plt.grid()
plt.xlabel("number of experiment")
plt.ylabel("sampleMeanVarHat")
plt.plot(sampleCountList, sampleMeansByObs, marker='o')<jupyter_output><empty_output><jupyter_text>Now by formula<jupyter_code>sampleMeanVarHat2 = np.var(data) / sampleSize
sampleMeanVarHat2
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6, 3))
plt.hist(sampleMeans)<jupyter_output><empty_output><jupyter_text>``` This is formatted as code```Both above are close to each other - bootstrap and formulaNow let us use more special meansHowever, our lecture does not cover the rest of this file.<jupyter_code>import sklearn
print('The scikit-learn version is {}.'.format(sklearn.__version__))
#from sklearn.cross_validation.Bootstrap<jupyter_output>The scikit-learn version is 0.22.2.post1.<jupyter_text>In the new sklearn there is no Bootstrap class. But it is present in bootstrapped library. So we should install it by pip.<jupyter_code>!pip install bootstrapped
import numpy as np
import bootstrapped.bootstrap as bs
import bootstrapped.stats_functions as bs_stats

mean = 100
stdev = 10
size = 50000

population = np.random.normal(loc=mean, scale=stdev, size=size)

xBarStd = 10 / np.sqrt(size)

# take 1k 'samples' from the larger population
subSampleSize = 1000
samples = population[:subSampleSize]

print(bs.bootstrap(samples, stat_func=bs_stats.mean))

print(bs.bootstrap(samples, stat_func=bs_stats.std))
print(bs.bootstrap(data, stat_func=bs_stats.mean))
print(bs.bootstrap(data, stat_func=bs_stats.std))
print(bs.bootstrap.__doc__)

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import numpy.random as npr
import collections
import bootstrapped.bootstrap as bs
import bootstrapped.compare_functions as bs_compare
import bootstrapped.stats_functions as bs_stats
# take 1k 'samples' from the larger population
samples = population[:1000]
np.std([samples], axis=1)
# calculate bootstrap estimates for the mean and standard deviation
mean_results = bs.bootstrap(samples, stat_func=bs_stats.mean)
# see advanced_bootstrap_features.ipynb for a discussion of how to use the stat_func arg
stdev_results = bs.bootstrap(samples, stat_func=bs_stats.std)
print('Bootstrapped mean should be: {}'.format(mean))
print('\t' + str(mean_results))
print('')
print('Bootstrapped stdev should be: {}'.format(stdev))
print('\t' + str(stdev_results))

import scipy.stats as st
sample_size = [100, 350, 500, 1000, 2500, 3500, 5000, 8000, 10000]
bootstrap_results = []
normal_results = []
for i in sample_size:
    samples = np.random.choice(population, i, replace=True)
    bsr = bs.bootstrap(samples, stat_func=bs_stats.mean, alpha=0.05)
    
    mr = st.t.interval(1-0.05, len(samples)-1, loc=np.mean(samples), scale=st.sem(samples))

    bootstrap_results.append((bsr.lower_bound, bsr.upper_bound))
    normal_results.append(mr)


plt.rcParams['figure.figsize'] = [12, 8]
plt.rcParams['figure.dpi'] = 100 # 200 e.g. is really fine, but slower
plt.grid()
plt.plot(sample_size, [x[1] for x in bootstrap_results], c='blue')
plt.plot(sample_size, [x[1] for x in normal_results], linestyle='--', c='orange')

plt.plot(sample_size, [x[0] for x in bootstrap_results], c='blue', 
         label='Bootstrap')
plt.plot(sample_size, [x[0] for x in normal_results], linestyle='--', c='orange', 
         label='t-distribution')

plt.axhline(population.mean(), c='black', label='True Mean')
plt.legend(loc='best')
plt.title('t-distribution vs Bootstrap')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11823425022182786, 'fraction_numerical': 0.024179236912156166, 'mean_word_length': 5.969088098918084, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '621218', 'license': 'MIT', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1493, 'n_words': 390, 'path': 'Chapter 5/Python/bootstrap/1. bootstrap - var of sample mean.ipynb', 'repo_name': 'borisgarbuzov/schulich_data_science_1', 'size': '4508'}