Skip to content

Instantly share code, notes, and snippets.

@npyoung
Created October 3, 2015 00:16
Show Gist options
  • Save npyoung/648c73f63c5be9fe869b to your computer and use it in GitHub Desktop.
Save npyoung/648c73f63c5be9fe869b to your computer and use it in GitHub Desktop.
def timeshuffle_bootstrap(dview, statistic, data, axis=-1, n_boot=1000):
"""Perform bootstrap resampling to assess the significance of a statistic by reshuffling in time.
Args:
dview: IPython client (i.e. dview = IPython.parallel.Client()[:]) for parallelization.
statistic: Function that takes the dataset and computes some statistic (or array of statistics).
data: Data from which the statistic is computed.
axis: Axis in the data the shuffle along (usually time).
"""
T = data.shape[axis]
shufflers = [np.random.permutation(T) for _ in xrange(n_boot)]
dview.push({'data': data, 'statistic':statistic, 'axis':axis}, block=True)
def shuffle_and_compute(shuffler):
global data, statistic, axis
shuffled = data.take(shuffler, axis)
return statistic(shuffled)
shuffles = dview.map_sync(shuffle_and_compute, shufflers)
original = statistic(data)
shuffle_arr = np.concatenate([shuffle[...,None] for shuffle in shuffles], axis=-1)
mean = shuffle_arr.mean(axis=-1)
sd = np.sqrt(shuffle_arr.var(axis=-1, ddof=1))
zscore = (original - mean) / sdå
return zscore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment