Skip to content

bench: update random value generation #7010

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 18, 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 uniform = require( '@stdlib/random/base/uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var mode = require( './../lib' );
Expand All @@ -31,20 +30,19 @@ var mode = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var len;
var opts;
var k;
var y;
var i;

len = 100;
k = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
k[ i ] = uniform( 1.0, 20.0 );
}
opts = {
'dtype': 'float64'
};
k = uniform( 100, 1.0, 20.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mode( k[ i % len ] );
y = mode( k[ i % k.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,9 +22,8 @@

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

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

len = 100;
k = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
k[ i ] = uniform( 1.0 + EPS, 20.0 );
}
opts = {
'dtype': 'float64'
};
k = uniform( 100, 1.0 + EPS, 20.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mode( k[ i % len ] );
y = mode( k[ i % k.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/stats/base/dists/chi/mode/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ tape( 'main export is a function', function test( t ) {

tape( 'if provided `NaN` for `k`, the function returns `NaN`', function test( t ) {
var v = mode( NaN );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );
t.end();
});

tape( 'if provided a degrees of freedom parameter `k` that is smaller than one, the function returns `NaN`', function test( t ) {
var v;

v = mode( 0.8 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

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

v = mode( -1.0 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( NINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

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

tape( 'if provided `NaN` for `k`, the function returns `NaN`', opts, function test( t ) {
var v = mode( NaN );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );
t.end();
});

tape( 'if provided a degrees of freedom parameter `k` that is smaller than one, the function returns `NaN`', opts, function test( t ) {
var v;

v = mode( 0.8 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

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

v = mode( -1.0 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( NINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand Down
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 pdf = require( './../lib' );
Expand All @@ -32,23 +31,21 @@ var pdf = require( './../lib' );
// MAIN //

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

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = pdf( x[ i % len ], k[ i % len ] );
y = pdf( x[ i % x.length ], k[ i % k.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -63,23 +60,23 @@ bench( pkg, function benchmark( b ) {

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

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

k = 10.0;
mypdf = pdf.factory( k );
len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 100.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mypdf( x[ i % len ] );
y = mypdf( 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 @@ -55,11 +55,11 @@ tape( 'if provided `NaN` for any parameter, the created function returns `NaN`',

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

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

t.end();
});
Expand All @@ -70,7 +70,7 @@ tape( 'if provided a finite `k`, the function returns a function which returns `

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

t.end();
});
Expand All @@ -81,7 +81,7 @@ tape( 'if provided a finite `k`, the function returns a function which returns `

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

t.end();
});
Expand All @@ -93,10 +93,10 @@ tape( 'if provided a negative `k`, the created function always returns `NaN`', f
pdf = factory( -1.0 );

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

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

t.end();
});
Expand All @@ -108,22 +108,22 @@ tape( 'if `k` equals `0`, the created function evaluates a degenerate distributi
pdf = factory( 0.0 );

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

y = pdf( 0.0 );
t.equal( y, PINF, 'returns +infinity for x equal to 0' );
t.equal( y, PINF, 'returns expected value' );

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

y = pdf( PINF );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

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

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

t.end();
});
Expand All @@ -138,7 +138,7 @@ tape( 'the returned function returns `0` for all `x < 0`', function test( t ) {
for ( i = 0; i < 100; i++ ) {
x = -( randu()*100.0 ) - EPS;
y = pdf( x );
t.equal( y, 0.0, 'returns 0 for x='+x );
t.equal( y, 0.0, 'returns expected value' );
}
t.end();
});
Expand Down
26 changes: 13 additions & 13 deletions lib/node_modules/@stdlib/stats/base/dists/chi/pdf/test/test.pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,35 @@ 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 = pdf( NaN, 1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
y = pdf( 0.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 `k`, the function returns `0`', function test( t ) {
var y = pdf( PINF, 1.0 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );
t.end();
});

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

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

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

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

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

t.end();
});
Expand All @@ -85,19 +85,19 @@ tape( 'if `k` equals `0`, the function evaluates a degenerate distribution cente
t.equal( y, PINF, 'returns +infinity for x equal to 0' );

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

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

y = pdf( PINF, 0.0 );
t.equal( y, 0.0, 'returns 0' );
t.equal( y, 0.0, 'returns expected value' );

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

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

t.end();
});
Expand All @@ -110,7 +110,7 @@ tape( 'the function returns `0` for all `x < 0`', function test( t ) {
for ( i = 0; i < 100; i++ ) {
x = -( randu()*100.0 ) - EPS;
y = pdf( x, 1.0 );
t.equal( y, 0.0, 'returns 0 for x='+x );
t.equal( y, 0.0, 'returns expected value' );
}
t.end();
});
Expand Down
Loading