Skip to content

Commit

Permalink
bench: refactor random number generation in stats/base/dists/arcsine
Browse files Browse the repository at this point in the history
PR-URL: #4744
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
anandkaranubc authored Jan 14, 2025
1 parent 99aa1e0 commit 727d50a
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/array/uniform' );
var randu = require( '@stdlib/random/base/randu' );
var uniformArray = require( '@stdlib/random/array/uniform' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );
Expand All @@ -44,9 +44,9 @@ bench( pkg, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
min[ i ] = ( randu() * 20.0 ) - 20.0;
max[ i ] = min[ i ] + ( randu() * 40.0 );
x[ i ] = uniform( -10.0, 10.0 );
min[ i ] = uniform( -20.0, 0.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 40.0 );
}

b.tic();
Expand Down Expand Up @@ -75,7 +75,7 @@ bench( pkg+':factory', function benchmark( b ) {
min = -1.5;
max = 1.5;
mycdf = cdf.factory( min, max );
x = uniform( 100, -2.0, 2.0 );
x = uniformArray( 100, -2.0, 2.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand Down Expand Up @@ -52,9 +52,9 @@ bench( pkg+'::native', opts, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
min[ i ] = ( randu() * 20.0 ) - 20.0;
max[ i ] = min[ i ] + ( randu() * 40.0 );
x[ i ] = uniform( -10.0, 10.0 );
min[ i ] = uniform( -20.0, 0.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 40.0 );
}

b.tic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/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 @@ -38,8 +39,8 @@ bench( pkg+'::instantiation', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
a = ( randu() * 10.0 ) + EPS;
b = ( randu() * 10.0 ) + a + EPS;
a = uniform( EPS, 10.0 );
b = uniform( a + EPS, a + 10.0 + EPS );
dist = new Arcsine( a, b );
if ( !( dist instanceof Arcsine ) ) {
bm.fail( 'should return a distribution instance' );
Expand Down Expand Up @@ -92,7 +93,7 @@ bench( pkg+'::set:a', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
y = ( 100.0*randu() ) + EPS;
y = uniform( EPS, 100.0 + EPS );
dist.a = y;
if ( dist.a !== y ) {
bm.fail( 'should return set value' );
Expand Down Expand Up @@ -145,7 +146,7 @@ bench( pkg+'::set:b', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
y = ( 100.0*randu() ) + a + EPS;
y = uniform( a + EPS, a + 100.0 + EPS );
dist.b = y;
if ( dist.b !== y ) {
bm.fail( 'should return set value' );
Expand All @@ -172,7 +173,7 @@ bench( pkg+':entropy', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + EPS;
dist.a = uniform( EPS, 100.0 + EPS );
y = dist.entropy;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand All @@ -199,7 +200,7 @@ bench( pkg+':kurtosis', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + EPS;
dist.a = uniform( EPS, 100.0 + EPS );
y = dist.kurtosis;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand All @@ -226,7 +227,7 @@ bench( pkg+':mean', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + EPS;
dist.a = uniform( EPS, 100.0 + EPS );
y = dist.mean;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand All @@ -253,7 +254,7 @@ bench( pkg+':median', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + EPS;
dist.a = uniform( EPS, 100.0 + EPS );
y = dist.median;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand All @@ -280,7 +281,7 @@ bench( pkg+':mode', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + 1.0 + EPS;
dist.a = uniform( 1.0 + EPS, 101.0 + EPS );
y = dist.mode;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand All @@ -307,7 +308,7 @@ bench( pkg+':skewness', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + EPS;
dist.a = uniform( EPS, 100.0 + EPS );
y = dist.skewness;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand All @@ -334,7 +335,7 @@ bench( pkg+':stdev', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + EPS;
dist.a = uniform( EPS, 100.0 + EPS );
y = dist.stdev;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand All @@ -361,7 +362,7 @@ bench( pkg+':variance', function benchmark( bm ) {

bm.tic();
for ( i = 0; i < bm.iterations; i++ ) {
dist.a = ( 100.0*randu() ) + EPS;
dist.a = uniform( EPS, 100.0 + EPS );
y = dist.variance;
if ( isnan( y ) ) {
bm.fail( 'should not return NaN' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var entropy = require( './../lib' );
Expand All @@ -41,8 +41,8 @@ bench( pkg, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
min[ i ] = randu() * 20.0;
max[ i ] = ( randu() * 20.0 ) + min[ i ];
min[ i ] = uniform( 0.0, 20.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 20.0 );
}

b.tic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var tryRequire = require( '@stdlib/utils/try-require' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;

Expand All @@ -50,8 +50,8 @@ bench( pkg+'::native', opts, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
min[ i ] = randu() * 20.0;
max[ i ] = ( randu() * 20.0 ) + min[ i ];
min[ i ] = uniform( 0.0, 20.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 20.0 );
}

b.tic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var kurtosis = require( './../lib' );
Expand All @@ -41,8 +41,8 @@ bench( pkg, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
min[ i ] = randu() * 20.0;
max[ i ] = ( randu() * 20.0 ) + min[ i ];
min[ i ] = uniform( 0.0, 20.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 20.0 );
}

b.tic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var tryRequire = require( '@stdlib/utils/try-require' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;

Expand All @@ -50,8 +50,8 @@ bench( pkg+'::native', opts, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
min[ i ] = randu() * 20.0;
max[ i ] = ( randu() * 20.0 ) + min[ i ];
min[ i ] = uniform( 0.0, 20.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 20.0 );
}

b.tic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var logcdf = require( './../lib' );
Expand All @@ -38,9 +38,9 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * 20.0 ) - 10.0;
min = ( randu() * 20.0 ) - 20.0;
max = min + ( randu() * 40.0 );
x = uniform( -10.0, 10.0 );
min = uniform( -20.0, 0.0 );
max = uniform( min, min + 40.0 );
y = logcdf( x, min, max );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down Expand Up @@ -68,7 +68,7 @@ bench( pkg+':factory', function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*2.0 ) - 2.0;
x = uniform( -2.0, 0.0 );
y = mylogcdf( 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,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var logpdf = require( './../lib' );
Expand All @@ -38,9 +38,9 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * 20.0 ) - 10.0;
min = ( randu() * 20.0 ) - 20.0;
max = min + ( randu() * 40.0 );
x = uniform( -10.0, 10.0 );
min = uniform( -20.0, 0.0 );
max = uniform( min, min + 40.0 );
y = logpdf( x, min, max );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down Expand Up @@ -68,7 +68,7 @@ bench( pkg+':factory', function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*2.0 ) - 2.0;
x = uniform( -2.0, 0.0 );
y = mylogpdf( x );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand Down Expand Up @@ -52,9 +52,9 @@ bench( pkg+'::native', opts, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
min[ i ] = ( randu() * 20.0 ) - 20.0;
max[ i ] = min[ i ] + ( randu() * 40.0 );
x[ i ] = uniform( -10.0, 10.0 );
min[ i ] = uniform( -20.0, 0.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 40.0 );
}

b.tic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var mean = require( './../lib' );
Expand All @@ -37,8 +37,8 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
min = ( randu()*10.0 );
max = ( randu()*10.0 ) + min;
min = uniform( 0.0, 10.0 );
max = uniform( min, min + 10.0 );
y = mean( min, max );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var median = require( './../lib' );
Expand All @@ -37,8 +37,8 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
min = ( randu()*10.0 );
max = ( randu()*10.0 ) + min;
min = uniform( 0.0, 10.0 );
max = uniform( min, min + 10.0 );
y = median( min, max );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var mode = require( './../lib' );
Expand All @@ -37,8 +37,8 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
min = ( randu()*10.0 );
max = ( randu()*10.0 ) + min;
min = uniform( 0.0, 10.0 );
max = uniform( min, min + 10.0 );
y = mode( min, max );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
Expand Down
Loading

1 comment on commit 727d50a

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/arcsine/cdf $\color{green}317/317$
$\color{green}+100.00\%$
$\color{green}27/27$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}317/317$
$\color{green}+100.00\%$
stats/base/dists/arcsine/ctor $\color{green}468/468$
$\color{green}+100.00\%$
$\color{green}43/43$
$\color{green}+100.00\%$
$\color{green}18/18$
$\color{green}+100.00\%$
$\color{green}468/468$
$\color{green}+100.00\%$
stats/base/dists/arcsine/entropy $\color{green}193/193$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}193/193$
$\color{green}+100.00\%$
stats/base/dists/arcsine/kurtosis $\color{green}186/186$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}186/186$
$\color{green}+100.00\%$
stats/base/dists/arcsine/logcdf $\color{green}237/237$
$\color{green}+100.00\%$
$\color{green}25/25$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}237/237$
$\color{green}+100.00\%$
stats/base/dists/arcsine/logpdf $\color{green}293/293$
$\color{green}+100.00\%$
$\color{green}25/25$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}293/293$
$\color{green}+100.00\%$
stats/base/dists/arcsine/mean $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$
stats/base/dists/arcsine/median $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$
stats/base/dists/arcsine/mode $\color{green}119/119$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}119/119$
$\color{green}+100.00\%$
stats/base/dists/arcsine/pdf $\color{green}291/291$
$\color{green}+100.00\%$
$\color{green}25/25$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}291/291$
$\color{green}+100.00\%$
stats/base/dists/arcsine/quantile $\color{green}218/218$
$\color{green}+100.00\%$
$\color{green}22/22$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}218/218$
$\color{green}+100.00\%$
stats/base/dists/arcsine/skewness $\color{green}119/119$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}119/119$
$\color{green}+100.00\%$
stats/base/dists/arcsine/stdev $\color{green}115/115$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}115/115$
$\color{green}+100.00\%$
stats/base/dists/arcsine/variance $\color{green}182/182$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}182/182$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.