Skip to content

bench: refactor random number generation in stats/base/dists/normal #5112

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

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

pranav-1720
Copy link
Contributor

@pranav-1720 pranav-1720 commented Feb 8, 2025

Resolves #4981.

Description

What is the purpose of this pull request?

This pull request:

  • Refactors random number generation in JS benchmarks for stats/base/dists/normal.
  • Replaces randu() with uniform() for cleaner and more consistent code.
  • Moves the random number generation outside the benchmarking loops.

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

…or stats/base/dists/normal

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Feb 8, 2025
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Feb 8, 2025

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/normal/cdf $\color{green}226/226$
$\color{green}+100.00\%$
$\color{green}23/23$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}226/226$
$\color{green}+100.00\%$
stats/base/dists/normal/ctor $\color{green}487/487$
$\color{green}+100.00\%$
$\color{green}36/36$
$\color{green}+100.00\%$
$\color{green}19/19$
$\color{green}+100.00\%$
$\color{green}487/487$
$\color{green}+100.00\%$
stats/base/dists/normal/entropy $\color{green}178/178$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}178/178$
$\color{green}+100.00\%$
stats/base/dists/normal/kurtosis $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$
stats/base/dists/normal/logcdf $\color{green}248/248$
$\color{green}+100.00\%$
$\color{green}25/25$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}248/248$
$\color{green}+100.00\%$
stats/base/dists/normal/logpdf $\color{green}235/235$
$\color{green}+100.00\%$
$\color{green}21/21$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}235/235$
$\color{green}+100.00\%$
stats/base/dists/normal/mean $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$
stats/base/dists/normal/median $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$
stats/base/dists/normal/mgf $\color{green}221/221$
$\color{green}+100.00\%$
$\color{green}17/17$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}221/221$
$\color{green}+100.00\%$
stats/base/dists/normal/mode $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$
stats/base/dists/normal/pdf $\color{green}239/239$
$\color{green}+100.00\%$
$\color{green}23/23$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}239/239$
$\color{green}+100.00\%$
stats/base/dists/normal/quantile $\color{green}229/229$
$\color{green}+100.00\%$
$\color{green}25/25$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}229/229$
$\color{green}+100.00\%$
stats/base/dists/normal/skewness $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$
stats/base/dists/normal/stdev $\color{green}175/175$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}175/175$
$\color{green}+100.00\%$
stats/base/dists/normal/variance $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$

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

@pranav-1720 pranav-1720 changed the title bench: Refactor stats/base/dists/normal bench: refactor stats/base/dists/normal Feb 8, 2025
Comment on lines 55 to 57
}

b.toc();
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
}
b.toc();
}
b.toc();

Copy link
Contributor

Choose a reason for hiding this comment

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

This change is still needed.

Comment on lines 82 to 84
}

b.toc();
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
}
b.toc();
}
b.toc();

Same comment as above, and applies throughout the PR

Copy link
Contributor

Choose a reason for hiding this comment

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

This still needs to be addressed.

Comment on lines 569 to 570
}
b.tic();
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
}
b.tic();
}
b.tic();

mu = ( randu()*100.0 ) - 50.0;
sigma = ( randu()*20.0 ) + EPS;
y = mode( mu, sigma );
y = mode( mu[ i % 100 ], sigma[ i % 100 ] );
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
y = mode( mu[ i % 100 ], sigma[ i % 100 ] );
y = mode( mu[ i % len ], sigma[ i % len ] );

mu = ( randu()*100.0 ) - 50.0;
sigma = ( randu()*20.0 ) + EPS;
y = pdf( x, mu, sigma );
y = pdf( x[ i % 100 ], mu[ i % 100 ], sigma[ i % 100 ] );
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.


b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*6.0 ) - 3.0;
y = mypdf( x );
y = mypdf( x[ i % 100 ] );
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
y = mypdf( x[ i % 100 ] );
y = mypdf( x[ i % len ] );

Applies throughout the PR.

@anandkaranubc
Copy link
Contributor

anandkaranubc commented Feb 10, 2025

Great work, @pranav-1720! Just a few minor changes needed; otherwise, everything looks good!

And also change in title:

bench: refactor random number generation in stats/base/dists/normal

@anandkaranubc anandkaranubc added the Needs Changes Pull request which needs changes before being merged. label Feb 10, 2025
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@pranav-1720 pranav-1720 changed the title bench: refactor stats/base/dists/normal bench: refactor random number generation in stats/base/dists/normal Feb 10, 2025
@pranav-1720
Copy link
Contributor Author

@anandkaranubc hey have made the changes you suggested

Comment on lines 536 to 538
}
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
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
}
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
}
b.tic();
for ( i = 0; i < b.iterations; i++ ) {

Copy link
Contributor

Choose a reason for hiding this comment

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

This change is still pending

Comment on lines 55 to 57
}

b.toc();
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is still needed.

Comment on lines 82 to 84
}

b.toc();
Copy link
Contributor

Choose a reason for hiding this comment

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

This still needs to be addressed.

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

---
type: pre_push_report
description: Results of running various checks prior to pushing changes.
report:
  - task: run_javascript_examples
    status: na
  - task: run_c_examples
    status: na
  - task: run_cpp_examples
    status: na
  - task: run_javascript_readme_examples
    status: na
  - task: run_c_benchmarks
    status: na
  - task: run_cpp_benchmarks
    status: na
  - task: run_fortran_benchmarks
    status: na
  - task: run_javascript_benchmarks
    status: passed
  - task: run_julia_benchmarks
    status: na
  - task: run_python_benchmarks
    status: na
  - task: run_r_benchmarks
    status: na
  - task: run_javascript_tests
    status: na
---
@pranav-1720
Copy link
Contributor Author

@anandkaranubc added the whitespace changes as well. Also is there an automated way to check these whitespace linting isues, the current linting config does not seem to catch them or perhaps i am missing something in my local setup. I get errors for trailing whitespace and some other issues but not the ones you highlighted for this PR

@anandkaranubc
Copy link
Contributor

is there an automated way to check these whitespace linting isues

I don’t think the linter catches all the whitespace linting issues. It does catch some (there might be a reason why it is configured this way). Maybe you can look into improving it, propose an idea in the OH tomorrow, or raise an issue?

Copy link
Contributor

@anandkaranubc anandkaranubc left a comment

Choose a reason for hiding this comment

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

Still some changes to be made. Rest, the PR looks good. Thanks for making those changes!

Comment on lines 50 to 51
}
b.tic();
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
}
b.tic();
}
b.tic();

len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( EPS + 1.0, 100.0 );
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[ i ] = uniform( EPS + 1.0, 100.0 );
x[ i ] = uniform( 1.0 + EPS, 100.0 );

Not something that makes a difference, but good for consistency.

Comment on lines 400 to 401
}
b.tic();
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
}
b.tic();
}
b.tic();

This applies throughout the PR

Comment on lines 536 to 538
}
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is still pending

@@ -21,7 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
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 uniform = require( '@stdlib/random/base/uniform' );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, how did you even find that. I dont even know how the space was replaced by tab

@@ -21,7 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
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

@anandkaranubc anandkaranubc removed the Needs Review A pull request which needs code review. label Feb 17, 2025
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@pranav-1720
Copy link
Contributor Author

@anandkaranubc made the changes, hopefully everything should be in order now. Cheers!

@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Changes Pull request which needs changes before being merged. Needs Review A pull request which needs code review. Statistics Issue or pull request related to statistical functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: Refactor random number generation in JS benchmarks for stats/base/dists/normal
3 participants