Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

similarity

similarity

Similarity metrics and correlation.

Methods:

NameDescription
compute_multivariate_similarityCompute multivariate similarity via OLS regression.
compute_similarityCompute similarity between two data arrays.
fisher_r_to_zUse Fisher transformation to convert correlation to z score.
fisher_z_to_rConvert Fisher z back to a correlation coefficient.
transform_pairwiseTransform data into pairs with balanced labels for ranking.

Methods

compute_multivariate_similarity

compute_multivariate_similarity(y, X, method = 'ols', tail = 2)

Compute multivariate similarity via OLS regression.

This is the functional core implementation for multivariate similarity computation. Used by BrainData.multivariate_similarity() to delegate computation to the functional core.

Predicts spatial distribution of y from linear combination of X columns. Computes OLS regression statistics including beta coefficients, t-statistics, p-values, and residuals.

Parameters:

NameTypeDescriptionDefault
yndarrayTarget data, shape (n_features,) - single imagerequired
XndarrayPredictor data, shape (n_features, n_predictors) where first column should be intercept (ones) if intercept is desired. If X does not include intercept, an intercept will be added automatically.required
methodstrRegression method (currently only ‘ols’ supported)‘ols’

Returns:

NameTypeDescription
dictDictionary with keys: - ‘beta’: Regression coefficients including intercept, shape (n_predictors+1,) - ‘t’: t-statistics, shape (n_predictors+1,) - ‘p’: p-values, shape (n_predictors+1,) - ‘df’: Degrees of freedom (int) - ‘sigma’: Residual standard deviation (float) - ‘residual’: Residuals, shape (n_features,)

Examples:

>>> y = np.random.randn(100)
>>> X = np.random.randn(100, 5)
>>> result = compute_multivariate_similarity(y, X, method='ols')
>>> 'beta' in result
True
>>> result['beta'].shape
(6,)  # 5 predictors + intercept

compute_similarity

compute_similarity(data1, data2, metric = 'correlation')

Compute similarity between two data arrays.

This is the functional core implementation for similarity computation. Used by BrainData.similarity() to delegate computation to the functional core.

Parameters:

NameTypeDescriptionDefault
data1ndarrayFirst data array, shape (n_samples1, n_features)required
data2ndarraySecond data array, shape (n_samples2, n_features)required
metricstrType of similarity metric - ‘correlation’ or ‘pearson’: Pearson correlation - ‘spearman’ or ‘rank_correlation’: Spearman rank correlation - ‘dot_product’: Dot product - ‘cosine’: Cosine similarity‘correlation’

Returns:

TypeDescription
np.ndarray: Similarity matrix or vector - If data1.shape[0] == 1 and data2.shape[0] == 1: scalar - If data1.shape[0] == 1 or data2.shape[0] == 1: 1D array - Otherwise: 2D array shape (n_samples1, n_samples2)

Examples:

>>> data1 = np.random.randn(10, 100)
>>> data2 = np.random.randn(5, 100)
>>> sim = compute_similarity(data1, data2, metric='correlation')
>>> sim.shape
(10, 5)

fisher_r_to_z

fisher_r_to_z(r)

Use Fisher transformation to convert correlation to z score.

Parameters:

NameTypeDescriptionDefault
rcorrelation coefficient(s)required

Returns:

NameTypeDescription
zFisher z-transformed correlation(s)

fisher_z_to_r

fisher_z_to_r(z)

Convert Fisher z back to a correlation coefficient.

Parameters:

NameTypeDescriptionDefault
zFisher z-transformed value(s)required

Returns:

NameTypeDescription
rcorrelation coefficient(s)

transform_pairwise

transform_pairwise(X, y)

Transform data into pairs with balanced labels for ranking.

Transforms a n-class ranking problem into a two-class classification problem. Subclasses implementing particular strategies for choosing pairs should override this method. In this method, all pairs are choosen, except for those that have the same target value. The output is an array of balanced classes, i.e. there are the same number of -1 as +1

Reference: “Large Margin Rank Boundaries for Ordinal Regression”, R. Herbrich, T. Graepel, K. Obermayer. Authors: Fabian Pedregosa fabian@fseoane.net Alexandre Gramfort alexandre.gramfort@inria.fr

Parameters:

NameTypeDescriptionDefault
X(np.array), shape (n_samples, n_features) The datarequired
y(np.array), shape (n_samples,) or (n_samples, 2) Target labels. If it’s a 2D array, the second column represents the grouping of samples, i.e., samples with different groups will not be considered.required

Returns:

NameTypeDescription
X_trans(np.array), shape (k, n_features) Data as pairs, where k = n_samples * (n_samples-1)) / 2 if grouping values were not passed. If grouping variables exist, then returns values computed for each group.
y_trans(np.array), shape (k,) Output class labels, where classes have values {-1, +1} If y was shape (n_samples, 2), then returns (k, 2) with groups on the second dimension.