Skip to content
class

Wishart

extendsDistribution
Wishart(df: Tensor | float, covariance_matrix: Tensor | None = None, precision_matrix: Tensor | None = None, scale_tril: Tensor | None = None, validate_args: bool | None = None)

Wishart distribution over symmetric positive-definite matrices.

Matrix-variate generalisation of the lucid.distributions.Chi2 / Gamma distribution: the distribution of the (unnormalised) sample covariance matrix S=∑i=1νzizi⊤S = \sum_{i=1}^{\nu} \mathbf{z}_i \mathbf{z}_i^\top formed from ν\nu independent Multivariate Normal draws zi∼N(0,Σ)\mathbf{z}_i \sim \mathcal{N}(\mathbf{0}, \Sigma). It is the conjugate prior of the precision matrix of a Multivariate Normal with known mean.

Specify exactly one of covariance_matrix, precision_matrix, or scale_tril — the other parameterisations are derived internally via Cholesky factorisation.

Parameters

dfTensor or float
Degrees of freedom ν>D−1\nu > D - 1 where DD is the matrix dimension. Values ν≤D−1\nu \leq D - 1 violate the positive-definiteness guarantee.
covariance_matrixTensor= None
Positive-definite scale matrix Σ\Sigma of shape (..., D, D).
precision_matrixTensor= None
Positive-definite precision matrix Σ−1\Sigma^{-1} of shape (..., D, D).
scale_trilTensor= None
Lower-triangular Cholesky factor LL of Σ\Sigma (so Σ=LL⊤\Sigma = L L^\top), shape (..., D, D) with positive diagonal.
validate_argsbool= None
If True, validate parameter constraints at construction time.

Notes

Probability density on the cone of symmetric positive-definite D×DD \times D matrices:

p(X;ν,Σ)=∣X∣(ν−D−1)/2exp⁡ ⁣(−12 tr(Σ−1X))2νD/2∣Σ∣ν/2ΓD(ν/2)p(\mathbf{X}; \nu, \Sigma) = \frac{|\mathbf{X}|^{(\nu - D - 1)/2} \exp\!\bigl(-\tfrac{1}{2}\,\mathrm{tr}(\Sigma^{-1}\mathbf{X})\bigr)} {2^{\nu D / 2} |\Sigma|^{\nu/2} \Gamma_D(\nu/2)}

where ΓD\Gamma_D is the multivariate gamma function:

ΓD(a)=πD(D−1)/4∏i=1DΓ ⁣(a+1−i2)\Gamma_D(a) = \pi^{D(D-1)/4} \prod_{i=1}^{D} \Gamma\!\left(a + \tfrac{1 - i}{2}\right)

Moments:

E[X]=νΣ,Var[Xij]=ν(Σij2+ΣiiΣjj)\mathbb{E}[\mathbf{X}] = \nu \Sigma, \qquad \mathrm{Var}[X_{ij}] = \nu (\Sigma_{ij}^2 + \Sigma_{ii} \Sigma_{jj})

Special cases / relations:

  • D=1D = 1 → Wishart(ν,σ2)=σ2χ2(ν)\mathrm{Wishart}(\nu, \sigma^2) = \sigma^2 \chi^2(\nu).
  • Inverse: if X∼Wishart(ν,Σ)\mathbf{X} \sim \mathrm{Wishart}(\nu, \Sigma) then X−1\mathbf{X}^{-1} follows the inverse-Wishart distribution, the conjugate prior of the covariance (not precision) matrix.
  • Bartlett decomposition: X=LAA⊤L⊤\mathbf{X} = L A A^\top L^\top with AA lower-triangular containing χ2\chi^2 draws on the diagonal and standard Normals below — used for sampling.

Examples

>>> import lucid
>>> from lucid.distributions import Wishart
>>> Sigma = lucid.tensor([[2.0, 0.5], [0.5, 1.0]])
>>> d = Wishart(df=5.0, covariance_matrix=Sigma)
>>> d.sample((4,)).shape
(4, 2, 2)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(df: Tensor | float, covariance_matrix: Tensor | None = None, precision_matrix: Tensor | None = None, scale_tril: Tensor | None = None, validate_args: bool | None = None)

Initialise a Wishart distribution.

