braintools.analysis.population.rsa

Contents

braintools.analysis.population.rsa#

braintools.analysis.population.rsa(rdm_a, rdm_b, method='spearman')#

Representational similarity: correlation between two RDMs.

The strict upper triangles of the two dissimilarity matrices are correlated [1] with Spearman’s rank correlation (ranks with ties averaged) or with Pearson’s correlation.

Parameters:
  • rdm_a (array_like or brainunit.Quantity) – Square (C, C) RDMs (e.g. from rdm()), or condensed vectors of shape (C (C - 1) / 2,) as returned by scipy.spatial.distance.pdist. Both must have the same shape. Units cancel.

  • rdm_b (array_like or brainunit.Quantity) – Square (C, C) RDMs (e.g. from rdm()), or condensed vectors of shape (C (C - 1) / 2,) as returned by scipy.spatial.distance.pdist. Both must have the same shape. Units cancel.

  • method (str) – Correlation coefficient.

Returns:

0-d correlation in [-1, 1]. nan if either upper triangle is constant or contains nan (scipy’s nan_policy='propagate').

Return type:

Array

Raises:

ValueError – If the shapes differ, are not square/1-D, or method is unknown.

See also

rdm

Build an RDM from condition patterns.

Notes

Matches scipy.stats.spearmanr(a, b).statistic and scipy.stats.pearsonr(a, b).statistic on the condensed vectors. Ranks are computed with a sort and two searchsorted calls, so the function is jittable and vmappable (e.g. over a batch of model RDMs). 'pearson' is differentiable; 'spearman' has a zero gradient almost everywhere.

References

Examples

>>> import jax.numpy as jnp
>>> from braintools.analysis import population
>>> x = jnp.array([[0., 1.], [1., 3.], [4., 2.], [5., 5.]])
>>> a = population.rdm(x, metric='euclidean')
>>> round(float(population.rsa(a, a ** 2)), 4)  # monotone transform
1.0
>>> round(float(population.rsa(a, a ** 2, method='pearson')), 4)
0.9882