Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken negative binomial sampling #328

Merged
merged 2 commits into from
Feb 24, 2025
Merged

Conversation

KOVALW
Copy link
Contributor

@KOVALW KOVALW commented Feb 12, 2025

Description

The negative binomial distribution sampler is not working because gamma is parameterized to sample using rate rather than scale. The original input was the scale as a function of self.p leading to incorrect samples for the specified distribution. The sample_unchecked is now supplied with the rate calculated from self.p and a test is provided to ensure behavior is in agreement with the theoretical mean and variance of the specified distribution.

Copy link

codecov bot commented Feb 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.93%. Comparing base (4dc5b98) to head (0371552).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #328      +/-   ##
==========================================
+ Coverage   94.40%   94.93%   +0.52%     
==========================================
  Files          59       59              
  Lines       13432    13448      +16     
==========================================
+ Hits        12681    12767      +86     
+ Misses        751      681      -70     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@YeungOnion
Copy link
Contributor

Thanks for catching this!

@@ -487,4 +487,25 @@ mod tests {
let sf = |arg: u64| move |x: NegativeBinomial| x.sf(arg);
test_absolute(3.0, 0.5, 5.282409836586059e-28, 1e-28, sf(100));
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for seeding so we don't have a flaky test! Would you be able to wrap this test with a cfg so that it only compiles with the rand feature?

let theoretical_mean = dist.mean().unwrap();
let theoretical_variance = dist.variance().unwrap();

assert!(prec::almost_eq(sample_mean, theoretical_mean, tol));
Copy link
Contributor

Choose a reason for hiding this comment

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

I have an optional suggestion here, would you use the hypothesis testing module to verify this sampler? I'd use chi square GoF. If not, I was thinking of adding these kinds of tests for sampling over time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good idea - will keep in mind to add over time as possible

@YeungOnion
Copy link
Contributor

The only failing test is an unrelated one we're working on in #325, thanks for contributing a fix!

@YeungOnion YeungOnion merged commit 7b26546 into statrs-dev:master Feb 24, 2025
9 of 10 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