mixle.stats.matrix.inverse_wishart module¶
Inverse-Wishart distribution – a distribution over symmetric positive-definite matrices.
If X^{-1} ~ Wishart(df, scale^{-1}) then X ~ InverseWishart(df, scale); it is the conjugate
prior for a multivariate-normal covariance and the standard model for a random covariance matrix
(rather than a random precision). With df > p - 1 and scale matrix Psi,
- log f(X) = df/2 log|Psi| - df p/2 log 2 - log Gamma_p(df/2)
(df+p+1)/2 log|X| - 1/2 tr(Psi X^{-1}).
df is a fixed, known parameter. When df > p + 1, the scale can be
estimated by the explicit method-of-moments identity
Psi = (df - p - 1) * mean(X). This package labels that operation as a
mean-moment fit; it is not the inverse-Wishart likelihood MLE, and it is
rejected where the required mean does not exist.
Reference: Mardia, Kent & Bibby, Multivariate Analysis (Academic Press, 1979).
- exception InverseWishartMomentFitError[source]
Bases:
RuntimeErrorRaised when the inverse-Wishart mean-moment fit is undefined.
- class InverseWishartDistribution(df, scale, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionInverse-Wishart distribution with
dfdegrees of freedom and scale matrixscale(p, p).- density(x)[source]
Return the density at a single
(p, p)SPD matrix.
- log_density(x)[source]
Return the log-density at a single
(p, p)SPD matrix (-infif not symmetric or not positive definite).Mirrors
seq_log_density()’s routines exactly rather than merely approximately: usesbatched_pd_logdet()(the same eigenvalue-based check) instead ofcholesky_logdet()for positive-definiteness/log-determinant, and the sameeinsumtrace contraction instead oftrace(A @ B). Near the positive-definiteness boundary a Cholesky factorization and an eigendecomposition can round differently and even disagree on whether a matrix is PD at all, and a full matrix product summed viatraceaccumulates in a different order than the directeinsumcontraction – both previously made this scalar path and the vectorized path diverge (in value, and occasionally in support) on the same input.
- seq_log_density(x)[source]
Vectorized log-density for a stack of SPD matrices, shape
(N, p, p)(-infper row that is not symmetric or not positive definite).np.linalg.inv, likenp.linalg.cholesky(seebatched_pd_logdet()’s docstring), raisesLinAlgErrorfor the WHOLE batch if even one matrix is singular – singular is just the not-positive-definite boundary case, already scored-infbelow viais_valid, so its inverse is never actually used. Substitute the identity for invalid rows before inverting so the call always succeeds, mirroring how the scalarlog_density()path never callsinvon a matrix it is about to reject.
- sampler(seed=None)[source]
Return a sampler for drawing SPD matrices from this distribution.
- Parameters:
seed (int | None)
- Return type:
InverseWishartSampler
- estimator(pseudo_count=None)[source]
Return the fixed-df mean-moment estimator when that moment exists.
- Parameters:
pseudo_count (float | None)
- Return type:
InverseWishartMeanMomentEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
InverseWishartDataEncoder
- class InverseWishartSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw SPD matrices by inverting a Bartlett-factorized precision draw.
- Parameters:
dist (InverseWishartDistribution)
seed (int | None)
- class InverseWishartAccumulator(dim, name=None, keys=None)[source]
Bases:
_MeanScatterAccumulatorAccumulate the weighted sum of matrices
sum_i w_i X_iand the total weight.- acc_to_encoder()[source]
Return the encoder compatible with the accumulated matrix statistics.
- Return type:
InverseWishartDataEncoder
- class InverseWishartAccumulatorFactory(dim, name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for InverseWishartAccumulator.
- make()[source]
Create an accumulator for weighted inverse-Wishart matrix observations.
- Return type:
InverseWishartAccumulator
- class InverseWishartMeanMomentEstimator(dim, df, name=None, keys=None)[source]
Bases:
ParameterEstimatorFixed-df method-of-moments estimator based on the inverse-Wishart mean.
This is not a likelihood MLE. It is defined only for
df > p + 1, whereE[X] = Psi / (df-p-1)exists.- accumulator_factory()[source]
Return an accumulator factory for estimating the fixed-df scale matrix.
- Return type:
InverseWishartAccumulatorFactory
- class InverseWishartDataEncoder(dim)[source]
Bases:
DataSequenceEncoderEncode a sequence of
(p, p)matrices as an(N, p, p)float array.- Parameters:
dim (int)
- seq_encode(x)[source]
Encode a sequence of SPD matrices as a floating matrix stack.
- InverseWishartEstimator
alias of
InverseWishartMeanMomentEstimator