Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,10 @@ tape( 'the function evaluates the logpdf for `x` given both large `k` and `lambd
}
t.end();
});

tape( 'the function returns a finite log-density when the PDF underflows (see #17004)', function test( t ) {
var y = logpdf( 1000.0, 10.0, 1.0 );
t.strictEqual( y !== NINF, true, 'does not return -Infinity' );
t.strictEqual( isAlmostSameValue( y, -950.6320299692422, 256 ), true, 'returns expected value' );
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
var degenerate = require( '@stdlib/stats/base/dists/degenerate/logpdf' ).factory;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var ln = require( '@stdlib/math/base/special/ln' );
var gammaln = require( '@stdlib/math/base/special/gammaln' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' );
var gammaDeriv = require( './gamma_p_derivative.js' );


Expand Down Expand Up @@ -73,13 +75,22 @@
* // returns <number>
*/
function logpdf( x ) {
var d;
var z;
if ( isnan( x ) ) {
return NaN;
}
if ( x < 0.0 || x === PINF ) {
return NINF;
}
return ln( gammaDeriv( alpha, x * beta ) ) + ln( beta );
z = x * beta;
d = gammaDeriv( alpha, z );
if ( d < SMALLEST_NORMAL ) {
// The PDF underflows (or is subnormal), so compute the
// log-density directly in log space to avoid `ln( 0 ) => -Infinity`:
return ( ( alpha - 1.0 ) * ln( z ) ) - z - gammaln( alpha ) + ln( beta );

Check warning on line 91 in lib/node_modules/@stdlib/stats/base/dists/gamma/logpdf/lib/factory.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 85. Maximum allowed is 80
}
return ln( d ) + ln( beta );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var ln = require( '@stdlib/math/base/special/ln' );
var gammaln = require( '@stdlib/math/base/special/gammaln' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' );
var gammaDeriv = require( './gamma_p_derivative.js' );


Expand Down Expand Up @@ -72,6 +74,8 @@
* // returns NaN
*/
function logpdf( x, alpha, beta ) {
var d;
var z;
if (
isnan( x ) ||
isnan( alpha ) ||
Expand All @@ -88,7 +92,14 @@
// Point mass at 0...
return ( x === 0.0 ) ? PINF : NINF;
}
return ln( gammaDeriv( alpha, x * beta ) ) + ln( beta );
z = x * beta;
d = gammaDeriv( alpha, z );
if ( d < SMALLEST_NORMAL ) {
// The PDF underflows (or is subnormal), so compute the
// log-density directly in log space to avoid `ln( 0 ) => -Infinity`:
return ( ( alpha - 1.0 ) * ln( z ) ) - z - gammaln( alpha ) + ln( beta );

Check warning on line 100 in lib/node_modules/@stdlib/stats/base/dists/gamma/logpdf/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 81. Maximum allowed is 80
}
return ln( d ) + ln( beta );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@stdlib/math/base/special/gammaln",
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/max",
"@stdlib/constants/float64/smallest-normal",
"@stdlib/math/base/special/gamma-lanczos-sum-expg-scaled",
"@stdlib/math/base/special/gamma",
"@stdlib/math/base/special/log1p",
Expand Down Expand Up @@ -80,6 +81,7 @@
"@stdlib/math/base/special/gammaln",
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/max",
"@stdlib/constants/float64/smallest-normal",
"@stdlib/math/base/special/gamma-lanczos-sum-expg-scaled",
"@stdlib/math/base/special/gamma",
"@stdlib/math/base/special/log1p",
Expand Down Expand Up @@ -115,6 +117,7 @@
"@stdlib/math/base/special/gammaln",
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/max",
"@stdlib/constants/float64/smallest-normal",
"@stdlib/math/base/special/gamma-lanczos-sum-expg-scaled",
"@stdlib/math/base/special/gamma",
"@stdlib/math/base/special/log1p",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "stdlib/math/base/special/gammaln.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/constants/float64/max.h"
#include "stdlib/constants/float64/smallest_normal.h"
#include "stdlib/math/base/special/gamma_lanczos_sum_expg_scaled.h"
#include "stdlib/math/base/special/gamma.h"
#include "stdlib/math/base/special/log1p.h"
Expand Down Expand Up @@ -187,6 +188,11 @@ double stdlib_base_dists_gamma_logpdf( const double x, const double alpha, const
// Point mass at 0...
return ( x == 0.0 ) ? STDLIB_CONSTANT_FLOAT64_PINF : STDLIB_CONSTANT_FLOAT64_NINF;
}
return stdlib_base_ln( gamma_p_derivative( alpha, x * beta ) ) + stdlib_base_ln( beta );
double d = gamma_p_derivative( alpha, x * beta );
if ( d < STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) {
// The PDF underflows (or is subnormal), so compute the log-density directly in log space to avoid `ln( 0 ) => -Infinity` and loss of digits:
return ( ( alpha - 1.0 ) * stdlib_base_ln( x * beta ) ) - ( x * beta ) - stdlib_base_gammaln( alpha ) + stdlib_base_ln( beta );
}
return stdlib_base_ln( d ) + stdlib_base_ln( beta );
}

Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,33 @@ tape( 'the created function evaluates the logpdf for `x` given a large rate para
}
t.end();
});

tape( 'the function returns a finite log-density when the PDF underflows (see #17004)', function test( t ) {
var expected;
var logpdf;
var values;
var i;
var y;

values = [
[ 10.0, 1.0, 1000.0 ],
[ 10.0, 100.0, 10.0 ],
[ 10.0, 0.01, 100000.0 ],
[ 0.1, 0.01, 100000.0 ],
[ 10.0, 1.0, 755.0 ]
];
expected = [
-950.6320299692422,
-946.0268597832542,
-955.2372001552303,
-1013.0748625888062,
-708.1613677368402
];
for ( i = 0; i < values.length; i++ ) {
logpdf = factory( values[i][0], values[i][1] );
y = logpdf( values[i][2] );
t.strictEqual( y !== NINF, true, 'does not return -Infinity' );
t.strictEqual( isAlmostSameValue( y, expected[i], 256 ), true, 'returns expected value for alpha='+values[i][0]+', beta='+values[i][1]+', x='+values[i][2] );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,31 @@ tape( 'the function evaluates the logpdf for `x` given large rate parameter `bet
}
t.end();
});

tape( 'the function returns a finite log-density when the PDF underflows (see #17004)', function test( t ) {
var expected;
var values;
var i;
var y;

values = [
[ 1000.0, 10.0, 1.0 ],
[ 10.0, 10.0, 100.0 ],
[ 100000.0, 10.0, 0.01 ],
[ 100000.0, 0.1, 0.01 ],
[ 755.0, 10.0, 1.0 ]
];
expected = [
-950.6320299692422,
-946.0268597832542,
-955.2372001552303,
-1013.0748625888062,
-708.1613677368402
];
for ( i = 0; i < values.length; i++ ) {
y = logpdf( values[i][0], values[i][1], values[i][2] );
t.strictEqual( y !== NINF, true, 'does not return -Infinity' );
t.strictEqual( isAlmostSameValue( y, expected[i], 256 ), true, 'returns expected value for x='+values[i][0]+', alpha='+values[i][1]+', beta='+values[i][2] );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,31 @@ tape( 'the function evaluates the logpdf for `x` given large rate parameter `bet
}
t.end();
});

tape( 'the function returns a finite log-density when the PDF underflows (see #17004)', opts, function test( t ) {
var expected;
var values;
var i;
var y;

values = [
[ 1000.0, 10.0, 1.0 ],
[ 10.0, 10.0, 100.0 ],
[ 100000.0, 10.0, 0.01 ],
[ 100000.0, 0.1, 0.01 ],
[ 755.0, 10.0, 1.0 ]
];
expected = [
-950.6320299692422,
-946.0268597832542,
-955.2372001552303,
-1013.0748625888062,
-708.1613677368402
];
for ( i = 0; i < values.length; i++ ) {
y = logpdf( values[i][0], values[i][1], values[i][2] );
t.strictEqual( y !== NINF, true, 'does not return -Infinity' );
t.strictEqual( isAlmostSameValue( y, expected[i], 256 ), true, 'returns expected value for x='+values[i][0]+', alpha='+values[i][1]+', beta='+values[i][2] );
}
t.end();
});