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,8 +22,9 @@

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 exp = require( '@stdlib/math/base/special/exp' );
var log1mexp = require( '@stdlib/math/base/special/log1mexp' );
var ln = require( '@stdlib/math/base/special/ln' );
var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PINF = require( '@stdlib/constants/float64/pinf' );

Expand Down Expand Up @@ -66,10 +67,15 @@ function factory( lambda ) {
* // returns <number>
*/
function logcdf( x ) {
var t;
if ( x < 0.0 ) {
return NINF;
}
return log1p( -exp( -lambda * x ) );
t = lambda * x;
if ( t < FLOAT64_SMALLEST_NORMAL ) {
return ln( x ) + ln( lambda );
}
return log1mexp( t );
}
}

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

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var exp = require( '@stdlib/math/base/special/exp' );
var log1mexp = require( '@stdlib/math/base/special/log1mexp' );
var ln = require( '@stdlib/math/base/special/ln' );
var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PINF = require( '@stdlib/constants/float64/pinf' );

Expand Down Expand Up @@ -62,6 +63,7 @@ var PINF = require( '@stdlib/constants/float64/pinf' );
* // returns NaN
*/
function logcdf( x, lambda ) {
var t;
if (
isnan( lambda ) ||
lambda < 0.0 ||
Expand All @@ -72,7 +74,11 @@ function logcdf( x, lambda ) {
if ( x < 0.0 ) {
return NINF;
}
return log1p( -exp( -lambda * x ) );
t = lambda * x;
if ( t < FLOAT64_SMALLEST_NORMAL ) {
return ln( x ) + ln( lambda );
}
return log1mexp( t );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/ninf",
"@stdlib/math/base/special/log1p"
"@stdlib/math/base/special/log1mexp",
"@stdlib/math/base/special/ln",
"@stdlib/constants/float64/smallest-normal"
]
},
{
Expand All @@ -60,10 +61,11 @@
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/ninf",
"@stdlib/math/base/special/log1p"
"@stdlib/math/base/special/log1mexp",
"@stdlib/math/base/special/ln",
"@stdlib/constants/float64/smallest-normal"
]
},
{
Expand All @@ -80,10 +82,11 @@
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/pinf",
"@stdlib/constants/float64/ninf",
"@stdlib/math/base/special/log1p"
"@stdlib/math/base/special/log1mexp",
"@stdlib/math/base/special/ln",
"@stdlib/constants/float64/smallest-normal"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

#include "stdlib/stats/base/dists/exponential/logcdf.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/log1p.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/smallest_normal.h"
#include "stdlib/constants/float64/pinf.h"
#include "stdlib/constants/float64/ninf.h"

Expand All @@ -35,6 +36,7 @@
* // returns ~-1.708
*/
double stdlib_base_dists_exponential_logcdf( const double x, const double lambda ) {
double t;
if (
stdlib_base_is_nan( x ) ||
stdlib_base_is_nan( lambda ) ||
Expand All @@ -46,5 +48,9 @@ double stdlib_base_dists_exponential_logcdf( const double x, const double lambda
if ( x < 0.0 ) {
return STDLIB_CONSTANT_FLOAT64_NINF;
}
return stdlib_base_log1p( -stdlib_base_exp( -lambda * x ) );
t = lambda * x;
if ( t < STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) {
return stdlib_base_ln( x ) + stdlib_base_ln( lambda );
}
return stdlib_base_log1mexp( t );
}
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,65 @@
#!/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( x, lambda, name )

Generate fixture data and write to file.

The logarithm of the CDF is evaluated in extended precision from the textbook formula `ln(1 - exp(-lambda*x))`, as forming `1 - exp(-lambda*x)` in double precision cancels for small `lambda*x` (to `0` for `lambda*x < 2^-53`).

# Arguments

* `x`: input value
* `lambda`: rate parameter
* `name::AbstractString`: output filename
"""
function gen( x, lambda, name )
z = setprecision( BigFloat, 2048 ) do
Float64.( log.( 1 .- exp.( .-BigFloat.( lambda ) .* BigFloat.( x ) ) ) )
end

# Store data to be written to file as a collection:
data = Dict([
("x", x),
("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 `x`:
x = exp10.( range( -300.0, stop = -1.0, length = 1000 ) );
lambda = exp10.( ( rand( 1000 ) .* 41.0 ) .- 40.0 );
gen( x, lambda, "small_x.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 smallX = require( './fixtures/julia/small_x.json' );


// TESTS //
Expand Down Expand Up @@ -138,3 +139,22 @@ tape( 'the created function evaluates the logcdf for `x` given parameter `lambda
}
t.end();
});

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

expected = smallX.expected;
x = smallX.x;
lambda = smallX.lambda;
for ( i = 0; i < expected.length; i++ ) {
logcdf = factory( lambda[i] );
y = logcdf( 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 @@ -31,6 +31,7 @@ var logcdf = require( './../lib' );
// FIXTURES //

var data = require( './fixtures/r/data.json' );
var smallX = require( './fixtures/julia/small_x.json' );


// TESTS //
Expand Down Expand Up @@ -110,3 +111,20 @@ tape( 'the function evaluates the logcdf for `x` given parameter `lambda`', func
}
t.end();
});

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

expected = smallX.expected;
x = smallX.x;
lambda = smallX.lambda;
for ( i = 0; i < expected.length; i++ ) {
y = logcdf( 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 @@ -32,6 +32,7 @@ var NINF = require( '@stdlib/constants/float64/ninf' );
// FIXTURES //

var data = require( './fixtures/r/data.json' );
var smallX = require( './fixtures/julia/small_x.json' );


// VARIABLES //
Expand Down Expand Up @@ -119,3 +120,20 @@ tape( 'the function evaluates the logcdf for `x` given parameter `lambda`', opts
}
t.end();
});

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

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