Repository navigation
Conversation
When `x` is small and `alpha > 1`, the regularized gamma prefix `x^a * exp(-x) / gamma(a)` becomes subnormal, so dividing it by `x` produced results with only a few correct bits (e.g., a 38% error for `pdf( 1.0e-215, 1.5, 1.0 )`). Use the existing log-space fallback in `gamma_p_derivative` whenever the prefix is below the smallest normal double, instead of only when it is exactly zero. Apply the same change to the copy of `gamma_p_derivative` used by `stats/base/dists/gamma/logpdf`. This also fixes `chisquare/pdf` and `erlang/pdf`, which delegate to `gamma/pdf`. Resolves: stdlib-js#17052 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- 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: na - 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 ---
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
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.
Resolves #17052.
Description
This pull request:
stats/base/dists/gamma/pdflosing most of its precision for smallxwhenalpha > 1.gamma_p_derivativecomputes the density asregularised_gamma_prefix( a, x ) / x. When the prefixx^a * exp(-x) / gamma(a)falls below the smallest normal double, it carries only a few significant bits, and dividing byxscales it back into the normal range without restoring them. For example,pdf( 1.0e-215, 1.5, 1.0 )was off by 38%, andpdf( 1.0e-161, 2.0, 1.0 )returned1.0375e-161instead of1.0e-161. The fix uses the existing log-space fallback whenever the prefix is belowFLOAT64_SMALLEST_NORMAL, instead of only when it is exactly0. This is a one-line condition change inlib/gamma_p_derivative.jsandsrc/main.c, plus the new constant dependency inmanifest.json.gamma_p_derivativeinstats/base/dists/gamma/logpdf, which had the same error (e.g.,logpdf( 1.0e-215, 1.5, 1.0 )returned-247.0817instead of-247.4071).chisquare/pdfanderlang/pdf, which delegate togamma/pdf(e.g.,chisquare/pdf( 3.0e-161, 4.0 )was off by 7.8%).Related Issues
This pull request has the following related issues:
stats/base/dists/gamma/pdfandgamma/logpdflose accuracy for smallx#17052stats/base/dists/gamma/logpdf#17022 / [BUG]:stats/base/dists/gamma/logpdfreturns-Infinitywhen the PDF underflows #17004 (gamma/logpdfreturns-Infinitywhen the PDF underflows). That PR changes thelogpdfwrapper (lib/main.js,lib/factory.js, and thestdlib_base_dists_gamma_logpdffunction insrc/main.c), while this PR changesgamma_p_derivative. The two are independent: here the derivative is a normal double but inaccurate, so thed < SMALLEST_NORMALcheck in fix: resolve log-density underflow instats/base/dists/gamma/logpdf#17022 doesn't apply. Both touchlogpdf/src/main.c, but in different functions.Questions
No.
Other
Checks done outside the test suite, against mpmath at 60 digits, on 20,000 random
(x, alpha, beta)withxfrom 1e-320 to 10,alphafrom 0.01 to 200, andbetafrom 1e-3 to 1e3. The figures below cover the 19,458 cases wherex*betais a normal double:gamma/pdfmax relative errorgamma/logpdfmax relative errorchisquare/pdf( x, 4 )vs. exactx/4forxin [1e-300, 1e-150]erlang/pdf( x, 2, 1 )vs. exactxforxin [1e-300, 1e-150]JS (
mainandfactory) and native results are identical on all of these. The remaining error of up to ~4.5e-12 comes fromexp()of a log-density of several hundred in magnitude. I kept the existing fallback expression; rewriting it as(a-1)*ln(x) - x - gammaln(a)gave the same maximum error.Two edge cases are out of scope here, as noted in the issue: inputs where
x*betais itself subnormal, and intermediate overflow ofgammaDeriv( alpha, x*beta )before it is multiplied bybeta < 1near the overflow threshold.The new tests (main, factory, and native, for both packages) use reference values for
alpha= 2, 1.5, and 1.75. Foralpha = 2, the PDF is exactlybeta^2 * x * exp(-beta*x). On the previous implementation, these tests are off by ~1e14 to ~1e15 ULPs for the PDF and ~1e12 ULPs for the log-PDF.Ran locally: the
gamma/pdf,gamma/logpdf,chisquare/pdf,erlang/pdf, andchi/pdftest suites (JS and native), and the commit-hook linters, all passing. The exception is one existing assertion inerlang/pdf/test/test.native.js(line 195), which fails on my machine on unmodifieddevelopas well.Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
@stdlib-js/reviewers