cca_zoo.gam¶
Spline-based nonlinear CCA methods: generalized additive models with mgcv-style P-spline
smooths (GAMCCA) and multivariate adaptive regression splines in the style of earth
(MARSCCA). Both fit every view jointly by a closed-form penalised eigenproblem. Built on
scikit-learn's SplineTransformer and SciPy — no optional dependency required.
GAMCCA ¶
GAMCCA(
n_components: int = 1,
*,
center: bool = True,
k: int | list[int] = 10,
m: int
| tuple[int, int]
| list[int | tuple[int, int]] = 2,
sp: float | list[float] = 0.01,
)
Bases: BaseModel
Nonlinear CCA with generalized additive model encoders.
Each view's encoder is \(f_i(x) = \sum_j s_{ij}(x_j)\), one mgcv
P-spline smooth s(x_j, bs="ps", k=k, m=m) per feature, fitted to
minimise the EY loss (:mod:cca_zoo._utils._ey) plus the smoothing
penalty \(\tfrac12 \mathrm{sp}_i \sum_j \beta_{ij}^\top D^\top D \beta_{ij}\),
with \(D\) the m[1]-th order difference matrix. The fit is a single
generalized eigenproblem, solved in closed form at its global optimum.
mgcv's GCV and REML have no EY counterpart, so choose sp by
cross-validation. An additive model cannot represent within-view
interactions; see :class:~cca_zoo.gam.MARSCCA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
k
|
int | list[int]
|
B-splines per feature, as |
10
|
m
|
int | tuple[int, int] | list[int | tuple[int, int]]
|
|
2
|
sp
|
float | list[float]
|
Smoothing parameter; larger is smoother. Per-view. Default is 0.01. |
0.01
|
Attributes:
| Name | Type | Description |
|---|---|---|
encoders_ |
Fitted per-view encoders. |
References
Chapman, J., Wang, H.-T., Wells, L., & Wiesner, J. (2021). CCA-Zoo: A collection of Regularized, Deep Learning based, Kernel, and Probabilistic CCA methods in a scikit-learn style framework. Journal of Open Source Software, 6(68), 3823. Chapman, J., Wells, L., & Lawry Aguila, A. (2024). Unconstrained Stochastic CCA: Unifying Multiview and Self-Supervised Learning. arXiv:2310.01012. Wood, S. N. (2017). Generalized Additive Models: An Introduction with R (2nd ed.). Chapman and Hall/CRC.
Eilers, P. H., & Marx, B. D. (1996). Flexible smoothing with B-splines and penalties. Statistical Science, 11(2), 89-121.
Examples:
>>> import numpy as np
>>> from cca_zoo.gam import GAMCCA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((200, 5))
>>> X2 = rng.standard_normal((200, 5))
>>> model = GAMCCA(n_components=2, k=[8, 12], sp=[0.1, 10.0]).fit([X1, X2])
>>> model.shape_function(view=0, feature=0, x=X1[:, 0]).shape
(200, 2)
Source code in cca_zoo/gam/_gamcca.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
GAMCCA
|
self. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in cca_zoo/gam/_gamcca.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
shape_function ¶
Evaluate one feature's fitted smooth, plot.gam's partial effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
int
|
Index of the view. |
required |
feature
|
int
|
Index of the feature. |
required |
x
|
ArrayLike
|
Raw values of that feature, shape (n,). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The feature's contribution, shape (n, n_components). |
Source code in cca_zoo/gam/_gamcca.py
MARSCCA ¶
MARSCCA(
n_components: int = 1,
*,
center: bool = True,
degree: int | list[int] = 1,
nk: int | list[int | None] | None = None,
thresh: float = 0.001,
minspan: int | list[int | None] | None = None,
endspan: int | list[int | None] | None = None,
alpha: float | list[float] = 0.01,
nprune: int | None = None,
)
Bases: BaseModel
Nonlinear CCA with multivariate adaptive regression spline encoders.
Each view's encoder is a MARS model (Friedman, 1991), a linear
combination of products of up to degree hinges
\(\max(0, \pm(x_j - t))\), fitted to minimise the ridge-penalised EY loss.
As in R's earth, a forward pass grows each basis by the hinge pair
that best absorbs the EY gradient, refitting every view jointly in
closed form, and a backward pass prunes it to nprune terms.
earth's GCV has no EY counterpart, so choose nprune by
cross-validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
degree
|
int | list[int]
|
Maximum hinges per term; 1 is additive, 2 allows pairwise interactions. Per-view. Default is 1. |
1
|
nk
|
int | list[int | None] | None
|
Maximum terms per view in the forward pass; None is |
None
|
thresh
|
float
|
Stop the forward pass once a round lowers the loss by less than this fraction. Default is 0.001. |
0.001
|
minspan
|
int | list[int | None] | None
|
Minimum support points between knots; 0 is Friedman's rule, None widens it to at most 20 knots per feature. Per-view. Default is None. |
None
|
endspan
|
int | list[int | None] | None
|
Support points at either end that may not carry a knot, doubled for interactions; None is Friedman's rule. Per-view. Default is None. |
None
|
alpha
|
float | list[float]
|
Ridge penalty on the coefficient of every basis function at unit variance, so that it ignores each feature's units. Per-view. Default is 0.01. |
0.01
|
nprune
|
int | None
|
Total terms, across views, kept by the backward pass; None keeps them all. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
encoders_ |
Fitted per-view encoders; |
|
backward_path_ |
|
|
backward_loss_ |
Training EY loss after each number of deletions. |
|
n_removed_ |
Terms the backward pass pruned, the first |
References
Chapman, J., Wang, H.-T., Wells, L., & Wiesner, J. (2021). CCA-Zoo: A collection of Regularized, Deep Learning based, Kernel, and Probabilistic CCA methods in a scikit-learn style framework. Journal of Open Source Software, 6(68), 3823. Chapman, J., Wells, L., & Lawry Aguila, A. (2024). Unconstrained Stochastic CCA: Unifying Multiview and Self-Supervised Learning. arXiv:2310.01012. Friedman, J. H. (1991). Multivariate Adaptive Regression Splines. The Annals of Statistics, 19(1), 1-67.
Examples:
>>> import numpy as np
>>> from cca_zoo.gam import MARSCCA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((200, 5))
>>> X2 = rng.standard_normal((200, 5))
>>> model = MARSCCA(degree=2, nk=[10, 20]).fit([X1, X2])
>>> len(model.basis_functions(0)) <= 10
True
Source code in cca_zoo/gam/_marscca.py
fit ¶
Fit the model: earth's forward pass, backward pass and final refit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
MARSCCA
|
self. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in cca_zoo/gam/_marscca.py
basis_functions ¶
Readable form of one view's selected basis functions.
Knots are in raw feature units, e.g. "h(x3 - 0.52) * h(1.1 - x0)"
with h(u) = max(0, u).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
int
|
Index of the view. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
One string per basis function, matching the rows of |
list[str]
|
|