braintools.analysis.spike.fano_factor

Contents

braintools.analysis.spike.fano_factor#

braintools.analysis.spike.fano_factor(x, bin_size=None, axis=0, *, n_bins=None)#

Compute the Fano factor, the variance-to-mean ratio of spike counts.

\[F = \frac{\operatorname{Var}[n]}{\operatorname{E}[n]},\]

with the population variance (ddof=0) [1].

  • If x is a SpikeTrains, it is binned with bin() into whole bins of width bin_size starting at t_start, and the ratio is taken across the bins, separately for every neuron. A partial bin at the end of the recording is dropped, because its lower count would inflate the variance.

  • Otherwise x is an array of spike counts, and the ratio is taken along axis (typically trials).

Parameters:
  • x (Any) – Spike trains, or spike counts with the trials on axis.

  • bin_size (Any) – Bin width; required for a SpikeTrains, and must be None for a count array. A plain number is in milliseconds.

  • axis (int) – Axis of the count array along which the ratio is computed. Ignored for a SpikeTrains.

  • n_bins (int | None) – Number of bins for a SpikeTrains. Defaults to the number of whole bins, floor(duration / bin_size) (a duration that is a multiple of bin_size up to float round-off keeps its last bin). The default needs concrete t_start and t_stop; pass n_bins explicitly when the trains are traced (e.g. an argument of a jax.jit-compiled function). An explicit value is used as given, so bins past t_stop (partial or empty) are included.

Returns:

For a SpikeTrains, shape (N,); for a count array, the shape of x without axis. nan where the mean count is 0, and for a SpikeTrains shorter than one bin.

Return type:

Array

Raises:
  • ValueError – If bin_size is missing for a SpikeTrains or given for a count array, if bin_size is not positive, or if n_bins is missing for traced t_start/t_stop.

  • TypeError – If the count array is a Quantity with a physical unit.

See also

braintools.spike.SpikeTrains.bin

The binning used for spike trains.

cv

The analogous statistic of the intervals.

Notes

For a Poisson process the Fano factor is 1 for every bin width. The count-array path matches elephant.statistics.fanofactor applied to one spike train per trial (which computes counts.var() / counts.mean()). Works under jax.jit and jax.vmap; the count-array path is differentiable with respect to the counts.

References

Examples

>>> import brainunit as u
>>> import jax.numpy as jnp
>>> from braintools.spike import SpikeTrains
>>> from braintools.analysis.spike import fano_factor
>>> raster = jnp.zeros((8, 2)).at[jnp.array([0, 1, 2, 4]), 0].set(1).at[::2, 1].set(1)
>>> st = SpikeTrains.from_dense(raster, dt=1. * u.ms)
>>> fano_factor(st, bin_size=2. * u.ms)
Array([0.5, 0. ], dtype=float32)
>>>
>>> fano_factor(jnp.array([[3., 1.], [5., 1.], [4., 1.]]), axis=0)
Array([0.16666667, 0.        ], dtype=float32)