elephant.spike_train_correlation.covariance¶

elephant.spike_train_correlation.
covariance
(binned_spiketrain, binary=False, fast=True)[source]¶ Calculate the NxN matrix of pairwise covariances between all combinations of N binned spike trains.
For each pair of spike trains , the covariance is obtained by binning and at the desired bin size. Let and denote the binned spike trains and and their respective averages. Then
where <., .> is the scalar product of two vectors, and is the number of bins.
For an input of N spike trains, an N x N matrix is returned containing the covariances for each combination of input spike trains.
If binary is True, the binned spike trains are clipped to 0 or 1 before computing the covariance, so that the binned vectors and are binary.
Parameters:  binned_spiketrain(N, ) elephant.conversion.BinnedSpikeTrain
A binned spike train containing the spike trains to be evaluated.
 binarybool, optional
If True, the spikes of a particular spike train falling in the same bin are counted as 1, resulting in binary binned vectors . If False, the binned vectors contain the spike counts per bin. Default: False
 fastbool, optional
If fast=True and the sparsity of binned_spiketrain is > 0.1, use np.cov(). Otherwise, use memory efficient implementation. See Notes [2]. Default: True
Returns:  C(N, N) np.ndarray
The square matrix of covariances. The element is the covariance between binned_spiketrain[i] and binned_spiketrain[j].
Raises:  MemoryError
When using fast=True and binned_spiketrain shape is large.
Warns:  UserWarning
If at least one row in binned_spiketrain is empty (has no spikes).
See also
correlation_coefficient
 Pearson correlation coefficient
Notes
 The spike trains in the binned structure are assumed to cover the complete time span [t_start, t_stop) of binned_spiketrain.
 Using fast=True might lead to MemoryError. If it’s the case, switch to fast=False.
Examples
Covariance matrix of two Poisson spike train processes.
>>> import neo >>> import numpy as np >>> import quantities as pq >>> from elephant.spike_train_generation import homogeneous_poisson_process >>> from elephant.conversion import BinnedSpikeTrain >>> from elephant.spike_train_correlation import covariance
>>> np.random.seed(1) >>> st1 = homogeneous_poisson_process(rate=10*pq.Hz, t_stop=10.0*pq.s) >>> st2 = homogeneous_poisson_process(rate=10*pq.Hz, t_stop=10.0*pq.s) >>> cov_matrix = covariance(BinnedSpikeTrain([st1, st2], bin_size=5*pq.ms)) >>> cov_matrix array([[ 0.05432316, 0.00152276], [0.00152276, 0.04917234]])