Skip to content

Implement Dirichlet(α) distribution#95

Merged
Axect merged 6 commits into
Axect:devfrom
jzeuzs:implement-dirichlet
Jul 7, 2026
Merged

Implement Dirichlet(α) distribution#95
Axect merged 6 commits into
Axect:devfrom
jzeuzs:implement-dirichlet

Conversation

@jzeuzs

@jzeuzs jzeuzs commented Apr 19, 2026

Copy link
Copy Markdown

This PR introduces support for multivariate distributions to peroxide, starting with the Dirichlet distribution.

A separate MVRNG trait was added specifically for N-dimensional random number generation and PDF evaluation. Sampling was implemented using independent Gamma variables.

@jzeuzs

jzeuzs commented Jun 5, 2026

Copy link
Copy Markdown
Author

Hello! Are there any updates on this?

@Axect Axect left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --check on every PR, and this branch currently fails it (trailing whitespace on a few lines in dist.rs and tests/dist.rs, import ordering). A plain cargo fmt fixes 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.

Comment thread src/statistics/dist.rs
Comment thread src/statistics/dist.rs
Comment thread src/statistics/dist.rs Outdated
@jzeuzs jzeuzs force-pushed the implement-dirichlet branch from 6cbb17a to fbf5896 Compare July 7, 2026 08:32
@jzeuzs jzeuzs requested a review from Axect July 7, 2026 08:35
@jzeuzs

jzeuzs commented Jul 7, 2026

Copy link
Copy Markdown
Author

@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 Axect left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@Axect Axect merged commit 77b91b6 into Axect:dev Jul 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants