diff --git a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/factory.js b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/factory.js index 36a5502465f5..7cfdf0185c9d 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/factory.js @@ -21,8 +21,10 @@ // MODULES // var constantFunction = require( '@stdlib/utils/constant-function' ); -var betaincinv = require( '@stdlib/math/base/special/betaincinv' ); +var kernelBetaincinv = require( '@stdlib/math/base/special/kernel-betaincinv' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); // MAIN // @@ -65,12 +67,19 @@ function factory( alpha, beta ) { * // returns */ function quantile( p ) { - var x; + var out; if ( isnan( p ) || p < 0.0 || p > 1.0 ) { return NaN; } - x = betaincinv( p, alpha, beta ); - return x / ( 1.0 - x ); + + // Use both `x` and `1-x` as returned by the incomplete beta inverse, as computing `1-x` by subtraction loses all precision when `x` is close to `1`: + out = kernelBetaincinv( alpha, beta, p, 1.0 - p ); + + // If `1-x` is below the smallest normal double, the quantile exceeds about `4.5e307`, and the incomplete beta inverse does not resolve `1-x` in this range (its search stops at the smallest normal double), so we return `+infinity`, as `x / (1-x)` would previously: + if ( out[ 1 ] < FLOAT64_SMALLEST_NORMAL ) { + return PINF; + } + return out[ 0 ] / out[ 1 ]; } } diff --git a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/main.js index bd448846656e..3f501a66d0df 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/lib/main.js @@ -20,8 +20,10 @@ // MODULES // -var betaincinv = require( '@stdlib/math/base/special/betaincinv' ); +var kernelBetaincinv = require( '@stdlib/math/base/special/kernel-betaincinv' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); // MAIN // @@ -71,7 +73,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); * // returns NaN */ function quantile( p, alpha, beta ) { - var x; + var out; if ( isnan( p ) || isnan( alpha ) || @@ -83,8 +85,15 @@ function quantile( p, alpha, beta ) { ) { return NaN; } - x = betaincinv( p, alpha, beta ); - return x / ( 1.0 - x ); + + // Use both `x` and `1-x` as returned by the incomplete beta inverse, as computing `1-x` by subtraction loses all precision when `x` is close to `1`: + out = kernelBetaincinv( alpha, beta, p, 1.0 - p ); + + // If `1-x` is below the smallest normal double, the quantile exceeds about `4.5e307`, and the incomplete beta inverse does not resolve `1-x` in this range (its search stops at the smallest normal double), so we return `+infinity`, as `x / (1-x)` would previously: + if ( out[ 1 ] < FLOAT64_SMALLEST_NORMAL ) { + return PINF; + } + return out[ 0 ] / out[ 1 ]; } diff --git a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/manifest.json index 4199068a6fa0..279bbf21af93 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/manifest.json @@ -40,7 +40,9 @@ "dependencies": [ "@stdlib/math/base/napi/ternary", "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/betaincinv" + "@stdlib/math/base/special/kernel-betaincinv", + "@stdlib/constants/float64/smallest-normal", + "@stdlib/constants/float64/pinf" ] }, { @@ -56,8 +58,10 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/betaincinv", - "@stdlib/constants/float64/eps" + "@stdlib/math/base/special/kernel-betaincinv", + "@stdlib/constants/float64/eps", + "@stdlib/constants/float64/smallest-normal", + "@stdlib/constants/float64/pinf" ] }, { @@ -73,8 +77,10 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/betaincinv", - "@stdlib/constants/float64/eps" + "@stdlib/math/base/special/kernel-betaincinv", + "@stdlib/constants/float64/eps", + "@stdlib/constants/float64/smallest-normal", + "@stdlib/constants/float64/pinf" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/src/main.c index bca950a073cd..56e7e2b4cfce 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/src/main.c @@ -18,7 +18,9 @@ #include "stdlib/stats/base/dists/betaprime/quantile.h" #include "stdlib/math/base/assert/is_nan.h" -#include "stdlib/math/base/special/betaincinv.h" +#include "stdlib/math/base/special/kernel_betaincinv.h" +#include "stdlib/constants/float64/smallest_normal.h" +#include "stdlib/constants/float64/pinf.h" /** * Evaluates the quantile function for a beta prime distribution with first shape parameter `alpha` and second shape parameter `beta` at a probability `p`. @@ -34,6 +36,7 @@ */ double stdlib_base_dists_betaprime_quantile( const double p, const double alpha, const double beta ) { double x; + double y; if ( stdlib_base_is_nan( p ) || stdlib_base_is_nan( alpha ) || @@ -45,6 +48,13 @@ double stdlib_base_dists_betaprime_quantile( const double p, const double alpha, ) { return 0.0 / 0.0; // NaN } - x = stdlib_base_betaincinv( p, alpha, beta, false ); - return x / ( 1.0 - x ); + + // Use both `x` and `1-x` as returned by the incomplete beta inverse, as computing `1-x` by subtraction loses all precision when `x` is close to `1`: + stdlib_base_kernel_betaincinv( alpha, beta, p, 1.0 - p, &x, &y ); + + // If `1-x` is below the smallest normal double, the quantile exceeds about `4.5e307`, and the incomplete beta inverse does not resolve `1-x` in this range (its search stops at the smallest normal double), so we return `+infinity`, as `x / (1-x)` would previously: + if ( y < STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) { + return STDLIB_CONSTANT_FLOAT64_PINF; + } + return x / y; } diff --git a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.factory.js b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.factory.js index 6e2608399d4c..d9698aa3b5ad 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.factory.js @@ -219,3 +219,75 @@ tape( 'the created function evaluates the quantile for `p` given large `beta`', } t.end(); }); + +tape( 'the created function evaluates the quantile when the quantile is large (i.e., when the incomplete beta inverse is close to `1`)', function test( t ) { + var expected; + var quantile; + var alpha; + var beta; + var p; + var y; + var i; + + // Reference values computed with 50 digits of precision from the closed form for `alpha = 1`, `(1-p)^(-1/beta) - 1` (for each `p`, `1-p` is exact in double precision): + p = [ + 0.99, + 0.5, + 0.9, + 0.75, + 0.9999999999990905, + 0.9999999990686774, + 0.9999999999999991 + ]; + alpha = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + beta = [ + 0.01, + 0.001, + 0.004, + 0.002, + 0.05, + 0.5, + 1.0 + ]; + expected = [ + 9.999999999999016e199, + 1.0715086071862519e301, + 1.0000000000000435e250, + 1.0715086071862519e301, + 6.668014432879649e240, + 1.152921504606847e18, + 1125899906842623.0 + ]; + + // For small `beta`, the inverse is ill-conditioned (a relative error `d` in the incomplete beta function corresponds to a relative error of about `d/beta` in the quantile), hence the tolerance: + for ( i = 0; i < p.length; i++ ) { + quantile = factory( alpha[ i ], beta[ i ] ); + y = quantile( p[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1000 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the created function returns `+infinity` when the quantile exceeds the largest double-precision floating-point number', function test( t ) { + var y; + + // For `alpha = 2` and `beta = 0.001`, the quantile already exceeds `1e301` for `p = 0.5`: + y = factory( 2.0, 0.001 )( 0.55 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + y = factory( 2.0, 0.001 )( 0.9 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + y = factory( 0.5, 0.001 )( 0.6 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.native.js index 62e7fb3ea1fb..4f29366e46d5 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.native.js @@ -180,3 +180,73 @@ tape( 'the function evaluates the quantile for `x` given large `beta`', opts, fu } t.end(); }); + +tape( 'the function evaluates the quantile when the quantile is large (i.e., when the incomplete beta inverse is close to `1`)', opts, function test( t ) { + var expected; + var alpha; + var beta; + var p; + var y; + var i; + + // Reference values computed with 50 digits of precision from the closed form for `alpha = 1`, `(1-p)^(-1/beta) - 1` (for each `p`, `1-p` is exact in double precision): + p = [ + 0.99, + 0.5, + 0.9, + 0.75, + 0.9999999999990905, + 0.9999999990686774, + 0.9999999999999991 + ]; + alpha = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + beta = [ + 0.01, + 0.001, + 0.004, + 0.002, + 0.05, + 0.5, + 1.0 + ]; + expected = [ + 9.999999999999016e199, + 1.0715086071862519e301, + 1.0000000000000435e250, + 1.0715086071862519e301, + 6.668014432879649e240, + 1.152921504606847e18, + 1125899906842623.0 + ]; + + // For small `beta`, the inverse is ill-conditioned (a relative error `d` in the incomplete beta function corresponds to a relative error of about `d/beta` in the quantile), hence the tolerance: + for ( i = 0; i < p.length; i++ ) { + y = quantile( p[ i ], alpha[ i ], beta[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1000 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function returns `+infinity` when the quantile exceeds the largest double-precision floating-point number', opts, function test( t ) { + var y; + + // For `alpha = 2` and `beta = 0.001`, the quantile already exceeds `1e301` for `p = 0.5`: + y = quantile( 0.55, 2.0, 0.001 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + y = quantile( 0.9, 2.0, 0.001 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + y = quantile( 0.6, 0.5, 0.001 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.quantile.js b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.quantile.js index c7fd987c6630..ad5b76f3f67b 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.quantile.js +++ b/lib/node_modules/@stdlib/stats/base/dists/betaprime/quantile/test/test.quantile.js @@ -171,3 +171,73 @@ tape( 'the function evaluates the quantile for `x` given large `beta`', function } t.end(); }); + +tape( 'the function evaluates the quantile when the quantile is large (i.e., when the incomplete beta inverse is close to `1`)', function test( t ) { + var expected; + var alpha; + var beta; + var p; + var y; + var i; + + // Reference values computed with 50 digits of precision from the closed form for `alpha = 1`, `(1-p)^(-1/beta) - 1` (for each `p`, `1-p` is exact in double precision): + p = [ + 0.99, + 0.5, + 0.9, + 0.75, + 0.9999999999990905, + 0.9999999990686774, + 0.9999999999999991 + ]; + alpha = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + beta = [ + 0.01, + 0.001, + 0.004, + 0.002, + 0.05, + 0.5, + 1.0 + ]; + expected = [ + 9.999999999999016e199, + 1.0715086071862519e301, + 1.0000000000000435e250, + 1.0715086071862519e301, + 6.668014432879649e240, + 1.152921504606847e18, + 1125899906842623.0 + ]; + + // For small `beta`, the inverse is ill-conditioned (a relative error `d` in the incomplete beta function corresponds to a relative error of about `d/beta` in the quantile), hence the tolerance: + for ( i = 0; i < p.length; i++ ) { + y = quantile( p[ i ], alpha[ i ], beta[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1000 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function returns `+infinity` when the quantile exceeds the largest double-precision floating-point number', function test( t ) { + var y; + + // For `alpha = 2` and `beta = 0.001`, the quantile already exceeds `1e301` for `p = 0.5`: + y = quantile( 0.55, 2.0, 0.001 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + y = quantile( 0.9, 2.0, 0.001 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + y = quantile( 0.6, 0.5, 0.001 ); + t.strictEqual( y, PINF, 'returns expected value' ); + + t.end(); +});