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 @@ -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 //
Expand Down Expand Up @@ -65,12 +67,19 @@ function factory( alpha, beta ) {
* // returns <number>
*/
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 ];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -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 ) ||
Expand All @@ -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 ];
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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 ) ||
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});