diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/README.md b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/README.md new file mode 100644 index 000000000000..3a2c1392be66 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/README.md @@ -0,0 +1,223 @@ + + +# incrnanmmeanvar + +> Compute a moving [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance] incrementally, **skipping `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 +``` + + + + + +and the [unbiased sample variance][sample-variance] is defined as + + + +```math +s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2 +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); +``` + +#### incrnanmmeanvar( \[out,] window ) + +Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance] **while skipping `NaN` values**. The `window` parameter defines the maximum number of most recent **non-NaN** values used to compute the moving statistics. + +```javascript +var accumulator = incrnanmmeanvar( 3 ); +``` + +By default, the returned accumulator `function` returns the accumulated values as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object. Unlike `incrmmeanvar`, this accumulator ignores (skips) any `NaN` values and does not allow them to propagate into the moving mean and variance. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var accumulator = incrnanmmeanvar( new Float64Array( 2 ), 3 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function updates and returns the current moving arithmetic mean and unbiased sample variance. If `x` is `NaN`, the value is ignored (i.e., it does not affect the window). If not provided an input value `x`, the accumulator function returns the current accumulated values without updating. + +```javascript +var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); + +var accumulator = incrnanmmeanvar( 3 ); +var out = accumulator(); +// returns null + +out = accumulator( 2.0 ); +// returns [ 2, 0 ] + +out = accumulator( NaN ); +// returns [ 2, 0 ] + +out = accumulator( 1.0 ); +// returns [ 1.5, 0.5 ] + +out = accumulator( 3.0 ); +// returns [ 2, 1 ] + +out = accumulator( -7.0 ); +// returns [ -1, 28 ] + +out = accumulator( NaN ); +// returns [ -1, 28 ] + +out = accumulator( -5.0 ); +// returns [ -3, 28 ] + +out = accumulator(); +// returns [ -3, 28 ] +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN`, the value is ignored and does not affect the moving window or the accumulated statistics. If non-numeric inputs are possible, you are advised to type check and handle them **before** passing values to the accumulator function. +- As `W` valid (non-`NaN`) values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window has received `W` valid values, each returned value is calculated from all valid inputs seen so far. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); + +var offset; +var acc; +var buf; +var out; +var mv; +var N; +var v; +var i; +var j; + +// Define the number of accumulators: +N = 5; + +// Create an array buffer for storing accumulator output: +buf = new ArrayBuffer( N*2*8 ); + +// Initialize accumulators: +acc = []; +for ( i = 0; i < N; i++ ) { + // Compute the byte offset for the i­th accumulator: + offset = i * 2 * 8; + + // Create a typed array view over the correct section of the buffer: + out = new Float64Array( buf, offset, 2 ); + + // Create a moving mean/variance accumulator with window W = 5: + acc.push( incrnanmmeanvar( out, 5 ) ); +} + +// Simulate streaming data updates: +for ( i = 0; i < 100; i++ ) { + for ( j = 0; j < N; j++ ) { + // Generate random values, but occasionally insert NaN. + // Any NaNs are ignored by the accumulator. + v = ( randu() > 0.1 ) ? randu() * 100 : NaN; + + // Update accumulator j: + acc[ j ]( v ); + } +} + +// Display final moving means and variances: +console.log( 'Mean\tVariance' ); +for ( i = 0; i < N; i++ ) { + mv = acc[ i ](); // Get the current result + console.log( mv[ 0 ].toFixed( 3 ) + '\t' + mv[ 1 ].toFixed( 3 ) ); +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/benchmark/benchmark.js new file mode 100644 index 000000000000..4af1ef2cee01 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 incrnanmmeanvar = require( './../lib' ); + + +// MAIN // + +bench(pkg, function benchmark(b) { + var f; + var i; + b.tic(); + for (i = 0; i < b.iterations; i++) { + f = incrnanmmeanvar((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 = incrnanmmeanvar(5); + + b.tic(); + for (i = 0; i < b.iterations; i++) { + v = acc(randu()); + if (v.length !== 2) { + b.fail('should contain two elements'); + } + } + b.toc(); + if (v[0] !== v[0] || v[1] !== v[1]) { + b.fail('should not return NaN'); + } + b.pass('benchmark finished'); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..52f3dffaa381 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,61 @@ + + + +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/nanmmeanvar/docs/img/equation_unbiased_sample_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/img/equation_unbiased_sample_variance.svg new file mode 100644 index 000000000000..b27c2f3d84b5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/img/equation_unbiased_sample_variance.svg @@ -0,0 +1,79 @@ + + + +s squared equals StartFraction 1 Over upper W minus 1 EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/repl.txt new file mode 100644 index 000000000000..228bfd32fcdc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/repl.txt @@ -0,0 +1,55 @@ + +{{alias}}( [out,] W ) + Returns an accumulator function which incrementally computes a moving + arithmetic mean and unbiased sample variance while skipping NaN values. + + + The `W` parameter defines the number of most recent non-NaN values over + which to compute the moving arithmetic mean and unbiased sample variance. + + + If provided a value, the accumulator function returns updated accumulated + values. If not provided a value, the accumulator function returns the + current moving accumulated values. + + + NaN values are ignored and do not affect the accumulator state. + + + Until the window contains at least one valid (non-NaN) value, the + accumulator returns `null`. If the window contains only one valid value, + the sample variance is `0.0`. + + + Parameters + ---------- + out: ArrayLike (optional) + Output array-like object. + + W: positive_integer + Window size. + + + Returns + ------- + accumulator: Function + Accumulator function. + + + Examples + -------- + > var acc = {{alias}}( 3 ) + > var v = acc() + null + > v = acc( 2.0 ) + [ 2, 0 ] + > v = acc( 4.0 ) + [ 3, 2 ] + > v = acc( 3.0 ) + [ 3, 1 ] + > v = acc() + [ 3, 1 ] + + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/index.d.ts new file mode 100644 index 000000000000..91040243efca --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/index.d.ts @@ -0,0 +1,104 @@ +/* +* @license Apache-2.0 +* +* 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. +* 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 + +/// + +import { ArrayLike } from '@stdlib/types/array'; + +/** +* If provided a value, the accumulator function returns an updated moving arithmetic mean and unbiased sample variance. If not provided a value, the accumulator function returns the current moving arithmetic mean and unbiased sample variance. +* +* ## Notes +* +* - If provided `NaN`, the value is **ignored** and the accumulator state is **not updated**. +* +* @param x - input value +* +* @returns output array or null +*/ +type accumulator = ( x?: number ) => ArrayLike | null; + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean and unbiased sample variance while **skipping NaN values**. +* +* ## Notes +* +* - The `window` parameter defines the number of most-recent **valid (non-NaN)** values used to compute the moving statistics. +* - Until at least one valid value has been provided, the accumulator returns `null`. +* - If only one valid value has been observed, the sample variance is `0.0`. +* +* @param out - output array +* @param window - window size +* @throws window size must be a positive integer +* @returns accumulator function +* +*/ +declare function incrnanmmeanvar( out: ArrayLike, window: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean and unbiased sample variance while **skipping NaN values**. +* +* ## Notes +* +* - The `window` parameter defines the number of most-recent **valid (non-NaN)** values used to compute the moving statistics. +* - Until at least one valid value has been provided, the accumulator returns `null`. +* - If only one valid value has been observed, the sample variance is `0.0`. +* +* @param window - window size +* @throws window size must be a positive integer +* @returns accumulator function +* +* @example +* var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); +* +* var accumulator = incrnanmmeanvar( 3 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns [ 2, 0 ] +* +* v = accumulator( NaN ); +* // returns [ 2, 0 ] +* +* v = accumulator( 4.0 ); +* // returns [ 3, 2 ] +* +* v = accumulator( 6.0 ); +* // returns [ 4, 4 ] +* +* v = accumulator( NaN ); +* // returns [ 4, 4 ] +* +* v = accumulator( 8.0 ); +* // Window is [4,6,8] +* // mean = 6, variance = ((4-6)^2+(6-6)^2+(8-6)^2) / 2 = 4 +* // returns [ 6, 4 ] +* +* v = accumulator(); +* // returns [ 6, 4 ] +*/ +declare function incrnanmmeanvar( window: number ): accumulator; + + +// EXPORTS // + +export = incrnanmmeanvar; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/test.ts new file mode 100644 index 000000000000..681e77db4cdd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/test.ts @@ -0,0 +1,103 @@ +/* +* @license Apache-2.0 +* +* 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. +* 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 incrnanmmeanvar from './index'; + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmmeanvar(3); // $ExpectType accumulator + const out = [0.0, 0.0]; + incrnanmmeanvar(out, 3); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided a last argument that is not a number... +{ + // @ts-expect-error + incrnanmmeanvar('5'); + // @ts-expect-error + incrnanmmeanvar(true); + // @ts-expect-error + incrnanmmeanvar(false); + // @ts-expect-error + incrnanmmeanvar(null); + // @ts-expect-error + incrnanmmeanvar({}); + // @ts-expect-error + incrnanmmeanvar((x: number): number => x); + + const out = [0.0, 0.0]; + // @ts-expect-error + incrnanmmeanvar(out, '5'); + // @ts-expect-error + incrnanmmeanvar(out, true); + // @ts-expect-error + incrnanmmeanvar(out, false); + // @ts-expect-error + incrnanmmeanvar(out, null); + // @ts-expect-error + incrnanmmeanvar(out, {}); + // @ts-expect-error + incrnanmmeanvar(out, (x: number): number => x); +} + +// The compiler throws an error if the function is provided an output array that is not an array-like object of numbers... +{ + // @ts-expect-error + incrnanmmeanvar('5', 3); + // @ts-expect-error + incrnanmmeanvar(true, 3); + // @ts-expect-error + incrnanmmeanvar(false, 3); + // @ts-expect-error + incrnanmmeanvar(null, 3); + // @ts-expect-error + incrnanmmeanvar({}, 3); + // @ts-expect-error + incrnanmmeanvar((x: number): number => x, 3); +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmmeanvar(3); + + acc(); // $ExpectType ArrayLike | null + acc(3.14); // $ExpectType ArrayLike | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanmmeanvar(3); + + // @ts-expect-error + acc('5'); + // @ts-expect-error + acc(true); + // @ts-expect-error + acc(false); + // @ts-expect-error + acc(null); + // @ts-expect-error + acc([]); + // @ts-expect-error + acc({}); + // @ts-expect-error + acc((x: number): number => x); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/examples/index.js new file mode 100644 index 000000000000..fe1a77856ed6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 incrnanmmeanvar = require('./../lib'); + +var acc; +var out; +var i; + +// Create an accumulator for a window size of 5: +acc = incrnanmmeanvar(5); + +// Simulated data stream: +var data = [2.0, NaN, 4.0, 3.0, 5.0, NaN, 6.0]; + +for (i = 0; i < data.length; i++) { + out = acc(data[i]); + console.log('x: %d -> mean: %d, variance: %d', data[i], out ? out[0] : null, out ? out[1] : null); +} + +console.log('\nCurrent values:', acc()); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/index.js new file mode 100644 index 000000000000..e37f2be6e412 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 and unbiased sample variance incrementally. +* +* @module @stdlib/stats/incr/nanmmeanvar +* +* @example +* var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); +* +* var accumulator = incrnanmmeanvar( 3 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns [ 2.0, 0.0 ] +* +* v = accumulator( NaN ); +* // returns [ 2.0, 0.0 ] // NaN skipped +* +* v = accumulator( 4.0 ); +* // returns [ 3.0, 2.0 ] +* +* v = accumulator(); +* // returns [ 3.0, 2.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/main.js new file mode 100644 index 000000000000..27b7bd187a96 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/main.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 incrmmeanvar = require( '@stdlib/stats/incr/mmeanvar' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean and unbiased sample variance, skipping NaN values. +* +* @param {(ArrayLike|PositiveInteger)} out - output array or window size +* @param {PositiveInteger} [W] - window size +* @throws {TypeError} must provide a positive integer number for window size +* @returns {Function} accumulator function +* +* @example +* var acc = incrnanmmeanvar( 3 ); +* acc( 2.0 ); +* acc( NaN ); +* acc( 4.0 ); +* +* @example +* var out = [ 0.0, 0.0 ]; +* var acc = incrnanmmeanvar( out, 3 ); +* acc( 2.0 ); +* console.log( out ); +* // => [ 2, 0 ] +*/ +function incrnanmmeanvar(out, W) { + var mmeanvar; + + if (arguments.length > 1) { + mmeanvar = incrmmeanvar(out, W); + } else { + mmeanvar = incrmmeanvar(out); + } + + return accumulator; + + /** + * Accumulator function. + * + * @private + * @param {number} [x] - input value + * @returns {(Array|null)} current mean and variance + */ + function accumulator(x) { + if (arguments.length === 0 || isnan(x)) { + return mmeanvar(); + } + return mmeanvar(x); + } +} + + +// EXPORTS // + +module.exports = incrnanmmeanvar; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/package.json b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/package.json new file mode 100644 index 000000000000..94f1594f3682 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/stats/incr/nanmmeanvar", + "version": "0.0.0", + "description": "Compute a moving arithmetic mean and unbiased sample variance 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", + "moving mean", + "moving average", + "variance", + "sample", + "sample variance", + "unbiased", + "stdev", + "standard", + "deviation", + "dispersion", + "incremental", + "accumulator", + "sliding window", + "sliding", + "window", + "moving" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/test/test.js new file mode 100644 index 000000000000..f8bbc52ef282 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/test/test.js @@ -0,0 +1,387 @@ +/** +* @license Apache-2.0 +* +* 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. +* 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 randu = require( '@stdlib/random/base/randu' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var incrnanmmeanvar = require( './../lib' ); + + +// TESTS // + +tape('main export is a function', function test(t) { + t.ok(true, __filename); + t.strictEqual(typeof incrnanmmeanvar, 'function', 'main export is a function'); + t.end(); +}); + +tape('the function throws an error if not provided a positive integer for the window size', 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() { + incrnanmmeanvar(value); + }; + } +}); + +tape('the function throws an error if not provided a positive integer for the window size', function test(t) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + false, + 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() { + incrnanmmeanvar(value); + }; + } +}); + +tape('the function throws an error if not provided a positive integer for the window size (output)', function test(t) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + false, + 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() { + incrnanmmeanvar([0.0, 0.0], value); + }; + } +}); + +tape('the function throws an error if not provided an array-like object for an output argument', function test(t) { + var values; + var i; + + values = [ + '5', + -5.0, + true, + false, + 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() { + incrnanmmeanvar(value, 3); + }; + } +}); + +tape('the function returns an accumulator function', function test(t) { + t.strictEqual(typeof incrnanmmeanvar(3), 'function', 'returns expected value'); + t.end(); +}); + +tape('the function returns an accumulator function (output)', function test(t) { + t.strictEqual(typeof incrnanmmeanvar([0.0, 0.0], 3), 'function', 'returns expected value'); + t.end(); +}); + +tape('the accumulator function computes a moving arithmetic mean and unbiased sample variance incrementally', function test(t) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [2.0, 3.0, 4.0, -1.0, 3.0, 1.0]; + N = data.length; + + acc = incrnanmmeanvar(3); + + expected = [ + [2.0, 0.0], + [2.5, 0.5], + [3.0, 1.0], + [2.0, 7.0], + [2.0, 7.0], + [1.0, 4.0] + ]; + for (i = 0; i < N; i++) { + actual = acc(data[i]); + t.deepEqual(actual, expected[i], 'returns expected value'); + } + t.end(); +}); + +tape('the accumulator function computes a moving arithmetic mean and unbiased sample variance incrementally (output)', function test(t) { + var expected; + var actual; + var data; + var acc; + var out; + var N; + var i; + + data = [2.0, 3.0, 4.0, -1.0, 3.0, 1.0]; + N = data.length; + + out = [0.0, 0.0]; + acc = incrnanmmeanvar(out, 3); + + expected = [ + [2.0, 0.0], + [2.5, 0.5], + [3.0, 1.0], + [2.0, 7.0], + [2.0, 7.0], + [1.0, 4.0] + ]; + for (i = 0; i < N; i++) { + actual = acc(data[i]); + t.strictEqual(actual, out, 'returns expected value'); + t.deepEqual(actual, expected[i], 'returns expected value'); + } + t.end(); +}); + +tape('if not provided an input value, the accumulator function returns the current arithmetic mean and unbiased sample variance', function test(t) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var i; + + data = [2.0, 3.0, 10.0]; + acc = incrnanmmeanvar(3); + for (i = 0; i < data.length - 1; i++) { + acc(data[i]); + } + t.deepEqual(acc(), [2.5, 0.5], 'returns expected value'); + + acc(data[data.length - 1]); + + expected = [5.0, 19.0]; + actual = acc(); + + t.strictEqual(actual[0], expected[0], 'returns expected value'); + + delta = abs(actual[1] - expected[1]); + tol = EPS * expected[1]; + t.strictEqual(delta < tol, true, 'expected: ' + expected[1] + '. actual: ' + actual + '. tol: ' + tol + '. delta: ' + delta + '.'); + + t.end(); +}); + +tape('if data has yet to be provided, the accumulator function returns `null`', function test(t) { + var acc = incrnanmmeanvar(3); + t.strictEqual(acc(), null, 'returns expected value'); + t.end(); +}); + +tape('if only one datum has been provided, the accumulator function returns `0` for the sample variance', function test(t) { + var acc = incrnanmmeanvar(3); + var v = acc(2.0); + t.strictEqual(v[0], 2.0, 'returns expected value'); + t.strictEqual(v[1], 0.0, 'returns expected value'); + t.end(); +}); + +tape('if the window size is `1`, the accumulator functions always returns `0` for the sample variance', function test(t) { + var acc; + var out; + var v; + var i; + + acc = incrnanmmeanvar(1); + for (i = 0; i < 100; i++) { + v = randu() * 100.0; + out = acc(v); + t.strictEqual(out[0], v, 'returns expected value'); + t.strictEqual(out[1], 0.0, 'returns expected value'); + } + t.end(); +}); + +tape('if provided `NaN`, the accumulator skips the value and returns the previous mean and variance', function test(t) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmmeanvar(3); + + data = [ + NaN, + 1.5, + 2.5, + NaN, + 3.5, + 4.5, + ]; + expected = [ + null, + [1.5, 0.0], + [2.0, 0.5], + [2.0, 0.5], + [2.5, 1.0], + [3.5, 1.0], + ]; + for (i = 0; i < data.length; i++) { + v = acc(data[i]); + + + if (i === 0) { + t.strictEqual(v, null, 'NaN ignored; still no data'); + continue; + } + if (isnan(data[i])) { + t.deepEqual(v, acc(), 'NaN skipped — state unchanged at step ' + i); + } + else { + t.strictEqual(isnan(v[0]), false, 'mean is not NaN'); + t.strictEqual(isnan(v[1]), false, 'variance is not NaN'); + } + } + t.end(); +}); + +tape('if provided `NaN`, the accumulator skips values when W=1', function test(t) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmmeanvar(1); + data = [ + NaN, + 7.2, + NaN, + 12.8, + 12.8, + NaN, + 4.6, + 4.6, + NaN, + 9.9, + NaN, + NaN, + 15.3, + 15.3, + NaN, + 8.1, + NaN, + NaN, + 3.7 + ]; + expected = [ + null, + [7.2, 0.0], + [7.2, 0.0], + [12.8, 0.0], + [12.8, 0.0], + [12.8, 0.0], + [4.6, 0.0], + [4.6, 0.0], + [4.6, 0.0], + [9.9, 0.0], + [9.9, 0.0], + [9.9, 0.0], + [15.3, 0.0], + [15.3, 0.0], + [15.3, 0.0], + [8.1, 0.0], + [8.1, 0.0], + [8.1, 0.0], + [3.7, 0.0] + ]; + for (i = 0; i < data.length; i++) { + v = acc(data[i]); + t.deepEqual(v, expected[i], 'returns expected value at step ' + i); + } + t.end(); +});