Repository navigation
fix: correct MGF in stats/base/dists/negative-binomial/mgf - #16213
Open
0PrashantYadav0 wants to merge 2 commits into
Open
0PrashantYadav0 wants to merge 2 commits into
0PrashantYadav0 wants to merge 2 commits into
Conversation
---
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
---
Contributor
Coverage Report
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
---
4 of 7 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This pull request:
stats/base/dists/negative-binomial/mgf(JavaScript,factory, and C) so that it returns the MGF of the number of failures before ther-th success,(p / (1 - (1-p) e^t))^r. That is the convention used by the rest of thenegative-binomialnamespace:pmfisC(x+r-1, x) p^r (1-p)^xforx = 0, 1, 2, ...andmeanisr(1-p)/p. The package returned((1-p) e^t / (1 - p e^t))^r, which swapspand1-pand counts trials instead of failures. For example,mgf( 0.1, 20.0, 0.8 )returned407636.99instead of1.7039, andM'(0) = r/(1-p)did not matchmean( r, p ).t >= -ln(p)tot >= -ln(1-p), the radius of convergence ofΣ e^{tx} pmf(x).p - (1-p) expm1(t)and the bound as-log1p(-p).1 - (1-p) e^tloses accuracy whenpis small andtis close to the bound: against the fixtures, the direct form is off by up to 2252 ULP, and theexpm1form by at most 35.TODO: Add test fixturesplaceholder tests intest.mgf.js,test.factory.js, andtest.native.jswith Julia fixtures generated byDistributions.jl.negative-binomial/mgfandnegative-binomial/ctor. Three examples (mgf( 0.1, 20.0, 0.1 ),mgf( 0.5, 10.0, 0.4 ), andmgf( 0.5, 7.4, 0.4 )) now land near the pole and return values up to~10^25, so I changed theirpto0.9or0.8. The README also describedras "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 themarkdown_equationsworkflow.Related Issues
This pull request has the following related issues:
stats/base/dists/geometric/mgfreturns the MGF for the number of trials instead of the number of failures #15780In #15780 (geometric/mgf, which is the
r = 1case of this distribution), @Planeshifter asked whether the negative binomial MGF has the same problem. It does, and this PR fixes it.Questions
No.
Other
Verification:
exp( t*x ) * pmf( x, r, p )overxwith stdlib'snegative-binomial/pmfmatches the new values (e.g.,1.703851511235214vs1.7038515112352146fort = 0.1, r = 20, p = 0.8).Distributions.jl0.25.131 computesmgf(::NegativeBinomial)with the failures convention (asr * cgf( Geometric( p ), t )). The fixtures user ∈ [0.5, 20.5],p ∈ (0, 1), andtfromln(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.develop(1000 failures each intest.mgf.jsandtest.factory.js).Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
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