observation.posteriors
Posterior distributions for observation model parameters.
Classes
-
class LoadingPosterior(
- mean: ndarray | None = None,
- cov: ndarray | None = None,
- moment: ndarray | None = None,
)[source]
Posterior distribution over loading matrices.
- Parameters:
- mean
ndarray of float, shape (y_dim, x_dim) or None, default None Posterior mean.
- cov
ndarray of float, shape (y_dim, x_dim, x_dim) or None, default None Posterior covariances.
- moment
ndarray of float, shape (y_dim, x_dim, x_dim) or None, default None Posterior second moments.
- Attributes:
- mean
ndarray of float, shape (y_dim, x_dim) or None Posterior mean.
- cov
ndarray of float, shape (y_dim, x_dim, x_dim) or None Posterior covariances.
- moment
ndarray of float, shape (y_dim, x_dim, x_dim) or None Posterior second moments.
-
get_groups(
- y_dims: ndarray,
) → tuple[list[ndarray], list[ndarray], list[ndarray]][source]
Get list of views into posterior parameters, one per group.
- Parameters:
- y_dims
ndarray of int, shape (n_groups,) Dimensionalities of each observed group.
- Returns:
- group_means
list of ndarray or None List of views into mean, one per group. None if mean is None.
- group_covs
list of ndarray or None List of views into cov, one per group. None if cov is None.
- group_moments
list of ndarray or None List of views into moment, one per group. None if moment is None.
-
compute_moment(in_place: bool = True) → ndarray[source]
Compute posterior second moments E[C_i C_i^T] for each row i.
- Parameters:
- in_placebool, default
True If True, store in self.moment and return reference.
If False, return new array.
- Returns:
ndarraySecond moments, shape (y_dim, x_dim, x_dim).
-
compute_squared_norms(y_dims: ndarray) → ndarray[source]
Compute expected squared norm of each column per group.
- Parameters:
- y_dims
ndarray of int, shape (n_groups,) Dimensionalities of each observed group.
- Returns:
ndarraySquared norms, shape (n_groups, x_dim).
-
get_subset_dims(
- x_indices: ndarray,
- in_place: bool = True,
) → Self[source]
Keep only specified latent dimensions.
- Parameters:
- x_indices
ndarray of int Indices of latent dimensions to keep.
- in_placebool, default
True If True, modify self. If False, return new instance.
- Returns:
SelfModified or new instance.
-
class ARDPosterior(
- a: ndarray | None = None,
- b: ndarray | None = None,
- mean: ndarray | None = None,
)[source]
Posterior distribution over ARD parameters.
- Parameters:
- a
ndarray of float, shape (n_groups,) or None, default None Shape parameters.
- b
ndarray of float, shape (n_groups, x_dim) or None, default None Rate parameters.
- mean
ndarray of float, shape (n_groups, x_dim) or None, default None Posterior mean a/b.
- Attributes:
- a
ndarray of float, shape (n_groups,) or None Shape parameters.
- b
ndarray of float, shape (n_groups, x_dim) or None Rate parameters.
- mean
ndarray of float, shape (n_groups, x_dim) or None Posterior mean a/b.
-
compute_mean(in_place: bool = True) → ndarray[source]
Compute posterior mean a/b.
- Parameters:
- in_placebool, default
True If True, store in self.mean and return reference.
If False, return new array.
- Returns:
ndarrayPosterior mean, shape (n_groups, x_dim).
-
get_subset_dims(
- x_indices: ndarray,
- in_place: bool = True,
) → Self[source]
Keep only specified latent dimensions.
- Parameters:
- x_indices
ndarray of int Indices of latent dimensions to keep.
- in_placebool, default
True If True, modify self. If False, return new instance.
- Returns:
SelfModified or new instance.
-
class ObsMeanPosterior(
- mean: ndarray | None = None,
- cov: ndarray | None = None,
)[source]
Posterior distribution over observation mean parameters.
- Parameters:
- mean
ndarray of float, shape (y_dim,) or None, default None Posterior mean.
- cov
ndarray of float, shape (y_dim,) or None, default None Posterior variance (diagonal).
- Attributes:
- mean
ndarray of float, shape (y_dim,) or None Posterior mean.
- cov
ndarray of float, shape (y_dim,) or None Posterior variance (diagonal).
-
get_groups(
- y_dims: ndarray,
) → tuple[list[ndarray], list[ndarray]][source]
Get list of views into posterior parameters, one per group.
- Parameters:
- y_dims
ndarray of int, shape (n_groups,) Dimensionalities of each observed group.
- Returns:
- group_means
list of ndarray or None List of views into mean, one per group. None if mean is None.
- group_covs
list of ndarray or None List of views into cov, one per group. None if cov is None.
-
class ObsPrecPosterior(
- a: float | None = None,
- b: ndarray | None = None,
- mean: ndarray | None = None,
)[source]
Posterior distribution over observation precision parameters.
- Parameters:
- a
float or None, default None Shape parameter (scalar, shared across dimensions).
- b
ndarray of float, shape (y_dim,) or None, default None Rate parameters.
- mean
ndarray of float, shape (y_dim,) or None, default None Posterior mean a/b.
- Attributes:
- a
float or None Shape parameter (scalar, shared across dimensions).
- b
ndarray of float, shape (y_dim,) or None Rate parameters.
- mean
ndarray of float, shape (y_dim,) or None Posterior mean a/b.
-
get_groups(
- y_dims: ndarray,
) → tuple[list[ndarray], list[ndarray]][source]
Get list of views into posterior parameters, one per group.
- Parameters:
- y_dims
ndarray of int, shape (n_groups,) Dimensionalities of each observed group.
- Returns:
- group_means
list of ndarray or None List of views into mean, one per group. None if mean is None.
- group_bs
list of ndarray or None List of views into b, one per group. None if b is None.
-
compute_mean(in_place: bool = True) → ndarray[source]
Compute posterior mean a/b.
- Parameters:
- in_placebool, default
True If True, store in self.mean and return reference.
If False, return new array.
- Returns:
ndarrayPosterior mean, shape (y_dim,).
-
class ObsParamsPosterior(
- x_dim: int | None = None,
- y_dims: ndarray | None = None,
- C: LoadingPosterior | None = None,
- alpha: ARDPosterior | None = None,
- d: ObsMeanPosterior | None = None,
- phi: ObsPrecPosterior | None = None,
)[source]
Posterior distributions over all observation model parameters.
Bundle of posteriors q(C), q(alpha), q(d), q(phi) with methods for
sampling, computing point estimates, and analysis.
- Parameters:
- x_dim
int or None, default None Number of latent dimensions.
- y_dims
ndarray of int, shape (n_groups,) or None, default None Dimensionalities of each observed group.
- C
LoadingPosterior or None, default None Posterior over loading matrices.
- alpha
ARDPosterior or None, default None Posterior over ARD parameters.
- d
ObsMeanPosterior or None, default None Posterior over observation means.
- phi
ObsPrecPosterior or None, default None Posterior over observation precisions.
- Attributes:
- x_dim
int or None Number of latent dimensions.
- y_dims
ndarray of int, shape (n_groups,) or None Dimensionalities of each observed group.
- C
LoadingPosterior Posterior over loading matrices.
- alpha
ARDPosterior Posterior over ARD parameters.
- d
ObsMeanPosterior Posterior over observation means.
- phi
ObsPrecPosterior Posterior over observation precisions.
Examples
After fitting a GFA model, access the observation posterior:
>>> model = GFAModel()
>>> model.fit(Y)
>>> obs_post = model.obs_posterior
Get posterior mean as a realization:
>>> params = obs_post.posterior_mean
>>> params.C.shape
(20, 5)
Sample from the posterior:
>>> rng = np.random.default_rng(42)
>>> sample = obs_post.sample(rng)
-
property posterior_mean: ObsParamsRealization
Return posterior means as a realization.
- Returns:
ObsParamsRealizationRealization with posterior mean values.
-
sample(
- rng: Generator,
) → ObsParamsRealization[source]
Draw a sample from the posterior distributions.
Samples from q(C), q(alpha), q(d), q(phi) independently.
- Parameters:
- rng
numpy.random.Generator Random number generator.
- Returns:
ObsParamsRealizationSampled parameter values.
-
is_initialized() → bool[source]
Check if all posterior parameters have been initialized.
- Returns:
- bool
True if all required arrays are non-None.
-
get_subset_dims(
- x_indices: ndarray,
- in_place: bool = True,
) → Self[source]
Keep only specified latent dimensions.
- Parameters:
- x_indices
ndarray of int Indices of latent dimensions to keep.
- in_placebool, default
True If True, modify self. If False, return new instance.
- Returns:
SelfModified or new instance.
-
copy() → Self[source]
Return a deep copy.
- Returns:
SelfDeep copy of this instance.
-
compute_snr(
- y_dims: ndarray | None = None,
) → ndarray[source]
Compute signal-to-noise ratio for each group.
- Parameters:
- y_dims
ndarray or None, default None Dimensionalities of observed groups. If None, uses self.y_dims.
- Returns:
ndarraySNR for each group, shape (n_groups,).
-
static get_dim_types(n_groups: int) → ndarray[source]
Generate all dimension types for n_groups.
- Parameters:
- n_groups
int Number of observed groups.
- Returns:
ndarrayBoolean array, shape (n_groups, 2^n_groups). Column j indicates
which groups are involved in dimension type j.
-
compute_dimensionalities(
- cutoff_shared_var: float = 0.02,
- cutoff_snr: float = 0.001,
) → tuple[ndarray, ndarray, ndarray, ndarray][source]
Compute dimensionalities and variance explained by dimension type.
- Parameters:
- cutoff_shared_var
float, default 0.02 Minimum fraction of shared variance for significance.
- cutoff_snr
float, default 0.001 Minimum SNR for any latents to be significant.
- Returns:
- num_dim
ndarray of int, shape (n_dim_types,) Number of each dimension type.
- sig_dims
ndarray of bool, shape (n_groups, x_dim) Significant dimensions.
- var_exp
ndarray of float, shape (n_groups, n_dim_types) Variance explained by type.
- dim_types
ndarray of bool, shape (n_groups, n_dim_types) Dimension type indicators.
-
static compute_dims_pairs(
- num_dim: ndarray,
- dim_types: ndarray,
- var_exp: ndarray,
) → tuple[ndarray, ndarray, ndarray][source]
Analyze shared dimensionalities between pairs of groups.
- Parameters:
- num_dim
ndarray Number of each dimension type, shape (n_dim_types,).
- dim_types
ndarray Dimension type indicators, shape (n_groups, n_dim_types).
- var_exp
ndarray Variance explained by type, shape (n_groups, n_dim_types).
- Returns:
- pair_dims
ndarray of int, shape (n_pairs, 3) Dimensionalities per pair.
- pair_var_exp
ndarray of float, shape (n_pairs, 2) Variance explained per pair.
- pairs
ndarray of int, shape (n_pairs, 2) Pair indices.