mixle.stats.matrix.lkj module¶
LKJ distribution over correlation matrices.
The Lewandowski-Kurowicka-Joe (LKJ) law places density f(R) = c_d(eta) * det(R)^(eta-1) on d x d
correlation matrices (symmetric, unit diagonal, positive definite). The concentration eta > 0 tilts
mass toward the identity: eta = 1 is uniform over correlation matrices, eta > 1 favours weak
correlations (R near I), and eta < 1 favours strong ones. It is the standard prior on
correlation matrices in hierarchical Bayesian models (Stan’s default), separating a covariance into
scales times a correlation.
Normalizer (C-vine derivation, verified to high precision against arbitrary-precision integration over
the correlation elliptope for d = 2, 3):
Z_d(eta) = prod_{k=1}^{d-1} B(eta + (d-1-k)/2, 1/2)^(d-k), c_d(eta) = 1 / Z_d(eta).
It samples exactly by the onion method (Lewandowski et al. 2009, sec. 3.2): each off-diagonal entry then
has the exact marginal (r + 1)/2 ~ Beta(eta + (d-2)/2, eta + (d-2)/2). Because the density depends on
R only through det(R), observations are encoded as log det(R), and eta is fit by maximum
likelihood (a 1-D root find: the mean log-determinant equals sum_k (d-k)[psi(eta+e_k) -
psi(eta+e_k+1/2)] with e_k = (d-1-k)/2).
Reference: Lewandowski, Kurowicka & Joe, “Generating random correlation matrices based on vines and extended onion method”, J. Multivariate Analysis 100 (2009).
- class LKJDistribution(dim, eta, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionLKJ distribution over
dim x dimcorrelation matrices with concentrationeta > 0.- density(x)[source]
Return the probability density at a correlation matrix
x.
- log_density(x)[source]
Return the log-density at a
dim x dimcorrelation matrix (-infif not positive definite).
- seq_log_density(x)[source]
Return vectorized log-density for a sequence-encoded array of
log det(R)values.
- sampler(seed=None)[source]
Return an onion-method sampler for correlation matrices.
- Parameters:
seed (int | None)
- Return type:
LKJSampler
- estimator(pseudo_count=None)[source]
Return a maximum-likelihood estimator for the concentration
eta(dimfixed).- Parameters:
pseudo_count (float | None)
- Return type:
LKJEstimator
- dist_to_encoder()[source]
Return the data encoder (a correlation matrix is encoded as its log-determinant).
- Return type:
LKJDataEncoder
- class LKJSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSample correlation matrices by the onion method (Lewandowski-Kurowicka-Joe 2009).
- Parameters:
dist (LKJDistribution)
seed (int | None)
- sample(size=None)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class LKJAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate
(count, sum of log det(R))– the sufficient statistics for the eta-MLE.- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
x (Any)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_initialize(x, weights, rng)[source]
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
LKJDataEncoder
- class LKJAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for LKJAccumulator.
- make()[source]
- Return type:
LKJAccumulator