Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -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 ) );
}
}

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


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