Created
February 20, 2016 15:28
-
-
Save atmb4u/6a4e5c0a032486a9b50f to your computer and use it in GitHub Desktop.
statistical bootstrap function in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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