Skip to content

bench: refactor random number generation in stats/base/dists/signrank #5088

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

Closed
wants to merge 2 commits into from
Closed
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,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var ceil = require( '@stdlib/math/base/special/ceil' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );

Copy link
Contributor

Choose a reason for hiding this comment

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

Float64Array should be used instead of Array

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );
Expand All @@ -31,16 +31,23 @@ var cdf = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var n;
var nValues;
var values;
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var nValues;
var values;
var len;
var n;

Please add only len and keep the original variable names unchanged (for consistency).

var x;
var y;
var i;
var y;
Comment on lines 37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var i;
var y;
var y;
var i;


values = new Array( b.iterations );
nValues = new Array( b.iterations );
for ( i = 0; i < b.iterations; i++ ) {
values[ i ] = uniform( 0.0, 20.0 );
nValues[ i ] = discreteUniform( 1, 20 );
}
Comment on lines +40 to +45
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
values = new Array( b.iterations );
nValues = new Array( b.iterations );
for ( i = 0; i < b.iterations; i++ ) {
values[ i ] = uniform( 0.0, 20.0 );
nValues[ i ] = discreteUniform( 1, 20 );
}
len = 100;
x = new Float64Array( len );
n = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 20.0 );
n[ i ] = discreteUniform( 1, 20 );
}


b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 20.0;
n = ceil( randu()*20.0 );
y = cdf( x, n );
x = values[ i ];
y = cdf( x, nValues[ i ] );
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
x = values[ i ];
y = cdf( x, nValues[ i ] );
y = cdf( x[ i % len ], n[ i % len ] );

if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -54,6 +61,7 @@ bench( pkg, function benchmark( b ) {
});

bench( pkg+':factory', function benchmark( b ) {
var values;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here and also applies throughout the PR.

var mycdf;
var n;
var x;
Expand All @@ -63,9 +71,14 @@ bench( pkg+':factory', function benchmark( b ) {
n = 20;
mycdf = cdf.factory( n );

values = new Array( b.iterations );
for ( i = 0; i < b.iterations; i++ ) {
values[ i ] = uniform( 0.0, 20.0 );
}
Comment on lines +74 to +77
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be updated too by following the example above.


b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 20.0;
x = values[ i ];
y = mycdf( x );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var ceil = require( '@stdlib/math/base/special/ceil' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var pdf = require( './../lib' );
Expand All @@ -31,16 +31,21 @@ var pdf = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var n;
var x;
var nValues;
var values;
var y;
var i;

values = new Array( b.iterations );
nValues = new Array( b.iterations );
for ( i = 0; i < b.iterations; i++ ) {
values[ i ] = uniform( 0.0, 10.0 );
nValues[ i ] = discreteUniform( 1, 20 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 20.0;
n = ceil( randu()*20.0 );
y = pdf( x, n );
y = pdf( values[ i ], nValues[ i ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -54,19 +59,22 @@ bench( pkg, function benchmark( b ) {
});

bench( pkg+':factory', function benchmark( b ) {
var values;
var mypdf;
var n;
var x;
var y;
var i;

values = new Array( b.iterations );
for ( i = 0; i < b.iterations; i++ ) {
values[ i ] = uniform( 0.0, 20.0 );
}
n = 20;
mypdf = pdf.factory( n );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 20.0;
y = mypdf( x );
y = mypdf( values[ i ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var randint = require( '@stdlib/random/base/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var pkg = require( './../package.json' ).name;
var quantile = require( './../lib' );
var EPS = 1e-10;
Copy link
Contributor

Choose a reason for hiding this comment

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

EPS is not needed in this benchmark file, as the original code uses:

p = randu();

Thus, using p = uniform( 0.0, 1.0 ); is perfectly fine in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it is ever used, it should be imported as:

var EPS = require( '@stdlib/constants/float64/eps' );



// MAIN //
Expand All @@ -38,8 +39,8 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
p = randu();
n = randint( 1, 200 );
p = uniform( EPS, 1.0 );
n = discreteUniform( 1, 200 );
y = quantile( p, n );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand All @@ -65,7 +66,7 @@ bench( pkg+':factory', function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
p = randu();
p = uniform( EPS, 1.0 );
y = myquantile( p );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down