From c9a6ad42deb1882aef86d81aa125ee400ac1acc2 Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Sun, 26 Jan 2025 21:50:29 -0800 Subject: [PATCH 1/4] refactor: update wmean implementation and tests to handle nonnegative weights --- 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: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/stats/incr/wmean/lib/main.js | 17 +++++- .../@stdlib/stats/incr/wmean/test/test.js | 58 ++++++++++++++----- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index 59e1253bdd55..cc08347185f4 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -18,6 +18,14 @@ 'use strict'; +// MODULES // + +var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + /** * Returns an accumulator function which incrementally computes a weighted arithmetic mean. * @@ -97,7 +105,8 @@ function incrwmean() { * * @private * @param {number} [x] - value - * @param {number} [w] - weight + * @param {NonNegativeNumber} [w] - weight + * @throws {TypeError} must provide a nonnegative number as the weight * @returns {(number|null)} weighted mean or null */ function accumulator( x, w ) { @@ -107,6 +116,12 @@ function incrwmean() { } return mu; } + if ( !isNonNegativeNumber( w ) ) { + throw new TypeError( format( 'invalid argument. Must provide a nonnegative number. Value: `%s`.', w ) ); + } + if (w === 0.0) { + return ( FLG === void 0 ) ? null : mu; + } FLG = true; wsum += w; mu += ( w/wsum ) * ( x-mu ); diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js index a238dac9005f..4975df1b8dfa 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -46,6 +46,47 @@ tape( 'the initial accumulated value is `null`', function test( t ) { t.end(); }); +tape( 'the accumulator function ignores zero weights and only updates the mean after encountering a non-zero weight', function test( t ) { + var acc = incrwmean(); + t.equal( acc(), null, 'returns expected value' ); + t.equal( acc( 2.0, 0.0 ), null, 'returns the previous accumulated mean' ); + t.equal( acc( 2.0, 2.0 ), 2.0, 'returns the current accumulated mean' ); + t.equal( acc( 5.0, 0.0 ), 2.0, 'returns the previous accumulated mean' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a nonnegative number', function test( t ) { + var values; + var acc; + var i; + + values = [ + '5', + -5.0, + NaN, + undefined, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + acc = incrwmean(); + acc( 2.0, value ); + }; + } +}); + tape( 'the accumulator function incrementally computes a weighted arithmetic mean', function test( t ) { var expected; var actual; @@ -99,24 +140,9 @@ tape( 'if not provided arguments, the accumulator function returns the current w t.end(); }); -tape( 'if not provided a weight, the accumulator function returns `NaN`', function test( t ) { +tape( 'if provided `NaN` for a value, the accumulator function returns `NaN`', function test( t ) { var acc = incrwmean(); - t.equal( isnan( acc( 2.0 ) ), true, 'returns NaN' ); - t.equal( isnan( acc( 3.14 ) ), true, 'returns NaN' ); - t.end(); -}); - -tape( 'if provided `NaN` for either a value or a weight, the accumulator function returns `NaN`', function test( t ) { - var acc = incrwmean(); - t.equal( isnan( acc( 2.0, NaN ) ), true, 'returns NaN' ); - t.equal( isnan( acc( 3.14, NaN ) ), true, 'returns NaN' ); - - acc = incrwmean(); t.equal( isnan( acc( NaN, 1.0 ) ), true, 'returns NaN' ); t.equal( isnan( acc( NaN, 1.0 ) ), true, 'returns NaN' ); - - acc = incrwmean(); - t.equal( isnan( acc( NaN, NaN ) ), true, 'returns NaN' ); - t.equal( isnan( acc( NaN, NaN ) ), true, 'returns NaN' ); t.end(); }); From 6d48a4b6b026931b4b0eeb94ebbaa3ae543807ae Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Mon, 27 Jan 2025 13:39:01 -0800 Subject: [PATCH 2/4] refactor: improve error message --- 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: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index cc08347185f4..1ec0cf6b1960 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -117,7 +117,7 @@ function incrwmean() { return mu; } if ( !isNonNegativeNumber( w ) ) { - throw new TypeError( format( 'invalid argument. Must provide a nonnegative number. Value: `%s`.', w ) ); + throw new TypeError( format( 'invalid argument. Must provide a nonnegative weight. Weight: `%s`.', w ) ); } if (w === 0.0) { return ( FLG === void 0 ) ? null : mu; From 12552c5b25024248aa6b5ff1be8cab8863986f92 Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Mon, 27 Jan 2025 18:09:04 -0800 Subject: [PATCH 3/4] refactor: apply suggestions from PR review --- 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: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed --- --- lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js | 6 +++--- lib/node_modules/@stdlib/stats/incr/wmean/test/test.js | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js index 1ec0cf6b1960..936ae6af39e3 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js @@ -106,7 +106,7 @@ function incrwmean() { * @private * @param {number} [x] - value * @param {NonNegativeNumber} [w] - weight - * @throws {TypeError} must provide a nonnegative number as the weight + * @throws {TypeError} second argument must be a nonnegative number * @returns {(number|null)} weighted mean or null */ function accumulator( x, w ) { @@ -117,9 +117,9 @@ function incrwmean() { return mu; } if ( !isNonNegativeNumber( w ) ) { - throw new TypeError( format( 'invalid argument. Must provide a nonnegative weight. Weight: `%s`.', w ) ); + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative number. Value: `%s`.', w ) ); } - if (w === 0.0) { + if ( w === 0.0 ) { return ( FLG === void 0 ) ? null : mu; } FLG = true; diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js index 4975df1b8dfa..cbf5ebcbd779 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/wmean/test/test.js @@ -49,13 +49,13 @@ tape( 'the initial accumulated value is `null`', function test( t ) { tape( 'the accumulator function ignores zero weights and only updates the mean after encountering a non-zero weight', function test( t ) { var acc = incrwmean(); t.equal( acc(), null, 'returns expected value' ); - t.equal( acc( 2.0, 0.0 ), null, 'returns the previous accumulated mean' ); - t.equal( acc( 2.0, 2.0 ), 2.0, 'returns the current accumulated mean' ); - t.equal( acc( 5.0, 0.0 ), 2.0, 'returns the previous accumulated mean' ); + t.equal( acc( 2.0, 0.0 ), null, 'returns expected value' ); + t.equal( acc( 2.0, 2.0 ), 2.0, 'returns expected value' ); + t.equal( acc( 5.0, 0.0 ), 2.0, 'returns expected value' ); t.end(); }); -tape( 'the function throws an error if not provided a nonnegative number', function test( t ) { +tape( 'the accumulator function throws an error if provided a second argument which is not a nonnegative number', function test( t ) { var values; var acc; var i; From f85aedd445bbc835f75b21209ade17339565359f Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Tue, 28 Jan 2025 13:38:28 -0800 Subject: [PATCH 4/4] docs: update the ts index file --- 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: na - 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: passed - 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: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/stats/incr/wmean/docs/types/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts index b91edc36cd26..11bb03f73fef 100644 --- a/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/wmean/docs/types/index.d.ts @@ -28,7 +28,8 @@ * - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. * * @param x - value -* @param w - weight +* @param {NonNegativeNumber} [w] - weight +* @throws {TypeError} second argument must be a nonnegative number * @returns weighted arithmetic mean */ type accumulator = ( x?: number, w?: number ) => number | null;