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 @@ -23,6 +23,8 @@
var constantFunction = require( '@stdlib/utils/constant-function' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var exp = require( '@stdlib/math/base/special/exp' );
var log1p = require( '@stdlib/math/base/special/log1p' );


// MAIN //
Expand Down Expand Up @@ -70,6 +72,10 @@ function factory( alpha, beta ) {
if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
return NaN;
}
// For small `p`, evaluating `1.0 - p` suffers from floating-point cancellation (and rounds to `1.0` when `p < 2^-53`), so evaluate in log-space via `log1p`:
if ( p < 1.0e-4 ) {
return beta * exp( -alphaInv * log1p( -p ) );
}
return beta / pow( 1.0 - p, alphaInv );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var exp = require( '@stdlib/math/base/special/exp' );
var log1p = require( '@stdlib/math/base/special/log1p' );


// MAIN //
Expand Down Expand Up @@ -86,6 +88,10 @@ function quantile( p, alpha, beta ) {
) {
return NaN;
}
// For small `p`, evaluating `1.0 - p` suffers from floating-point cancellation (and rounds to `1.0` when `p < 2^-53`), so evaluate in log-space via `log1p`:
if ( p < 1.0e-4 ) {
return beta * exp( -log1p( -p ) / alpha );
}
return beta / pow( 1.0 - p, 1.0/alpha );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"dependencies": [
"@stdlib/math/base/napi/ternary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/pow"
]
},
Expand All @@ -56,6 +58,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/pow"
]
},
Expand All @@ -72,6 +76,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/pow"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "stdlib/stats/base/dists/pareto-type1/quantile.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/log1p.h"

/**
* Evaluates the quantile function for a Pareto (Type I) distribution with shape parameter `alpha` and scale parameter `beta` at a probability `p`.
Expand All @@ -44,5 +46,9 @@ double stdlib_base_dists_pareto_type1_quantile( const double p, const double alp
) {
return 0.0 / 0.0; // NaN
}
// For small `p`, evaluating `1.0 - p` suffers from floating-point cancellation (and rounds to `1.0` when `p < 2^-53`), so evaluate in log-space via `log1p`:
if ( p < 1.0e-4 ) {
return beta * stdlib_base_exp( -stdlib_base_log1p( -p ) / alpha );
}
return beta / stdlib_base_pow( 1.0 - p, 1.0/alpha );
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,27 @@
}
t.end();
});

tape( 'the created function evaluates the quantile for small `p` without cancellation in `1.0 - p`', function test( t ) {
var quantile;
var expected;
var alpha;
var beta;
var p;
var y;
var i;

// Expected values computed with 100-digit precision via mpmath (see #17008):

Check warning on line 232 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.factory.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "mpmath"
expected = [ 32286.5313986437347, 7.87278208484850842, 252.665540030620453, 284.11789600200217, 2094.75483060059775 ];

Check warning on line 233 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.factory.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 122. Maximum allowed is 80
p = [ 3.487713003317677e-14, 9.848016845161383e-17, 1.3302736633414168e-17, 3.627963640422887e-15, 1.590320234441396e-19 ];

Check warning on line 234 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.factory.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 127. Maximum allowed is 80
alpha = [ 3.770177179633989e-15, 4.028717220737787e-17, 2.201607940364203e-18, 6.742762523455147e-16, 2.1816061229888833e-20 ];

Check warning on line 235 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.factory.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 131. Maximum allowed is 80
beta = [ 3.1006509521973644, 0.6831483634551567, 0.6003661040099838, 1.30847092784763, 1.4297727967473501 ];

Check warning on line 236 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.factory.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 112. Maximum allowed is 80
for ( i = 0; i < expected.length; i++ ) {
quantile = factory( alpha[i], beta[i] );
y = quantile( p[i] );
t.strictEqual( isAlmostSameValue( y, expected[i], 16 ), true, 'within tolerance. p: '+p[i]+'. alpha: '+alpha[i]+'. beta: '+beta[i]+'. y: '+y+'. E: '+expected[i]+'.' );
}
quantile = factory( 2.0, 1.0 );
t.strictEqual( quantile( 1.0 ), PINF, 'returns +Infinity for `p` equal to `1`' );
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,24 @@
}
t.end();
});

tape( 'the function evaluates the quantile for small `p` without cancellation in `1.0 - p`', function test( t ) {
var expected;
var alpha;
var beta;
var p;
var y;
var i;

// Expected values computed with 100-digit precision via mpmath (see #17008):

Check warning on line 183 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.quantile.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "mpmath"
expected = [ 32286.5313986437347, 7.87278208484850842, 252.665540030620453, 284.11789600200217, 2094.75483060059775 ];

Check warning on line 184 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.quantile.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 122. Maximum allowed is 80
p = [ 3.487713003317677e-14, 9.848016845161383e-17, 1.3302736633414168e-17, 3.627963640422887e-15, 1.590320234441396e-19 ];

Check warning on line 185 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.quantile.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 127. Maximum allowed is 80
alpha = [ 3.770177179633989e-15, 4.028717220737787e-17, 2.201607940364203e-18, 6.742762523455147e-16, 2.1816061229888833e-20 ];

Check warning on line 186 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.quantile.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 131. Maximum allowed is 80
beta = [ 3.1006509521973644, 0.6831483634551567, 0.6003661040099838, 1.30847092784763, 1.4297727967473501 ];

Check warning on line 187 in lib/node_modules/@stdlib/stats/base/dists/pareto-type1/quantile/test/test.quantile.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 112. Maximum allowed is 80
for ( i = 0; i < expected.length; i++ ) {
y = quantile( p[i], alpha[i], beta[i] );
t.strictEqual( isAlmostSameValue( y, expected[i], 16 ), true, 'within tolerance. p: ' + p[ i ] + '. alpha: ' + alpha[ i ] + '. beta: ' + beta[ i ] + '. y: ' + y + '. E: ' + expected[ i ] + '.' );
}
t.strictEqual( quantile( 1.0, 2.0, 1.0 ), PINF, 'returns +Infinity for `p` equal to `1`' );
t.end();
});