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..4580be6662d7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/README.md @@ -0,0 +1,218 @@ + + +# incrnanmmeanvar + +> Compute a moving [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance] incrementally, ignoring `NaN` values. + +
+ +For a window of size `W`, the [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i +``` + + + + + +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], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance]. + +```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. + +```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 returns updated accumulated values. If not provided an input value `x`, the accumulator function returns the current accumulated values. + +```javascript +var accumulator = incrnanmmeanvar( 3 ); + +var out = accumulator(); +// returns null + +// Fill the window... +out = accumulator( 2.0 ); // [2.0] +// returns [ 2.0, 0.0 ] + +out = accumulator( NaN ); // [2.0] +// returns [ 2.0, 0.0 ] + +out = accumulator( 1.0 ); // [2.0, 1.0] +// returns [ 1.5, 0.5 ] + +out = accumulator( 3.0 ); // [2.0, 1.0, 3.0] +// returns [ 2.0, 1.0 ] + +// Window begins sliding... +out = accumulator( -7.0 ); // [1.0, 3.0, -7.0] +// returns [ -1.0, 28.0 ] + +out = accumulator( -5.0 ); // [3.0, -7.0, -5.0] +// returns [ -3.0, 28.0 ] + +out = accumulator(); +// returns [ -3.0, 28.0 ] +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. +- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +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 ); // 8 bytes per element + +// Initialize accumulators: +acc = []; +for ( i = 0; i < N; i++ ) { + // Compute the byte offset: + offset = i * 2 * 8; // stride=2, bytes_per_element=8 + + // Create a new view for storing accumulated values: + out = new Float64Array( buf, offset, 2 ); + + // Initialize an accumulator which will write results to the view: + acc.push( incrnanmmeanvar( out, 5 ) ); +} + +// Simulate data and update the moving sample means and variances... +for ( i = 0; i < 100; i++ ) { + for ( j = 0; j < N; j++ ) { + v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0*(j+1) ); + acc[ j ]( v ); + } +} + +// Print the final results: +console.log( 'Mean\tVariance' ); +for ( i = 0; i < N; i++ ) { + mv = acc[ i ](); + console.log( '%d\t%d', mv[ 0 ].toFixed( 3 ), 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..af47d929f77a --- /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 format = require( '@stdlib/string/format' ); +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( format( '%s::accumulator', pkg ), function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanmmeanvar( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( i ); + 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..1a89a2bfb996 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,43 @@ + +x overbar equals StartFraction 1 Over upper W EndFraction sigma-summation Underscript i equals 0 Overscript upper W minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/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..f4722f986a70 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/img/equation_unbiased_sample_variance.svg @@ -0,0 +1,61 @@ + +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..6ff55f9c214b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( [out,] W ) + Returns an accumulator function which incrementally computes a moving + arithmetic mean and unbiased sample variance, ignoring `NaN` values. + + The `W` parameter defines the number of 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 provided `NaN` or no value, the accumulator function returns the + current moving accumulated values. + + As `W` values are needed to fill the window buffer, the first `W-1` returned + accumulated values are calculated from smaller sample sizes. Until the + window is full, each returned accumulated value is calculated from all + provided values. + + Parameters + ---------- + out: Array|TypedArray (optional) + Output array. + + W: integer + Window size. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var v = accumulator() + null + > accumulator( 2.0 ); + > accumulator( NaN ); + > accumulator( -5.0 ); + > v = accumulator() + [ -1.5, 24.5 ] + + 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..54ca15f8b576 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/index.d.ts @@ -0,0 +1,94 @@ +/* +* @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 provided `NaN` or no value, the accumulator function returns the current moving arithmetic mean and unbiased sample variance. +* +* @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, ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving arithmetic mean and unbiased sample variance. +* - As `W` values are needed to fill the window buffer, the first `W-1` returned moving arithmetic mean and unbiased sample variance are calculated from smaller sample sizes. Until the window is full, each returned moving arithmetic mean and unbiased sample variance is calculated from all provided values. +* +* @param out - output array +* @param window - window size +* @throws window size must be a positive integer +* @returns accumulator function +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var accumulator = incrnanmmeanvar( new Float64Array( 2 ), 3 ); +* +* var mv = accumulator(); +* // returns null +*/ +declare function incrnanmmeanvar( out: ArrayLike, window: number ): accumulator; + +/** +* Returns an accumulator function which incrementally computes a moving arithmetic mean and unbiased sample variance, ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving arithmetic mean and unbiased sample variance. +* - As `W` values are needed to fill the window buffer, the first `W-1` returned moving arithmetic mean and unbiased sample variance are calculated from smaller sample sizes. Until the window is full, each returned moving arithmetic mean and unbiased sample variance is calculated from all provided values. +* +* @param window - window size +* @throws window size must be a positive integer +* @returns accumulator function +* +* @example +* 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 ] +* +* v = accumulator( -5.0 ); +* // returns [ -1.5, 24.5 ] +* +* v = accumulator( 3.0 ); +* // returns [ 0.0, 19.0 ] +* +* v = accumulator(); +* // returns [ 0.0, 19.0 ] +*/ +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..a623f157c2da --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/docs/types/test.ts @@ -0,0 +1,78 @@ +/* +* @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 = require( './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... +{ + incrnanmmeanvar( '5' ); // $ExpectError + incrnanmmeanvar( true ); // $ExpectError + incrnanmmeanvar( false ); // $ExpectError + incrnanmmeanvar( null ); // $ExpectError + incrnanmmeanvar( {} ); // $ExpectError + incrnanmmeanvar( ( x: number ): number => x ); // $ExpectError + + const out = [ 0.0, 0.0 ]; + incrnanmmeanvar( out, '5' ); // $ExpectError + incrnanmmeanvar( out, true ); // $ExpectError + incrnanmmeanvar( out, false ); // $ExpectError + incrnanmmeanvar( out, null ); // $ExpectError + incrnanmmeanvar( out, {} ); // $ExpectError + incrnanmmeanvar( out, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an output array which is not an array-like object of numbers... +{ + incrnanmmeanvar( '5', 3 ); // $ExpectError + incrnanmmeanvar( true, 3 ); // $ExpectError + incrnanmmeanvar( false, 3 ); // $ExpectError + incrnanmmeanvar( null, 3 ); // $ExpectError + incrnanmmeanvar( {}, 3 ); // $ExpectError + incrnanmmeanvar( ( x: number ): number => x, 3 ); // $ExpectError +} + +// 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 ); + + 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/nanmmeanvar/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/examples/index.js new file mode 100644 index 000000000000..efb26f8156c4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/examples/index.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'; + +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var incrnanmmeanvar = require( './../lib' ); + +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 ); // 8 bytes per element + +// Initialize accumulators: +acc = []; +for ( i = 0; i < N; i++ ) { + // Compute the byte offset: + offset = i * 2 * 8; // stride=2, bytes_per_element=8 + + // Create a new view for storing accumulated values: + out = new Float64Array( buf, offset, 2 ); + + // Initialize an accumulator which will write results to the view: + acc.push( incrnanmmeanvar( out, 5 ) ); +} + +// Simulate data and update the moving sample means and variances... +for ( i = 0; i < 100; i++ ) { + for ( j = 0; j < N; j++ ) { + v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0*(j+1) ); + acc[ j ]( v ); + } +} + +// Print the final results: +console.log( 'Mean\tVariance' ); +for ( i = 0; i < N; i++ ) { + mv = acc[ i ](); + console.log( '%d\t%d', mv[ 0 ].toFixed( 3 ), mv[ 1 ].toFixed( 3 ) ); +} 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..1002a765a4ab --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/index.js @@ -0,0 +1,57 @@ +/** +* @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, ignoring `NaN` values. +* +* @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 ] +* +* v = accumulator( -5.0 ); +* // returns [ -1.5, 24.5 ] +* +* v = accumulator( 3.0 ); +* // returns [ 0.0, 19.0 ] +* +* v = accumulator(); +* // returns [ 0.0, 19.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..e5d2fbeced8e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/lib/main.js @@ -0,0 +1,86 @@ +/** +* @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, ignoring `NaN` values. +* +* @param {Collection} [out] - output array +* @param {PositiveInteger} window - window size +* @throws {TypeError} output argument must be array-like +* @throws {TypeError} window size must be a positive integer +* @returns {Function} accumulator function +* +* @example +* 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 ] +* +* v = accumulator( -5.0 ); +* // returns [ -1.5, 24.5 ] +* +* v = accumulator( 3.0 ); +* // returns [ 0.0, 19.0 ] +* +* v = accumulator(); +* // returns [ 0.0, 19.0 ] +*/ +function incrnanmmeanvar( out, window ) { + var mmeanvar; + if ( arguments.length > 1 ) { + mmeanvar = incrmmeanvar( out, window ); + } else { + mmeanvar = incrmmeanvar( out ); + } + return accumulator; + + /** + * If provided a value, the accumulator function returns updated accumulated values. If provided `NaN` or no value, the accumulator function returns the current accumulated values. + * + * @private + * @param {number} [x] - input value + * @returns {(ArrayLikeObject|null)} output array or null + */ + 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..b1b33a35f1f8 --- /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, ignoring NaN values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "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..3da5bae7628d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmmeanvar/test/test.js @@ -0,0 +1,306 @@ +/** +* @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 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, + 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, ignoring `NaN` values', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, NaN, 3.0, 4.0, NaN, -1.0, 3.0, 1.0 ]; + N = data.length; + + acc = incrnanmmeanvar( 3 ); + + expected = [ + [ 2.0, 0.0 ], + [ 2.0, 0.0 ], + [ 2.5, 0.5 ], + [ 3.0, 1.0 ], + [ 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, ignoring `NaN` values (output)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var out; + var N; + var i; + + data = [ 2.0, NaN, 3.0, 4.0, NaN, -1.0, 3.0, 1.0 ]; + N = data.length; + + out = [ 0.0, 0.0 ]; + acc = incrnanmmeanvar( out, 3 ); + + expected = [ + [ 2.0, 0.0 ], + [ 2.0, 0.0 ], + [ 2.5, 0.5 ], + [ 3.0, 1.0 ], + [ 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 accumulated values', function test( t ) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var i; + + data = [ 2.0, NaN, 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 `NaN` values have been provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmmeanvar( 3 ); + acc( NaN ); + acc( NaN ); + acc( NaN ); + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided `NaN`, the accumulator function ignores the value and returns the previous accumulated values', function test( t ) { + var acc = incrnanmmeanvar( 3 ); + t.strictEqual( acc( NaN ), null, 'returns expected value' ); + t.deepEqual( acc( 2.0 ), [ 2.0, 0.0 ], 'returns expected value' ); + t.deepEqual( acc( NaN ), [ 2.0, 0.0 ], 'returns expected value' ); + t.deepEqual( acc( NaN ), [ 2.0, 0.0 ], 'returns expected value' ); + t.deepEqual( acc( 4.0 ), [ 3.0, 2.0 ], '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 function 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 the window size is `1`, the accumulator function ignores `NaN` values and returns the previous value and `0` for the sample variance', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmmeanvar( 1 ); + + data = [ NaN, 7.2, NaN, 12.8, NaN, NaN, 4.6, NaN, 9.9 ]; + 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 ], + [ 9.9, 0.0 ] + ]; + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + t.deepEqual( v, expected[ i ], 'returns expected value' ); + } + t.end(); +});