Converts covariance_matrix or precision_matrix to a Cholesky factor internally. Exactly one scale specification must be provided. The batch shape is the broadcast of df with the leading axes of the matrix: a vector of degrees of freedom over one scale matrix, or a stack of matrices over one df, is a batch.

Parameters

dfTensor | float
Degrees of freedom ν>D−1\nu > D - 1 where DD is the matrix dimension. Values ν≤D−1\nu \leq D - 1 violate the positive-definiteness guarantee.
covariance_matrixTensor | None= None
Positive-definite scale matrix Σ\Sigma of shape (..., D, D).
precision_matrixTensor | None= None
Positive-definite precision matrix Σ−1\Sigma^{-1} of shape (..., D, D). Converted via Σ=(Σ−1)−1\Sigma = (\Sigma^{-1})^{-1}.
scale_trilTensor | None= None
Lower-triangular Cholesky factor LL of Σ\Sigma, shape (..., D, D) with positive diagonal.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Raises

ValueError
If not exactly one scale specification is given, if ν≤D−1\nu \leq D - 1, if the matrix given is not a square (..., D, D) matrix, or — under validation — if it is not positive-definite (a scale_tril: not lower-triangular with a positive diagonal).
lucid.linalg.LinAlgError
With validation off, if a covariance or precision matrix has no Cholesky factorisation.

Properties

4
prop

mode

→Tensor
mode: Tensor

Mode of the Wishart distribution: (ν−D−1) Σ(\nu - D - 1)\, \Sigma.

nan where ν≤D+1\nu \leq D + 1, as the reference framework answers.

prop

support

→Constraint
support: Constraint

Constraint: positive-definite symmetric matrices.

prop

variance

→Tensor
variance: Tensor

Var[W_{ij}] = df · (Σ_{ij}² + Σ_{ii} Σ_{jj}).

Instance methods

7
fn

covariance_matrix

→Tensor
covariance_matrix()

Scale matrix Σ=LL⊤\Sigma = L L^\top — as given, or derived from scale_tril on access.

fn

entropy

→Tensor
entropy()

Differential entropy of the Wishart distribution, in nats.

With pp the matrix dimension, ν\nu the degrees of freedom and LL the Cholesky factor of the scale matrix:

H=(p+1)(plog⁡22+∑ilog⁡Lii)+log⁡Γp ⁣(ν2)−ν−p−12 ψp ⁣(ν2)+νp2H = (p + 1)\left(\tfrac{p \log 2}{2} + \sum_i \log L_{ii}\right) + \log\Gamma_p\!\left(\tfrac{\nu}{2}\right) - \tfrac{\nu - p - 1}{2}\,\psi_p\!\left(\tfrac{\nu}{2}\right) + \tfrac{\nu p}{2}

where ψp(a)=∑i=0p−1ψ(a−i/2)\psi_p(a) = \sum_{i=0}^{p-1} \psi(a - i/2) is the multivariate digamma function. perplexity is its exponential.

Returns

Tensor

Entropy of shape batch_shape.

Examples

>>> import lucid
>>> from lucid.distributions import Wishart
>>> Wishart(df=4.0, covariance_matrix=lucid.eye(2)).entropy()
tensor(6.301)
fn

expand

→Self
expand(batch_shape: Sequence[int], _instance: Self | None = None)

Return a new Wishart instance with parameters expanded to batch_shape.

fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-density of the Wishart distribution.

log p(X) = (df − d − 1)/2 · log|X|
         − tr(Σ⁻¹ X) / 2
         − df · d / 2 · log 2
         − df/2 · log|Σ|
         − log Γ_d(df/2)

where Γ_d is the multivariate gamma function.

fn

precision_matrix

→Tensor
precision_matrix()

Σ−1\Sigma^{-1} — as given, or derived from scale_tril on access.

fn

rsample

→Tensor
rsample(sample_shape: tuple[int, ...] = ())

Draw a reparameterised sample via the Bartlett decomposition.

fn

sample

→Tensor
sample(sample_shape: tuple[int, ...] = ())

Draw a sample via the Bartlett decomposition.

Each sample is L A Aᵀ Lᵀ where L = scale_tril and A is a random lower-triangular matrix with:

  • diagonal A_{ii} ~ sqrt(χ²_{df − i}) for i = 0, …, d−1,
  • off-diagonal A_{ij} ~ N(0, 1) for i > j.