Skip to content

[BUG]: stats/base/dists/gamma/pdf and gamma/logpdf lose accuracy for small x #17052

Backlog

Description

@Udaygund

Description

stats/base/dists/gamma/pdf and stats/base/dists/gamma/logpdf lose most of their accuracy for small x (roughly x*beta below 1e-150 when alpha > 1), where the density is still a normal double. For example, pdf( 1.0e-215, 1.5, 1.0 ) is off by 38% and pdf( 1.0e-161, 2.0, 1.0 ) by 3.8%, although for alpha = 2 and beta = 1 the exact value is x*exp(-x), which is 1.0e-161 here. The error carries over to chisquare/pdf and erlang/pdf, which delegate to gamma/pdf.

gamma_p_derivative computes the density as regularised_gamma_prefix( a, x ) / x. For small x with a > 1, the prefix x^a * exp(-x) / gamma(a) is far smaller than the result (it is the result times x). Once it drops below the smallest normal double, regularised_gamma_prefix returns a subnormal value: in its overflow/underflow branch it computes sq = pow( z/agh, a/2 ) * exp( amz/2 ) and returns sq * sq (lib/regularised_gamma_prefix.js lines 106 and 115). That subnormal value has only a few significant bits left, and dividing by x scales it back into the normal range without restoring the lost precision. For instance, pdf( 1.0e-215, 1.5, 1.0 ) returns 4.9406564584124655e-108 because the prefix underflows to 5e-323 (ten times Number.MIN_VALUE, i.e., about three significant bits), which is then divided by 1.0e-215.

gamma_p_derivative already has a log-space fallback, but it only uses it when the prefix is exactly 0 (lib/gamma_p_derivative.js line 75, src/main.c line 142). Using the same fallback whenever the prefix is below the smallest normal double fixes this. I tested that change against mpmath on 20,000 random (x, alpha, beta) with x from 1e-320 to 10 and alpha from 0.01 to 200. Over all cases where x*beta is a normal double, the maximum relative error dropped from 0.95 to 4.5e-12.

gamma/logpdf contains a copy of the same two files (ln( gammaDeriv( alpha, x*beta ) )), so it has the same error. For example, logpdf( 1.0e-215, 1.5, 1.0 ) returns -247.0817 instead of -247.4071.

Two smaller edge cases remain even with that change, so I'm only noting them here. When x*beta is itself subnormal, precision is already lost in the product. When beta < 1 and the result is near the overflow threshold, gammaDeriv( alpha, x*beta ) can overflow before it is multiplied by beta.

Found while comparing stats/base/dists functions against SciPy and mpmath. The comparison and this report were done with AI assistance (Claude Code); I checked the reproduction and reference values.

Related Issues

Questions

No.

Demo

N/A

Reproduction

var gammaPDF = require( '@stdlib/stats/base/dists/gamma/pdf' );
var gammaLogPDF = require( '@stdlib/stats/base/dists/gamma/logpdf' );
var chisquarePDF = require( '@stdlib/stats/base/dists/chisquare/pdf' );
var erlangPDF = require( '@stdlib/stats/base/dists/erlang/pdf' );

console.log( gammaPDF( 1.0e-215, 1.5, 1.0 ) );
console.log( gammaPDF( 1.0e-184, 1.75, 1.0 ) );
console.log( gammaPDF( 1.0e-161, 2.0, 1.0 ) );
console.log( chisquarePDF( 3.0e-161, 4.0 ) );
console.log( erlangPDF( 1.0e-161, 2, 1.0 ) );
console.log( gammaLogPDF( 1.0e-215, 1.5, 1.0 ) );

Expected Results

3.5682482323055423e-108
1.0880652521310174e-138
1e-161
7.5e-162
1e-161
-247.40711525922467


Reference values were computed with mpmath at 50 digits. The third through fifth values are exact (`x*exp(-x)` and `(x/4)*exp(-x/2)`). SciPy's `gamma.pdf` agrees.

Actual Results

4.9406564584124655e-108
7.410984687618698e-139
1.0375378562666178e-161
6.916919041777451e-162
1.0375378562666178e-161
-247.0816918346674

Version

develop (008bd34)

Environments

Node.js

Browser Version

No response

Node.js / npm Version

v22.16.0

Platform

macOS

Checklist

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions