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; since E[X] = Psi / (df - p - 1) the scale is estimated in closed form as Psi = (df - p - 1) * mean(X) (for df > p + 1).

Reference: Mardia, Kent & Bibby, Multivariate Analysis (Academic Press, 1979).

class InverseWishartDistribution(df, scale, name=None, keys=None)[source]

Bases: SequenceEncodableProbabilityDistribution

Inverse-Wishart distribution with df degrees of freedom and scale matrix scale (p, p).

Parameters:
density(x)[source]

Return the density at a single (p, p) SPD matrix.

Parameters:

x (ndarray)

Return type:

float

log_density(x)[source]

Return the log-density at a single (p, p) SPD matrix (-inf if not positive definite).

Parameters:

x (ndarray)

Return type:

float

seq_log_density(x)[source]

Vectorized log-density for a stack of SPD matrices, shape (N, p, p).

Parameters:

x (ndarray)

Return type:

ndarray

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 a closed-form estimator for the scale at the fixed degrees of freedom df.

Parameters:

pseudo_count (float | None)

Return type:

InverseWishartEstimator

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: DistributionSampler

Draw SPD matrices by inverting a Wishart(df, scale^{-1}) draw.

Parameters:
  • dist (InverseWishartDistribution)

  • seed (int | None)

sample(size=None)[source]

Draw one or more inverse-Wishart SPD matrix samples.

Parameters:

size (int | None)

Return type:

ndarray

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

Bases: _MeanScatterAccumulator

Accumulate the weighted sum of matrices sum_i w_i X_i and the total weight.

Parameters:
  • dim (int)

  • name (str | None)

  • keys (str | None)

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: StatisticAccumulatorFactory

Factory for InverseWishartAccumulator.

Parameters:
  • dim (int)

  • name (str | None)

  • keys (str | None)

make()[source]

Create an accumulator for weighted inverse-Wishart matrix observations.

Return type:

InverseWishartAccumulator

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

Bases: ParameterEstimator

Closed-form scale estimator at fixed df: Psi = (df-p-1) * mean(X) since E[X] = Psi/(df-p-1).

Parameters:
accumulator_factory()[source]

Return an accumulator factory for estimating the fixed-df scale matrix.

Return type:

InverseWishartAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate the inverse-Wishart scale matrix from weighted matrix means.

Parameters:
Return type:

InverseWishartDistribution

class InverseWishartDataEncoder[source]

Bases: DataSequenceEncoder

Encode a sequence of (p, p) matrices as an (N, p, p) float array.

seq_encode(x)[source]

Encode a sequence of SPD matrices as a floating matrix stack.

Parameters:

x (Sequence[ndarray])

Return type:

ndarray