Implement Wishart(v, 𝗩) matrix-variate distribution#106
Open
jzeuzs wants to merge 1 commit into
Open
Conversation
Axect
requested changes
Jul 10, 2026
Axect
left a comment
Owner
There was a problem hiding this comment.
Thanks for this, the Bartlett decomposition sampling and the scipy verification are much appreciated. The math (multivariate gamma, the vectorized p^2 x p^2 covariance) checks out on my side. A few points I'd like to sort out before merging.
On the O3 gating of pdf / ln_pdf.
sample_with_rnggenuinely needsO3becausecholeskyhas no pure-Rust path yet, so that gating is fine.- But
ln_pdfonly usesdet()andinv(), and both of those already have pure-Rust LU fallbacks (_ => self.lu().det()/self.lu().inv()). So theunimplemented!()on the non-O3branch ofln_pdfis stricter than it needs to be. - The side effect is that a user on the default feature set who calls
pdfgets a runtime panic instead of a working result. Could you drop theO3gate onln_pdfand keep it only on the sampling path?
On input/support validation.
- Right now
ln_pdfcomputes a finite value for inputs outside the support. For example, in even dimensionx = -Ihasdet = 1 > 0, so it returns a plausible-looking density even though the true value should be0. MatDist::Wishart(df, scale)also does not checkdf > p - 1, soChiSquared::new(df - i)can panic during sampling.- For reference, the existing
Dirichletln_pdfalready asserts its support (x_i in (0,1),sum ~ 1), so matching that level of checking here would be consistent with the rest of the crate. Returningln_pdf = -inf/pdf = 0outside the support would be even nicer than a panic.
On tests.
The current tests focus on fixed 1x1 / 2x2 cases. It would be good to also cover:
- invalid
dfand non-SPDscale/x sample_with_rngreproducibility with a fixed seed- empirical mean / covariance of many samples converging to
mean()/cov()
Minor. In src/util/print.rs, impl Printable for Vec<Matrix> and impl Printable for &Vec<Matrix> are identical. You can forward one to the other to avoid the duplication.
Nice work overall. Once these are addressed I'm happy to merge. Thanks again.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This implements the Wishart Distribution or$\mathcal{W}_p (\nu, \mathbf{V})$ , enabling matrix-variate statistical modeling.
Sampling is implemented via the Bartlett decomposition ($\mathbf{X} = \mathbf{L} \mathbf{A} \mathbf{A}^T \mathbf{L}^T$ ). This provides a numerically stable and efficient way to draw samples using the Cholesky factor of the scale matrix rather than using matrix multiplication.
Further, because the covariance of a Wishart distribution is a 4th-order tensor, this implementation flattens the result into a$p^2 \times p^2$
Matrixin order to allow us to stay within the existingStatisticstrait's design constraints while providing full covariance/correlation information.Results have been verified with
scipy.stats.wishart.