From 90676b643259d640b1afb10185e6b2d9c263a079 Mon Sep 17 00:00:00 2001 From: sanchay Date: Wed, 19 Mar 2025 07:55:32 +0000 Subject: [PATCH 1/2] feat: add stats/incr/nanmmean --- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/incr/nanmmean/README.md | 184 ++++++++++++++++++ .../incr/nanmmean/benchmark/benchmark.js | 69 +++++++ .../docs/img/equation_arithmetic_mean.svg | 43 ++++ .../@stdlib/stats/incr/nanmmean/docs/repl.txt | 47 +++++ .../stats/incr/nanmmean/docs/types/index.d.ts | 72 +++++++ .../stats/incr/nanmmean/docs/types/test.ts | 66 +++++++ .../stats/incr/nanmmean/examples/index.js | 42 ++++ .../@stdlib/stats/incr/nanmmean/lib/index.js | 60 ++++++ .../@stdlib/stats/incr/nanmmean/lib/main.js | 122 ++++++++++++ .../@stdlib/stats/incr/nanmmean/package.json | 73 +++++++ .../@stdlib/stats/incr/nanmmean/test/test.js | 111 +++++++++++ 11 files changed, 889 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/docs/img/equation_arithmetic_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmean/README.md new file mode 100644 index 000000000000..5aa1e669b3fe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/README.md @@ -0,0 +1,184 @@ + + +# incrnanmmean + +> Compute a moving [arithmetic mean][arithmetic-mean] incrementally, ignoring `NaN` values. + +
+ +For a window of size `W`, the [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanmmean = require( '@stdlib/stats/incr/nanmmean' ); +``` + +#### incrnanmmean( window ) + +Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean]. The `window` parameter defines the number of values over which to compute the moving mean. + +```javascript +var accumulator = incrnanmmean( 3 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated mean. If not provided an input value `x`, the accumulator function returns the current mean. + +```javascript +var accumulator = incrnanmmean( 3 ); + +var mu = accumulator(); +// returns null + +// Fill the window... +mu = accumulator( 2.0 ); // [2.0] +// returns 2.0 + +mu = accumulator( 1.0 ); // [2.0, 1.0] +// returns 1.5 + +mu = accumulator( 3.0 ); // [2.0, 1.0, 3.0] +// returns 2.0 + +// Window begins sliding... +mu = accumulator( -7.0 ); // [1.0, 3.0, -7.0] +// returns -1.0 + +mu = accumulator( NaN ); // [1.0, 3.0, -7.0] +// returns -1.0 + +mu = accumulator( -5.0 ); // [3.0, -7.0, -5.0] +// returns -3.0 + +mu = accumulator(); +// returns -3.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. +- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmmean = require( '@stdlib/stats/incr/nanmmean' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmmean( 5 ); + +// For each simulated datum, update the moving mean... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js new file mode 100644 index 000000000000..da0a8eb7b3f7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrnanmmean = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmmean( (i%5)+1 ); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanmmean( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..1a89a2bfb996 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,43 @@ + +x overbar equals StartFraction 1 Over upper W EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt new file mode 100644 index 000000000000..0cb115557fee --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt @@ -0,0 +1,47 @@ + +{{alias}}( W ) + Returns an accumulator function which incrementally computes a moving + arithmetic mean, ignorning `NaN` values. + + The `W` parameter defines the number of values over which to compute the + moving mean. + + If provided a value, the accumulator function returns an updated moving + mean. If not provided a value, the accumulator function returns the current + moving mean. + + As `W` values are needed to fill the window buffer, the first `W-1` returned + values are calculated from smaller sample sizes. Until the window is full, + each returned value is calculated from all provided values. + + Parameters + ---------- + W: integer + Window size. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var mu = accumulator() + null + > mu = accumulator( 2.0 ) + 2.0 + > mu = accumulator( -5.0 ) + -1.5 + > mu = accumulator( NaN ) + -1.5 + > mu = accumulator( 3.0 ) + 0.0 + > mu = accumulator( 5.0 ) + 1.0 + > mu = accumulator() + 1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts new file mode 100644 index 000000000000..94a08ecc2fe9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts @@ -0,0 +1,72 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided a value, returns an updated arithmetic mean; otherwise, returns the current arithmetic mean. +* +* @param x - value +* @returns arithmetic mean +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean, ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving mean. +* - As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. +* +* @param W - window size +* @throws must provide a positive integer +* @returns accumulator function +* +* @example +* var accumulator = incrnanmmean( 3 ); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns -1.5 +* +* mu = accumulator( NaN ); +* // returns -1.5 +* +* mu = accumulator( 3.0 ); +* // returns 0.0 +* +* mu = accumulator( 5.0 ); +* // returns 1.0 +* +* mu = accumulator(); +* // returns 1.0 +*/ +declare function incrnanmmean( W: number ): accumulator; + + +// EXPORTS // + +export = incrnanmmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts new file mode 100644 index 000000000000..713220f78dee --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts @@ -0,0 +1,66 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanmmean = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmmean( 3 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + incrnanmmean( '5' ); // $ExpectError + incrnanmmean( true ); // $ExpectError + incrnanmmean( false ); // $ExpectError + incrnanmmean( null ); // $ExpectError + incrnanmmean( undefined ); // $ExpectError + incrnanmmean( [] ); // $ExpectError + incrnanmmean( {} ); // $ExpectError + incrnanmmean( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmmean(); // $ExpectError + incrnanmmean( 2, 5 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmmean( 3 ); + + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanmmean( 3 ); + + acc( '5' ); // $ExpectError + acc( true ); // $ExpectError + acc( false ); // $ExpectError + acc( null ); // $ExpectError + acc( [] ); // $ExpectError + acc( {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js new file mode 100644 index 000000000000..f02ab4b74f06 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmmean = require( './../lib' ); + +var accumulator; +var mu; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmmean( 5 ); + +// For each simulated datum, update the moving mean... +console.log( '\nValue\tMean\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + mu = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), ( mu === null ) ? NaN : mu.toFixed( 4 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js new file mode 100644 index 000000000000..a6c11c2ba961 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a moving arithmetic mean incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanmmean +* +* @example +* var incrnanmmean = require( '@stdlib/stats/incr/nanmmean' ); +* +* var accumulator = incrnanmmean( 3 ); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns -1.5 +* +* mu = accumulator( 3.0 ); +* // returns 0.0 +* +* mu = accumulator( 5.0 ); +* // returns 1.0 +* +* mu = accumulator( NaN ); +* // returns 1.0 +* +* mu = accumulator(); +* // returns 1.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js new file mode 100644 index 000000000000..b13da87a11ab --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js @@ -0,0 +1,122 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean, ignoring `NaN` values. +* +* @param {PositiveInteger} W - window size +* @throws {TypeError} must provide a positive integer +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmmean( 3 ); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns -1.5 +* +* mu = accumulator( NaN ); +* // returns -1.5 +* +* mu = accumulator( 3.0 ); +* // returns 0.0 +* +* mu = accumulator( 5.0 ); +* // returns 1.0 +* +* mu = accumulator( NaN ); +* // returns 1.0 +* +* mu = accumulator(); +* // returns 1.0 +*/ +function incrnanmmean( W ) { + var delta; + var buf; + var mu; + var N; + var i; + if ( !isPositiveInteger( W ) ) { + throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) ); + } + buf = new Float64Array( W ); + mu = 0.0; + i = -1; + N = 0; + + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated mean. If not provided a value, the accumulator function returns the current mean. + * + * @private + * @param {number} [x] - input value + * @returns {(number|null)} mean or null + */ + function accumulator( x ) { + if ( arguments.length === 0 ) { + if ( N === 0 ) { + return null; + } + return mu; + } + + if ( isnan( x ) ) { + return mu; + } + // Update the index for managing the circular buffer: + i = (i+1) % W; + + // Case: initial window... + if ( N < W ) { + N += 1; + delta = x - mu; + mu += delta / N; + } + // Case: buffer is full, so we need to update the sample mean... + else { + delta = x - buf[ i ]; + mu += delta / W; + } + + buf[ i ] = x; + return mu; + } +} + + +// EXPORTS // + +module.exports = incrnanmmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/package.json b/lib/node_modules/@stdlib/stats/incr/nanmmean/package.json new file mode 100644 index 000000000000..1be5b0d77547 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/stats/incr/nanmmean", + "version": "0.0.0", + "description": "Compute a moving arithmetic mean incrementally, ignoring `NaN` values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "incremental", + "accumulator", + "moving mean", + "moving average", + "sliding window", + "sliding", + "nan", + "window", + "moving" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js new file mode 100644 index 000000000000..46af0bcab731 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js @@ -0,0 +1,111 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var incrnanmmean = require( '@stdlib/stats/incr/nanmmean/lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmmean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a positive integer', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + null, + void 0, + NaN, + [], + {}, + 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() { + incrnanmmean( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmmean( 3 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving arithmetic mean incrementally', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 2.0, NaN, 4.0, 3.0, 4.0 ]; + N = data.length; + + acc = incrnanmmean( 3 ); + + actual = []; + for ( i = 0; i < N; i++ ) { + actual.push( acc( data[ i ] ) ); + } + expected = [ 2.0, 2.5, 7.0/3.0, 7.0/3.0, 3.0, 3.0, 11.0/3.0 ]; + + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current mean', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, NaN, 5.0 ]; + acc = incrnanmmean( 2 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 4.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmmean( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); From 4dc019b13e2d96d3333c0a608ea7973088b0d8a1 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 15 Jul 2026 23:49:22 -0500 Subject: [PATCH 2/2] fix: address review feedback - Refactor to wrap incrmmean instead of reimplementing - Fix NaN-before-data bug (was returning 0.0, now returns null) - Fix typo in repl.txt (ignorning -> ignoring) - Add NaN handling mention in README description - Replace randu with uniform/bernoulli in README and examples - Remove randu import from benchmark, use loop index - Fix format specifiers in examples/index.js - Use relative require path in test.js - Remove backticks from NaN in package.json description - Update copyright years to 2026 --- .../@stdlib/stats/incr/nanmmean/README.md | 33 ++---------- .../incr/nanmmean/benchmark/benchmark.js | 8 +-- .../@stdlib/stats/incr/nanmmean/docs/repl.txt | 4 +- .../stats/incr/nanmmean/docs/types/index.d.ts | 5 +- .../stats/incr/nanmmean/docs/types/test.ts | 2 +- .../stats/incr/nanmmean/examples/index.js | 13 ++--- .../@stdlib/stats/incr/nanmmean/lib/index.js | 5 +- .../@stdlib/stats/incr/nanmmean/lib/main.js | 51 +++---------------- .../@stdlib/stats/incr/nanmmean/package.json | 2 +- .../@stdlib/stats/incr/nanmmean/test/test.js | 29 +++++++++-- 10 files changed, 58 insertions(+), 94 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmean/README.md index 5aa1e669b3fe..3190f2692f41 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2025 The Stdlib Authors. +Copyright (c) 2026 The Stdlib Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ var incrnanmmean = require( '@stdlib/stats/incr/nanmmean' ); #### incrnanmmean( window ) -Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean]. The `window` parameter defines the number of values over which to compute the moving mean. +Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving mean. ```javascript var accumulator = incrnanmmean( 3 ); @@ -115,7 +115,8 @@ mu = accumulator(); ```javascript -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanmmean = require( '@stdlib/stats/incr/nanmmean' ); var accumulator; @@ -127,11 +128,7 @@ accumulator = incrnanmmean( 5 ); // For each simulated datum, update the moving mean... for ( i = 0; i < 100; i++ ) { - if ( randu() < 0.2 ) { - v = NaN; - } else { - v = randu() * 100.0; - } + v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ); accumulator( v ); } console.log( accumulator() ); @@ -145,16 +142,6 @@ console.log( accumulator() ); @@ -167,16 +154,6 @@ console.log( accumulator() ); -[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean - -[@stdlib/stats/incr/msum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msum - -[@stdlib/stats/incr/mstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mstdev - -[@stdlib/stats/incr/msummary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msummary - -[@stdlib/stats/incr/mvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mvariance - diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js index da0a8eb7b3f7..d364403a72c5 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var incrnanmmean = require( './../lib' ); @@ -46,7 +46,7 @@ bench( pkg, function benchmark( b ) { b.end(); }); -bench( pkg+'::accumulator', function benchmark( b ) { +bench( format( '%s::accumulator', pkg ), function benchmark( b ) { var acc; var v; var i; @@ -55,7 +55,7 @@ bench( pkg+'::accumulator', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = acc( randu() ); + v = acc( i ); if ( v !== v ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt index 0cb115557fee..e6060ed3a9ef 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/repl.txt @@ -1,7 +1,7 @@ {{alias}}( W ) Returns an accumulator function which incrementally computes a moving - arithmetic mean, ignorning `NaN` values. + arithmetic mean, ignoring `NaN` values. The `W` parameter defines the number of values over which to compute the moving mean. @@ -39,6 +39,8 @@ 0.0 > mu = accumulator( 5.0 ) 1.0 + > mu = accumulator( NaN ) + 1.0 > mu = accumulator() 1.0 diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts index 94a08ecc2fe9..146cce3794e4 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,6 +61,9 @@ type accumulator = ( x?: number ) => number | null; * mu = accumulator( 5.0 ); * // returns 1.0 * +* mu = accumulator( NaN ); +* // returns 1.0 +* * mu = accumulator(); * // returns 1.0 */ diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts index 713220f78dee..ebb4a7699a70 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js index f02ab4b74f06..517931d9bc99 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,8 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanmmean = require( './../lib' ); var accumulator; @@ -32,11 +33,7 @@ accumulator = incrnanmmean( 5 ); // For each simulated datum, update the moving mean... console.log( '\nValue\tMean\n' ); for ( i = 0; i < 100; i++ ) { - if ( randu() < 0.2 ) { - v = NaN; - } else { - v = randu() * 100.0; - } + v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ); mu = accumulator( v ); - console.log( '%d\t%d', v.toFixed( 4 ), ( mu === null ) ? NaN : mu.toFixed( 4 ) ); + console.log( '%s\t%s', v.toFixed( 4 ), ( mu === null ) ? NaN : mu.toFixed( 4 ) ); } diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js index a6c11c2ba961..96871322a682 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,9 @@ * mu = accumulator( -5.0 ); * // returns -1.5 * +* mu = accumulator( NaN ); +* // returns -1.5 +* * mu = accumulator( 3.0 ); * // returns 0.0 * diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js index b13da87a11ab..6c3f5a2c3d39 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/lib/main.js @@ -1,7 +1,7 @@ -/* +/** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +20,8 @@ // MODULES // -var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var incrmmean = require( '@stdlib/stats/incr/mmean' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var Float64Array = require( '@stdlib/array/float64' ); -var format = require( '@stdlib/string/format' ); // MAIN // @@ -63,19 +61,7 @@ var format = require( '@stdlib/string/format' ); * // returns 1.0 */ function incrnanmmean( W ) { - var delta; - var buf; - var mu; - var N; - var i; - if ( !isPositiveInteger( W ) ) { - throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) ); - } - buf = new Float64Array( W ); - mu = 0.0; - i = -1; - N = 0; - + var mmean = incrmmean( W ); return accumulator; /** @@ -86,33 +72,10 @@ function incrnanmmean( W ) { * @returns {(number|null)} mean or null */ function accumulator( x ) { - if ( arguments.length === 0 ) { - if ( N === 0 ) { - return null; - } - return mu; - } - - if ( isnan( x ) ) { - return mu; + if ( arguments.length === 0 || isnan( x ) ) { + return mmean(); } - // Update the index for managing the circular buffer: - i = (i+1) % W; - - // Case: initial window... - if ( N < W ) { - N += 1; - delta = x - mu; - mu += delta / N; - } - // Case: buffer is full, so we need to update the sample mean... - else { - delta = x - buf[ i ]; - mu += delta / W; - } - - buf[ i ] = x; - return mu; + return mmean( x ); } } diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/package.json b/lib/node_modules/@stdlib/stats/incr/nanmmean/package.json index 1be5b0d77547..6a3d32a383cc 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/stats/incr/nanmmean", "version": "0.0.0", - "description": "Compute a moving arithmetic mean incrementally, ignoring `NaN` values.", + "description": "Compute a moving arithmetic mean incrementally, ignoring NaN values.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js index 46af0bcab731..c3dbcda193a0 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmean/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* Copyright (c) 2026 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ // MODULES // var tape = require( 'tape' ); -var incrnanmmean = require( '@stdlib/stats/incr/nanmmean/lib' ); +var incrnanmmean = require( './../lib' ); // TESTS // @@ -63,7 +63,7 @@ tape( 'the function throws an error if not provided a positive integer', functio }); tape( 'the function returns an accumulator function', function test( t ) { - t.equal( typeof incrnanmmean( 3 ), 'function', 'returns a function' ); + t.strictEqual( typeof incrnanmmean( 3 ), 'function', 'returns a function' ); t.end(); }); @@ -100,12 +100,31 @@ tape( 'if not provided an input value, the accumulator function returns the curr for ( i = 0; i < data.length; i++ ) { acc( data[ i ] ); } - t.equal( acc(), 4.0, 'returns expected value' ); + t.strictEqual( acc(), 4.0, 'returns expected value' ); t.end(); }); tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { var acc = incrnanmmean( 3 ); - t.equal( acc(), null, 'returns null' ); + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'if only NaN values have been provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmmean( 3 ); + acc( NaN ); + acc( NaN ); + acc( NaN ); + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided NaN, the accumulator function ignores it and returns the previous result', function test( t ) { + var acc = incrnanmmean( 3 ); + t.strictEqual( acc( NaN ), null, 'returns expected value' ); + t.strictEqual( acc( 2.0 ), 2.0, 'returns expected value' ); + t.strictEqual( acc( NaN ), 2.0, 'returns expected value' ); + t.strictEqual( acc( NaN ), 2.0, 'returns expected value' ); + t.strictEqual( acc( 4.0 ), 3.0, 'returns expected value' ); t.end(); });