Skip to content

bench: update random value generation #6853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
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 @@ -21,9 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );
Expand All @@ -32,26 +31,23 @@ var cdf = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var len;
var opts;
var n;
var p;
var x;
var y;
var i;

len = 100;
x = new Float64Array( len );
n = new Float64Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 100.0 );
n[ i ] = discreteUniform( 1, 100 );
p[ i ] = uniform( 0.0, 1.0 );
}
opts = {
'dtype': 'float64'
};
x = uniform( 100, 0.0, 100.0, opts );
n = discreteUniform( 100, 1, 100, opts );
p = uniform( 100, 0, 1, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cdf( x[ i % len ], n[ i % len ], p[ i % len ] );
y = cdf( x[ i % x.length ], n[ i % n.length ], p[ i % p.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -66,25 +62,25 @@ bench( pkg, function benchmark( b ) {

bench( pkg+':factory', function benchmark( b ) {
var mycdf;
var len;
var opts;
var n;
var p;
var x;
var y;
var i;

opts = {
'dtype': 'float64'
};
x = uniform( 100, 0.0, 80.0, opts );

n = 80;
p = 0.4;
mycdf = cdf.factory( n, p );
len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 80.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mycdf( x[ i % len ] );
y = mycdf( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,36 @@ tape( 'main export is a function', function test( t ) {

tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) {
var y = cdf( NaN, 10, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
y = cdf( 0.0, NaN, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
y = cdf( 0.0, 10, NaN );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
t.end();
});

tape( 'if provided an `x` greater than or equal to `n`, the function returns `1` (provided `n` and `p` are valid)', function test( t ) {
var y = cdf( PINF, 20, 0.5 );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 200, 20, 0.5 );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 21, 20, 0.5 );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );

t.end();
});

tape( 'if provided a negative number for `x` and a valid `n` and `p`, the function returns `0`', function test( t ) {
var y = cdf( NINF, 20, 0.5 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

y = cdf( -10.0, 20, 0.5 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

y = cdf( -1.0, 20, 0.5 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

t.end();
});
Expand All @@ -85,19 +85,19 @@ tape( 'if provided a `n` which is not a nonnegative integer, the function return
var y;

y = cdf( 2.0, 1.5, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 2.0, -2, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 2.0, -1, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 0.0, 2.5, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 0.0, PINF, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand All @@ -106,16 +106,16 @@ tape( 'if provided a success probability `p` outside of `[0,1]`, the function re
var y;

y = cdf( 2.0, 20, -1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 0.0, 20, 1.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 2.0, 20, NINF );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 2.0, 20, PINF );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ tape( 'if provided `NaN` for any parameter, the created function returns `NaN`',

cdf = factory( 20, 0.5 );
y = cdf( NaN );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( NaN, 0.5 );
y = cdf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( 20, NaN );
y = cdf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( NaN, NaN );
y = cdf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( NaN, NaN );
y = cdf( NaN );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand All @@ -84,13 +84,13 @@ tape( 'if provided a valid `n` and `p`, the function returns a function which re

cdf = factory( 20, 0.5 );
y = cdf( PINF );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 200.0 );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 20.0 );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );

t.end();
});
Expand All @@ -101,16 +101,16 @@ tape( 'if provided a valid `n` and `p`, the function returns a function which re

cdf = factory( 20, 0.5 );
y = cdf( NINF );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

y = cdf( -10.0 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

y = cdf( -1.0 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

y = cdf( -0.5 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

t.end();
});
Expand All @@ -122,22 +122,22 @@ tape( 'if provided a `n` which is not a nonnegative integer, the created functio
cdf = factory( 20.5, 0.5 );

y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( -10, 0.5 );
y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( PINF, 0.5 );
y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( NINF, 0.5 );
y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand All @@ -149,22 +149,22 @@ tape( 'if provided a success probability `p` outside `[0,1]`, the created functi
cdf = factory( 20, 1.5 );

y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = cdf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( 20, -0.5 );
y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( 20, PINF );
y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

cdf = factory( 20, NINF );
y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand Down
Loading