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 @@ -68,7 +68,7 @@ y = logcdf( -Infinity, 4.0, 2.0 );
// returns -Infinity

y = logcdf( +Infinity, 4.0, 2.0 );
// returns 0.0
// returns -0.0
```

If provided `NaN` as any argument, the function returns `NaN`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
> y = {{alias}}( -1.0, 2.0, 2.0 )
-Infinity
> y = {{alias}}( {{alias:@stdlib/constants/float64/pinf}}, 4.0, 2.0 )
0.0
-0.0
> y = {{alias}}( {{alias:@stdlib/constants/float64/ninf}}, 4.0, 2.0 )
-Infinity
> y = {{alias}}( NaN, 0.0, 1.0 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interface LogCDF {
*
* @example
* var y = logcdf( +Infinity, 4.0, 2.0 );
* // returns 0.0
* // returns -0.0
*
* @example
* var y = logcdf( -Infinity, 4.0, 2.0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var exp = require( '@stdlib/math/base/special/exp' );
var pow = require( '@stdlib/math/base/special/pow' );
var ln = require( '@stdlib/math/base/special/ln' );
var LNHALF = require( '@stdlib/constants/float64/ln-half' );
var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' );
var NINF = require( '@stdlib/constants/float64/ninf' );


Expand Down Expand Up @@ -71,13 +72,25 @@ function factory( k, lambda ) {
* // returns <number>
*/
function logcdf( x ) {
var lt;
var r;
var p;
if ( isnan( x ) ) {
return NaN;
}
if ( x < 0.0 ) {
return NINF;
}
r = x / lambda;
if ( r < FLOAT64_SMALLEST_NORMAL ) {
lt = k * ( ln( x ) - ln( lambda ) );
} else {
lt = k * ln( r );
}
if ( lt < -36.0 ) {
// For `t = (x/lambda)^k < e^-36`, `ln(1 - e^-t) = ln(t) + ln(1 - t/2 + ...)` and the correction is below half an ULP of `ln(t)`. Using `ln(t)` directly avoids `t` underflowing to `0`:
return lt;
}
p = -pow( x / lambda, k );
return ( p < LNHALF ) ? log1p( -exp( p ) ) : ln( -expm1( p ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var exp = require( '@stdlib/math/base/special/exp' );
var pow = require( '@stdlib/math/base/special/pow' );
var ln = require( '@stdlib/math/base/special/ln' );
var LNHALF = require( '@stdlib/constants/float64/ln-half' );
var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' );
var NINF = require( '@stdlib/constants/float64/ninf' );


Expand All @@ -50,7 +51,7 @@ var NINF = require( '@stdlib/constants/float64/ninf' );
*
* @example
* var y = logcdf( +Infinity, 4.0, 2.0 );
* // returns 0.0
* // returns -0.0
*
* @example
* var y = logcdf( -Infinity, 4.0, 2.0 );
Expand All @@ -73,6 +74,8 @@ var NINF = require( '@stdlib/constants/float64/ninf' );
* // returns NaN
*/
function logcdf( x, k, lambda ) {
var lt;
var r;
var p;
if (
isnan( k ) ||
Expand All @@ -85,6 +88,16 @@ function logcdf( x, k, lambda ) {
if ( x < 0.0 ) {
return NINF;
}
r = x / lambda;
if ( r < FLOAT64_SMALLEST_NORMAL ) {
lt = k * ( ln( x ) - ln( lambda ) );
} else {
lt = k * ln( r );
}
if ( lt < -36.0 ) {
// For `t = (x/lambda)^k < e^-36`, `ln(1 - e^-t) = ln(t) + ln(1 - t/2 + ...)` and the correction is below half an ULP of `ln(t)`. Using `ln(t)` directly avoids `t` underflowing to `0`:
return lt;
}
p = -pow( x / lambda, k );
return ( p < LNHALF ) ? log1p( -exp( p ) ) : ln( -expm1( p ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var addon = require( './../src/addon.node' );
*
* @example
* var y = logcdf( +Infinity, 4.0, 2.0 );
* // returns 0.0
* // returns -0.0
*
* @example
* var y = logcdf( -Infinity, 4.0, 2.0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@stdlib/math/base/special/ln",
"@stdlib/math/base/special/pow",
"@stdlib/constants/float64/ln-half",
"@stdlib/constants/float64/smallest-normal",
"@stdlib/constants/float64/ninf"
]
},
Expand All @@ -71,6 +72,7 @@
"@stdlib/math/base/special/ln",
"@stdlib/math/base/special/pow",
"@stdlib/constants/float64/ln-half",
"@stdlib/constants/float64/smallest-normal",
"@stdlib/constants/float64/ninf"
]
},
Expand All @@ -94,6 +96,7 @@
"@stdlib/math/base/special/ln",
"@stdlib/math/base/special/pow",
"@stdlib/constants/float64/ln-half",
"@stdlib/constants/float64/smallest-normal",
"@stdlib/constants/float64/ninf"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/ln.h"
#include "stdlib/constants/float64/ln_half.h"
#include "stdlib/constants/float64/smallest_normal.h"
#include "stdlib/constants/float64/ninf.h"

/**
Expand All @@ -39,6 +40,8 @@
* // returns ~-0.145
*/
double stdlib_base_dists_weibull_logcdf( const double x, const double k, const double lambda ) {
double lt;
double r;
double p;
if (
stdlib_base_is_nan( x ) ||
Expand All @@ -51,6 +54,16 @@ double stdlib_base_dists_weibull_logcdf( const double x, const double k, const d
if ( x < 0.0 ) {
return STDLIB_CONSTANT_FLOAT64_NINF;
}
r = x / lambda;
if ( r < STDLIB_CONSTANT_FLOAT64_SMALLEST_NORMAL ) {
lt = k * ( stdlib_base_ln( x ) - stdlib_base_ln( lambda ) );
} else {
lt = k * stdlib_base_ln( r );
}
if ( lt < -36.0 ) {
// For `t = (x/lambda)^k < e^-36`, `ln(1 - e^-t) = ln(t) + ln(1 - t/2 + ...)` and the correction is below half an ULP of `ln(t)`. Using `ln(t)` directly avoids `t` underflowing to `0`:
return lt;
}
p = -stdlib_base_pow( x / lambda, k );
return ( p < STDLIB_CONSTANT_FLOAT64_LN_HALF ) ? stdlib_base_log1p( -stdlib_base_exp( p ) ) : stdlib_base_ln( -stdlib_base_expm1( p ) );
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Distributions 0.23.8
julia 1.5
JSON 0.21

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import Distributions: logcdf, Weibull
import JSON

"""
gen( x, k, 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(-(x/lambda)^k))`, written with `expm1` so that it stays exact when `(x/lambda)^k` is far below the working precision, as `(x/lambda)^k` underflows in double precision for small `x`.

