van_rossum_distance#
- class braintools.metric.van_rossum_distance(spike_times_1, spike_times_2, tau=1.0, t_max=None)#
Calculate van Rossum distance between two spike trains.
The van Rossum distance measures dissimilarity between spike trains by convolving each with an exponential kernel and computing the Euclidean distance between the resulting continuous functions.
Each spike train is convolved with kernel \(K(t) = \frac{1}{\tau}e^{-t/\tau}H(t)\) where \(H(t)\) is the Heaviside step function. The distance is:
\[D_{vR} = \sqrt{\int_0^{T} [f_1(t) - f_2(t)]^2 dt}\]where \(f_i(t)\) is the convolved spike train.
- Parameters:
spike_times_1 (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – First spike train as array of spike times.spike_times_2 (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Second spike train as array of spike times.tau (
float) – Time constant of the exponential kernel. Larger values emphasize longer-term dependencies.t_max (
float) – Maximum time to consider. If None, uses maximum spike time + 5*tau.
- Returns:
van Rossum distance between the two spike trains.
- Return type:
Notes
Convention: each spike train is convolved with the rate-normalized kernel \(K(t) = \frac{1}{\tau} e^{-t/\tau} H(t)\) and the distance is \(\sqrt{\int (f_1 - f_2)^2\, dt}\) (no extra \(1/\tau\) prefactor on the integral). Other texts use \(K(t) = e^{-t/\tau}\) with a \(1/\tau\) integral prefactor; both differ only by an overall scale. The integral is truncated at
t_max(defaultmax(spike_time) + 5*tau) and discretized atdt = tau / 20; the5*tautail captures >99% of the exponential.This function runs on host (concrete) arrays (Python loop over spikes,
len) and returns a Pythonfloat, so it is notjit/vmap/grad-compatible.tau,t_maxand the spike times must use consistent time units.Examples
>>> import jax.numpy as jnp >>> import braintools >>> spikes1 = jnp.array([1.0, 3.0, 5.0]) >>> spikes2 = jnp.array([1.2, 3.2, 5.2]) >>> d = braintools.metric.van_rossum_distance(spikes1, spikes2, tau=0.5) >>> bool(d >= 0.0) True
References