diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/README.md new file mode 100644 index 000000000000..d511e20bd2ae --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/README.md @@ -0,0 +1,174 @@ + + +# incrnanmmeanabs2 + +> Compute a moving [arithmetic mean][arithmetic-mean] of squared absolute values incrementally, ignoring `NaN` values. + +
+ +For a window of size `W`, the [arithmetic mean][arithmetic-mean] of squared absolute values is defined as + + + +```math +m = \frac{1}{W} \sum_{i=0}^{W-1} x_i^2 +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanmmeanabs2 = require( '@stdlib/stats/incr/nanmmeanabs2' ); +``` + +#### incrnanmmeanabs2( window ) + +Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean] of squared absolute values, ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving mean. + +```javascript +var accumulator = incrnanmmeanabs2( 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 = incrnanmmeanabs2( 3 ); + +var m = accumulator(); +// returns null + +// Fill the window... +m = accumulator( 2.0 ); // [2.0] +// returns 4.0 + +m = accumulator( -1.0 ); // [2.0, -1.0] +// returns 2.5 + +m = accumulator( 3.0 ); // [2.0, -1.0, 3.0] +// returns ~4.67 + +// Window begins sliding... +m = accumulator( -7.0 ); // [-1.0, 3.0, -7.0] +// returns ~19.67 + +m = accumulator( NaN ); // [-1.0, 3.0, -7.0] +// returns ~19.67 + +m = accumulator( -5.0 ); // [3.0, -7.0, -5.0] +// returns ~27.67 + +m = accumulator(); +// returns ~27.67 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **at least** `W-1` future invocations. 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 incrnanmmeanabs2 = require( '@stdlib/stats/incr/nanmmeanabs2' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmmeanabs2( 5 ); + +// For each simulated datum, update the moving mean... +for ( i = 0; i < 100; i++ ) { + v = ( randu()*100.0 ) - 50.0; + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/benchmark/benchmark.js new file mode 100644 index 000000000000..fb0f998ca497 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/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 incrmmeanabs2 = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrmmeanabs2( (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 = incrmmeanabs2( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( 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/nanmmeanabs2/docs/img/equation_arithmetic_mean_squared_absolute_values.svg b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/img/equation_arithmetic_mean_squared_absolute_values.svg new file mode 100644 index 000000000000..aaf2435d7ce9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/img/equation_arithmetic_mean_squared_absolute_values.svg @@ -0,0 +1,44 @@ + +m equals StartFraction 1 Over upper W EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts x Subscript i Superscript 2 + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/repl.txt new file mode 100644 index 000000000000..c11cbcdeab6e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/repl.txt @@ -0,0 +1,46 @@ +{{alias}}( W ) + Returns an accumulator function which incrementally computes a moving + arithmetic mean of squared absolute values, ignoring `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 m = accumulator() + null + > m = accumulator( 2.0 ) + 4.0 + > m = accumulator( -5.0 ) + 14.5 + > m = accumulator( 3.0 ) + ~12.67 + > m = accumulator( NaN ) + ~12.67 + > m = accumulator( 5.0 ) + ~19.67 + > m = accumulator() + ~19.67 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/types/index.d.ts new file mode 100644 index 000000000000..9277e33a90cd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/types/index.d.ts @@ -0,0 +1,72 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 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 of squared absolute values +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean of squared absolute values, 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 = incrmmeanabs2( 3 ); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0 ); +* // returns 4.0 +* +* m = accumulator( -5.0 ); +* // returns 14.5 +* +* m = accumulator( 3.0 ); +* // returns ~12.67 +* +* m = accumulator( NaN ); +* // returns ~12.67 +* +* m = accumulator( 5.0 ); +* // returns ~19.67 +* +* m = accumulator(); +* // returns ~19.67 +*/ +declare function incrmmeanabs2( W: number ): accumulator; + + +// EXPORTS // + +export = incrmmeanabs2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/types/test.ts new file mode 100644 index 000000000000..a8c8b2d320b2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/docs/types/test.ts @@ -0,0 +1,66 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 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 incrnanmmeanabs2 = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmmeanabs2( 3 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + incrnanmmeanabs2( '5' ); // $ExpectError + incrnanmmeanabs2( true ); // $ExpectError + incrnanmmeanabs2( false ); // $ExpectError + incrnanmmeanabs2( null ); // $ExpectError + incrnanmmeanabs2( undefined ); // $ExpectError + incrnanmmeanabs2( [] ); // $ExpectError + incrnanmmeanabs2( {} ); // $ExpectError + incrnanmmeanabs2( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmmeanabs2(); // $ExpectError + incrnanmmeanabs2( 3, 2 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmmeanabs2( 4 ); + + 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 = incrnanmmeanabs2( 4 ); + + 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/nanmmeanabs2/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/examples/index.js new file mode 100644 index 000000000000..478d1e088c9e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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 incrmmeanabs2 = require( './../lib' ); + +var accumulator; +var mu; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrmmeanabs2( 5 ); + +// For each simulated datum, update the moving mean... +console.log( '\nValue\tMean\n' ); +for ( i = 0; i < 100; i++ ) { + v = ( randu()*100.0 ) - 50.0; + mu = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 3 ), mu.toFixed( 3 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/lib/index.js new file mode 100644 index 000000000000..c4e778f58679 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/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 of squared absolute values incrementally. +* +* @module @stdlib/stats/incr/nanmmeanabs2 +* +* @example +* var incrnanmmeanabs2 = require( '@stdlib/stats/incr/nanmmeanabs2' ); +* +* var accumulator = incrnanmmeanabs2( 3 ); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0 ); +* // returns 4.0 +* +* m = accumulator( -5.0 ); +* // returns 14.5 +* +* m = accumulator( 3.0 ); +* // returns ~12.67 +* +* m = accumulator( NaN ); +* // returns ~12.67 +* +* m = accumulator( 5.0 ); +* // returns ~19.67 +* +* m = accumulator(); +* // returns ~19.67 +*/ + +// MODULES // + +var incrmmeanabs2 = require( './main.js' ); + + +// EXPORTS // + +module.exports = incrmmeanabs2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/lib/main.js new file mode 100644 index 000000000000..c704ea77703c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/lib/main.js @@ -0,0 +1,82 @@ +/** +* @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 incrmmeanabs2 = require( '@stdlib/stats/incr/mmeanabs2' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean of squared absolute values. +* +* @param {PositiveInteger} W - window size +* @throws {TypeError} must provide a positive integer +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmmeanabs2( 3 ); +* +* var m = accumulator(); +* // returns null +* +* m = accumulator( 2.0 ); +* // returns 4.0 +* +* m = accumulator( -5.0 ); +* // returns 14.5 +* +* m = accumulator( 3.0 ); +* // returns ~12.67 +* +* m = accumulator(NaN); +* // returns ~12.67 +* +* m = accumulator( 5.0 ); +* // returns ~19.67 +* +* m = accumulator(); +* // returns ~19.67 +*/ +function incrnanmmeanabs2( W ) { + var mean = incrmmeanabs2( W ); + 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 || isnan( x ) ) { + return mean(); + } + return mean( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanmmeanabs2; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/package.json b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/package.json new file mode 100644 index 000000000000..5062ca8da263 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/stats/incr/nanmmeanabs2", + "version": "0.0.0", + "description": "Compute a moving arithmetic mean of squared absolute values 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", + "incremental", + "accumulator", + "moving mean", + "moving average", + "sliding window", + "sliding", + "window", + "moving", + "rolling", + "absolute", + "value", + "abs", + "abs2", + "math.abs", + "magnitude", + "squared" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/test/test.js new file mode 100644 index 000000000000..0da5a08b9fae --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs2/test/test.js @@ -0,0 +1,118 @@ +/** +* @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 abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrmmeanabs2 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrmmeanabs2, '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, + [], + {}, + 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() { + incrmmeanabs2( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.strictEqual( typeof incrmmeanabs2( 3 ), 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving arithmetic mean of squared absolute values incrementally', function test( t ) { + var expected; + var actual; + var delta; + var data; + var acc; + var tol; + var N; + var i; + + data = [ 2.0, -3.0, 2.0, -4.0, 3.0, -4.0 ]; + N = data.length; + + acc = incrmmeanabs2( 3 ); + + expected = [ 4.0, 6.5, 17.0/3.0, 29.0/3.0, 29.0/3.0, 41.0/3.0 ]; + for ( i = 0; i < N; i++ ) { + actual = acc( data[ i ] ); + if ( actual === expected[i] ) { + t.strictEqual( actual, expected[i], 'returns expected value' ); + } else { + delta = abs( expected[i] - actual ); + tol = 1.0 * EPS * abs( expected[i] ); + t.strictEqual( delta <= tol, true, 'within tolerance. Actual: '+actual+'. Expected: '+expected[i]+'. Delta: '+delta+'. Tol: '+tol+'.' ); + } + } + 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, -5.0 ]; + acc = incrmmeanabs2( 2 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.strictEqual( acc(), 17.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrmmeanabs2( 3 ); + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +});