Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first version of permutation interence method in the model_selection module. #136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cca_zoo/model_selection/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from ._search import GridSearchCV, RandomizedSearchCV
from ._permutation import permutation_test_score as permutation_test_score
118 changes: 118 additions & 0 deletions cca_zoo/model_selection/_permutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import numpy as np
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import numpy as np
from typing import Optional, Tuple, Dict, Union

from sklearn.utils import check_random_state
from tqdm import tqdm
from cca_zoo.models._cca_base import _CCA_Base


def permutation_test_score(
        estimator: _CCA_Base, X: np.ndarray, Y: np.ndarray, latent_dims: int = 1,
        n_perms: int = 1000, Z: Optional[np.ndarray] = None, W: Optional[np.ndarray] = None,
        sel: Optional[np.ndarray] = None, partial: bool = True,
        parameters: Optional[Dict] = None,
        random_state: Union[int, np.random.RandomState] = None,
) -> Tuple[float, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
    """
    Permutation inference for canonical correlation analysis (CCA) _[1].

    This code is adapted from the Matlab function accompagning the paper:
    https://github.com/andersonwinkler/PermCCA/blob/6098d35da79618588b8763c5b4a519438703dba4/permcca.m#L131-L164

    Parameters
    ----------
    estimator : _CCA_Base
        The object to use to fit the data. This must be one of the CCA models from
        py:class:`cca_zoo-models` and implementing a `fit` method.
    Y : np.ndarray
        Left set of variables, size N by P.
    X : np.ndarray
        Right set of variables, size N by Q.
    latent_dims : int
        The number of latent dimensions infered during the model fitting. Defaults to
        `1`.
    n_perms : int
        An integer representing the number of permutations. Default is 1000 permutations.
    Z : np.ndarray
        (Optional) Nuisance variables for both (partial CCA) or left
        side (part CCA) only.
    W : np.ndarray
        (Optional) Nuisance variables for the right side only (bipartial CCA).
    sel : np.ndarray
        (Optional) Selection matrix or a selection vector, to use Theil's residuals
        instead of Huh-Jhun's projection. If specified as a vector, it can be made
        of integer indices or logicals. The R unselected rows of Z (S of W) must be full
        rank. Use -1 to randomly select N-R (or N-S) rows.
    partial : bool
        (Optional) Boolean indicating whether this is partial (true) or part (false) CCA.
        Default is true, i.e., partial CCA.
    parameters : dict | None
        (Optional) Any additional keyword arguments required by the given estimator.

    Returns
    -------
    p : float
        p-values, FWER corrected via closure.
    r : np.ndarray
        Canonical correlations.
    A : np.ndarray
        Canonical coefficients (X).
    B : np.ndarray
        Canonical coefficients (Y).
    U : np.ndarray
        Canonical variables (X).
    V : np.ndarray
        Canonical variables (Y).

    References
    ----------
    .. [1] Winkler AM, Renaud O, Smith SM, Nichols TE. Permutation Inference for
        Canonical Correlation Analysis. NeuroImage. 2020; 117065.
    """

    random_state = check_random_state(random_state)
    lW, cnt = np.zeros(latent_dims), np.zeros(latent_dims)
    n_obs = X.shape[0]
    if parameters is None:
        parameters = {}

    # Initial fit of the CCA model (without any permutation)
    init_model = estimator(latent_dims=latent_dims, random_state=random_state, **parameters).fit((X, Y))
    A, B = init_model.weights
    U, V = X @ np.hstack((A, null(A.T))), Y @ np.hstack((B, null(B.T)))
    x_idx = np.arange(n_obs)
    y_idx = np.arange(n_obs)
    for i in tqdm(range(n_perms)):

        # If user didn't supply a set of permutations, permute randomly both Y and X.
        if i > 0:
            random_state.shuffle(x_idx)
            random_state.shuffle(y_idx)

        # For each canonical variable
        for k in range(latent_dims):
            # Fit the CCA model using the permuted datasets
            perm_model = estimator(latent_dims=(latent_dims - k), random_state=random_state, **parameters)
            perm_model.fit((U[x_idx, k:], V[y_idx, k:]))

            # Estimate correlation coefficient for this CCA fit
            r_perm = perm_model.score((U[x_idx, k:], V[y_idx, k:]))

            lWtmp = -1 * np.cumsum(np.log(1 - r_perm ** 2)[::-1])[::-1]
            lW[k] = lWtmp[0]

        if i == 0:
            #copy otherwise lw1 and lW share memory
            lw1 = lW.copy()
        cnt = cnt + (lW >= lw1)

    # compute p-values
    p = np.maximum.accumulate(cnt / n_perms)

    return p, A, B, U, V


def null(a, rtol=1e-5):
    # https://stackoverflow.com/questions/19820921/a-simple-matlab-like-way-of-finding-the-null-space-of-a-small-matrix-in-numpy
    u, s, v = np.linalg.svd(a)
    rank = (s > rtol * s[0]).sum()
    return v[rank:].T.copy()

Really nice work translating the matlab. I think I've found the sources of the bugs you mention. Take a look and see what you think.

from typing import Optional, Tuple, Dict
from tqdm import tqdm
from cca_zoo.models._cca_base import _CCA_Base


def permutation_test_score(
estimator: _CCA_Base, X: np.ndarray, Y: np.ndarray, latent_dims: int = 1,
n_perms: int = 1000, Z: Optional[np.ndarray] = None, W: Optional[np.ndarray] = None,
sel: Optional[np.ndarray] = None, partial: bool = True,
parameters: Optional[Dict] = None
) -> Tuple[float, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
"""
Permutation inference for canonical correlation analysis (CCA) _[1].

This code is adapted from the Matlab function accompagning the paper:
https://github.com/andersonwinkler/PermCCA/blob/6098d35da79618588b8763c5b4a519438703dba4/permcca.m#L131-L164

Parameters
----------
estimator : _CCA_Base
The object to use to fit the data. This must be one of the CCA models from
py:class:`cca_zoo-models` and implementing a `fit` method.
Y : np.ndarray
Left set of variables, size N by P.
X : np.ndarray
Right set of variables, size N by Q.
latent_dims : int
The number of latent dimensions infered during the model fitting. Defaults to
`1`.
n_perms : int
An integer representing the number of permutations. Default is 1000 permutations.
Z : np.ndarray
(Optional) Nuisance variables for both (partial CCA) or left
side (part CCA) only.
W : np.ndarray
(Optional) Nuisance variables for the right side only (bipartial CCA).
sel : np.ndarray
(Optional) Selection matrix or a selection vector, to use Theil's residuals
instead of Huh-Jhun's projection. If specified as a vector, it can be made
of integer indices or logicals. The R unselected rows of Z (S of W) must be full
rank. Use -1 to randomly select N-R (or N-S) rows.
partial : bool
(Optional) Boolean indicating whether this is partial (true) or part (false) CCA.
Default is true, i.e., partial CCA.
parameters : dict | None
(Optional) Any additional keyword arguments required by the given estimator.


Returns
-------
p : float
p-values, FWER corrected via closure.
r : np.ndarray
Canonical correlations.
A : np.ndarray
Canonical coefficients (X).
B : np.ndarray
Canonical coefficients (Y).
U : np.ndarray
Canonical variables (X).
V : np.ndarray
Canonical variables (Y).

References
----------
.. [1] Winkler AM, Renaud O, Smith SM, Nichols TE. Permutation Inference for
Canonical Correlation Analysis. NeuroImage. 2020; 117065.

"""

rng = np.random.RandomState(42)
lW, cnt = np.zeros(latent_dims), np.zeros(latent_dims)
n_obs = X.shape[0]
if parameters is None:
parameters = {}

# Initial fit of the CCA model (without any permutation)
init_model = estimator(latent_dims=(latent_dims), **parameters)
init_model.fit((X, Y))

A, B = init_model.get_loadings((X, Y))
U, V = init_model.transform((X, Y))

for i in tqdm(range(n_perms)):

# If user didn't supply a set of permutations, permute randomly both Y and X.
# Otherwise, use the permtuation set to shuffle one side only.
if i == 0:
# First permutation is no permutation
X_perm = X
Y_perm = Y
else:
x_idx, y_idx = rng.permutation(n_obs), rng.permutation(n_obs)
X_perm = X[x_idx]
Y_perm = Y[y_idx]

# For each canonical variable
for k in range(latent_dims):

# Fit the CCA model using the permuted datasets
perm_model = estimator(latent_dims=(latent_dims - k), **parameters)
perm_model.fit((X_perm[:, k:], Y_perm[:, k:]))

# Estimate correlation coefficient for this CCA fit
r_perm = perm_model.correlations((X_perm[:, k:], Y_perm[:, k:]))[0][1]

lWtmp = -1 * np.cumsum(np.log(1 - r_perm ** 2)[::-1])[::-1]
lW[k] = lWtmp[0]

if i == 0:
lw1 = lW
cnt = cnt + (lW >= lw1)

# compute p-values
p = np.maximum.accumulate(cnt/n_perms)

return p, A, B, U, V
12 changes: 12 additions & 0 deletions cca_zoo/test/test_model_selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from cca_zoo.model_selection import permutation_test_score
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from cca_zoo.model_selection import permutation_test_score
from sklearn.utils.validation import check_random_state
from cca_zoo.models import CCA
from cca_zoo.data import generate_covariance_data

n = 50
rng = check_random_state(0)

# this generates data with ground truth number of latent dimensions
(X, Y), (_, _) = generate_covariance_data(1000, [5, 5], latent_dims=4, correlation=[1, 1, 0, 0], random_state=rng)


def test_perm_test():
    p, A, B, U, V = permutation_test_score(X=X, Y=Y, estimator=CCA, latent_dims=4, random_state=rng)
    assert p[0] < 0.05
    assert p[1] < 0.05
    assert p[2] > 0.05
    assert p[3] > 0.05

Wonder if something like this might make a good test.

from sklearn.utils.validation import check_random_state
from cca_zoo.models import CCA

n = 50
rng = check_random_state(0)
X = rng.rand(n, 4)
Y = rng.rand(n, 5)


def test_permutation_test_score():
p, A, B, U, V = permutation_test_score(X=X, Y=Y, estimator=CCA, latent_dims=2)