cca_zoo.probabilistic¶
Probabilistic CCA via MCMC, black-box variational inference, or closed-form coordinate-ascent
variational Bayes. GFA has no extra dependencies; ProbabilisticCCA and
VariationalBayesCCA require pip install cca-zoo[probabilistic].
GFA ¶
GFA(
n_components: int = 1,
*,
center: bool = True,
max_iter: int = 10000,
tol: float = 0.0001,
drop_k: bool = True,
n_posterior_samples: int = 1000,
random_state: int | None = None,
)
Bases: BaseProbabilistic
Group Factor Analysis: Bayesian CCA with per-view ARD.
A port of GFA() from the R package CCAGFA. A shared latent \(z\)
generates every view, with an ARD precision per view and dimension:
so a dimension is shared when \(\alpha_{i,k}\) is small in several views
and private when small in one. Inference is closed-form coordinate-ascent
variational Bayes. n_components is an upper bound: dimensions with
vanishing loadings are pruned, leaving n_components_. Convergence is
judged on the change in \(z\) rather than the lower bound, so raise
max_iter if n_components_ is larger than expected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Upper bound on the number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
max_iter
|
int
|
Maximum coordinate-ascent iterations. Default is 10000. |
10000
|
tol
|
float
|
Tolerance on the relative change in \(z\), held for 1000 iterations. Default is 1e-4. |
0.0001
|
drop_k
|
bool
|
Whether to prune unused dimensions, as CCAGFA's |
True
|
n_posterior_samples
|
int
|
Draws from the fitted posterior. Default is 1000. |
1000
|
random_state
|
int | None
|
Seed for the initialisation. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
list[ndarray]
|
Posterior mean loadings of each view, shape (n_features_i, n_components_). |
ard_precision_ |
list[ndarray]
|
Posterior mean ARD precisions, shape (n_views, n_components_); large means shrunk away. |
n_components_ |
list[ndarray]
|
Number of dimensions kept. |
posterior_samples_ |
dict[str, Any]
|
Posterior draws keyed |
n_iter_ |
dict[str, Any]
|
Number of iterations run. |
References
Klami, A., Virtanen, S., Leppäaho, E., & Kaski, S. (2015). Group factor analysis. IEEE Transactions on Neural Networks and Learning Systems, 26(9), 2136-2147. Klami, A., Virtanen, S., & Kaski, S. (2013). Bayesian Canonical Correlation Analysis. Journal of Machine Learning Research, 14, 965-1003.
Examples:
>>> import numpy as np
>>> from cca_zoo.probabilistic import GFA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((50, 4))
>>> X2 = rng.standard_normal((50, 3))
>>> model = GFA(n_components=2, max_iter=50).fit([X1, X2])
Source code in cca_zoo/probabilistic/_gfa.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 |
|---|---|
GFA
|
self. |
Source code in cca_zoo/probabilistic/_gfa.py
ProbabilisticCCA ¶
ProbabilisticCCA(
n_components: int = 1,
*,
center: bool = True,
n_warmup: int = 500,
n_posterior_samples: int = 1000,
random_state: int | None = None,
)
Bases: BaseProbabilistic
Probabilistic CCA with posterior sampling by NUTS.
with each \(\Psi_i\) a full covariance, so that \(z\) models only what the
views share: at the maximum likelihood each view's posterior mean spans
its canonical variates (Bach & Jordan, 2005). \(\Psi_i\) has an LKJ prior
on its correlation and log-normal scales, and \(z\) is integrated out. The
likelihood is invariant to a shared rotation of the loadings, so the
draws are aligned by generalized Procrustes before averaging. Requires
the probabilistic extra.
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
|
n_warmup
|
int
|
NUTS warm-up steps. Default is 500. |
500
|
n_posterior_samples
|
int
|
NUTS draws. Default is 1000. |
1000
|
random_state
|
int | None
|
Seed for the JAX PRNG. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
list[ndarray]
|
Posterior mean loadings of each view, shape (n_features_i, n_components). |
posterior_samples_ |
dict[str, Any]
|
Aligned posterior draws keyed by site name. |
References
Bach, F. R., & Jordan, M. I. (2005). A probabilistic interpretation of canonical correlation analysis. Technical Report 688, UC Berkeley.
Examples:
>>> import numpy as np
>>> from cca_zoo.probabilistic import ProbabilisticCCA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((50, 4))
>>> X2 = rng.standard_normal((50, 3))
>>> model = ProbabilisticCCA(
... n_components=2, n_warmup=10, n_posterior_samples=10
... ).fit([X1, X2])
Source code in cca_zoo/probabilistic/_pcca.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 |
|---|---|
ProbabilisticCCA
|
self. |
Source code in cca_zoo/probabilistic/_pcca.py
VariationalBayesCCA ¶
VariationalBayesCCA(
n_components: int = 1,
*,
center: bool = True,
n_iter: int = 2000,
learning_rate: float = 0.01,
n_posterior_samples: int = 1000,
random_state: int | None = None,
)
Bases: BaseProbabilistic
Variational Bayesian CCA with automatic relevance determination.
The probabilistic CCA model with an ARD prior shared across views:
with each \(\Psi_i\) a full covariance, as in :class:ProbabilisticCCA.
Dimensions no view supports are shrunk away, so set n_components
generously and read ard_precision_. Inference is mean-field SVI in
numpyro, cheaper than :class:ProbabilisticCCA's NUTS. Requires the
probabilistic extra.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Upper bound on the number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
n_iter
|
int
|
SVI steps; there is no stopping rule, so all are run. Default is 2000. |
2000
|
learning_rate
|
float
|
Adam learning rate for SVI. Default is 1e-2. |
0.01
|
n_posterior_samples
|
int
|
Draws from the fitted posterior. Default is 1000. |
1000
|
random_state
|
int | None
|
Seed for the JAX PRNG. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
list[ndarray]
|
Posterior mean loadings of each view, shape (n_features_i, n_components). |
ard_precision_ |
list[ndarray]
|
Posterior mean ARD precision of each dimension; large means shrunk away. |
posterior_samples_ |
dict[str, Any]
|
Posterior draws keyed by site name. |
losses_ |
dict[str, Any]
|
SVI loss at each step. |
References
Wang, C. (2007). Variational Bayesian approach to canonical correlation analysis. IEEE Transactions on Neural Networks, 18(3), 905-910.
Examples:
>>> import numpy as np
>>> from cca_zoo.probabilistic import VariationalBayesCCA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((50, 4))
>>> X2 = rng.standard_normal((50, 3))
>>> model = VariationalBayesCCA(n_components=2, n_iter=50).fit([X1, X2])
Source code in cca_zoo/probabilistic/_vbcca.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 |
|---|---|
VariationalBayesCCA
|
self. |