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 @@ -23,7 +23,8 @@
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 pow = require( '@stdlib/math/base/special/pow' );
var expm1 = require( '@stdlib/math/base/special/expm1' );
var log1p = require( '@stdlib/math/base/special/log1p' );
var PINF = require( '@stdlib/constants/float64/pinf' );


Expand Down Expand Up @@ -71,7 +72,7 @@ function factory( p ) {
return 1.0;
}
x = floor( x );
return 1.0 - pow( 1.0 - p, x + 1.0 );
return -expm1( ( x+1.0 ) * log1p( -p ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

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


Expand Down Expand Up @@ -76,7 +77,7 @@ function cdf( x, p ) {
return 1.0;
}
x = floor( x );
return 1.0 - pow( 1.0 - p, x + 1.0 );
return -expm1( ( x+1.0 ) * log1p( -p ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/pinf",
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/pow"
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/log1p"
]
},
{
Expand All @@ -61,7 +62,8 @@
"@stdlib/constants/float64/pinf",
"@stdlib/math/base/special/floor",
"@stdlib/constants/float64/eps",
"@stdlib/math/base/special/pow"
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/log1p"
]
},
{
Expand All @@ -79,7 +81,8 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/constants/float64/pinf",
"@stdlib/math/base/special/floor",
"@stdlib/math/base/special/pow"
"@stdlib/math/base/special/expm1",
"@stdlib/math/base/special/log1p"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/constants/float64/pinf.h"
#include "stdlib/math/base/special/floor.h"
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/expm1.h"
#include "stdlib/math/base/special/log1p.h"

/**
* Evaluates the cumulative distribution function (CDF) for a geometric distribution with success probability `p` at a value `x`.
Expand Down Expand Up @@ -48,5 +49,5 @@ double stdlib_base_dists_geometric_cdf( const double x, const double p ) {
if ( x == STDLIB_CONSTANT_FLOAT64_PINF ) {
return 1.0;
}
return 1.0 - stdlib_base_pow( 1.0 - p, stdlib_base_floor( x ) + 1.0 );
return -stdlib_base_expm1( ( stdlib_base_floor( x ) + 1.0 ) * stdlib_base_log1p( -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.

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

"""
gen( x, p, name )

Generate fixture data and write to file.

The CDF is evaluated in extended precision from the textbook formula `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 ] = cdf( Geometric( p[i] ), x[i] );
z = setprecision( BigFloat, 2048 ) do
Float64.( 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,13 @@ 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;

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.

Non-blocking: with x <= 100, every tiny_p case with p < 2^-53 gives a result below 1e-14, so results of order 1 aren't tested (e.g. cdf( 1e20, 1e-20 ) is 0.632, develop gives 0). Worth adding a few cases with x near 1/p? x = rand( 1000 ) .* 3.0 ./ p passes at 1 ULP.

p = exp10.( range( -300.0, stop = -1.0, length = 1000 ) );
gen( x, p, "tiny_p.json" );

# Tiny `p` with `x` near `1/p`, where the CDF is of order `1`:
p = exp10.( range( -300.0, stop = -1.0, length = 1000 ) );
x = floor.( rand( 1000 ) .* 3.0 ./ p );
gen( x, p, "tiny_p_large_x.json" );

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

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


Expand Down Expand Up @@ -99,7 +101,7 @@ tape( 'the function evaluates the cdf for `x` given small parameter `p`', functi
p = smallP.p;
for ( i = 0; i < x.length; i++ ) {
y = cdf( x[i], p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 20 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -120,3 +122,37 @@ tape( 'the function evaluates the cdf for `x` given large parameter `p`', functi
}
t.end();
});

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

tape( 'the function evaluates the cdf for `x` given tiny parameter `p` and `x` near `1/p`', function test( t ) {
var expected;
var x;
var p;
var y;
var i;

expected = tinyPLargeX.expected;
x = tinyPLargeX.x;
p = tinyPLargeX.p;
for ( i = 0; i < x.length; i++ ) {
y = cdf( x[i], p[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,8 @@ var factory = require( './../lib/factory.js' );
// FIXTURES //

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


Expand Down Expand Up @@ -133,7 +135,7 @@ tape( 'the created function evaluates the cdf for `x` given small parameter `p`'
for ( i = 0; i < x.length; i++ ) {
cdf = factory( p[i] );
y = cdf( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 20 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -156,3 +158,41 @@ tape( 'the created function evaluates the cdf for `x` given large parameter `p`'
}
t.end();
});

tape( 'the created function evaluates the cdf for `x` given tiny parameter `p`', function test( t ) {
var expected;
var cdf;
var x;
var p;
var y;
var i;

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

tape( 'the created function evaluates the cdf for `x` given tiny parameter `p` and `x` near `1/p`', function test( t ) {
var expected;
var cdf;
var x;
var p;
var y;
var i;

expected = tinyPLargeX.expected;
x = tinyPLargeX.x;
p = tinyPLargeX.p;
for ( i = 0; i < x.length; i++ ) {
cdf = factory( p[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 @@ -32,6 +32,8 @@ 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 tinyPLargeX = require( './fixtures/julia/tiny_p_large_x.json' );
var largeP = require( './fixtures/julia/large_p.json' );


Expand Down Expand Up @@ -108,7 +110,7 @@ tape( 'the function evaluates the cdf for `x` given small parameter `p`', opts,
p = smallP.p;
for ( i = 0; i < x.length; i++ ) {
y = cdf( x[i], p[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 20 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -129,3 +131,37 @@ tape( 'the function evaluates the cdf for `x` given large parameter `p`', opts,
}
t.end();
});

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

tape( 'the function evaluates the cdf for `x` given tiny parameter `p` and `x` near `1/p`', opts, function test( t ) {
var expected;
var x;
var p;
var y;
var i;

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