mixle.analysis.covariance_shrinkage module

Ledoit-Wolf covariance-shrinkage estimator for the multivariate Gaussian.

The sample covariance is a poor estimate when the number of observations is not large relative to the dimension: its extreme eigenvalues are biased (the largest too large, the smallest too small), which is exactly what wrecks anything that inverts it – Markowitz portfolios, Mahalanobis distances, GP precisions. Ledoit and Wolf (2004, A well-conditioned estimator for large-dimensional covariance matrices) give the optimal convex combination of the sample covariance S and a well-conditioned target F (a scaled identity F = (tr S / d) I):

Sigma_hat = (1 - delta) S + delta F, delta = clip( b^2 / d^2 , 0, 1 ),

where d^2 = ||S - F||_F^2 and b^2 = (1/n^2) sum_t ||y_t y_t^T - S||_F^2 (y_t the centered observations) – a data-driven shrinkage intensity, no cross-validation needed.

LedoitWolfEstimator is a first-class mixle estimator: it follows the accumulator/factory/encoder contract, so it composes with estimate, mixtures, HMMs, and anything else that takes a ParameterEstimator, and it returns an ordinary MultivariateGaussianDistribution. The shrinkage intensity is computed exactly from streaming sufficient statistics – the centered 4th moment sum_t (y_t . y_t)^2 decomposes into sum x, sum x x^T, sum x ||x||^2 and sum ||x||^4 – so it works under seq_update and combine (distributed) without holding the data.

Reference: Ledoit & Wolf, ‘A well-conditioned estimator for large-dimensional covariance matrices’, J. Multivariate Anal. (2004).

class LedoitWolfEstimator(dim=None, name=None, keys=None)[source]

Bases: ParameterEstimator

Estimate a multivariate Gaussian with a Ledoit-Wolf-shrunk covariance.

Returns a MultivariateGaussianDistribution whose mean is the sample mean and whose covariance is shrunk toward a scaled identity by the data-driven Ledoit-Wolf intensity. The chosen intensity is exposed on the returned distribution as dist.shrinkage for inspection. When the accumulated sufficient statistics carry too little effective weight or no identifiable dispersion, raises LedoitWolfInsufficientData.

Parameters:
  • dim (int | None)

  • name (str | None)

  • keys (str | None)

accumulator_factory()[source]

Return an accumulator factory for streaming Ledoit-Wolf sufficient statistics.

Return type:

LedoitWolfAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate a Gaussian distribution from accumulated Ledoit-Wolf sufficient statistics.

Raises:
  • LedoitWolfInsufficientData – If the accumulated weight is below _MIN_EFFECTIVE_COUNT or the observations contain no numerically identifiable dispersion.

  • ValueError – If the shrinkage computation itself produces a non-finite mean or covariance despite adequate, already-validated sufficient statistics (a numerical-overflow defect, not an insufficient-data condition). A substantively non-positive-semidefinite result is rejected by MultivariateGaussianDistribution’s own construction, which this relies on as the final PSD gate.

Return type:

MultivariateGaussianDistribution

exception LedoitWolfInsufficientData(reason, effective_count, dim)[source]

Bases: ValueError

Specific estimation error raised when the data cannot identify covariance.

effective_count and dim let a caller decide whether to accumulate more data and retry. Raising, rather than returning a foreign non-distribution object, preserves the common ParameterEstimator result protocol.

Parameters:
  • reason (str)

  • effective_count (float)

  • dim (int | None)