braintools.analysis.stats.bootstrap_ci

Contents

braintools.analysis.stats.bootstrap_ci#

braintools.analysis.stats.bootstrap_ci(key, data, statistic, n_resamples=1000, confidence=0.95, method='percentile', axis=0)#

Bootstrap confidence interval of a statistic.

n_resamples bootstrap samples are drawn by resampling the entries of data along axis with replacement, and statistic is evaluated on each [1]. With \(\alpha = (1 - \mathrm{confidence}) / 2\) and \(q_p\) the \(p\)-quantile of the bootstrap distribution \(\hat\theta^*\):

  • 'percentile': \((q_\alpha,\ q_{1-\alpha})\);

  • 'basic': \((2\hat\theta - q_{1-\alpha},\ 2\hat\theta - q_\alpha)\), with \(\hat\theta\) the statistic of the original data.

Resampling is one of the building blocks of significance testing, next to the randomised copies (surrogate data) produced by jitter_spikes(), shuffle_isi() and phase_randomize().

Parameters:
  • key (Array) – PRNG key, from jax.random.key or jax.random.PRNGKey.

  • data (Any) – Observations along axis; other axes are carried along (for example (n_trials, n_neurons)).

  • statistic (Callable[[Any], Any]) – Function of one resampled array (same shape and unit as data) returning an array or Quantity, e.g. lambda d: jnp.mean(d, axis=0). It is traced once and vmapped over the resamples, so it must be JAX-traceable.

  • n_resamples (int) – Number of bootstrap samples (static).

  • confidence (float) – Confidence level, in (0, 1).

  • method (str) – Interval type.

  • axis (int) – Axis of the observations that is resampled.

Returns:

low, high – Interval bounds with the shape (and unit) of the statistic.

Return type:

Tuple[Any, Any]

Raises:
  • TypeError – If statistic is not callable or n_resamples is not an integer.

  • ValueError – If confidence is outside (0, 1), method is unknown, n_resamples < 1, or data is 0-d or empty along axis.

See also

phase_randomize

Randomised copies of continuous signals.

jitter_spikes

Randomised copies of spike trains.

Notes

The result matches scipy.stats.bootstrap(..., method='percentile') or method='basic' within Monte-Carlo error. Quantiles use linear interpolation, as numpy.percentile. The vmapped resamples hold n_resamples copies of data in memory at once. Works under jax.jit (with statistic closed over) and jax.vmap (for example over keys).

References

Examples

>>> import jax
>>> import jax.numpy as jnp
>>> from braintools.analysis.stats import bootstrap_ci
>>> data = 2. + jax.random.normal(jax.random.key(0), (200,))
>>> low, high = bootstrap_ci(jax.random.key(1), data, jnp.mean)
>>> bool(low < jnp.mean(data) < high), bool(high - low < 0.4)
(True, True)
>>>
>>> # Per-neuron intervals of the mean rate over trials:
>>> rates = jax.random.poisson(jax.random.key(2), 5., (40, 3)).astype(float)
>>> low, high = bootstrap_ci(jax.random.key(3), rates, lambda r: r.mean(axis=0),
...                          method='basic')
>>> low.shape
(3,)