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

var constantFunction = require( '@stdlib/utils/constant-function' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var ln = require( '@stdlib/math/base/special/ln' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var PINF = require( '@stdlib/constants/float64/pinf' );


Expand Down Expand Up @@ -63,7 +63,7 @@ function factory( lambda ) {
if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
return NaN;
}
return -ln( 1.0 - p ) / lambda;
return -log1p( -p ) / lambda;
}
}

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

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


Expand Down Expand Up @@ -74,7 +74,7 @@ function quantile( p, lambda ) {
) {
return NaN;
}
return -ln( 1.0 - p ) / lambda;
return -log1p( -p ) / lambda;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/pinf",
"@stdlib/math/base/special/ln"
"@stdlib/math/base/special/log1p"
]
},
{
Expand All @@ -59,7 +59,7 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/eps",
"@stdlib/constants/float64/pinf",
"@stdlib/math/base/special/ln"
"@stdlib/math/base/special/log1p"
]
},
{
Expand All @@ -76,7 +76,7 @@
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/pinf",
"@stdlib/math/base/special/ln"
"@stdlib/math/base/special/log1p"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "stdlib/stats/base/dists/exponential/quantile.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/constants/float64/pinf.h"
#include "stdlib/math/base/special/ln.h"
#include "stdlib/math/base/special/log1p.h"

/**
* Evaluates the quantile function for an exponential distribution with rate parameter `lambda` at a probability `p`.
Expand All @@ -42,5 +42,5 @@ double stdlib_base_dists_exponential_quantile( const double p, const double lamb
) {
return 0.0 / 0.0; // NaN
}
return -stdlib_base_ln( 1.0 - p ) / lambda;
return -stdlib_base_log1p( -p ) / lambda;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
julia 1.5
JSON 0.21
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env julia
#
# @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.

import JSON

"""
gen( p, lambda, name )

Generate fixture data and write to file.

The quantile is evaluated in extended precision from the textbook formula `-ln(1-p) / lambda`, as forming `1-p` in double precision rounds away the low-order bits of `p` (all of them for `p < 2^-53`).

# Arguments

* `p`: input value
* `lambda`: rate parameter
* `name::AbstractString`: output filename

# Examples

``` julia
julia> p = rand( 1000 );
julia> lambda = rand( 1000 ) .* 100.0;
julia> gen( p, lambda, "data.json" );
```
"""
function gen( p, lambda, name )
z = setprecision( BigFloat, 2048 ) do
Float64.( .-log.( 1 .- BigFloat.( p ) ) ./ BigFloat.( lambda ) )
end

# Store data to be written to file as a collection:
data = Dict([
("p", p),
("lambda", lambda),
("expected", z)
]);

# Based on the script directory, create an output filepath:
filepath = joinpath( dir, name );

# Write the data to the output filepath as JSON:
outfile = open( filepath, "w" );
write( outfile, JSON.json(data) );
write( outfile, "\n" );
close( outfile );
end

# Get the filename:
file = @__FILE__;

# Extract the directory in which this file resides:
dir = dirname( file );

# Small `p`:
p = exp10.( range( -300.0, stop = -1.0, length = 1000 ) );
lambda = ( rand( 1000 ) .* 100.0 ) .+ 0.1;
gen( p, lambda, "small_p.json" );

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var factory = require( './../lib/factory.js' );
// FIXTURES //

var data = require( './fixtures/r/data.json' );
var smallP = require( './fixtures/julia/small_p.json' );
var i;
for ( i = 0; i < data.expected.length; i++ ) {
if ( data.expected[ i ] === 'Inf' ) {
Expand Down Expand Up @@ -135,7 +136,26 @@ tape( 'the created function evaluates the quantile for `x` given parameter `lamb
for ( i = 0; i < x.length; i++ ) {
quantile = factory( lambda[i] );
y = quantile( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 172 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the created function evaluates the quantile for small `x`', function test( t ) {
var expected;
var quantile;
var lambda;
var x;
var y;
var i;

expected = smallP.expected;
x = smallP.p;
lambda = smallP.lambda;
for ( i = 0; i < x.length; i++ ) {
quantile = factory( lambda[i] );
y = quantile( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var opts = {
// FIXTURES //

var data = require( './fixtures/r/data.json' );
var smallP = require( './fixtures/julia/small_p.json' );
var i;
for ( i = 0; i < data.expected.length; i++ ) {
if ( data.expected[ i ] === 'Inf' ) {
Expand Down Expand Up @@ -120,7 +121,24 @@ tape( 'the function evaluates the quantile for `x` given parameter `lambda`', op
lambda = data.lambda;
for ( i = 0; i < x.length; i++ ) {
y = quantile( x[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 172 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function evaluates the quantile for small `x`', opts, function test( t ) {
var expected;
var lambda;
var x;
var y;
var i;

expected = smallP.expected;
x = smallP.p;
lambda = smallP.lambda;
for ( i = 0; i < x.length; i++ ) {
y = quantile( x[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var quantile = require( './../lib' );
// FIXTURES //

var data = require( './fixtures/r/data.json' );
var smallP = require( './fixtures/julia/small_p.json' );
var i;
for ( i = 0; i < data.expected.length; i++ ) {
if ( data.expected[ i ] === 'Inf' ) {
Expand Down Expand Up @@ -111,7 +112,24 @@ tape( 'the function evaluates the quantile for `x` given parameter `lambda`', fu
lambda = data.lambda;
for ( i = 0; i < x.length; i++ ) {
y = quantile( x[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 172 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function evaluates the quantile for small `x`', function test( t ) {
var expected;
var lambda;
var x;
var y;
var i;

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