Created
February 29, 2016 00:44
-
-
Save smutch/518a89a9ed048b4a2a26 to your computer and use it in GitHub Desktop.
py: boostrap generator
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 | |
class Bootstrap(): | |
def __init__(self, arr, n, statistic=np.mean): | |
self.ii = 0 | |
self.n = n | |
self.arr = np.array(arr).squeeze() | |
self.statistic = statistic | |
def __iter__(self): | |
return self | |
def __next__(self): | |
if self.ii < self.n: | |
self.ii += 1 | |
if self.statistic is not None: | |
return self.statistic(np.random.choice(self.arr, self.arr.size)) | |
else: | |
return np.random.choice(self.arr, self.arr.size) | |
else: | |
raise StopIteration() | |
def all(self): | |
return np.fromiter(self, 'f') | |
def ci(self, intervals=[95.0]): | |
intervals = list(intervals) | |
n_percentiles = len(intervals)*2 | |
percentiles = [(v/2.0, 100.0-(v/2.0)) for v in intervals] | |
reduced = self.all() | |
return [np.percentile(reduced, pc, overwrite_input=True) for pc in percentiles] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment