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_resamplesbootstrap samples are drawn by resampling the entries ofdataalongaxiswith replacement, andstatisticis 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()andphase_randomize().- Parameters:
key (
Array) – PRNG key, fromjax.random.keyorjax.random.PRNGKey.data (
Any) – Observations alongaxis; other axes are carried along (for example(n_trials, n_neurons)).statistic (
Callable[[Any],Any]) – Function of one resampled array (same shape and unit asdata) 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:
- Raises:
TypeError – If
statisticis not callable orn_resamplesis not an integer.ValueError – If
confidenceis outside(0, 1),methodis unknown,n_resamples < 1, ordatais 0-d or empty alongaxis.
See also
phase_randomizeRandomised copies of continuous signals.
jitter_spikesRandomised copies of spike trains.
Notes
The result matches
scipy.stats.bootstrap(..., method='percentile')ormethod='basic'within Monte-Carlo error. Quantiles use linear interpolation, asnumpy.percentile. The vmapped resamples holdn_resamplescopies ofdatain memory at once. Works underjax.jit(withstatisticclosed over) andjax.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,)