From 846c54c29f05cce629419f1b1e838d3157781dc0 Mon Sep 17 00:00:00 2001 From: Uday Gund Date: Fri, 4 Dec 2026 19:16:17 +0530 Subject: [PATCH 1/2] fix: improve accuracy of `stats/base/dists/pareto-type1/{cdf,logcdf}` Evaluates `t = alpha * ln(x/beta)` accurately and computes the CDF as `-expm1( -t )` and the log-CDF as `ln( -expm1( -t ) )` for `t <= ln(2)`, avoiding cancellation when `(beta/x)^alpha` is close to `1`, and handles underflow of `beta/x`. Resolves: https://github.com/stdlib-js/stdlib/issues/17147 Co-Authored-By: Claude Opus 5.5 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../dists/pareto-type1/cdf/lib/factory.js | 7 +- .../dists/pareto-type1/cdf/lib/ln_ratio.js | 61 ++++++++++++++++ .../base/dists/pareto-type1/cdf/lib/main.js | 7 +- .../base/dists/pareto-type1/cdf/manifest.json | 15 +++- .../base/dists/pareto-type1/cdf/src/main.c | 31 +++++++- .../dists/pareto-type1/cdf/test/test.cdf.js | 67 ++++++++++++++++- .../pareto-type1/cdf/test/test.factory.js | 69 +++++++++++++++++- .../pareto-type1/cdf/test/test.native.js | 67 ++++++++++++++++- .../dists/pareto-type1/logcdf/lib/factory.js | 20 +++++- .../dists/pareto-type1/logcdf/lib/ln_ratio.js | 61 ++++++++++++++++ .../dists/pareto-type1/logcdf/lib/main.js | 20 +++++- .../dists/pareto-type1/logcdf/manifest.json | 24 ++++++- .../base/dists/pareto-type1/logcdf/src/main.c | 43 ++++++++++- .../pareto-type1/logcdf/test/test.factory.js | 71 ++++++++++++++++++- .../pareto-type1/logcdf/test/test.logcdf.js | 69 +++++++++++++++++- .../pareto-type1/logcdf/test/test.native.js | 69 +++++++++++++++++- 16 files changed, 671 insertions(+), 30 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/dists/pareto-type1/cdf/lib/ln_ratio.js create mode 100644 lib/node_modules/@stdlib/stats/base/dists/pareto-type1/logcdf/lib/ln_ratio.js 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..33fc14497aba 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 // @@ -67,7 +68,9 @@ function factory( alpha, 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..f8e5e11da481 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 // @@ -87,7 +88,9 @@ function cdf( x, alpha, 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..749339acad3b 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,33 @@ #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 = 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`. @@ -45,5 +71,6 @@ double stdlib_base_dists_pareto_type1_cdf( const double x, const double alpha, c 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..99650cb645b3 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) match the previous implementation and have errors of up to about 228 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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) match the previous implementation and have errors of up to about 1030 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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,8 +180,67 @@ 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) match the previous implementation and have errors of up to about 359 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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(); 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..6ad10ea8cbc8 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) match the previous implementation and have errors of up to about 228 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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) match the previous implementation and have errors of up to about 1030 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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,9 +244,70 @@ 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) match the previous implementation and have errors of up to about 359 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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(); 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..6cb1f46120fb 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) match the previous implementation and have errors of up to about 228 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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) match the previous implementation and have errors of up to about 1030 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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,8 +189,67 @@ 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) match the previous implementation and have errors of up to about 359 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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(); 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..1276ea3bd7d9 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 @@ -23,8 +23,14 @@ var constantFunction = require( '@stdlib/utils/constant-function' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var log1p = require( '@stdlib/math/base/special/log1p' ); +var expm1 = require( '@stdlib/math/base/special/expm1' ); var pow = require( '@stdlib/math/base/special/pow' ); +var exp = require( '@stdlib/math/base/special/exp' ); +var ln = require( '@stdlib/math/base/special/ln' ); +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 +69,25 @@ function factory( alpha, beta ) { * @returns {number} evaluated logCDF */ function logcdf( x ) { + var r; + var t; if ( isnan( x ) ) { return NaN; } if ( x < beta ) { return NINF; } - return log1p( -pow( beta / x, alpha ) ); + t = alpha * lnRatio( x, beta ); + if ( t <= LN2 ) { + // When `(beta/x)^alpha = exp(-t)` is close to `1`, evaluate `ln( 1 - exp(-t) )` without cancellation: + return ln( -expm1( -t ) ); + } + r = beta / x; + if ( r >= FLOAT64_SMALLEST_NORMAL ) { + // As `(beta/x)^alpha < 0.5`, there is no cancellation, and `pow` avoids amplifying the rounding error of `t`, which is significant for large `t`: + return log1p( -pow( r, alpha ) ); + } + return log1p( -exp( -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..2a7e63e82df2 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 @@ -22,8 +22,14 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' ); var log1p = require( '@stdlib/math/base/special/log1p' ); +var expm1 = require( '@stdlib/math/base/special/expm1' ); var pow = require( '@stdlib/math/base/special/pow' ); +var exp = require( '@stdlib/math/base/special/exp' ); +var ln = require( '@stdlib/math/base/special/ln' ); +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 +83,8 @@ var NINF = require( '@stdlib/constants/float64/ninf' ); * // returns NaN */ function logcdf( x, alpha, beta ) { + var r; + var t; if ( isnan( x ) || isnan( alpha ) || @@ -89,7 +97,17 @@ function logcdf( x, alpha, beta ) { if ( x < beta ) { return NINF; } - return log1p( -pow( beta / x, alpha ) ); + t = alpha * lnRatio( x, beta ); + if ( t <= LN2 ) { + // When `(beta/x)^alpha = exp(-t)` is close to `1`, evaluate `ln( 1 - exp(-t) )` without cancellation: + return ln( -expm1( -t ) ); + } + r = beta / x; + if ( r >= FLOAT64_SMALLEST_NORMAL ) { + // As `(beta/x)^alpha < 0.5`, there is no cancellation, and `pow` avoids amplifying the rounding error of `t`, which is significant for large `t`: + return log1p( -pow( r, alpha ) ); + } + return log1p( -exp( -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..3628d4c6affa 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,13 @@ "@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/expm1", + "@stdlib/math/base/special/exp", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf", + "@stdlib/constants/float64/ln-two", + "@stdlib/constants/float64/smallest-normal" ] }, { @@ -60,7 +66,13 @@ "@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/expm1", + "@stdlib/math/base/special/exp", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf", + "@stdlib/constants/float64/ln-two", + "@stdlib/constants/float64/smallest-normal" ] }, { @@ -78,7 +90,13 @@ "@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/expm1", + "@stdlib/math/base/special/exp", + "@stdlib/math/base/special/ln", + "@stdlib/constants/float64/pinf", + "@stdlib/constants/float64/ln-two", + "@stdlib/constants/float64/smallest-normal" ] } ] 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..0ee50200defb 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/expm1.h" +#include "stdlib/math/base/special/exp.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 = 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 ) || @@ -47,5 +78,15 @@ double stdlib_base_dists_pareto_type1_logcdf( const double x, const double alpha if ( x < beta ) { return STDLIB_CONSTANT_FLOAT64_NINF; } - return stdlib_base_log1p( -stdlib_base_pow( beta / x, alpha ) ); + t = alpha * ln_ratio( x, beta ); + if ( t <= STDLIB_CONSTANT_FLOAT64_LN2 ) { + // When `(beta/x)^alpha = exp(-t)` is close to `1`, evaluate `ln( 1 - exp(-t) )` without cancellation: + return stdlib_base_ln( -stdlib_base_expm1( -t ) ); + } + r = beta / x; + if ( r >= STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) { + // As `(beta/x)^alpha < 0.5`, there is no cancellation, and `pow` avoids amplifying the rounding error of `t`, which is significant for large `t`: + return stdlib_base_log1p( -stdlib_base_pow( r, alpha ) ); + } + return stdlib_base_log1p( -stdlib_base_exp( -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..5657244ffbe9 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) match the previous implementation and have errors of up to about 47 ULP relative to exact values for these inputs (see the tests against exact values below); 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 ], 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) match the previous implementation and have errors of up to about 15 ULP relative to exact values for these inputs (see the tests against exact values below); 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 ], 0 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 8 ), true, 'returns expected value' ); } t.end(); }); @@ -240,10 +244,71 @@ 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) match the previous implementation and have errors of up to about 107 ULP relative to exact values for these inputs (see the tests against exact values below); 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 ], 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(); }); 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..2c62a6b64494 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) match the previous implementation and have errors of up to about 47 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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) match the previous implementation and have errors of up to about 15 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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 ], 8 ), true, 'returns expected value' ); } t.end(); }); @@ -176,9 +180,68 @@ 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) match the previous implementation and have errors of up to about 107 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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(); }); 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..c83f559042c3 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) match the previous implementation and have errors of up to about 47 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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) match the previous implementation and have errors of up to about 15 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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 ], 8 ), true, 'returns expected value' ); } t.end(); }); @@ -185,9 +189,68 @@ 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) match the previous implementation and have errors of up to about 107 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: 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(); }); From a1d18656c3b33552a1d685785cc097861ab7739e Mon Sep 17 00:00:00 2001 From: Uday Gund Date: Fri, 4 Dec 2026 20:23:09 +0530 Subject: [PATCH 2/2] fix: handle `x = beta` with infinite `alpha` and improve `logcdf` for `x` near `beta` - returns `0` (`cdf`) and `-Infinity` (`logcdf`) for `x = beta`, even when `alpha` is `+Infinity`, instead of `NaN` - uses `pow( beta/x, alpha )` in `logcdf` only for `x >= 2*beta`, and `log1mexp( alpha*ln(x/beta) )` otherwise, as `pow` amplifies the rounding error of `beta/x` by `alpha` Ref: https://github.com/stdlib-js/stdlib/issues/17147 Co-Authored-By: Claude Opus 5.5 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../dists/pareto-type1/cdf/lib/factory.js | 2 +- .../base/dists/pareto-type1/cdf/lib/main.js | 2 +- .../base/dists/pareto-type1/cdf/src/main.c | 5 +- .../dists/pareto-type1/cdf/test/test.cdf.js | 18 ++++- .../pareto-type1/cdf/test/test.factory.js | 18 ++++- .../pareto-type1/cdf/test/test.native.js | 18 ++++- .../dists/pareto-type1/logcdf/lib/factory.js | 17 ++--- .../dists/pareto-type1/logcdf/lib/main.js | 17 ++--- .../dists/pareto-type1/logcdf/manifest.json | 15 ++-- .../base/dists/pareto-type1/logcdf/src/main.c | 19 +++--- .../pareto-type1/logcdf/test/test.factory.js | 68 +++++++++++++++++-- .../pareto-type1/logcdf/test/test.logcdf.js | 66 ++++++++++++++++-- .../pareto-type1/logcdf/test/test.native.js | 66 ++++++++++++++++-- 13 files changed, 264 insertions(+), 67 deletions(-) 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 33fc14497aba..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 @@ -65,7 +65,7 @@ function factory( alpha, beta ) { if ( isnan( x ) ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return 0.0; } 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 f8e5e11da481..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 @@ -85,7 +85,7 @@ function cdf( x, alpha, beta ) { ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return 0.0; } 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 749339acad3b..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 @@ -36,7 +36,8 @@ * @return natural logarithm of `x/beta` */ static double ln_ratio( const double x, const double beta ) { - double r = x / beta; + double r; + r = x / beta; if ( r < 2.0 ) { return stdlib_base_log1p( ( x-beta ) / beta ); } @@ -68,7 +69,7 @@ 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; } // Evaluate `1 - (beta/x)^alpha` as `-expm1( -alpha*ln(x/beta) )` to avoid cancellation when `(beta/x)^alpha` is close to `1`: 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 99650cb645b3..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 @@ -139,7 +139,7 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` and `beta`', f alpha = bothLarge.alpha; beta = bothLarge.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 228 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 228 ), true, 'returns expected value' ); @@ -160,7 +160,7 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` parameter', fu alpha = largeAlpha.alpha; beta = largeAlpha.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 1030 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 1033 ), true, 'returns expected value' ); @@ -181,7 +181,7 @@ tape( 'the function evaluates the cdf for `x` given large rate parameter `beta`' alpha = largeBeta.alpha; beta = largeBeta.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 359 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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' ); @@ -245,3 +245,15 @@ tape( 'the function evaluates the cdf accurately when `(beta/x)^alpha` is close } 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 6ad10ea8cbc8..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 @@ -199,7 +199,7 @@ tape( 'the created function evaluates the cdf for `x` given large `alpha` and `b alpha = bothLarge.alpha; beta = bothLarge.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 228 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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] ); @@ -222,7 +222,7 @@ tape( 'the created function evaluates the cdf for `x` given large `alpha` parame alpha = largeAlpha.alpha; beta = largeAlpha.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 1030 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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] ); @@ -245,7 +245,7 @@ tape( 'the created function evaluates the cdf for `x` given large `beta` paramet alpha = largeBeta.alpha; beta = largeBeta.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 359 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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] ); @@ -312,3 +312,15 @@ tape( 'the created function evaluates the cdf accurately when `(beta/x)^alpha` i } 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 6cb1f46120fb..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 @@ -148,7 +148,7 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` and `beta`', o alpha = bothLarge.alpha; beta = bothLarge.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 228 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 228 ), true, 'returns expected value' ); @@ -169,7 +169,7 @@ tape( 'the function evaluates the cdf for `x` given large `alpha` parameter', op alpha = largeAlpha.alpha; beta = largeAlpha.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 1030 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 1033 ), true, 'returns expected value' ); @@ -190,7 +190,7 @@ tape( 'the function evaluates the cdf for `x` given large rate parameter `beta`' alpha = largeBeta.alpha; beta = largeBeta.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 359 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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' ); @@ -254,3 +254,15 @@ tape( 'the function evaluates the cdf accurately when `(beta/x)^alpha` is close } 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 1276ea3bd7d9..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,11 +22,9 @@ 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 expm1 = require( '@stdlib/math/base/special/expm1' ); var pow = require( '@stdlib/math/base/special/pow' ); -var exp = require( '@stdlib/math/base/special/exp' ); -var ln = require( '@stdlib/math/base/special/ln' ); 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' ); @@ -74,20 +72,17 @@ function factory( alpha, beta ) { if ( isnan( x ) ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return NINF; } t = alpha * lnRatio( x, beta ); - if ( t <= LN2 ) { - // When `(beta/x)^alpha = exp(-t)` is close to `1`, evaluate `ln( 1 - exp(-t) )` without cancellation: - return ln( -expm1( -t ) ); - } r = beta / x; - if ( r >= FLOAT64_SMALLEST_NORMAL ) { - // As `(beta/x)^alpha < 0.5`, there is no cancellation, and `pow` avoids amplifying the rounding error of `t`, which is significant for large `t`: + 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 ) ); } - return log1p( -exp( -t ) ); + // Evaluate `ln( 1 - exp(-t) )` without cancellation: + return log1mexp( t ); } } 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 2a7e63e82df2..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,11 +21,9 @@ // 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 expm1 = require( '@stdlib/math/base/special/expm1' ); var pow = require( '@stdlib/math/base/special/pow' ); -var exp = require( '@stdlib/math/base/special/exp' ); -var ln = require( '@stdlib/math/base/special/ln' ); 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' ); @@ -94,20 +92,17 @@ function logcdf( x, alpha, beta ) { ) { return NaN; } - if ( x < beta ) { + if ( x <= beta ) { return NINF; } t = alpha * lnRatio( x, beta ); - if ( t <= LN2 ) { - // When `(beta/x)^alpha = exp(-t)` is close to `1`, evaluate `ln( 1 - exp(-t) )` without cancellation: - return ln( -expm1( -t ) ); - } r = beta / x; - if ( r >= FLOAT64_SMALLEST_NORMAL ) { - // As `(beta/x)^alpha < 0.5`, there is no cancellation, and `pow` avoids amplifying the rounding error of `t`, which is significant for large `t`: + 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 ) ); } - return log1p( -exp( -t ) ); + // 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 3628d4c6affa..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 @@ -43,12 +43,11 @@ "@stdlib/math/base/special/log1p", "@stdlib/constants/float64/ninf", "@stdlib/math/base/special/pow", - "@stdlib/math/base/special/expm1", - "@stdlib/math/base/special/exp", "@stdlib/math/base/special/ln", "@stdlib/constants/float64/pinf", "@stdlib/constants/float64/ln-two", - "@stdlib/constants/float64/smallest-normal" + "@stdlib/constants/float64/smallest-normal", + "@stdlib/math/base/special/log1mexp" ] }, { @@ -67,12 +66,11 @@ "@stdlib/math/base/special/log1p", "@stdlib/constants/float64/ninf", "@stdlib/math/base/special/pow", - "@stdlib/math/base/special/expm1", - "@stdlib/math/base/special/exp", "@stdlib/math/base/special/ln", "@stdlib/constants/float64/pinf", "@stdlib/constants/float64/ln-two", - "@stdlib/constants/float64/smallest-normal" + "@stdlib/constants/float64/smallest-normal", + "@stdlib/math/base/special/log1mexp" ] }, { @@ -91,12 +89,11 @@ "@stdlib/math/base/special/log1p", "@stdlib/constants/float64/ninf", "@stdlib/math/base/special/pow", - "@stdlib/math/base/special/expm1", - "@stdlib/math/base/special/exp", "@stdlib/math/base/special/ln", "@stdlib/constants/float64/pinf", "@stdlib/constants/float64/ln-two", - "@stdlib/constants/float64/smallest-normal" + "@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 0ee50200defb..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,8 +21,7 @@ #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/expm1.h" -#include "stdlib/math/base/special/exp.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" @@ -41,7 +40,8 @@ * @return natural logarithm of `x/beta` */ static double ln_ratio( const double x, const double beta ) { - double r = x / beta; + double r; + r = x / beta; if ( r < 2.0 ) { return stdlib_base_log1p( ( x-beta ) / beta ); } @@ -75,18 +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; } t = alpha * ln_ratio( x, beta ); - if ( t <= STDLIB_CONSTANT_FLOAT64_LN2 ) { - // When `(beta/x)^alpha = exp(-t)` is close to `1`, evaluate `ln( 1 - exp(-t) )` without cancellation: - return stdlib_base_ln( -stdlib_base_expm1( -t ) ); - } r = beta / x; - if ( r >= STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) { - // As `(beta/x)^alpha < 0.5`, there is no cancellation, and `pow` avoids amplifying the rounding error of `t`, which is significant for large `t`: + 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 ) ); } - return stdlib_base_log1p( -stdlib_base_exp( -t ) ); + // 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 5657244ffbe9..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 @@ -199,7 +199,7 @@ tape( 'the created function evaluates the logcdf for `x` given large `alpha` and alpha = bothLarge.alpha; beta = bothLarge.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 47 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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] ); @@ -222,11 +222,11 @@ tape( 'the created function evaluates the logcdf for `x` given large `alpha` par alpha = largeAlpha.alpha; beta = largeAlpha.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 15 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 8 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 22 ), true, 'returns expected value' ); } t.end(); }); @@ -245,7 +245,7 @@ tape( 'the created function evaluates the logcdf for `x` given large `beta` para alpha = largeBeta.alpha; beta = largeBeta.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 107 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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] ); @@ -312,3 +312,63 @@ tape( 'the created function evaluates the logcdf accurately when `(beta/x)^alpha } 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 2c62a6b64494..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 @@ -139,7 +139,7 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` and `beta`' alpha = bothLarge.alpha; beta = bothLarge.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 47 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 47 ), true, 'returns expected value' ); @@ -160,10 +160,10 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` parameter', alpha = largeAlpha.alpha; beta = largeAlpha.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 15 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 8 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 22 ), true, 'returns expected value' ); } t.end(); }); @@ -181,7 +181,7 @@ tape( 'the function evaluates the logcdf for `x` given large rate parameter `bet alpha = largeBeta.alpha; beta = largeBeta.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 107 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 107 ), true, 'returns expected value' ); @@ -245,3 +245,61 @@ tape( 'the function evaluates the logcdf accurately when `(beta/x)^alpha` is clo } 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 c83f559042c3..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 @@ -148,7 +148,7 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` and `beta`' alpha = bothLarge.alpha; beta = bothLarge.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 47 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 47 ), true, 'returns expected value' ); @@ -169,10 +169,10 @@ tape( 'the function evaluates the logcdf for `x` given large `alpha` parameter', alpha = largeAlpha.alpha; beta = largeAlpha.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 15 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 8 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 22 ), true, 'returns expected value' ); } t.end(); }); @@ -190,7 +190,7 @@ tape( 'the function evaluates the logcdf for `x` given large rate parameter `bet alpha = largeBeta.alpha; beta = largeBeta.beta; - // The fixture values (computed with Distributions.jl) match the previous implementation and have errors of up to about 107 ULP relative to exact values for these inputs (see the tests against exact values below); hence the tolerance: + // 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 ], 107 ), true, 'returns expected value' ); @@ -254,3 +254,61 @@ tape( 'the function evaluates the logcdf accurately when `(beta/x)^alpha` is clo } 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(); +});