diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/README.md new file mode 100644 index 000000000000..322b5b2e3d28 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/README.md @@ -0,0 +1,178 @@ + + +# incrnanmmeanabs + +> Compute a moving [arithmetic mean][arithmetic-mean] of absolute values incrementally, ignoring `NaN` values. + +
+ +For a window of size `W`, the [arithmetic mean][arithmetic-mean] of absolute values is defined as + + + +```math +\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} |x_i| +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanmmeanabs = require( '@stdlib/stats/incr/nanmmeanabs' ); +``` + +#### incrnanmmeanabs( window ) + +Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean] of absolute values. The `window` parameter defines the number of values over which to compute the moving mean. + +```javascript +var accumulator = incrnanmmeanabs( 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 = incrnanmmeanabs( 3 ); + +var mu = accumulator(); +// returns null + +// Fill the window... +mu = accumulator( 2.0 ); // [2.0] +// returns 2.0 + +mu = accumulator( -5.0 ); // [2.0, -5.0] +// returns 3.5 + +mu = accumulator( NaN); // [2.0, -5.0] +// returns 3.5 + +mu = accumulator( 3.0 ); // [2.0, -5.0, 3.0] +// returns ~3.33 + +mu = accumulator( 5.0 ); // [-5.0, 3.0, 5.0] +// returns ~4.33 + +mu = accumulator(); +// returns ~4.33 +``` + +
+ + + +
+ +## Notes + +- Input values are **not type-checked**. If a `NaN` or any non-numeric value is provided, the accumulator will ignore the `NaN` and return the current mean. +- When a `NaN` is encountered, the mean remains unchanged for **at least** the next `W-1` calls, where `W` is the window size. +- During the initial phase, when fewer than `W` values are available, the returned mean is calculated using the available values. + + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmmeanabs = require( '@stdlib/stats/incr/nanmmeanabs' ); + +var accumulator; +var mu; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmmeanabs( 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 ) - 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/nanmmeanabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/benchmark/benchmark.js new file mode 100644 index 000000000000..1733b6c84416 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/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 incrnanmmeanabs = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmmeanabs( (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 = incrnanmmeanabs( 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/nanmmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg new file mode 100644 index 000000000000..5876ec520c93 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg @@ -0,0 +1,46 @@ + +x overbar equals StartFraction 1 Over upper W EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/repl.txt new file mode 100644 index 000000000000..072c22a56a53 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/repl.txt @@ -0,0 +1,47 @@ + +{{alias}}( W ) + Returns an accumulator function which incrementally computes a moving + arithmetic mean of 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 mu = accumulator() + null + > mu = accumulator( 2.0 ) + 2.0 + > mu = accumulator( -5.0 ) + 3.5 + > mu = accumulator( NaN ) + 3.5 + > mu = accumulator( 3.0 ) + ~3.33 + > mu = accumulator( 5.0 ) + ~4.33 + > mu = accumulator() + ~4.33 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/types/index.d.ts new file mode 100644 index 000000000000..a5f2df79c1f0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/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 a value, returns an updated arithmetic mean; otherwise, returns the current arithmetic mean. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations. +* +* @param x - value +* @returns arithmetic mean of absolute values +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean of absolute 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 = incrnanmmeanabs( 3 ); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns 3.5 +* +* mu = accumulator( NaN); +* // returns 3.5 +* +* mu = accumulator( 3.0 ); +* // returns ~3.33 +* +* mu = accumulator( 5.0 ); +* // returns ~4.33 +* +* mu = accumulator(); +* // returns ~4.33 +*/ +declare function incrnanmmeanabs( W: number ): accumulator; + + +// EXPORTS // + +export = incrnanmmeanabs; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/docs/types/test.ts new file mode 100644 index 000000000000..8aa203bf201e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/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 incrnanmmeanabs = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmmeanabs( 3 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + incrnanmmeanabs( '5' ); // $ExpectError + incrnanmmeanabs( true ); // $ExpectError + incrnanmmeanabs( false ); // $ExpectError + incrnanmmeanabs( null ); // $ExpectError + incrnanmmeanabs( undefined ); // $ExpectError + incrnanmmeanabs( [] ); // $ExpectError + incrnanmmeanabs( {} ); // $ExpectError + incrnanmmeanabs( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmmeanabs(); // $ExpectError + incrnanmmeanabs( 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmmeanabs( 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 = incrnanmmeanabs( 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/nanmmeanabs/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/examples/index.js new file mode 100644 index 000000000000..e32df6e8e208 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/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 incrnanmmeanabs = require( './../lib' ); + +var accumulator; +var mu; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmmeanabs( 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 ) - 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/nanmmeanabs/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/lib/index.js new file mode 100644 index 000000000000..7c5207abec40 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/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 absolute values incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanmmeanabs +* +* @example +* var incrnanmmeanabs = require( '@stdlib/stats/incr/nanmmeanabs' ); +* +* var accumulator = incrnanmmeanabs( 3 ); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns 3.5 +* +* mu = accumulator( NaN); +* // returns 3.5 +* +* mu = accumulator( 3.0 ); +* // returns ~3.33 +* +* mu = accumulator( 5.0 ); +* // returns ~4.33 +* +* mu = accumulator(); +* // returns ~4.33 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/lib/main.js new file mode 100644 index 000000000000..f311cc50b788 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/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 incrmmeanabs = require( '@stdlib/stats/incr/mmeanabs' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean of absolute values. +* +* @param {PositiveInteger} W - window size +* @throws {TypeError} must provide a positive integer +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmmeanabs( 3 ); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns 3.5 +* +* mu = accumulator( NaN); +* // returns 3.5 +* +* mu = accumulator( 3.0 ); +* // returns ~3.33 +* +* mu = accumulator( 5.0 ); +* // returns ~4.33 +* +* mu = accumulator(); +* // returns ~4.33 +*/ +function incrnanmmeanabs( W ) { + var mean = incrmmeanabs( 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] - new value + * @returns {(number|null)} mean or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return mean(); + } + return mean( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanmmeanabs; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/package.json b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/package.json new file mode 100644 index 000000000000..affeb5648f92 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/stats/incr/nanmmeanabs", + "version": "0.0.0", + "description": "Compute a moving arithmetic mean of absolute values incrementally.", + "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", + "window", + "moving", + "absolute", + "value", + "abs", + "math.abs", + "magnitude" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/test/test.js new file mode 100644 index 000000000000..6fa65de48174 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanabs/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 incrnanmmeanabs = require( '@stdlib/stats/incr/nanmmeanabs/lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmmeanabs, '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() { + incrnanmmeanabs( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmmeanabs( 3 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving arithmetic mean of absolute values incrementally', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, -3.0, NaN, -4.0, NaN, -4.0 ]; + N = data.length; + + acc = incrnanmmeanabs( 3 ); + + actual = []; + for ( i = 0; i < N; i++ ) { + actual.push( acc( data[ i ] ) ); + } + expected = [ 2.0, 2.5, 2.5, 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, NaN, -5.0 ]; + acc = incrnanmmeanabs( 2 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 7.0/2.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmmeanabs( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +});