Distributions.jl icon indicating copy to clipboard operation
Distributions.jl copied to clipboard

Add multivariate hypergeometric distribution

Open Michael-Howes opened this issue 7 months ago • 1 comments

Overview

This pull requests adds the multivariate hypergeometric distribution. This is a generalization of the hypergeometric distribution. It includes:

  • The file scr/multivariate/mvhypergeom.jl that implements MvHypergeom as a subtype of DiscreteMultivariateDistribution.
  • The file scr/samplers/mvhypergeom.jl that implements sampling.
  • A test file test/multivariate/mvherpgeom.jl.

Motivation

The multivariate hypergeometric distribution is an important distribution in statistics for testing independence in contingency tables. It is implemented in the numpy and scipy Python packages but currently it is not supported in Distributions.jl.

Implementation details

The type MvHypergeometric is created as a subtype of DiscreteMultivariateDistribution. Functions for the mean, variance and covariance matrix are implemented. Evaluation of the log pdf and sampling are also implemented. Sampling is implemented in the file scr/samplers/mvhypergeom.jl. The procedure is analogous to sampling from a multinomial distribution. The entries are sampled sequentially from univariate hypergeometric distributions.

Testing

Tests are include in test/multivariate/mvherpgeom.jl. The statistics, pdf and sampling are all tested. The pdf is also compared to the pdf of the hypergeometric distribution. Specifically, for the marginal and conditional distributions of the multivariate hypergeometric are univariate hypergeometric (as used in the sampling).

Dependencies

No new dependencies are added.

Michael-Howes avatar Apr 09 '25 22:04 Michael-Howes

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 86.46%. Comparing base (0421b18) to head (afe12d1).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1963      +/-   ##
==========================================
+ Coverage   86.36%   86.46%   +0.10%     
==========================================
  Files         146      147       +1     
  Lines        8786     8852      +66     
==========================================
+ Hits         7588     7654      +66     
  Misses       1198     1198              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Apr 09 '25 22:04 codecov-commenter

@devmotion Thank you for the review. I have resolved most of them but left two open (one about documentation, another about computing the covariance matrix). The PR is ready for a second round of review. Thanks again.

Michael-Howes avatar Nov 10 '25 22:11 Michael-Howes