# Arguments

* `x`: input value
Expand All @@ -41,9 +42,8 @@ julia> gen( x, k, lambda, "data.json" );
```
"""
function gen( x, k, lambda, name )
z = Array{Float64}( undef, length(x) );
for i in eachindex(x)
z[ i ] = logcdf( Weibull( k[i], lambda[i] ), x[i] );
z = setprecision( BigFloat, 2048 ) do
Float64.( log.( .-expm1.( .-( ( BigFloat.( x ) ./ BigFloat.( lambda ) ) .^ BigFloat.( k ) ) ) ) )
end

# Store data to be written to file as a collection:
Expand Down Expand Up @@ -87,3 +87,9 @@ x = rand( 1000 ) .* 5.0;
lambda = ( rand( 1000 ) .* 5.0 ) .+ 10.0;
k = ( rand( 1000 ) .* 5.0 ) .+ 10.0;
gen( x, k, lambda, "both_large.json" );

# Small `x`:
x = exp10.( range( -323.0, stop = -2.0, length = 1000 ) );
lambda = ( rand( 1000 ) .* 5.0 ) .+ 0.5;
k = ( rand( 1000 ) .* 5.0 ) .+ 0.5;
gen( x, k, 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 largeScale = require( './fixtures/julia/large_scale.json' );
var smallX = require( './fixtures/julia/small_x.json' );
var largeShape = require( './fixtures/julia/large_shape.json' );
var bothLarge = require( './fixtures/julia/both_large.json' );

Expand Down Expand Up @@ -192,7 +193,7 @@ tape( 'the created function evaluates the logcdf for `x` given large `lambda` an
for ( i = 0; i < x.length; i++ ) {
logcdf = factory( k[i], lambda[i] );
y = logcdf( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -213,7 +214,7 @@ tape( 'the created function evaluates the logcdf for `x` given large shape param
for ( i = 0; i < x.length; i++ ) {
logcdf = factory( k[i], lambda[i] );
y = logcdf( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3236 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -234,7 +235,28 @@ tape( 'the created function evaluates the logcdf for `x` given large scale param
for ( i = 0; i < x.length; i++ ) {
logcdf = factory( k[i], lambda[i] );
y = logcdf( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

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

expected = smallX.expected;
x = smallX.x;
lambda = smallX.lambda;
k = smallX.k;
for ( i = 0; i < x.length; i++ ) {
logcdf = factory( k[i], 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 largeScale = require( './fixtures/julia/large_scale.json' );
var smallX = require( './fixtures/julia/small_x.json' );
var largeShape = require( './fixtures/julia/large_shape.json' );
var bothLarge = require( './fixtures/julia/both_large.json' );

Expand Down Expand Up @@ -133,7 +134,7 @@ tape( 'the function evaluates the logcdf for `x` given large `lambda` and `k`',
k = bothLarge.k;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], k[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -152,7 +153,7 @@ tape( 'the function evaluates the logcdf for `x` given large shape parameter `la
k = largeShape.k;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], k[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3236 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -171,7 +172,26 @@ tape( 'the function evaluates the logcdf for `x` given large scale parameter `k`
k = largeScale.k;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], k[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

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

expected = smallX.expected;
x = smallX.x;
lambda = smallX.lambda;
k = smallX.k;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], k[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 largeScale = require( './fixtures/julia/large_scale.json' );
var smallX = require( './fixtures/julia/small_x.json' );
var largeShape = require( './fixtures/julia/large_shape.json' );
var bothLarge = require( './fixtures/julia/both_large.json' );

Expand Down Expand Up @@ -142,7 +143,7 @@ tape( 'the function evaluates the logcdf for `x` given large `lambda` and `k`',
k = bothLarge.k;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], k[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -161,7 +162,7 @@ tape( 'the function evaluates the logcdf for `x` given large shape parameter `la
k = largeShape.k;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], k[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3236 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -180,7 +181,26 @@ tape( 'the function evaluates the logcdf for `x` given large scale parameter `k`
k = largeScale.k;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], k[i], lambda[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

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

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