Skip to content

Instantly share code, notes, and snippets.

@atmb4u
Created February 20, 2016 15:28
Show Gist options
  • Save atmb4u/6a4e5c0a032486a9b50f to your computer and use it in GitHub Desktop.
Save atmb4u/6a4e5c0a032486a9b50f to your computer and use it in GitHub Desktop.
statistical bootstrap function in python
import numpy as np
import numpy.random as npr
import pylab
def bootstrap(data, num_samples, statistic, alpha):
"""Returns bootstrap estimate of 100.0*(1-alpha) CI for statistic."""
n = len(data)
idx = npr.randint(0, n, (num_samples, n))
samples = data[idx]
stat = np.sort(statistic(samples, 1))
return (stat[int((alpha/2.0)*num_samples)],
stat[int((1-alpha/2.0)*num_samples)])
data = np.array([1, 2, 3, 1000, 5])
low, high = bootstrap(data, 100000, np.mean, 0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment