Skip to content

Instantly share code, notes, and snippets.

@StrikingLoo
Last active July 14, 2022 05:03
Show Gist options
  • Save StrikingLoo/f309573b19d075751ebf010c4c863b9a to your computer and use it in GitHub Desktop.
Save StrikingLoo/f309573b19d075751ebf010c4c863b9a to your computer and use it in GitHub Desktop.
A simple implementation for a confidence interval using bootstrapping
import random
import numpy as np
population = np.random.uniform(size = 5000)
def confidence_interval(population, confidence = 95, aggregation = np.mean, samples = 500, sample_n = 500):
if population.shape[0] < sample_n:
sample_n = population.shape[0]//2
aggs = []
for _ in range(samples):
aggs.append(aggregation(
np.random.choice(population, sample_n)))
lower, higher = (100 - confidence)/2, confidence/2 + 50
return np.percentile(np.asarray(aggs), [lower, 50, higher])
for _ in range(5):
print(confidence_interval(population, samples =100, sample_n = 1000, aggregation= np.median))
print(np.median(population))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment