Skip to content

bench: update random value generation #7093

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
May 26, 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,8 +21,7 @@
// MODULES //

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

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

len = 100;
x = new Float64Array( len );
mu = new Float64Array( len );
s = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( -50.0, 50.0 );
mu[ i ] = uniform( -10.0, 10.0 );
s[ i ] = uniform( EPS, 5.0 );
}
opts = {
'dtype': 'float64'
};
x = uniform( 100, -50.0, 50.0, opts );
mu = uniform( 100, -10.0, 10.0, opts );
s = uniform( 100, EPS, 5.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cdf( x[ i % len ], mu[ i % len ], s[ i % len ] );
y = cdf( x[ i % x.length ], mu[ i % mu.length ], s[ i % s.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 mu;
var s;
var x;
var y;
var i;

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

mu = 10.0;
s = 4.0;
mycdf = cdf.factory( mu, s );
len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 50.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 @@ -22,8 +22,7 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var EPS = require( '@stdlib/constants/float64/eps' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
Expand All @@ -41,26 +40,23 @@ var opts = {
// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var len;
var opts;
var mu;
var s;
var x;
var y;
var i;

len = 100;
x = new Float64Array( len );
mu = new Float64Array( len );
s = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( -50.0, 50.0 );
mu[ i ] = uniform( -10.0, 10.0 );
s[ i ] = uniform( EPS, 5.0 );
}
opts = {
'dtype': 'float64'
};
x = uniform( 100, -50.0, 50.0, opts );
mu = uniform( 100, -10.0, 10.0, opts );
s = uniform( 100, EPS, 5.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cdf( x[ i % len ], mu[ i % len ], s[ i % len ] );
y = cdf( x[ i % x.length ], mu[ i % mu.length ], s[ i % s.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,46 @@ 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, 0.0, 1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
y = cdf( 0.0, NaN, 1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
y = cdf( 0.0, 1.0, NaN );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
t.end();
});

tape( 'if provided `+infinity` for `x` and a finite `mu` and `s`, the function returns `1`', function test( t ) {
var y = cdf( PINF, 0.0, 1.0 );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );
t.end();
});

tape( 'if provided `-infinity` for `x` and a finite `mu` and `s`, the function returns `0`', function test( t ) {
var y = cdf( NINF, 0.0, 1.0 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );
t.end();
});

tape( 'if provided a negative `s`, the function returns `NaN`', function test( t ) {
var y;

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

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

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

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

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

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

t.end();
});
Expand All @@ -94,13 +94,13 @@ tape( 'if provided `s` equals `0`, the function evaluates a degenerate distribut
var y;

y = cdf( 2.0, 2.0, 0.0 );
t.equal( y, 1.0, 'returns 1 for x equal to mu' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 3.0, 2.0, 0.0 );
t.equal( y, 1.0, 'returns 1 for x greater than mu' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 1.0, 2.0, 0.0 );
t.equal( y, 0.0, 'returns 0 for x smaller than mu' );
t.equal( y, 0.0, 'returns expected value' );

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

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

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

cdf = factory( 1.0, 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 @@ -83,7 +83,7 @@ tape( 'if provided a valid `mu` and `s`, the function returns a function which r

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

t.end();
});
Expand All @@ -94,7 +94,7 @@ tape( 'if provided a valid `mu` and `s`, the function returns a function which r

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

t.end();
});
Expand All @@ -106,26 +106,26 @@ tape( 'if provided a negative `s`, the created function always returns `NaN`', f
cdf = factory( 0.0, -1.0 );

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( 0.0, NINF );
y = cdf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

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

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

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

t.end();
});
Expand All @@ -137,13 +137,13 @@ tape( 'if `s` equals `0`, the created function evaluates a degenerate distributi
cdf = factory( 2.0, 0.0 );

y = cdf( 2.0, 2.0, 0.0 );
t.equal( y, 1.0, 'returns 1 for x equal to mu' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 3.0, 2.0, 0.0 );
t.equal( y, 1.0, 'returns 1 for x greater than mu' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 1.0, 2.0, 0.0 );
t.equal( y, 0.0, 'returns 0 for x smaller than mu' );
t.equal( y, 0.0, 'returns expected value' );

t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,46 +55,46 @@ tape( 'main export is a function', opts, function test( t ) {

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

tape( 'if provided `+infinity` for `x` and a finite `mu` and `s`, the function returns `1`', opts, function test( t ) {
var y = cdf( PINF, 0.0, 1.0 );
t.equal( y, 1.0, 'returns 1' );
t.equal( y, 1.0, 'returns expected value' );
t.end();
});

tape( 'if provided `-infinity` for `x` and a finite `mu` and `s`, the function returns `0`', opts, function test( t ) {
var y = cdf( NINF, 0.0, 1.0 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );
t.end();
});

tape( 'if provided a negative `s`, the function returns `NaN`', opts, function test( t ) {
var y;

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

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

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

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

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

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

t.end();
});
Expand All @@ -103,13 +103,13 @@ tape( 'if provided `s` equals `0`, the function evaluates a degenerate distribut
var y;

y = cdf( 2.0, 2.0, 0.0 );
t.equal( y, 1.0, 'returns 1 for x equal to mu' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 3.0, 2.0, 0.0 );
t.equal( y, 1.0, 'returns 1 for x greater than mu' );
t.equal( y, 1.0, 'returns expected value' );

y = cdf( 1.0, 2.0, 0.0 );
t.equal( y, 0.0, 'returns 0 for x smaller than mu' );
t.equal( y, 0.0, 'returns expected value' );

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