Last active
September 30, 2016 01:30
-
-
Save phobson/30f2428395cd9cfeef2c6f31d59eb747 to your computer and use it in GitHub Desktop.
Bootstrapping Stats in Python and JS
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
var jStat = require('jStat').jStat; | |
function make_bs_samples(somearray, num_iter) { | |
var all_samples = []; | |
for (var ii = num_iter - 1; ii >= 0; ii--) { | |
var this_sample = []; | |
for (var jj = somearray.length - 1; jj >= 0; jj--) { | |
var index = Math.floor(Math.random() * somearray.length); | |
this_sample.push(somearray[index]); | |
} | |
all_samples.push(this_sample); | |
} | |
return all_samples; | |
}; | |
function percentile_bootstrap(somearray, statfxn, num_iter, alpha) { | |
var lower_percentile = alpha * 0.5; | |
var upper_percentile = 1 - lower_percentile; | |
var samples = make_bs_samples(somearray, num_iter); | |
var stat_array = []; | |
for (var ii = samples.length - 1; ii >= 0; ii--) { | |
stat_array.push(statfxn(samples[ii])); | |
} | |
result = { | |
'primary': statfxn(somearray), | |
'lower': jStat.percentile(stat_array, lower_percentile), | |
'upper': jStat.percentile(stat_array, upper_percentile), | |
} | |
return result; | |
}; |
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 | |
def make_boot_index(num_elements, num_iter): | |
return numpy.random.randint(low=0, high=num_elements, size=(num_iter, num_elements)) | |
def percentile(somearray, statfxn, num_iter, alpha=0.05): | |
index = make_boot_index(data.shape[0], num_iter) | |
boot_stats = statfxn(somearray[index], axis=-1) | |
# compute the `alpha/2` and `1-alpha/2` percentiles of `boot_stats` | |
CI = numpy.percentile(boot_stats, [alpha*50, 100-(alpha*50)], axis=0) | |
return statfxn(somearray), CI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment