Skip to content

fix: correct MGF in stats/base/dists/negative-binomial/mgf - #16213

Open
0PrashantYadav0 wants to merge 2 commits into
stdlib-js:developfrom
0PrashantYadav0:fix/negative-binomial-mgf
Open

0PrashantYadav0 wants to merge 2 commits into
stdlib-js:developfrom
0PrashantYadav0:fix/negative-binomial-mgf

Conversation

@0PrashantYadav0

Copy link
Copy Markdown
Member

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/negative-binomial/mgf (JavaScript, factory, and C) so that it returns the MGF of the number of failures before the r-th success, (p / (1 - (1-p) e^t))^r. That is the convention used by the rest of the negative-binomial namespace: pmf is C(x+r-1, x) p^r (1-p)^x for x = 0, 1, 2, ... and mean is r(1-p)/p. The package returned ((1-p) e^t / (1 - p e^t))^r, which swaps p and 1-p and counts trials instead of failures. For example, mgf( 0.1, 20.0, 0.8 ) returned 407636.99 instead of 1.7039, and M'(0) = r/(1-p) did not match mean( r, p ).
  • fixes the domain check from t >= -ln(p) to t >= -ln(1-p), the radius of convergence of Σ e^{tx} pmf(x).
  • evaluates the denominator as p - (1-p) expm1(t) and the bound as -log1p(-p). 1 - (1-p) e^t loses accuracy when p is small and t is close to the bound: against the fixtures, the direct form is off by up to 2252 ULP, and the expm1 form by at most 35.
  • replaces the TODO: Add test fixtures placeholder tests in test.mgf.js, test.factory.js, and test.native.js with Julia fixtures generated by Distributions.jl.
  • updates the README equation and the documented example values in negative-binomial/mgf and negative-binomial/ctor. Three examples (mgf( 0.1, 20.0, 0.1 ), mgf( 0.5, 10.0, 0.4 ), and mgf( 0.5, 7.4, 0.4 )) now land near the pole and return values up to ~10^25, so I changed their p to 0.9 or 0.8. The README also described r as "the number of failures until the experiment is stopped"; it now says "successes", matching the rest of the package's docs. The equation SVG will be regenerated by the markdown_equations workflow.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

In #15780 (geometric/mgf, which is the r = 1 case of this distribution), @Planeshifter asked whether the negative binomial MGF has the same problem. It does, and this PR fixes it.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

Verification:

  • Summing exp( t*x ) * pmf( x, r, p ) over x with stdlib's negative-binomial/pmf matches the new values (e.g., 1.703851511235214 vs 1.7038515112352146 for t = 0.1, r = 20, p = 0.8).
  • Distributions.jl 0.25.131 computes mgf(::NegativeBinomial) with the failures convention (as r * cgf( Geometric( p ), t )). The fixtures use r ∈ [0.5, 20.5], p ∈ (0, 1), and t from ln(1-p) up to half of -ln(1-p). The maximum difference is 35 ULP for JavaScript and 42 ULP for the native addon, so the tolerances are 40 and 50 ULP.
  • The new tests fail on develop (1000 failures each in test.mgf.js and test.factory.js).

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

I found this bug manually while reviewing other PRs. The code changes were generated with Claude Code, and I reviewed them and verified the results.


@stdlib-js/reviewers

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: passed
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: passed
  - task: lint_license_headers
    status: passed
---
@0PrashantYadav0
0PrashantYadav0 requested a review from a team October 17, 2026 16:11
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Oct 17, 2026
@stdlib-bot

stdlib-bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/negative-binomial/ctor $\\color{green}420/420$
$\\color{green}+0.00\\%$
$\\color{green}31/31$
$\\color{green}+0.00\\%$
$\\color{green}16/16$
$\\color{green}+0.00\\%$
$\\color{green}420/420$
$\\color{green}+0.00\\%$
stats/base/dists/negative-binomial/mgf $\\color{green}299/299$
$\\color{green}+0.00\\%$
$\\color{green}25/25$
$\\color{green}+0.00\\%$
$\\color{green}4/4$
$\\color{green}+0.00\\%$
$\\color{green}299/299$
$\\color{green}+0.00\\%$

The above coverage report was generated for the changes in this PR.

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review A pull request which needs code review. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants