Covariance regularization¶
Covariance regularization utilities are imported from
gmm_divergence.covariance.
Covariance regularization strategies for covariance matrices are techniques used to ensure that the covariance matrices of the components in a GMM (or single Gaussians) are well-conditioned and do not lead to numerical instability during estimation. These strategies can help prevent issues such as singular covariance matrices, which can arise when the data is not sufficiently diverse or when there are too few data points relative to the number of parameters being estimated.
gmm_divergence.covariance
¶
Public covariance regularization API.
Functions¶
regularize_covariance
¶
regularize_covariance(
covariance: ArrayLike,
/,
*,
method: CovarianceRegularizer = "diagonal_loading",
batched: Literal[False] = False,
copy: bool = True,
) -> Covariance
regularize_covariance(
covariance: ArrayLike,
/,
*,
method: CovarianceRegularizer = "diagonal_loading",
batched: bool | None = None,
copy: bool = True,
) -> Covariance | Covariances
Regularize a covariance matrix or a batch of covariance matrices.
The covariances are validated and regularized according to the specified
method. The input can be either a single covariance matrix with shape (d,
d)` or a batch of covariance matrices with shape `(n, d, d). If the
batch flag is not explicitly provided, the shape of the input is used to infer whether it is
batched or not.
Parameters:
-
covariance(array - like) –A covariance matrix with shape
(d, d)or a batch with shape(n, d, d). -
method(str or covariance regularizer configuration, default:"diagonal_loading") –Regularization method to apply. Passing a string uses the method's default configuration.
DiagonalLoadingandLowRankalso accept epsilon heuristic strategy objects through theirepsfield. -
batched(bool or None, default:None) –Whether to interpret the input as batched. If
None, the shape is inferred from the array rank. -
copy(bool, default:True) –Whether to regularize a copy of the input.
Returns:
-
Covariance or batch of covariances–Regularized covariance matrix or matrices.
Source code in src/gmm_divergence/covariance/_api.py
estimate_epsilon
¶
estimate_epsilon(
covariance: ArrayLike,
/,
*,
method: EpsilonMethod = "relative_trace",
batched: Literal[False] = False,
) -> float
estimate_epsilon(
covariance: ArrayLike,
/,
*,
method: EpsilonMethod = "relative_trace",
batched: bool | None = None,
) -> float | FloatArray
Estimate a diagonal-loading epsilon from covariance scale or spectrum.
Parameters:
-
covariance(array - like) –Covariance matrix with shape
(d, d)or batch of matrices with shape(n, d, d). -
method(str or epsilon heuristic configuration, default:"relative_trace") –Heuristic used to estimate the epsilon value.
-
batched(bool or None, default:None) –Whether to interpret the input as batched. If
None, the shape is inferred from the input rank.
Returns:
-
float or array–Estimated epsilon value(s). If the input is a single covariance, a float is returned. If the input is a batch of covariances, an array of shape
(n,)is returned with one epsilon per covariance.
Source code in src/gmm_divergence/covariance/_epsilon.py
Regularizers¶
DiagonalLoading
dataclass
¶
Diagonal loading regularization configuration.
Applies the regularizer
where \(\Sigma\) is the input covariance, \(I\) is the identity matrix, and \(\varepsilon \ge 0\) is the diagonal loading amount.
eps
class-attribute
instance-attribute
¶
Diagonal loading amount or epsilon heuristic.
LinearShrinkage
dataclass
¶
Linear shrinkage toward an isotropic covariance target.
Applies the shrinkage
where \(d\) is the dimensionality and \(\tau I\) is an isotropic covariance with the same average marginal variance as \(\Sigma\).
alpha
class-attribute
instance-attribute
¶
Interpolation weight between the covariance and isotropic target.
DiagonalShrinkage
dataclass
¶
Shrinkage toward the covariance diagonal.
Applies the shrinkage
where \(\mathrm{diag}(\Sigma)\) is the diagonal matrix containing the marginal variances of \(\Sigma\).
alpha
class-attribute
instance-attribute
¶
Interpolation weight between the covariance and diagonal target.
EigenvalueClipping
dataclass
¶
Eigenvalue-clipping regularization configuration.
Given an eigendecomposition
this method returns
where \(\lambda_{\min} > 0\) is the eigenvalue floor.
min_eigenvalue
class-attribute
instance-attribute
¶
Smallest allowed eigenvalue after clipping.
LowRank
dataclass
¶
Low-rank covariance approximation with diagonal loading.
For an eigendecomposition
the approximation keeps the leading rank eigenpairs and returns
where \(U_r\) contains the leading eigenvectors, \(\Lambda_r\) the corresponding eigenvalues, and \(\varepsilon \ge 0\) is the diagonal loading applied after truncation.
Epsilon Heuristics¶
RelativeToTrace
dataclass
¶
Scale epsilon with the covariance trace.
Sets
where \(d\) is the covariance dimension.
c
class-attribute
instance-attribute
¶
Multiplier \(c\) in \(\varepsilon = c\,\mathrm{tr}(\Sigma)/d\).
TargetConditionNumber
dataclass
¶
Choose epsilon to enforce a target condition number.
For
picks the smallest \(\varepsilon \ge 0\) such that \(\kappa(\Sigma_{\mathrm{reg}}) \le \text{kappa}\).
kappa
class-attribute
instance-attribute
¶
Target upper bound \(\kappa(\Sigma + \varepsilon I)\).
ResidualVariance
dataclass
¶
Scale epsilon from discarded low-rank variance.
With target rank \(r\), sets
where \(\lambda_i^{\mathrm{disc}}\) are the discarded eigenvalues.