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 @@ -24,6 +24,7 @@ var constantFunction = require( '@stdlib/utils/constant-function' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var floor = require( '@stdlib/math/base/special/floor' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var log1mexp = require( '@stdlib/math/base/special/log1mexp' );
var pow = require( '@stdlib/math/base/special/pow' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PINF = require( '@stdlib/constants/float64/pinf' );
Expand Down Expand Up @@ -73,7 +74,12 @@ function factory( p ) {
return 0.0;
}
x = floor( x );
return log1p( -pow( 1.0 - p, x + 1.0 ) );
if ( p < 0.5 ) {
// Forming `1-p` would round away the low-order bits of `p`:
return log1mexp( ( x+1.0 ) * log1p( -p ) );
}
// `1-p` is exact for `p >= 0.5`:
return log1p( -pow( 1.0-p, x+1.0 ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var floor = require( '@stdlib/math/base/special/floor' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var log1mexp = require( '@stdlib/math/base/special/log1mexp' );
var pow = require( '@stdlib/math/base/special/pow' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PINF = require( '@stdlib/constants/float64/pinf' );
Expand Down Expand Up @@ -78,7 +79,12 @@ function logcdf( x, p ) {
return 0.0;
}
x = floor( x );
return log1p( -pow( 1.0 - p, x + 1.0 ) );
if ( p < 0.5 ) {
// Forming `1-p` would round away the low-order bits of `p`:
return log1mexp( ( x+1.0 ) * log1p( -p ) );
}
// `1-p` is exact for `p >= 0.5`:
return log1p( -pow( 1.0-p, x+1.0 ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@stdlib/constants/float64/ninf",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/log1mexp",
"@stdlib/math/base/special/pow"
]
},
Expand All @@ -64,6 +65,7 @@
"@stdlib/constants/float64/ninf",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/log1mexp",
"@stdlib/math/base/special/pow",
"@stdlib/constants/float64/eps"
]
Expand All @@ -85,6 +87,7 @@
"@stdlib/constants/float64/ninf",
"@stdlib/math/base/special/log1p",
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/log1mexp",
"@stdlib/math/base/special/pow"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "stdlib/constants/float64/ninf.h"
#include "stdlib/math/base/special/log1p.h"
#include "stdlib/math/base/special/floor.h"
#include "stdlib/math/base/special/log1mexp.h"
#include "stdlib/math/base/special/pow.h"

/**
Expand Down Expand Up @@ -50,5 +51,10 @@ double stdlib_base_dists_geometric_logcdf( const double x, const double p ) {
if ( x == STDLIB_CONSTANT_FLOAT64_PINF ) {
return 0.0;
}
if ( p < 0.5 ) {
// Forming `1-p` would round away the low-order bits of `p`:
return stdlib_base_log1mexp( ( stdlib_base_floor( x ) + 1.0 ) * stdlib_base_log1p( -p ) );
}
// `1-p` is exact for `p >= 0.5`:
return stdlib_base_log1p( -stdlib_base_pow( 1.0 - p, stdlib_base_floor( x ) + 1.0 ) );
}
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.

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, Geometric
import JSON

"""
gen( x, p, name )

Generate fixture data and write to file.

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

# Arguments

* `x`: input value
Expand All @@ -39,9 +40,8 @@ julia> gen( x, p, "data.json" );
```
"""
function gen( x, p, name )
z = Array{Float64}( undef, length(x) );
for i in eachindex(x)
z[ i ] = logcdf( Geometric( p[i] ), x[i] );
z = setprecision( BigFloat, 2048 ) do
Float64.( log.( 1 .- ( 1 .- BigFloat.( p ) ) .^ ( floor.( x ) .+ 1 ) ) )
end

# Store data to be written to file as a collection:
Expand Down Expand Up @@ -76,3 +76,8 @@ gen( x, p, "large_p.json" );
x = rand( 1000 ) .* 10.0;
p = rand( 1000 ) .* 0.5;
gen( x, p, "small_p.json" );

# Tiny `p`:
x = rand( 1000 ) .* 100.0;
p = exp10.( range( -300.0, stop = -1.0, length = 1000 ) );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p = exp10.( range( -300.0, stop = -1.0, length = 1000 ) );
p = exp10.( range( -323.0, stop = -1.0, length = 1000 ) );

Non-blocking: this also covers subnormal p, and the tests still pass. Unlike #15797 (comment), no extra branch is needed here, since x+1 >= 1 keeps (x+1)*log1p(-p) exact.

gen( x, p, "tiny_p.json" );

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 @@ -31,6 +31,7 @@ var factory = require( './../lib/factory.js' );
// FIXTURES //

var smallP = require( './fixtures/julia/small_p.json' );
var tinyP = require( './fixtures/julia/tiny_p.json' );
var largeP = require( './fixtures/julia/large_p.json' );


Expand Down Expand Up @@ -132,7 +133,7 @@ tape( 'the created function evaluates the logcdf for `x` given small parameter `
for ( i = 0; i < x.length; i++ ) {
logcdf = factory( p[i] );
y = logcdf( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 9 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -151,7 +152,26 @@ tape( 'the created function evaluates the logcdf for `x` given large parameter `
for ( i = 0; i < x.length; i++ ) {
logcdf = factory( p[i] );
y = logcdf( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 42 ), 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` given tiny parameter `p`', function test( t ) {
var expected;
var logcdf;
var x;
var p;
var y;
var i;

expected = tinyP.expected;
x = tinyP.x;
p = tinyP.p;
for ( i = 0; i < x.length; i++ ) {
logcdf = factory( p[i] );
y = logcdf( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), 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 smallP = require( './fixtures/julia/small_p.json' );
var tinyP = require( './fixtures/julia/tiny_p.json' );
var largeP = require( './fixtures/julia/large_p.json' );


Expand Down Expand Up @@ -99,7 +100,7 @@ tape( 'the function evaluates the logcdf for `x` given small parameter `p`', fun
p = smallP.p;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 9 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -116,7 +117,24 @@ tape( 'the function evaluates the logcdf for `x` given large parameter `p`', fun
p = largeP.p;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 42 ), 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` given tiny parameter `p`', function test( t ) {
var expected;
var x;
var p;
var y;
var i;

expected = tinyP.expected;
x = tinyP.x;
p = tinyP.p;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var tryRequire = require( '@stdlib/utils/try-require' );
// FIXTURES //

var smallP = require( './fixtures/julia/small_p.json' );
var tinyP = require( './fixtures/julia/tiny_p.json' );
var largeP = require( './fixtures/julia/large_p.json' );


Expand Down Expand Up @@ -108,7 +109,7 @@ tape( 'the function evaluates the logcdf for `x` given small parameter `p`', opt
p = smallP.p;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 9 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -125,7 +126,24 @@ tape( 'the function evaluates the logcdf for `x` given large parameter `p`', opt
p = largeP.p;
for ( i = 0; i < x.length; i++ ) {
y = logcdf( x[i], p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 42 ), 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` given tiny parameter `p`', opts, function test( t ) {
var expected;
var x;
var p;
var y;
var i;

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