Repository navigation
Description
Description
@stdlib/stats/base/dists/gamma/logpdf computes the PDF first and then takes its logarithm:
return ln( gammaDeriv( alpha, x * beta ) ) + ln( beta );
(same in lib/main.js, lib/factory.js, and src/main.c).
When the density is smaller than the smallest double (about 1e-308), gammaDeriv underflows to 0, so the function returns -Infinity even though the log-density is a finite, ordinary number (for example -998.39). When the density is subnormal, the result is finite but loses digits.
Avoiding this is the main reason to have a separate logpdf. The internal gamma_p_derivative already has a log-space branch for this case, but it calls exp() on the result, so the value underflows again before ln() is applied.
Because erlang/logpdf and chisquare/logpdf call gamma/logpdf, they are affected too.
In a scan of 990 inputs (alpha from 0.1 to 1e4, beta from 0.01 to 100, x from 1e-300 to 1e5), 474 returned -Infinity where the true value is finite.
Possible fix: keep the current path when the PDF is a normal number, and only when it is below FLOAT64_SMALLEST_NORMAL return the log-space expression (alpha-1)*ln(z) - z - gammaln(alpha) + ln(beta), with z = x*beta. This leaves all currently accurate results unchanged. A local prototype of this matches mpmath (50 digits) to within ~30 ULP on all 474 failing inputs. Happy to open a PR.
Related Issues
None.
Questions
Is a fallback only for the underflow case (rather than always computing in log space) the preferred approach? Always using gammaln would be simpler but could lose accuracy for large alpha near the mode, which is what gamma_p_derivative is designed to handle.
Demo
No response
Reproduction
var logpdf = require( '@stdlib/stats/base/dists/gamma/logpdf' );
var erlangLogPDF = require( '@stdlib/stats/base/dists/erlang/logpdf' );
var chisquareLogPDF = require( '@stdlib/stats/base/dists/chisquare/logpdf' );
console.log( logpdf( 200.0, 1.0, 5.0 ) );
console.log( logpdf( 1000.0, 0.5, 1.0 ) );
console.log( logpdf( 0.5, 1000.0, 1.0 ) );
console.log( logpdf( 1.0e-300, 3.0, 1.0 ) );
console.log( logpdf( 750.0, 3.0, 1.0 ) );
console.log( erlangLogPDF( 1000.0, 3, 1.0 ) );
console.log( chisquareLogPDF( 2000.0, 4.0 ) );Expected Results
-998.3905620875659
-1004.0262425824158
-6598.1744565885666
-1382.2442029769874
-737.4530007674992
-986.8776366225957
-993.7853919015778
(Reference values computed with mpmath at 50 digits using the closed-form log-density `alpha*ln(beta) + (alpha-1)*ln(x) - beta*x - lgamma(alpha)`.)Actual Results
-Infinity
-Infinity
-Infinity
-Infinity
-737.4525816743803
-Infinity
-InfinityVersion
develop (4d917ee)
Environments
Node.js
Browser Version
No response
Node.js / npm Version
Node.js v22.18.0
Platform
macOS
Checklist
- Read and understood the Code of Conduct.
- Searched for existing issues and pull requests.
Activity
stdlib-bot commentedon Nov 30, 2026
👋 Hi there! 👋
And thank you for opening your first issue! We will get back to you shortly. 🏃 💨
If you have any further questions while waiting for a response, please join our Zulip community to chat with project maintainers and other community members.
Fix stdlib-js#17004: address lint (vars-on-top, line length)
Fix stdlib-js#17004: order test variable declarations by length