Skip to content

Instantly share code, notes, and snippets.

@3kwa
Last active August 29, 2015 14:05
Show Gist options
  • Save 3kwa/df3a6f512fba18a85c8c to your computer and use it in GitHub Desktop.
Save 3kwa/df3a6f512fba18a85c8c to your computer and use it in GitHub Desktop.
"""
Bootstrap in Python using numpy ... mind expanding!
source: http://people.duke.edu/~ccc14/pcfb/analysis.html
"""
from collections import namedtuple
import numpy
CI = namedtuple('CI', ['lower', 'upper'])
def bootstrap(data, num_samples, statistic, percentage):
"""
returns the bootstrap estimate of ci for statistic
data is a numpy.array
num_samples (an integer) is the number of samples with replacement to use
statistic is the statistical (universal) function to compute
percentage is the confidence interval range
>>> bootstrap(numpy.array([1, 4, 2, 5, 6, 2, 3, 5]),
... 1000,
... numpy.mean,
... 95) # doctest: +ELLIPSIS
CI(lower=..., upper=...)
"""
N = len(data)
index = numpy.random.randint(0, N, (num_samples, N))
samples = data[index]
alpha = 1 - percentage / 100.
statistics = numpy.sort(statistic(samples, 1))
return CI(statistics[int((alpha / 2) * num_samples)],
statistics[int((1 - alpha / 2) * num_samples)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment