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,9 @@

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 exp = require( '@stdlib/math/base/special/exp' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var PINF = require( '@stdlib/constants/float64/pinf' );


// MAIN //
Expand Down Expand Up @@ -70,7 +72,10 @@ function factory( alpha, beta ) {
if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
return NaN;
}
return beta / pow( 1.0 - p, alphaInv );
if ( p === 1.0 ) {
return PINF;
}
return beta * exp( -log1p( -p ) * alphaInv );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
// MODULES //

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var exp = require( '@stdlib/math/base/special/exp' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var PINF = require( '@stdlib/constants/float64/pinf' );


// MAIN //
Expand Down Expand Up @@ -86,7 +88,10 @@ function quantile( p, alpha, beta ) {
) {
return NaN;
}
return beta / pow( 1.0 - p, 1.0/alpha );
if ( p === 1.0 ) {
return PINF;
}
return beta * exp( -log1p( -p ) / alpha );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"dependencies": [
"@stdlib/math/base/napi/ternary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/pow"
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/log1p",
"@stdlib/constants/float64/pinf"
]
},
{
Expand All @@ -56,7 +58,9 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/pow"
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/log1p",
"@stdlib/constants/float64/pinf"
]
},
{
Expand All @@ -72,7 +76,9 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/pow"
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/log1p",
"@stdlib/constants/float64/pinf"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "stdlib/stats/base/dists/pareto-type1/quantile.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/log1p.h"
#include "stdlib/constants/float64/pinf.h"

/**
* Evaluates the quantile function for a Pareto (Type I) distribution with shape parameter `alpha` and scale parameter `beta` at a probability `p`.
Expand All @@ -44,5 +46,8 @@ double stdlib_base_dists_pareto_type1_quantile( const double p, const double alp
) {
return 0.0 / 0.0; // NaN
}
return beta / stdlib_base_pow( 1.0 - p, 1.0/alpha );
if ( p == 1.0 ) {
return STDLIB_CONSTANT_FLOAT64_PINF;
}
return beta * stdlib_base_exp( -stdlib_base_log1p( -p ) / alpha );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
#
# @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.

"""Generate fixtures."""

import os
import json
from decimal import Decimal, localcontext

FILE = os.path.realpath(__file__)
DIR = os.path.dirname(FILE)


def eval_quantile(p, alpha, beta):
"""Evaluate Pareto Type I quantile at 100-digit precision."""
with localcontext() as ctx:
ctx.prec = 100
dec_p = Decimal.from_float(p)
dec_alpha = Decimal.from_float(alpha)
dec_beta = Decimal.from_float(beta)
return float(dec_beta * (-(Decimal(1) - dec_p).ln() / dec_alpha).exp())


def gen(cases, name):
"""Generate fixture data and write to file."""
p = [c[0] for c in cases]
alpha = [c[1] for c in cases]
beta = [c[2] for c in cases]
expected = [eval_quantile(c[0], c[1], c[2]) for c in cases]

data = {
"p": p,
"alpha": alpha,
"beta": beta,
"expected": expected
}
filepath = os.path.join(DIR, name)
with open(filepath, "w", encoding="utf-8", newline="\n") as outfile:
json.dump(data, outfile)
outfile.write("\n")


def main():
"""Generate fixture data."""
cases = [
(3.487713003317677e-14, 3.770177179633989e-15, 3.1006509521973644),
(9.848016845161383e-17, 4.028717220737787e-17, 0.6831483634551567),
(1.3302736633414168e-17, 2.201607940364203e-18, 0.6003661040099838),
(3.627963640422887e-15, 6.742762523455147e-16, 1.30847092784763),
(1.590320234441396e-19, 2.1816061229888833e-20, 1.4297727967473501),
(1.0e-16, 1.0e-16, 1.0),
(1.0e-18, 1.0e-18, 2.5),
(1.0e-20, 5.0e-21, 0.5),
(1.0e-12, 1.0, 1.0),
(1.0e-16, 1.0, 3.0)
]
gen(cases, "small_p.json")


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"p": [3.487713003317677e-14, 9.848016845161383e-17, 1.3302736633414168e-17, 3.627963640422887e-15, 1.590320234441396e-19, 1e-16, 1e-18, 1e-20, 1e-12, 1e-16], "alpha": [3.770177179633989e-15, 4.028717220737787e-17, 2.201607940364203e-18, 6.742762523455147e-16, 2.1816061229888833e-20, 1e-16, 1e-18, 5e-21, 1.0, 1.0], "beta": [3.1006509521973644, 0.6831483634551567, 0.6003661040099838, 1.30847092784763, 1.4297727967473501, 1.0, 2.5, 0.5, 1.0, 3.0], "expected": [32286.53139864373, 7.872782084848507, 252.66554003062035, 284.1178960020023, 2094.7548306005983, 2.7182818284590455, 6.795704571147613, 3.694528049465325, 1.000000000001, 3.0000000000000004]}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var factory = require( './../lib/factory.js' );
var largeAlpha = require( './fixtures/julia/large_alpha.json' );
var largeBeta = require( './fixtures/julia/large_beta.json' );
var bothLarge = require( './fixtures/julia/both_large.json' );
var smallP = require( './fixtures/python/small_p.json' );


// TESTS //
Expand Down Expand Up @@ -157,6 +158,36 @@ tape( 'if provided a nonpositive `alpha`, the created function always returns `N
t.end();
});

tape( 'if provided `0.0` for `p` and valid `alpha` and `beta`, the created function returns `beta`', function test( t ) {
var quantile;
var y;

quantile = factory( 2.0, 3.0 );
y = quantile( 0.0 );
t.strictEqual( y, 3.0, 'returns expected value' );

quantile = factory( 0.5, 1.5 );
y = quantile( 0.0 );
t.strictEqual( y, 1.5, 'returns expected value' );

t.end();
});

tape( 'if provided `1.0` for `p` and valid `alpha` and `beta`, the created function returns `+Infinity`', function test( t ) {
var quantile;
var y;

quantile = factory( 2.0, 3.0 );
y = quantile( 1.0 );
t.strictEqual( y, PINF, 'returns expected value' );

quantile = factory( 0.5, 1.5 );
y = quantile( 1.0 );
t.strictEqual( y, PINF, 'returns expected value' );

t.end();
});

tape( 'the created function evaluates the quantile for `p` given large `alpha` and `beta`', function test( t ) {
var expected;
var quantile;
Expand All @@ -173,7 +204,7 @@ tape( 'the created function evaluates the quantile for `p` given large `alpha` a
for ( i = 0; i < p.length; i++ ) {
quantile = factory( alpha[i], beta[i] );
y = quantile( p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 4 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -194,7 +225,7 @@ tape( 'the created function evaluates the quantile for `p` given large parameter
for ( i = 0; i < p.length; i++ ) {
quantile = factory( alpha[i], beta[i] );
y = quantile( p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 4 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -215,7 +246,28 @@ tape( 'the created function evaluates the quantile for `p` given large parameter
for ( i = 0; i < p.length; i++ ) {
quantile = factory( alpha[i], beta[i] );
y = quantile( p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 20 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the created function evaluates the quantile accurately for small `p`', function test( t ) {
var expected;
var quantile;
var alpha;
var beta;
var i;
var p;
var y;

expected = smallP.expected;
p = smallP.p;
alpha = smallP.alpha;
beta = smallP.beta;
for ( i = 0; i < p.length; i++ ) {
quantile = factory( alpha[i], beta[i] );
y = quantile( p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 20 ), true, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var opts = {
var largeAlpha = require( './fixtures/julia/large_alpha.json' );
var largeBeta = require( './fixtures/julia/large_beta.json' );
var bothLarge = require( './fixtures/julia/both_large.json' );
var smallP = require( './fixtures/python/small_p.json' );


// TESTS //
Expand Down Expand Up @@ -124,7 +125,23 @@ tape( 'if provided a nonpositive `beta`, the function returns `NaN`', opts, func
t.end();
});

tape( 'the function evaluates the quantile for `x` given large parameters `alpha` and `beta`', opts, function test( t ) {
tape( 'if provided `0.0` for `p` and valid `alpha` and `beta`, the function returns `beta`', opts, function test( t ) {
var y = quantile( 0.0, 2.0, 3.0 );
t.strictEqual( y, 3.0, 'returns expected value' );
y = quantile( 0.0, 0.5, 1.5 );
t.strictEqual( y, 1.5, 'returns expected value' );
t.end();
});

tape( 'if provided `1.0` for `p` and valid `alpha` and `beta`, the function returns `+Infinity`', opts, function test( t ) {
var y = quantile( 1.0, 2.0, 3.0 );
t.strictEqual( y, PINF, 'returns expected value' );
y = quantile( 1.0, 0.5, 1.5 );
t.strictEqual( y, PINF, 'returns expected value' );
t.end();
});

tape( 'the function evaluates the quantile for `p` given large parameters `alpha` and `beta`', opts, function test( t ) {
var expected;
var alpha;
var beta;
Expand All @@ -138,12 +155,12 @@ tape( 'the function evaluates the quantile for `x` given large parameters `alpha
beta = bothLarge.beta;
for ( i = 0; i < p.length; i++ ) {
y = quantile( p[i], alpha[i], beta[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 4 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function evaluates the quantile for `x` given large parameter `alpha`', opts, function test( t ) {
tape( 'the function evaluates the quantile for `p` given large parameter `alpha`', opts, function test( t ) {
var expected;
var alpha;
var beta;
Expand All @@ -157,12 +174,12 @@ tape( 'the function evaluates the quantile for `x` given large parameter `alpha`
beta = largeAlpha.beta;
for ( i = 0; i < p.length; i++ ) {
y = quantile( p[i], alpha[i], beta[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 4 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function evaluates the quantile for `x` given large parameter `beta`', opts, function test( t ) {
tape( 'the function evaluates the quantile for `p` given large parameter `beta`', opts, function test( t ) {
var expected;
var alpha;
var beta;
Expand All @@ -176,7 +193,26 @@ tape( 'the function evaluates the quantile for `x` given large parameter `beta`'
beta = largeBeta.beta;
for ( i = 0; i < p.length; i++ ) {
y = quantile( p[i], alpha[i], beta[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 20 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function evaluates the quantile accurately for small `p`', opts, function test( t ) {
var expected;
var alpha;
var beta;
var i;
var p;
var y;

expected = smallP.expected;
p = smallP.p;
alpha = smallP.alpha;
beta = smallP.beta;
for ( i = 0; i < p.length; i++ ) {
y = quantile( p[i], alpha[i], beta[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 20 ), true, 'returns expected value' );
}
t.end();
});
Loading