From 78840493c96e453d868417e9d11148f80afbbd9e Mon Sep 17 00:00:00 2001 From: Anshu Date: Wed, 12 Mar 2025 22:47:57 +0530 Subject: [PATCH 1/4] feat(stats): add nanmmae package --- .../@stdlib/stats/incr/nanmmae/README.md | 160 +++++++++++++ .../stats/incr/nanmmae/benchmark/benchmark.js | 69 ++++++ .../docs/img/equation_mean_absolute_error.svg | 55 +++++ .../@stdlib/stats/incr/nanmmae/docs/repl.txt | 49 ++++ .../stats/incr/nanmmae/docs/types/index.d.ts | 76 ++++++ .../stats/incr/nanmmae/docs/types/test.ts | 74 ++++++ .../stats/incr/nanmmae/examples/index.js | 40 ++++ .../@stdlib/stats/incr/nanmmae/lib/index.js | 63 +++++ .../@stdlib/stats/incr/nanmmae/lib/main.js | 83 +++++++ .../@stdlib/stats/incr/nanmmae/package.json | 83 +++++++ .../@stdlib/stats/incr/nanmmae/test/test.js | 224 ++++++++++++++++++ 11 files changed, 976 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/docs/img/equation_mean_absolute_error.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md new file mode 100644 index 000000000000..90c42e34ba53 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md @@ -0,0 +1,160 @@ + + +# incrnanmmae + +> Compute a moving [mean absolute error][mean-absolute-error] (MAE) incrementally, ignoring `NaN` values. + +
+ +For a window of size `W`, the [mean absolute error][mean-absolute-error] is defined as + + + +```math +\mathop{\mathrm{MAE}} = \frac{1}{W} \sum_{i=0}^{W-1} |y_i - x_i| +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanmmae = require( '@stdlib/stats/incr/nanmmae' ); +``` + +#### incrnanmmae( window ) + +Returns an accumulator function which incrementally computes a moving [mean absolute error][mean-absolute-error]. The `window` parameter defines the number of values over which to compute the moving [mean absolute error][mean-absolute-error], ignoring `NaN` values. + +```javascript +var accumulator = incrnanmmae( 3 ); +``` + +#### accumulator( \[x, y] ) + +If provided input values `x` and `y`, the accumulator function returns an updated [mean absolute error][mean-absolute-error]. If not provided input values `x` and `y`, the accumulator function returns the current [mean absolute error][mean-absolute-error]. + +```javascript +var accumulator = incrnanmmae( 3 ); + +var m = accumulator(); +// returns null + +// Fill the window... +m = accumulator( 2.0, 3.0 ); // [(2.0,3.0)] +// returns 1.0 + +m = accumulator( NaN, 2.0 ); +// returns 1.0 + +m = accumulator( -5.0, NaN ); +// returns 1.0 + +m = accumulator( -1.0, 4.0 ); // [(2.0,3.0), (-1.0,4.0)] +// returns 3.0 + +m = accumulator( 3.0, 9.0 ); // [(2.0,3.0), (-1.0,4.0), (3.0,9.0)] +// returns 4.0 + +// Window begins sliding... +m = accumulator( -7.0, 3.0 ); // [(-1.0,4.0), (3.0,9.0), (-7.0,3.0)] +// returns 7.0 + +m = accumulator( -5.0, -3.0 ); // [(3.0,9.0), (-7.0,3.0), (-5.0,-3.0)] +// returns 6.0 + +m = accumulator(); +// returns 6.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` (x,y) pairs 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. +- **Warning**: the [mean absolute error][mean-absolute-error] is scale-dependent and, thus, the measure should **not** be used to make comparisons between datasets having different scales. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmmae = require( '@stdlib/stats/incr/nanmmae' ); + +// Initialize an accumulator: +var accumulator = incrnanmmae( 5 ); + +// For each simulated datum, update the moving mean absolute error... +var v1; +var v2; +var i; +for ( i = 0; i < 100; i++ ) { + v1 = ( randu()*100.0 ) - 50.0; + v2 = ( randu()*100.0 ) - 50.0; + accumulator( v1, v2 ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/benchmark/benchmark.js new file mode 100644 index 000000000000..a131e78dab5d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/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 incrnanmmae = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmmae( (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 = incrnanmmae( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu()-0.5, randu()-0.5 ); + 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/nanmmae/docs/img/equation_mean_absolute_error.svg b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/img/equation_mean_absolute_error.svg new file mode 100644 index 000000000000..48f2e2779e49 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/img/equation_mean_absolute_error.svg @@ -0,0 +1,55 @@ + +upper M upper A upper E equals StartFraction 1 Over upper W EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts StartAbsoluteValue y Subscript i Baseline minus x Subscript i Baseline EndAbsoluteValue + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/repl.txt new file mode 100644 index 000000000000..c54e788361f9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/repl.txt @@ -0,0 +1,49 @@ + +{{alias}}( W ) + Returns an accumulator function which incrementally computes a moving + mean absolute error (MAE), ignoring `NaN` values. + + The `W` parameter defines the number of values over which to compute the + moving mean absolute error. + + If provided a value, the accumulator function returns an updated moving + mean absolute error. If not provided a value, the accumulator function + returns the current moving mean absolute error. + + 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 m = accumulator() + null + > m = accumulator( 2.0, 3.0 ) + 1.0 + > m = accumulator( NaN, 2.0 ) + 1.0 + > m = accumulator( -5.0, NaN ) + 1.0 + > m = accumulator( -5.0, 2.0 ) + 4.0 + > m = accumulator( 3.0, 2.0 ) + 3.0 + > m = accumulator( 5.0, -2.0 ) + 5.0 + > m = accumulator() + 5.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts new file mode 100644 index 000000000000..2eaaf065ed13 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts @@ -0,0 +1,76 @@ +/* +* @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 input values, the accumulator function returns an updated mean absolute error. If not provided input values, the accumulator function returns the current mean absolute error. +* +* @param x - input value +* @param y - input value +* @returns mean absolute error or null +*/ +type accumulator = ( x?: number, y?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving mean absolute error, ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving mean absolute error. +* - 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 = incrnanmmae( 3 ); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* m = accumulator( NaN, 2.0 ); +* // returns 1.0 +* +* m = accumulator( -5.0, NaN ); +* // returns 1.0 +* +* m = accumulator( -5.0, 2.0 ); +* // returns 4.0 +* +* m = accumulator( 3.0, 2.0 ); +* // returns 3.0 +* +* m = accumulator( 5.0, -2.0 ); +* // returns 5.0 +* +* m = accumulator(); +* // returns 5.0 +*/ +declare function incrnanmmae( W: number ): accumulator; + + +// EXPORTS // + +export = incrnanmmae; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/test.ts new file mode 100644 index 000000000000..1b533f77df96 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/test.ts @@ -0,0 +1,74 @@ +/* +* @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 incrnanmmae = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmmae( 3 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + incrnanmmae( '5' ); // $ExpectError + incrnanmmae( true ); // $ExpectError + incrnanmmae( false ); // $ExpectError + incrnanmmae( null ); // $ExpectError + incrnanmmae( undefined ); // $ExpectError + incrnanmmae( [] ); // $ExpectError + incrnanmmae( {} ); // $ExpectError + incrnanmmae( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmmae(); // $ExpectError + incrnanmmae( 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmmae( 3 ); + + acc(); // $ExpectType number | null + acc( 3.14, 2.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanmmae( 3 ); + + acc( '5', 2.0 ); // $ExpectError + acc( true, 2.0 ); // $ExpectError + acc( false, 2.0 ); // $ExpectError + acc( null, 2.0 ); // $ExpectError + acc( [], 2.0 ); // $ExpectError + acc( {}, 2.0 ); // $ExpectError + acc( ( x: number ): number => x, 2.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js new file mode 100644 index 000000000000..6b306a5c3e83 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var incrnanmmae = require( './../lib' ); + +// Initialize an accumulator: +var accumulator = incrnanmmae( 5 ); + +// For each simulated datum, update the moving mean absolute error... +console.log( '\nValue\tValue\tMean\n' ); +var err; +var v1; +var v2; +var i; +for ( i = 0; i < 100; i++ ) { + v1 = ( randu() < 0.2 ) ? NaN : ( randu() * 100.0 ) - 50.0; + v2 = ( randu() < 0.2 ) ? NaN : ( randu() * 100.0 ) - 50.0; + err = accumulator( v1, v2 ); + console.log( '%d\t%d\t%d', v1.toFixed( 3 ), v2.toFixed( 3 ), ( err === null ) ? NaN : err.toFixed( 3 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js new file mode 100644 index 000000000000..755afae35d50 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 mean absolute error incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanmmae +* +* @example +* var incrnanmmae = require( '@stdlib/stats/incr/nanmmae' ); +* +* var accumulator = incrnanmmae( 3 ); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* m = accumulator( NaN, 2.0 ); +* // returns 1.0 +* +* m = accumulator( -5.0, NaN ); +* // returns 1.0 +* +* m = accumulator( -5.0, 2.0 ); +* // returns 4.0 +* +* m = accumulator( 3.0, 2.0 ); +* // returns 3.0 +* +* m = accumulator( 5.0, -2.0 ); +* // returns 5.0 +* +* m = accumulator(); +* // returns 5.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js new file mode 100644 index 000000000000..597614478270 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js @@ -0,0 +1,83 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrmmae = require( '@stdlib/stats/incr/mmae' ); + +/** +* Returns an accumulator function which incrementally computes a moving mean absolute error, ignoring `NaN` values. +* +* @param {PositiveInteger} W - window size +* @throws {TypeError} must provide a positive integer +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmmae( 3 ); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0, 3.0 ); +* // returns 1.0 +* +* m = accumulator( NaN, 2.0 ); +* // returns 1.0 +* +* m = accumulator( -5.0, NaN ); +* // returns 1.0 +* +* m = accumulator( -5.0, 2.0 ); +* // returns 4.0 +* +* m = accumulator( 3.0, 2.0 ); +* // returns 3.0 +* +* m = accumulator( 5.0, -2.0 ); +* // returns 5.0 +* +* m = accumulator(); +* // returns 5.0 +*/ +function incrnanmmae( W ) { + var mmae = incrmmae( W ); + return accumulator; + + /** + * If provided input values, the accumulator function returns an updated mean absolute error. If not provided input values, the accumulator function returns the current mean absolute error. + * + * @private + * @param {number} [x] - input value + * @param {number} [y] - input value + * @returns {(number|null)} mean absolute error or null + */ + function accumulator( x, y ) { + if ( arguments.length === 0 || isnan( x ) || isnan( y ) ) { + return mmae(); + } + return mmae( x, y ); + } +} + + +// EXPORTS // + +module.exports = incrnanmmae; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/package.json b/lib/node_modules/@stdlib/stats/incr/nanmmae/package.json new file mode 100644 index 000000000000..45ba9dc58afd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/package.json @@ -0,0 +1,83 @@ +{ + "name": "@stdlib/stats/incr/nanmmae", + "version": "0.0.0", + "description": "Compute a moving mean absolute error (MAE) 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", + "error", + "err", + "absolute", + "mae", + "incremental", + "accumulator", + "moving mean", + "moving average", + "sliding window", + "sliding", + "window", + "moving", + "time series", + "timeseries", + "forecasting", + "forecast", + "manhattan", + "cityblock", + "city-block", + "distance", + "dist" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js new file mode 100644 index 000000000000..e419d0fca5d3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js @@ -0,0 +1,224 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrnanmmae = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmmae, '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() { + incrnanmmae( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmmae( 3 ), 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving mean absolute error incrementally', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ + [ 2.0, 3.0 ], + [ 3.0, -1.0 ], + [ 2.0, 5.0 ], + [ 4.0, -4.0 ], + [ 3.0, 0.0 ], + [ -4.0, 5.0 ] + ]; + N = data.length; + + acc = incrnanmmae( 3 ); + + actual = []; + for ( i = 0; i < N; i++ ) { + actual.push( acc( data[i][0], data[i][1] ) ); + } + expected = [ 1.0, 2.5, 8.0/3.0, 5.0, 14.0/3.0, 20.0/3.0 ]; + + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current mean absolute error', function test( t ) { + var data; + var acc; + var i; + + data = [ + [ 2.0, 3.0 ], + [ 3.0, -5.0 ], + [ 1.0, 10.0 ] + ]; + acc = incrnanmmae( 2 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[i][0], data[i][1] ); + } + t.equal( acc(), 8.5, 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmmae( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'ignores `NaN` values and correctly computes moving mean absolute error', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + expected = [ + null, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14 + ]; + + data = [ + [ NaN, 1.0 ], + [ 3.14, 1.0 ], + [ 3.14, 1.0 ], + [ NaN, 1.0 ], + [ 3.14, 1.0 ], + [ 3.14, 1.0 ], + [ 3.14, 1.0 ], + [ NaN, 1.0 ], + [ 3.14, 1.0 ], + [ 3.14, 1.0 ], + [ 3.14, 1.0 ], + [ NaN, 1.0 ], + [ 3.14, 1.0 ], + [ 3.14, 1.0 ], + [ NaN, 1.0 ], + [ NaN, 1.0 ], + [ NaN, 1.0 ], + [ NaN, 1.0 ], + [ 3.14, 1.0 ] + ]; + + acc = incrnanmmae( 3 ); + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[i][0], data[i][1] ); + if ( isnan( expected[ i ] ) ) { + t.equal( isnan( v ), true, 'returns expected value for window '+i ); + t.equal( isnan( acc() ), true, 'returns expected value for window '+i ); + } else { + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + } + + data = [ + [ 1.0, NaN ], + [ 1.0, 3.14 ], + [ 1.0, 3.14 ], + [ 1.0, NaN ], + [ 1.0, 3.14 ], + [ 1.0, 3.14 ], + [ 1.0, 3.14 ], + [ 1.0, NaN ], + [ 1.0, 3.14 ], + [ 1.0, 3.14 ], + [ 1.0, 3.14 ], + [ 1.0, NaN ], + [ 1.0, 3.14 ], + [ 1.0, 3.14 ], + [ 1.0, NaN ], + [ 1.0, NaN ], + [ 1.0, NaN ], + [ 1.0, NaN ], + [ 1.0, 3.14 ] + ]; + + acc = incrnanmmae( 3 ); + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[i][0], data[i][1] ); + if ( isnan( expected[ i ] ) ) { + t.equal( isnan( v ), true, 'returns expected value for window '+i ); + t.equal( isnan( acc() ), true, 'returns expected value for window '+i ); + } else { + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + t.equal( acc(), expected[ i ], 'returns expected value for window '+i ); + } + } + t.end(); +}); From 2bea6b08889231005d4ce7fe241412f8cfba34ee Mon Sep 17 00:00:00 2001 From: Anshu Date: Wed, 12 Mar 2025 23:13:10 +0530 Subject: [PATCH 2/4] style: fix style related lint errors --- .../@stdlib/stats/incr/nanmmae/README.md | 4 +-- .../stats/incr/nanmmae/docs/types/index.d.ts | 2 +- .../@stdlib/stats/incr/nanmmae/lib/index.js | 2 +- .../@stdlib/stats/incr/nanmmae/lib/main.js | 2 +- .../@stdlib/stats/incr/nanmmae/test/test.js | 36 +++++++++---------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md index 90c42e34ba53..9383036a2bd4 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md @@ -77,7 +77,7 @@ m = accumulator( NaN, 2.0 ); // returns 1.0 m = accumulator( -5.0, NaN ); -// returns 1.0 +// returns 1.0 m = accumulator( -1.0, 4.0 ); // [(2.0,3.0), (-1.0,4.0)] // returns 3.0 @@ -123,7 +123,7 @@ var randu = require( '@stdlib/random/base/randu' ); var incrnanmmae = require( '@stdlib/stats/incr/nanmmae' ); // Initialize an accumulator: -var accumulator = incrnanmmae( 5 ); +var accumulator = incrnanmmae( 5 ); // For each simulated datum, update the moving mean absolute error... var v1; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts index 2eaaf065ed13..2680c1e53021 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts @@ -54,7 +54,7 @@ type accumulator = ( x?: number, y?: number ) => number | null; * // returns 1.0 * * m = accumulator( -5.0, NaN ); -* // returns 1.0 +* // returns 1.0 * * m = accumulator( -5.0, 2.0 ); * // returns 4.0 diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js index 755afae35d50..94b8561f7ebf 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/index.js @@ -38,7 +38,7 @@ * // returns 1.0 * * m = accumulator( -5.0, NaN ); -* // returns 1.0 +* // returns 1.0 * * m = accumulator( -5.0, 2.0 ); * // returns 4.0 diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js index 597614478270..560e4a6da4ac 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/lib/main.js @@ -43,7 +43,7 @@ var incrmmae = require( '@stdlib/stats/incr/mmae' ); * // returns 1.0 * * m = accumulator( -5.0, NaN ); -* // returns 1.0 +* // returns 1.0 * * m = accumulator( -5.0, 2.0 ); * // returns 4.0 diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js index e419d0fca5d3..7b34fb6e7cef 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/test/test.js @@ -131,24 +131,24 @@ tape( 'ignores `NaN` values and correctly computes moving mean absolute error', expected = [ null, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14, - 2.14 + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14, + 2.14 ]; data = [ From afe3efa8675248d6e5ea70f9a9973399966a7b31 Mon Sep 17 00:00:00 2001 From: Anshu Date: Sat, 15 Mar 2025 15:36:05 +0530 Subject: [PATCH 3/4] style: fix examples code to maintain uniformity --- lib/node_modules/@stdlib/stats/incr/nanmmae/README.md | 7 ++++--- .../@stdlib/stats/incr/nanmmae/examples/index.js | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md index 9383036a2bd4..b54fee912441 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/README.md @@ -119,7 +119,8 @@ m = accumulator(); ```javascript -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanmmae = require( '@stdlib/stats/incr/nanmmae' ); // Initialize an accumulator: @@ -130,8 +131,8 @@ var v1; var v2; var i; for ( i = 0; i < 100; i++ ) { - v1 = ( randu()*100.0 ) - 50.0; - v2 = ( randu()*100.0 ) - 50.0; + v1 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ); + v2 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ); accumulator( v1, v2 ); } console.log( accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js index 6b306a5c3e83..c66e4c7aa5dd 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/examples/index.js @@ -20,7 +20,8 @@ // MODULES // -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanmmae = require( './../lib' ); // Initialize an accumulator: @@ -33,8 +34,8 @@ var v1; var v2; var i; for ( i = 0; i < 100; i++ ) { - v1 = ( randu() < 0.2 ) ? NaN : ( randu() * 100.0 ) - 50.0; - v2 = ( randu() < 0.2 ) ? NaN : ( randu() * 100.0 ) - 50.0; + v1 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ); + v2 = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ); err = accumulator( v1, v2 ); console.log( '%d\t%d\t%d', v1.toFixed( 3 ), v2.toFixed( 3 ), ( err === null ) ? NaN : err.toFixed( 3 ) ); } From 6f963277d88c6ce07b8a8b2f3c771f8a47106ca0 Mon Sep 17 00:00:00 2001 From: Anshu Kumar Date: Tue, 18 Mar 2025 14:33:42 +0530 Subject: [PATCH 4/4] chore: remove unnecessary line in index.d.ts Signed-off-by: Anshu Kumar --- .../@stdlib/stats/incr/nanmmae/docs/types/index.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts index 2680c1e53021..9be00d4de14c 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanmmae/docs/types/index.d.ts @@ -18,8 +18,6 @@ // TypeScript Version: 4.1 -/// - /** * If provided input values, the accumulator function returns an updated mean absolute error. If not provided input values, the accumulator function returns the current mean absolute error. *