Implement Dirichlet(α) distribution#95
Conversation
|
Hello! Are there any updates on this? |
Axect
left a comment
There was a problem hiding this comment.
Thanks @jzeuzs, and sorry for the slow review. The overall design is right: MVDist / MVRNG mirror the existing OPDist / TPDist / RNG patterns, the gamma-normalization sampling is the standard algorithm, and the mean/var/cov formulas check out against the test values.
Requesting changes for a handful of small items. Three are inline; the remaining two are branch-level:
- Formatting: CI now runs
cargo fmt --all --checkon every PR, and this branch currently fails it (trailing whitespace on a few lines indist.rsandtests/dist.rs, import ordering). A plaincargo fmtfixes all of it. - Rebase onto current
dev: there are no textual conflicts, but the fmt / clippy CI gates were added after this branch diverged, so CI should run against the current base.
Happy to merge once these are in.
6cbb17a to
fbf5896
Compare
|
@Axect thank you for the review. I have addressed all the feedback. This PR also opens the way for implementing the Multivariate Normal, Multinomial (Multivariate Binomial), Categorical (Multivariate Bernoulli), and the Multivariate t-Distribution. I also noticed the Wishart distribution is on the TODO.md. If it is okay with you, I would love to take that on next. Let me know if this PR is good to merge! |
Axect
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround, @jzeuzs! I pulled the branch and everything checks out:
- fmt / clippy / tests: cargo fmt --all --check passes, clippy is clean (no errors), and the doctests plus tests/dist.rs all pass.
- The three inline items: all handled. pub use self::MVDist::*; sorts out the doctest scoping, the no_run example compiles now, and moving to ln_gamma keeps ln_pdf finite even for large alpha.
- Numerical check: I cross-checked mean / var / cov / pdf / ln_pdf against scipy.stats.dirichlet on a few alpha vectors, and they match to floating-point rounding (densities agree to ~1e-11, just the ln_gamma vs gammaln gap). The alpha=200 case that used to overflow now returns a correct finite value, and the Gamma-normalization sampler reproduces the theoretical mean over a large draw. Nicely done. 👍
Merging this now, thanks for the contribution!
And yes, I'd be happy for you to take on Wishart next. Looking forward to it!
This PR introduces support for multivariate distributions to peroxide, starting with the Dirichlet distribution.
A separate
MVRNGtrait was added specifically for N-dimensional random number generation and PDF evaluation. Sampling was implemented using independent Gamma variables.