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


Expand Down Expand Up @@ -64,10 +64,10 @@ function factory( lambda ) {
* // returns <number>
*/
function cdf( x ) {
if ( x < 0.0 ) {
if ( x <= 0.0 ) {
return 0.0;
}
return 1.0 - exp( -lambda * x );
return -expm1( -lambda * x );
}
}

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


Expand Down Expand Up @@ -67,10 +67,10 @@ function cdf( x, lambda ) {
) {
return NaN;
}
if ( x < 0.0 ) {
if ( x <= 0.0 ) {
return 0.0;
}
return 1.0 - exp( -lambda * x );
return -expm1( -lambda * x );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/expm1",
"@stdlib/constants/float64/pinf"
]
},
Expand All @@ -58,7 +58,7 @@
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/expm1",
"@stdlib/constants/float64/pinf"
]
},
Expand All @@ -76,7 +76,7 @@
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/exp",
"@stdlib/math/base/special/expm1",
"@stdlib/constants/float64/pinf"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "stdlib/stats/base/dists/exponential/cdf.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/expm1.h"
#include "stdlib/constants/float64/pinf.h"

/**
Expand All @@ -41,8 +41,8 @@ double stdlib_base_dists_exponential_cdf( const double x, const double lambda )
) {
return 0.0/0.0; // NaN
}
if ( x < 0.0 ) {
if ( x <= 0.0 ) {
return 0.0;
}
return 1.0 - stdlib_base_exp( -lambda * x );
return -stdlib_base_expm1( -lambda * x );
}
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 CDF is evaluated in extended precision from the textbook formula `1 - exp(-lambda*x)`, as subtracting `exp(-lambda*x)` from `1` 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.( 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 = ( rand( 1000 ) .* 10.0 ) .+ 0.1;
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 @@ -23,6 +23,7 @@
var tape = require( 'tape' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var cdf = require( './../lib' );
Expand All @@ -31,6 +32,7 @@ var cdf = require( './../lib' );
// FIXTURES //

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


// TESTS //
Expand Down Expand Up @@ -61,6 +63,18 @@ tape( 'if provided `-infinity` for `x` and a finite `lambda`, the function retur
t.end();
});

tape( 'if provided `-0` or `+0` for `x` and a finite `lambda`, the function returns `+0`', function test( t ) {
var y;

y = cdf( -0.0, 1.0 );
t.strictEqual( isPositiveZero( y ), true, 'returns expected value' );

y = cdf( 0.0, 1.0 );
t.strictEqual( isPositiveZero( y ), true, 'returns expected value' );

t.end();
});

tape( 'if provided `+infinity` for `lambda`, the function returns `NaN`', function test( t ) {
var y;

Expand Down Expand Up @@ -110,3 +124,20 @@ tape( 'the function evaluates the cdf for `x` given parameter `lambda`', functio
}
t.end();
});

tape( 'the function evaluates the cdf 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 = cdf( 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 @@ -23,6 +23,7 @@
var tape = require( 'tape' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var factory = require( './../lib/factory.js' );
Expand All @@ -31,6 +32,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 @@ -84,6 +86,21 @@ tape( 'if provided a finite `lambda`, the function returns a function which retu
t.end();
});

tape( 'if provided a finite `lambda`, the function returns a function which returns `+0` when provided `-0` or `+0` for `x`', function test( t ) {
var cdf;
var y;

cdf = factory( 1.0 );

y = cdf( -0.0 );
t.strictEqual( isPositiveZero( y ), true, 'returns expected value' );

y = cdf( 0.0 );
t.strictEqual( isPositiveZero( y ), true, 'returns expected value' );

t.end();
});

tape( 'if provided `+infinity` for `lambda`, the function returns a function which returns `NaN` for any `x`', function test( t ) {
var cdf;
var y;
Expand Down Expand Up @@ -138,3 +155,22 @@ tape( 'the created function evaluates the cdf for `x` given parameter `lambda`',
}
t.end();
});

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

expected = smallX.expected;
x = smallX.x;
lambda = smallX.lambda;
for ( i = 0; i < expected.length; i++ ) {
cdf = factory( lambda[i] );
y = cdf( 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 @@ -25,13 +25,15 @@ var tape = require( 'tape' );
var tryRequire = require( '@stdlib/utils/try-require' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
var PINF = require( '@stdlib/constants/float64/pinf' );
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 @@ -70,6 +72,18 @@ tape( 'if provided `-infinity` for `x` and a finite `lambda`, the function retur
t.end();
});

tape( 'if provided `-0` or `+0` for `x` and a finite `lambda`, the function returns `+0`', opts, function test( t ) {
var y;

y = cdf( -0.0, 1.0 );
t.strictEqual( isPositiveZero( y ), true, 'returns expected value' );

y = cdf( 0.0, 1.0 );
t.strictEqual( isPositiveZero( y ), true, 'returns expected value' );

t.end();
});

tape( 'if provided `+infinity` for `lambda`, the function returns `NaN`', opts, function test( t ) {
var y;

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

tape( 'the function evaluates the cdf 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 = cdf( x[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Loading