Skip to content
Merged
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
3 changes: 2 additions & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
.. currentmodule:: fastcan


FastCan Class
API Reference
~~~~~~~~~~~~~
.. autosummary::
:toctree: generated/

FastCan
ssc


...................
Expand Down
2 changes: 2 additions & 0 deletions fastcan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""

from ._fastcan import FastCan
from ._ssc import ssc

__all__ = [
"FastCan",
"ssc",
]
47 changes: 47 additions & 0 deletions fastcan/_ssc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Sum squared of correlation."""

import numpy as np
from sklearn.cross_decomposition import CCA
from sklearn.utils import check_X_y
from sklearn.utils._param_validation import validate_params


@validate_params(
{
"X": ["array-like"],
"y": ["array-like"],
},
prefer_skip_nested_validation=True,
)
def ssc(X, y):
"""Sum of the squared canonical correlation coefficients.

Parameters
----------
X : array-like of shape (n_samples, n_features)
Feature matrix.

y : array-like of shape (n_samples, n_outputs)
Target matrix.

Returns
-------
ssc : float
Sum of the squared canonical correlation coefficients.

Examples
--------
>>> from fastcan import ssc
>>> X = [[1], [-1], [0]]
>>> y = [[0], [1], [-1]]
>>> ssc(X, y)
np.float64(0.25)
"""
X, y = check_X_y(
X, y, dtype=float, ensure_2d=True, multi_output=True, ensure_min_samples=2
)
n_components = min(X.shape[1], y.shape[1])
cca = CCA(n_components=n_components)
X_c, y_c = cca.fit_transform(X, y)
corrcoef = np.diagonal(np.corrcoef(X_c, y_c, rowvar=False), offset=n_components)
return sum(corrcoef**2)
1 change: 0 additions & 1 deletion tests/test_correlation.py → tests/test_fastcan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# pylint: skip-file
"""Test FastCan"""

import numpy as np
Expand Down
32 changes: 32 additions & 0 deletions tests/test_ssc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"Test ssc"

import numpy as np
from numpy.testing import assert_almost_equal
from sklearn.linear_model import LinearRegression

from fastcan import ssc


def test_pearson_r():
"""Test Pearson's correlation."""
rng = np.random.default_rng(12345)
X = rng.random(100)
y = rng.random(100)
r2 = ssc(X.reshape(-1, 1), y.reshape(-1, 1))
gtruth_r2 = np.corrcoef(X, y)[0, 1]**2
assert_almost_equal(actual=r2, desired=gtruth_r2)

def test_multi_r():
"""Test multiple correlation."""
rng = np.random.default_rng(12345)
X = rng.random((100, 10))
y = rng.random(100)
r2 = ssc(X, y.reshape(-1, 1))
gtruth_r2 = LinearRegression().fit(X, y).score(X, y)
assert_almost_equal(actual=r2, desired=gtruth_r2)

X = rng.random(100)
y = rng.random((100, 10))
r2 = ssc(X.reshape(-1, 1), y)
gtruth_r2 = LinearRegression().fit(y, X).score(y, X)
assert_almost_equal(actual=r2, desired=gtruth_r2)