diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/factory.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/factory.js index 71da33a5091f..56fe28d37ee0 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/factory.js @@ -22,7 +22,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 expm1 = require( '@stdlib/math/base/special/expm1' ); +var lnRatio = require( './ln_ratio.js' ); // MAIN // @@ -64,10 +65,12 @@ function factory( alpha, beta ) { if ( isnan( x ) ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return 0.0; } - return 1.0 - pow( beta / x, alpha ); + + // Evaluate `1 - (beta/x)^alpha` as `-expm1( -alpha*ln(x/beta) )` to avoid cancellation when `(beta/x)^alpha` is close to `1`: + return -expm1( -alpha * lnRatio( x, beta ) ); } } diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/ln_ratio.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/ln_ratio.js new file mode 100644 index 000000000000..57fb638e8210 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/ln_ratio.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var log1p = require( '@stdlib/math/base/special/log1p' ); +var ln = require( '@stdlib/math/base/special/ln' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); + + +// MAIN // + +/** +* Evaluates the natural logarithm of `x/beta` for `x >= beta`. +* +* ## Notes +* +* - For `x < 2*beta`, `x-beta` is computed exactly, so evaluating `log1p( (x-beta)/beta )` avoids the loss of precision incurred by evaluating `ln(x/beta)` when `x/beta` is close to `1`. +* - If `x/beta` overflows, the logarithm is evaluated as `ln(x) - ln(beta)`, which does not suffer from cancellation, as `x` and `beta` then differ by many orders of magnitude. +* +* @private +* @param {number} x - input value +* @param {PositiveNumber} beta - scale parameter +* @returns {NonNegativeNumber} natural logarithm of `x/beta` +* +* @example +* var v = lnRatio( 2.0, 1.0 ); +* // returns ~0.693 +*/ +function lnRatio( x, beta ) { + var r = x / beta; + if ( r < 2.0 ) { + return log1p( ( x-beta ) / beta ); + } + if ( r === PINF ) { + return ln( x ) - ln( beta ); + } + return ln( r ); +} + + +// EXPORTS // + +module.exports = lnRatio; diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/main.js index ec95f9ec4fce..1104e58940c1 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/main.js @@ -21,7 +21,8 @@ // MODULES // var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var pow = require( '@stdlib/math/base/special/pow' ); +var expm1 = require( '@stdlib/math/base/special/expm1' ); +var lnRatio = require( './ln_ratio.js' ); // MAIN // @@ -84,10 +85,12 @@ function cdf( x, alpha, beta ) { ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return 0.0; } - return 1.0 - pow( beta / x, alpha ); + + // Evaluate `1 - (beta/x)^alpha` as `-expm1( -alpha*ln(x/beta) )` to avoid cancellation when `(beta/x)^alpha` is close to `1`: + return -expm1( -alpha * lnRatio( x, beta ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/manifest.json index f64b3a3964a0..bd632bfc4f0d 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/manifest.json @@ -40,7 +40,10 @@ "dependencies": [ "@stdlib/math/base/napi/ternary", "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/pow" + "@stdlib/math/base/special/expm1", + "@stdlib/math/base/special/log1p", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf" ] }, { @@ -56,7 +59,10 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/pow" + "@stdlib/math/base/special/expm1", + "@stdlib/math/base/special/log1p", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf" ] }, { @@ -72,7 +78,10 @@ "libpath": [], "dependencies": [ "@stdlib/math/base/assert/is-nan", - "@stdlib/math/base/special/pow" + "@stdlib/math/base/special/expm1", + "@stdlib/math/base/special/log1p", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/src/main.c index f342b4448faf..35dc2610e1f2 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/src/main.c @@ -18,7 +18,34 @@ #include "stdlib/stats/base/dists/pareto-type1/cdf.h" #include "stdlib/math/base/assert/is_nan.h" -#include "stdlib/math/base/special/pow.h" +#include "stdlib/math/base/special/expm1.h" +#include "stdlib/math/base/special/log1p.h" +#include "stdlib/math/base/special/ln.h" +#include "stdlib/constants/float64/pinf.h" + +/** +* Evaluates the natural logarithm of `x/beta` for `x >= beta`. +* +* ## Notes +* +* - For `x < 2*beta`, `x-beta` is computed exactly, so evaluating `log1p( (x-beta)/beta )` avoids the loss of precision incurred by evaluating `ln(x/beta)` when `x/beta` is close to `1`. +* - If `x/beta` overflows, the logarithm is evaluated as `ln(x) - ln(beta)`, which does not suffer from cancellation, as `x` and `beta` then differ by many orders of magnitude. +* +* @param x input value +* @param beta scale parameter +* @return natural logarithm of `x/beta` +*/ +static double ln_ratio( const double x, const double beta ) { + double r; + r = x / beta; + if ( r < 2.0 ) { + return stdlib_base_log1p( ( x-beta ) / beta ); + } + if ( r == STDLIB_CONSTANT_FLOAT64_PINF ) { + return stdlib_base_ln( x ) - stdlib_base_ln( beta ); + } + return stdlib_base_ln( r ); +} /** * Evaluates the cumulative distribution function (CDF) for a Pareto (Type I) distribution with shape parameter `alpha` and scale parameter `beta` at a value `x`. @@ -42,8 +69,9 @@ double stdlib_base_dists_pareto_type1_cdf( const double x, const double alpha, c ) { return 0.0 / 0.0; // NaN } - if ( x < beta ) { + if ( x <= beta ) { return 0.0; } - return 1.0 - stdlib_base_pow( beta / x, alpha ); + // Evaluate `1 - (beta/x)^alpha` as `-expm1( -alpha*ln(x/beta) )` to avoid cancellation when `(beta/x)^alpha` is close to `1`: + return -stdlib_base_expm1( -alpha * ln_ratio( x, beta ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.cdf.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.cdf.js index d4d1b00aec7f..f90e38c9563c 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.cdf.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.cdf.js @@ -138,9 +138,11 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` and `beta`', f x = bothLarge.x; alpha = bothLarge.alpha; beta = bothLarge.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 228 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = cdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 228 ), true, 'returns expected value' ); } t.end(); }); @@ -157,9 +159,11 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` parameter', fu x = largeAlpha.x; alpha = largeAlpha.alpha; beta = largeAlpha.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 1030 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = cdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1033 ), true, 'returns expected value' ); } t.end(); }); @@ -176,9 +180,80 @@ tape( 'the function evaluates the cdf for `x` given large rate parameter `beta`' x = largeBeta.x; alpha = largeBeta.alpha; beta = largeBeta.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 359 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = cdf( x[i], alpha[i], beta[i] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 358 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates the cdf accurately when `(beta/x)^alpha` is close to `1` or `beta/x` underflows', function test( t ) { + var expected; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 120 digits of precision from `1 - (beta/x)^alpha`: + x = [ + 1.0e300, + 2.0, + 1.0000001, + 3.0000000027939677, + 10000000000.0, + 1.5, + 1.0e300, + 4.0 + ]; + alpha = [ + 1.0e-20, + 1.0e-10, + 0.5, + 2.0, + 1.0e-12, + 1.0e-15, + 0.001, + 2.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 3.0, + 1.0, + 1.0, + 1.0e-300, + 1.0 + ]; + expected = [ + 6.907755278982137e-18, + 6.931471805359227e-11, + 4.999999627919367e-08, + 1.8626451466288718e-09, + 2.3025850929675362e-11, + 4.0546510810816433e-16, + 0.748811356849042, + 0.9375 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = cdf( x[ i ], alpha[ i ], beta[ i ] ); t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); } t.end(); }); + +tape( 'the function returns `0` if provided `x` equal to `beta`, even when `alpha` is `+infinity`', function test( t ) { + var y; + + y = cdf( 1.0, 2.0, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + y = cdf( 1.0, PINF, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.factory.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.factory.js index 510359f8dc98..4e39afbb2032 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.factory.js @@ -198,10 +198,12 @@ tape( 'the created function evaluates the cdf for `x` given large `alpha` and `b x = bothLarge.x; alpha = bothLarge.alpha; beta = bothLarge.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 228 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { cdf = factory( alpha[i], beta[i] ); y = cdf( x[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 228 ), true, 'returns expected value' ); } t.end(); }); @@ -219,10 +221,12 @@ tape( 'the created function evaluates the cdf for `x` given large `alpha` parame x = largeAlpha.x; alpha = largeAlpha.alpha; beta = largeAlpha.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 1030 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { cdf = factory( alpha[i], beta[i] ); y = cdf( x[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1033 ), true, 'returns expected value' ); } t.end(); }); @@ -240,10 +244,83 @@ tape( 'the created function evaluates the cdf for `x` given large `beta` paramet x = largeBeta.x; alpha = largeBeta.alpha; beta = largeBeta.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 359 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { cdf = factory( alpha[i], beta[i] ); y = cdf( x[i] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 358 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the created function evaluates the cdf accurately when `(beta/x)^alpha` is close to `1` or `beta/x` underflows', function test( t ) { + var expected; + var alpha; + var beta; + var cdf; + var x; + var y; + var i; + + // Reference values computed with 120 digits of precision from `1 - (beta/x)^alpha`: + x = [ + 1.0e300, + 2.0, + 1.0000001, + 3.0000000027939677, + 10000000000.0, + 1.5, + 1.0e300, + 4.0 + ]; + alpha = [ + 1.0e-20, + 1.0e-10, + 0.5, + 2.0, + 1.0e-12, + 1.0e-15, + 0.001, + 2.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 3.0, + 1.0, + 1.0, + 1.0e-300, + 1.0 + ]; + expected = [ + 6.907755278982137e-18, + 6.931471805359227e-11, + 4.999999627919367e-08, + 1.8626451466288718e-09, + 2.3025850929675362e-11, + 4.0546510810816433e-16, + 0.748811356849042, + 0.9375 + ]; + + for ( i = 0; i < x.length; i++ ) { + cdf = factory( alpha[ i ], beta[ i ] ); + y = cdf( x[ i ] ); t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); } t.end(); }); + +tape( 'the created function returns `0` if provided `x` equal to `beta`, even when `alpha` is `+infinity`', function test( t ) { + var y; + + y = factory( 2.0, 1.0 )( 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + y = factory( PINF, 1.0 )( 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.native.js index 386d93d8b7d9..e4895ed380d4 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/test/test.native.js @@ -147,9 +147,11 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` and `beta`', o x = bothLarge.x; alpha = bothLarge.alpha; beta = bothLarge.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 228 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = cdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 228 ), true, 'returns expected value' ); } t.end(); }); @@ -166,9 +168,11 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` parameter', op x = largeAlpha.x; alpha = largeAlpha.alpha; beta = largeAlpha.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 1030 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = cdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1033 ), true, 'returns expected value' ); } t.end(); }); @@ -185,9 +189,80 @@ tape( 'the function evaluates the cdf for `x` given large rate parameter `beta`' x = largeBeta.x; alpha = largeBeta.alpha; beta = largeBeta.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 359 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = cdf( x[i], alpha[i], beta[i] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 358 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates the cdf accurately when `(beta/x)^alpha` is close to `1` or `beta/x` underflows', opts, function test( t ) { + var expected; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 120 digits of precision from `1 - (beta/x)^alpha`: + x = [ + 1.0e300, + 2.0, + 1.0000001, + 3.0000000027939677, + 10000000000.0, + 1.5, + 1.0e300, + 4.0 + ]; + alpha = [ + 1.0e-20, + 1.0e-10, + 0.5, + 2.0, + 1.0e-12, + 1.0e-15, + 0.001, + 2.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 3.0, + 1.0, + 1.0, + 1.0e-300, + 1.0 + ]; + expected = [ + 6.907755278982137e-18, + 6.931471805359227e-11, + 4.999999627919367e-08, + 1.8626451466288718e-09, + 2.3025850929675362e-11, + 4.0546510810816433e-16, + 0.748811356849042, + 0.9375 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = cdf( x[ i ], alpha[ i ], beta[ i ] ); t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); } t.end(); }); + +tape( 'the function returns `0` if provided `x` equal to `beta`, even when `alpha` is `+infinity`', opts, function test( t ) { + var y; + + y = cdf( 1.0, 2.0, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + y = cdf( 1.0, PINF, 1.0 ); + t.strictEqual( y, 0.0, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/factory.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/factory.js index 9a7531d4c09a..d1ec41f724a7 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/factory.js @@ -22,9 +22,13 @@ var constantFunction = require( '@stdlib/utils/constant-function' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var log1mexp = require( '@stdlib/math/base/special/log1mexp' ); var log1p = require( '@stdlib/math/base/special/log1p' ); var pow = require( '@stdlib/math/base/special/pow' ); +var LN2 = require( '@stdlib/constants/float64/ln-two' ); +var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' ); var NINF = require( '@stdlib/constants/float64/ninf' ); +var lnRatio = require( './ln_ratio.js' ); // MAIN // @@ -63,13 +67,22 @@ function factory( alpha, beta ) { * @returns {number} evaluated logCDF */ function logcdf( x ) { + var r; + var t; if ( isnan( x ) ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return NINF; } - return log1p( -pow( beta / x, alpha ) ); + t = alpha * lnRatio( x, beta ); + r = beta / x; + if ( t > LN2 && r <= 0.5 && r >= FLOAT64_SMALLEST_NORMAL ) { + // As `(beta/x)^alpha < 0.5`, there is no cancellation. For `x >= 2*beta`, `pow` is more accurate than `exp(-t)`, as the rounding error of `beta/x` is amplified by `alpha`, while that of `t` is amplified by `t = alpha*ln(x/beta) >= alpha*ln(2)`: + return log1p( -pow( r, alpha ) ); + } + // Evaluate `ln( 1 - exp(-t) )` without cancellation: + return log1mexp( t ); } } diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/ln_ratio.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/ln_ratio.js new file mode 100644 index 000000000000..57fb638e8210 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/ln_ratio.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var log1p = require( '@stdlib/math/base/special/log1p' ); +var ln = require( '@stdlib/math/base/special/ln' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); + + +// MAIN // + +/** +* Evaluates the natural logarithm of `x/beta` for `x >= beta`. +* +* ## Notes +* +* - For `x < 2*beta`, `x-beta` is computed exactly, so evaluating `log1p( (x-beta)/beta )` avoids the loss of precision incurred by evaluating `ln(x/beta)` when `x/beta` is close to `1`. +* - If `x/beta` overflows, the logarithm is evaluated as `ln(x) - ln(beta)`, which does not suffer from cancellation, as `x` and `beta` then differ by many orders of magnitude. +* +* @private +* @param {number} x - input value +* @param {PositiveNumber} beta - scale parameter +* @returns {NonNegativeNumber} natural logarithm of `x/beta` +* +* @example +* var v = lnRatio( 2.0, 1.0 ); +* // returns ~0.693 +*/ +function lnRatio( x, beta ) { + var r = x / beta; + if ( r < 2.0 ) { + return log1p( ( x-beta ) / beta ); + } + if ( r === PINF ) { + return ln( x ) - ln( beta ); + } + return ln( r ); +} + + +// EXPORTS // + +module.exports = lnRatio; diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/main.js index 1d57226284ac..417959dbd53e 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/main.js @@ -21,9 +21,13 @@ // MODULES // var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var log1mexp = require( '@stdlib/math/base/special/log1mexp' ); var log1p = require( '@stdlib/math/base/special/log1p' ); var pow = require( '@stdlib/math/base/special/pow' ); +var LN2 = require( '@stdlib/constants/float64/ln-two' ); +var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' ); var NINF = require( '@stdlib/constants/float64/ninf' ); +var lnRatio = require( './ln_ratio.js' ); // MAIN // @@ -77,6 +81,8 @@ var NINF = require( '@stdlib/constants/float64/ninf' ); * // returns NaN */ function logcdf( x, alpha, beta ) { + var r; + var t; if ( isnan( x ) || isnan( alpha ) || @@ -86,10 +92,17 @@ function logcdf( x, alpha, beta ) { ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return NINF; } - return log1p( -pow( beta / x, alpha ) ); + t = alpha * lnRatio( x, beta ); + r = beta / x; + if ( t > LN2 && r <= 0.5 && r >= FLOAT64_SMALLEST_NORMAL ) { + // As `(beta/x)^alpha < 0.5`, there is no cancellation. For `x >= 2*beta`, `pow` is more accurate than `exp(-t)`, as the rounding error of `beta/x` is amplified by `alpha`, while that of `t` is amplified by `t = alpha*ln(x/beta) >= alpha*ln(2)`: + return log1p( -pow( r, alpha ) ); + } + // Evaluate `ln( 1 - exp(-t) )` without cancellation: + return log1mexp( t ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/manifest.json b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/manifest.json index 8b0088e7bd3b..1c45e75207de 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/manifest.json @@ -42,7 +42,12 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/log1p", "@stdlib/constants/float64/ninf", - "@stdlib/math/base/special/pow" + "@stdlib/math/base/special/pow", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf", + "@stdlib/constants/float64/ln-two", + "@stdlib/constants/float64/smallest-normal", + "@stdlib/math/base/special/log1mexp" ] }, { @@ -60,7 +65,12 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/log1p", "@stdlib/constants/float64/ninf", - "@stdlib/math/base/special/pow" + "@stdlib/math/base/special/pow", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf", + "@stdlib/constants/float64/ln-two", + "@stdlib/constants/float64/smallest-normal", + "@stdlib/math/base/special/log1mexp" ] }, { @@ -78,7 +88,12 @@ "@stdlib/math/base/assert/is-nan", "@stdlib/math/base/special/log1p", "@stdlib/constants/float64/ninf", - "@stdlib/math/base/special/pow" + "@stdlib/math/base/special/pow", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf", + "@stdlib/constants/float64/ln-two", + "@stdlib/constants/float64/smallest-normal", + "@stdlib/math/base/special/log1mexp" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/src/main.c b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/src/main.c index eda300300b56..8e04c14987ab 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/src/main.c @@ -21,6 +21,35 @@ #include "stdlib/math/base/special/log1p.h" #include "stdlib/constants/float64/ninf.h" #include "stdlib/math/base/special/pow.h" +#include "stdlib/math/base/special/log1mexp.h" +#include "stdlib/math/base/special/ln.h" +#include "stdlib/constants/float64/pinf.h" +#include "stdlib/constants/float64/ln_two.h" +#include "stdlib/constants/float64/smallest_normal.h" + +/** +* Evaluates the natural logarithm of `x/beta` for `x >= beta`. +* +* ## Notes +* +* - For `x < 2*beta`, `x-beta` is computed exactly, so evaluating `log1p( (x-beta)/beta )` avoids the loss of precision incurred by evaluating `ln(x/beta)` when `x/beta` is close to `1`. +* - If `x/beta` overflows, the logarithm is evaluated as `ln(x) - ln(beta)`, which does not suffer from cancellation, as `x` and `beta` then differ by many orders of magnitude. +* +* @param x input value +* @param beta scale parameter +* @return natural logarithm of `x/beta` +*/ +static double ln_ratio( const double x, const double beta ) { + double r; + r = x / beta; + if ( r < 2.0 ) { + return stdlib_base_log1p( ( x-beta ) / beta ); + } + if ( r == STDLIB_CONSTANT_FLOAT64_PINF ) { + return stdlib_base_ln( x ) - stdlib_base_ln( beta ); + } + return stdlib_base_ln( r ); +} /** * Evaluates the natural logarithm of the cumulative distribution function (CDF) for a Pareto (Type I) distribution with shape parameter `alpha` and scale parameter `beta` at a value `x`. @@ -35,6 +64,8 @@ * // returns ~-0.693 */ double stdlib_base_dists_pareto_type1_logcdf( const double x, const double alpha, const double beta ) { + double r; + double t; if ( stdlib_base_is_nan( alpha ) || stdlib_base_is_nan( beta ) || @@ -44,8 +75,15 @@ double stdlib_base_dists_pareto_type1_logcdf( const double x, const double alpha ) { return 0.0 / 0.0; // NaN } - if ( x < beta ) { + if ( x <= beta ) { return STDLIB_CONSTANT_FLOAT64_NINF; } - return stdlib_base_log1p( -stdlib_base_pow( beta / x, alpha ) ); + t = alpha * ln_ratio( x, beta ); + r = beta / x; + if ( t > STDLIB_CONSTANT_FLOAT64_LN2 && r <= 0.5 && r >= STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) { + // As `(beta/x)^alpha < 0.5`, there is no cancellation. For `x >= 2*beta`, `pow` is more accurate than `exp(-t)`, as the rounding error of `beta/x` is amplified by `alpha`, while that of `t` is amplified by `t = alpha*ln(x/beta) >= alpha*ln(2)`: + return stdlib_base_log1p( -stdlib_base_pow( r, alpha ) ); + } + // Evaluate `ln( 1 - exp(-t) )` without cancellation: + return stdlib_base_log1mexp( t ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.factory.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.factory.js index 61728204ad2f..38105b394d61 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.factory.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.factory.js @@ -198,10 +198,12 @@ tape( 'the created function evaluates the logcdf for `x` given large `alpha` and x = bothLarge.x; alpha = bothLarge.alpha; beta = bothLarge.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 47 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { logcdf = factory( alpha[i], beta[i] ); y = logcdf( x[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 47 ), true, 'returns expected value' ); } t.end(); }); @@ -219,10 +221,12 @@ tape( 'the created function evaluates the logcdf for `x` given large `alpha` par x = largeAlpha.x; alpha = largeAlpha.alpha; beta = largeAlpha.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 15 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { logcdf = factory( alpha[i], beta[i] ); y = logcdf( x[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 22 ), true, 'returns expected value' ); } t.end(); }); @@ -240,10 +244,131 @@ tape( 'the created function evaluates the logcdf for `x` given large `beta` para x = largeBeta.x; alpha = largeBeta.alpha; beta = largeBeta.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 107 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { logcdf = factory( alpha[i], beta[i] ); y = logcdf( x[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 107 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the created function evaluates the logcdf accurately when `(beta/x)^alpha` is close to `1` or `beta/x` underflows', function test( t ) { + var expected; + var logcdf; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 120 digits of precision from `1 - (beta/x)^alpha` (and its logarithm): + x = [ + 1.0e300, + 2.0, + 1.0000001, + 3.0000000027939677, + 10000000000.0, + 1.5, + 1.0e300, + 4.0 + ]; + alpha = [ + 1.0e-20, + 1.0e-10, + 0.5, + 2.0, + 1.0e-12, + 1.0e-15, + 0.001, + 2.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 3.0, + 1.0, + 1.0, + 1.0e-300, + 1.0 + ]; + expected = [ + -39.51388693997676, + -23.39236385055678, + -16.811242905934396, + -20.1012682376354, + -24.49440357769806, + -35.44149685062857, + -0.2892681872016077, + -0.06453852113757118 + ]; + + for ( i = 0; i < x.length; i++ ) { + logcdf = factory( alpha[ i ], beta[ i ] ); + y = logcdf( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the created function returns `-infinity` if provided `x` equal to `beta`, even when `alpha` is `+infinity`', function test( t ) { + var y; + + y = factory( 2.0, 1.0 )( 1.0 ); + t.strictEqual( y, NINF, 'returns expected value' ); + + y = factory( PINF, 1.0 )( 1.0 ); + t.strictEqual( y, NINF, 'returns expected value' ); + + t.end(); +}); + +tape( 'the created function evaluates the logcdf for large `alpha` and `x` close to `beta`', function test( t ) { + var expected; + var logcdf; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 300 digits of precision from `ln( 1 - (beta/x)^alpha )`: + x = [ + 1.0000000001, + 1.00000001, + 1.000001, + 1.0000001, + 1.5 + ]; + alpha = [ + 1000000000000.0, + 10000000000.0, + 30000000.0, + 100000000.0, + 1000.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + expected = [ + -3.7200452147017624e-44, + -3.7200800969264467e-44, + -9.357763357239234e-14, + -4.5400982806401446e-05, + -8.104774656527566e-177 + ]; + + // The result is ill-conditioned with respect to `alpha` (a relative change `d` in `alpha` changes the result by about `alpha*ln(x/beta)*d` relative), hence the tolerance: + for ( i = 0; i < x.length; i++ ) { + logcdf = factory( alpha[ i ], beta[ i ] ); + y = logcdf( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 100 ), true, 'returns expected value' ); } t.end(); }); diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.logcdf.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.logcdf.js index 5efdcbf296c4..5bd71da884d7 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.logcdf.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.logcdf.js @@ -138,9 +138,11 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` and `beta`' x = bothLarge.x; alpha = bothLarge.alpha; beta = bothLarge.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 47 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = logcdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 47 ), true, 'returns expected value' ); } t.end(); }); @@ -157,9 +159,11 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` parameter', x = largeAlpha.x; alpha = largeAlpha.alpha; beta = largeAlpha.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 15 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = logcdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 22 ), true, 'returns expected value' ); } t.end(); }); @@ -176,9 +180,126 @@ tape( 'the function evaluates the logcdf for `x` given large rate parameter `bet x = largeBeta.x; alpha = largeBeta.alpha; beta = largeBeta.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 107 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = logcdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 107 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates the logcdf accurately when `(beta/x)^alpha` is close to `1` or `beta/x` underflows', function test( t ) { + var expected; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 120 digits of precision from `1 - (beta/x)^alpha` (and its logarithm): + x = [ + 1.0e300, + 2.0, + 1.0000001, + 3.0000000027939677, + 10000000000.0, + 1.5, + 1.0e300, + 4.0 + ]; + alpha = [ + 1.0e-20, + 1.0e-10, + 0.5, + 2.0, + 1.0e-12, + 1.0e-15, + 0.001, + 2.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 3.0, + 1.0, + 1.0, + 1.0e-300, + 1.0 + ]; + expected = [ + -39.51388693997676, + -23.39236385055678, + -16.811242905934396, + -20.1012682376354, + -24.49440357769806, + -35.44149685062857, + -0.2892681872016077, + -0.06453852113757118 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = logcdf( x[ i ], alpha[ i ], beta[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `x` equal to `beta`, even when `alpha` is `+infinity`', function test( t ) { + var y; + + y = logcdf( 1.0, 2.0, 1.0 ); + t.strictEqual( y, NINF, 'returns expected value' ); + + y = logcdf( 1.0, PINF, 1.0 ); + t.strictEqual( y, NINF, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function evaluates the logcdf for large `alpha` and `x` close to `beta`', function test( t ) { + var expected; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 300 digits of precision from `ln( 1 - (beta/x)^alpha )`: + x = [ + 1.0000000001, + 1.00000001, + 1.000001, + 1.0000001, + 1.5 + ]; + alpha = [ + 1000000000000.0, + 10000000000.0, + 30000000.0, + 100000000.0, + 1000.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + expected = [ + -3.7200452147017624e-44, + -3.7200800969264467e-44, + -9.357763357239234e-14, + -4.5400982806401446e-05, + -8.104774656527566e-177 + ]; + + // The result is ill-conditioned with respect to `alpha` (a relative change `d` in `alpha` changes the result by about `alpha*ln(x/beta)*d` relative), hence the tolerance: + for ( i = 0; i < x.length; i++ ) { + y = logcdf( x[ i ], alpha[ i ], beta[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 100 ), true, 'returns expected value' ); } t.end(); }); diff --git a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.native.js index c17d045bdb86..6a0fb75bb019 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/test/test.native.js @@ -147,9 +147,11 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` and `beta`' x = bothLarge.x; alpha = bothLarge.alpha; beta = bothLarge.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 47 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = logcdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 47 ), true, 'returns expected value' ); } t.end(); }); @@ -166,9 +168,11 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` parameter', x = largeAlpha.x; alpha = largeAlpha.alpha; beta = largeAlpha.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 15 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = logcdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 22 ), true, 'returns expected value' ); } t.end(); }); @@ -185,9 +189,126 @@ tape( 'the function evaluates the logcdf for `x` given large rate parameter `bet x = largeBeta.x; alpha = largeBeta.alpha; beta = largeBeta.beta; + + // The fixture values (computed with Distributions.jl) have errors of up to about 107 ULP relative to exact values for these inputs, so the tolerance is the largest difference between this implementation and the fixture values (see the tests against exact values below): for ( i = 0; i < x.length; i++ ) { y = logcdf( x[i], alpha[i], beta[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 107 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates the logcdf accurately when `(beta/x)^alpha` is close to `1` or `beta/x` underflows', opts, function test( t ) { + var expected; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 120 digits of precision from `1 - (beta/x)^alpha` (and its logarithm): + x = [ + 1.0e300, + 2.0, + 1.0000001, + 3.0000000027939677, + 10000000000.0, + 1.5, + 1.0e300, + 4.0 + ]; + alpha = [ + 1.0e-20, + 1.0e-10, + 0.5, + 2.0, + 1.0e-12, + 1.0e-15, + 0.001, + 2.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 3.0, + 1.0, + 1.0, + 1.0e-300, + 1.0 + ]; + expected = [ + -39.51388693997676, + -23.39236385055678, + -16.811242905934396, + -20.1012682376354, + -24.49440357769806, + -35.44149685062857, + -0.2892681872016077, + -0.06453852113757118 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = logcdf( x[ i ], alpha[ i ], beta[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function returns `-infinity` if provided `x` equal to `beta`, even when `alpha` is `+infinity`', opts, function test( t ) { + var y; + + y = logcdf( 1.0, 2.0, 1.0 ); + t.strictEqual( y, NINF, 'returns expected value' ); + + y = logcdf( 1.0, PINF, 1.0 ); + t.strictEqual( y, NINF, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function evaluates the logcdf for large `alpha` and `x` close to `beta`', opts, function test( t ) { + var expected; + var alpha; + var beta; + var x; + var y; + var i; + + // Reference values computed with 300 digits of precision from `ln( 1 - (beta/x)^alpha )`: + x = [ + 1.0000000001, + 1.00000001, + 1.000001, + 1.0000001, + 1.5 + ]; + alpha = [ + 1000000000000.0, + 10000000000.0, + 30000000.0, + 100000000.0, + 1000.0 + ]; + beta = [ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ]; + expected = [ + -3.7200452147017624e-44, + -3.7200800969264467e-44, + -9.357763357239234e-14, + -4.5400982806401446e-05, + -8.104774656527566e-177 + ]; + + // The result is ill-conditioned with respect to `alpha` (a relative change `d` in `alpha` changes the result by about `alpha*ln(x/beta)*d` relative), hence the tolerance: + for ( i = 0; i < x.length; i++ ) { + y = logcdf( x[ i ], alpha[ i ], beta[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 100 ), true, 'returns expected value' ); } t.end(); });