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. fromrdm()), or condensed vectors of shape(C (C - 1) / 2,)as returned byscipy.spatial.distance.pdist. Both must have the same shape. Units cancel.rdm_b (array_like or brainunit.Quantity) – Square
(C, C)RDMs (e.g. fromrdm()), or condensed vectors of shape(C (C - 1) / 2,)as returned byscipy.spatial.distance.pdist. Both must have the same shape. Units cancel.method (
str) – Correlation coefficient.
- Returns:
0-d correlation in
[-1, 1].nanif either upper triangle is constant or containsnan(scipy’snan_policy='propagate').- Return type:
Array- Raises:
ValueError – If the shapes differ, are not square/1-D, or
methodis unknown.
See also
rdmBuild an RDM from condition patterns.
Notes
Matches
scipy.stats.spearmanr(a, b).statisticandscipy.stats.pearsonr(a, b).statisticon the condensed vectors. Ranks are computed with a sort and twosearchsortedcalls, 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