cca_zoo.metrics¶
Metrics for evaluating fitted multiview CCA models. Functions take
already-computed arrays (latent scores, loadings, a correlation matrix)
rather than a fitted model, the same convention sklearn.metrics uses.
pairwise_correlations ¶
Full pairwise Pearson correlation matrix between views' latent scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transformed
|
Sequence[ArrayLike]
|
List of arrays, each of shape (n_samples,
latent_dimensions) -- one per view's own canonical variate
(e.g. the output of a fitted model's |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (n_views, n_views, latent_dimensions) where entry |
ndarray
|
|
ndarray
|
j's d-th canonical variate. |
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> t1 = rng.standard_normal((20, 1))
>>> t2 = 0.8 * t1 + 0.2 * rng.standard_normal((20, 1))
>>> corrs = pairwise_correlations([t1, t2])
>>> corrs.shape
(2, 2, 1)
>>> round(float(corrs[0, 1, 0]), 2)
0.98
Source code in cca_zoo/metrics/_correlation.py
average_pairwise_correlations ¶
Mean off-diagonal pairwise correlation per canonical dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
correlations
|
ArrayLike
|
Array of shape (n_views, n_views, latent_dimensions)
-- typically the output of :func: |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (latent_dimensions,) with the average off-diagonal |
ndarray
|
pairwise correlation for each canonical dimension. |
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> t1 = rng.standard_normal((20, 1))
>>> t2 = 0.8 * t1 + 0.2 * rng.standard_normal((20, 1))
>>> corrs = pairwise_correlations([t1, t2])
>>> avg = average_pairwise_correlations(corrs)
>>> avg.shape
(1,)
>>> round(float(avg[0]), 2)
0.98
Source code in cca_zoo/metrics/_correlation.py
factor_loadings ¶
factor_loadings(
views: Sequence[ArrayLike],
transformed: Sequence[ArrayLike],
) -> list[np.ndarray]
Pearson correlation between each raw feature and its view's own variate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
Sequence[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). |
required |
transformed
|
Sequence[ArrayLike]
|
List of arrays, each of shape (n_samples,
latent_dimensions), aligned with |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of arrays, each of shape (n_features_i, latent_dimensions), |
list[ndarray]
|
where entry |
list[ndarray]
|
view i and the d-th canonical variate of view i. |
Examples:
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> t1 = rng.standard_normal((20, 1))
>>> view1 = np.column_stack(
... [t1[:, 0] + 0.3 * rng.standard_normal(20), rng.standard_normal(20)]
... )
>>> loadings = factor_loadings([view1], [t1])
>>> loadings[0].shape
(2, 1)
>>> round(float(loadings[0][0, 0]), 2)
0.97
Source code in cca_zoo/metrics/_correlation.py
adequacy_coefficient ¶
Own-set variance each view's canonical variates extract from that view.
Also known as the adequacy coefficient (Cramer & Nicewander, 1979) or per-dimension communality: the mean squared factor loading, i.e. the average proportion of a view's own feature variance captured by each of its canonical variates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loadings
|
Sequence[ArrayLike]
|
List of arrays, each of shape (n_features_i,
latent_dimensions) -- typically the output of
:func: |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of arrays, each of shape (latent_dimensions,): view i's own |
list[ndarray]
|
variance extracted by each of its canonical dimensions. |
Examples:
>>> import numpy as np
>>> from cca_zoo.metrics import factor_loadings
>>> rng = np.random.default_rng(0)
>>> t1 = rng.standard_normal((20, 1))
>>> view1 = np.column_stack(
... [t1[:, 0] + 0.3 * rng.standard_normal(20), rng.standard_normal(20)]
... )
>>> loadings = factor_loadings([view1], [t1])
>>> adequacy = adequacy_coefficient(loadings)
>>> adequacy[0].shape
(1,)
>>> round(float(adequacy[0][0]), 2)
0.48
Source code in cca_zoo/metrics/_redundancy.py
redundancy_index ¶
Stewart & Love (1968) redundancy: variance in view i explained via view j.
For each ordered pair of views and canonical dimension, the proportion
of view i's own variance that is both captured by its d-th canonical
variate (:func:adequacy_coefficient) and shared with view j (the
squared canonical correlation between their d-th variates). Unlike a
canonical correlation, redundancy is asymmetric: view i's redundancy
given view j need not equal view j's redundancy given view i, since
each view's own adequacy can differ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loadings
|
Sequence[ArrayLike]
|
List of arrays, each of shape (n_features_i,
latent_dimensions) -- typically the output of
:func: |
required |
correlations
|
ArrayLike
|
Array of shape (n_views, n_views, latent_dimensions)
-- typically the output of
:func: |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (n_views, n_views, latent_dimensions), where entry |
ndarray
|
|
ndarray
|
canonical variate. The diagonal |
ndarray
|
adequacy coefficient, since a view's correlation with itself is 1. |
Examples:
>>> import numpy as np
>>> from cca_zoo.metrics import factor_loadings, pairwise_correlations
>>> rng = np.random.default_rng(0)
>>> t1 = rng.standard_normal((20, 1))
>>> t2 = 0.8 * t1 + 0.2 * rng.standard_normal((20, 1))
>>> view1 = np.column_stack(
... [t1[:, 0] + 0.1 * rng.standard_normal(20), rng.standard_normal(20)]
... )
>>> view2 = np.column_stack(
... [t2[:, 0] + 0.1 * rng.standard_normal(20), rng.standard_normal(20)]
... )
>>> loadings = factor_loadings([view1, view2], [t1, t2])
>>> corrs = pairwise_correlations([t1, t2])
>>> redundancy = redundancy_index(loadings, corrs)
>>> redundancy.shape
(2, 2, 1)
>>> round(float(redundancy[0, 1, 0]), 2)
0.52
Source code in cca_zoo/metrics/_redundancy.py
total_redundancy ¶
Cumulative redundancy across every retained canonical dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redundancy
|
ArrayLike
|
Array of shape (n_views, n_views, latent_dimensions) --
typically the output of :func: |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (n_views, n_views): entry |
ndarray
|
proportion of view i's variance explained by view j's canonical |
ndarray
|
variates, summed over every retained dimension. |
Examples:
>>> import numpy as np
>>> from cca_zoo.metrics import factor_loadings, pairwise_correlations
>>> rng = np.random.default_rng(0)
>>> t1 = rng.standard_normal((20, 1))
>>> t2 = 0.8 * t1 + 0.2 * rng.standard_normal((20, 1))
>>> view1 = np.column_stack(
... [t1[:, 0] + 0.1 * rng.standard_normal(20), rng.standard_normal(20)]
... )
>>> view2 = np.column_stack(
... [t2[:, 0] + 0.1 * rng.standard_normal(20), rng.standard_normal(20)]
... )
>>> loadings = factor_loadings([view1, view2], [t1, t2])
>>> corrs = pairwise_correlations([t1, t2])
>>> redundancy = redundancy_index(loadings, corrs)
>>> total = total_redundancy(redundancy)
>>> total.shape
(2, 2)
>>> round(float(total[0, 1]), 2)
0.52