From 71360b843005c0a1fd51e2829d9538480b247cfe Mon Sep 17 00:00:00 2001 From: AaryaBalwadkarPHC Date: Tue, 18 Mar 2025 17:03:01 +0530 Subject: [PATCH] feat(stats): add nanmeanstdev package --- .../@stdlib/stats/incr/nanmeanstdev/README.md | 231 ++++++++++++++++++ .../incr/nanmeanstdev/benchmark/benchmark.js | 69 ++++++ .../docs/img/equation_arithmetic_mean.svg | 43 ++++ ...on_corrected_sample_standard_deviation.svg | 73 ++++++ .../stats/incr/nanmeanstdev/docs/repl.txt | 40 +++ .../incr/nanmeanstdev/docs/types/index.d.ts | 70 ++++++ .../incr/nanmeanstdev/docs/types/test.ts | 61 +++++ .../stats/incr/nanmeanstdev/examples/index.js | 72 ++++++ .../stats/incr/nanmeanstdev/lib/index.js | 60 +++++ .../stats/incr/nanmeanstdev/lib/main.js | 74 ++++++ .../stats/incr/nanmeanstdev/package.json | 73 ++++++ .../stats/incr/nanmeanstdev/test/test.js | 185 ++++++++++++++ 12 files changed, 1051 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/img/equation_arithmetic_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/img/equation_corrected_sample_standard_deviation.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmeanstdev/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/README.md b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/README.md new file mode 100644 index 000000000000..d55082aa95be --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/README.md @@ -0,0 +1,231 @@ + + +# incrnanmeanstdev + +> Compute an [arithmetic mean][arithmetic-mean] and a [corrected sample standard deviation][sample-stdev] incrementally, ignoring `NaN` values. + +
+ +The [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +and the [corrected sample standard deviation][sample-stdev] is defined as + + + +```math +s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2} +``` + + + + + +
+ +## Usage + +```javascript +var incrnanmeanstdev = require( '@stdlib/stats/incr/nanmeanstdev' ); +``` + +#### incrnanmeanstdev( \[out] ) + +Returns an accumulator `function` which incrementally computes an [arithmetic mean][arithmetic-mean] and [corrected sample standard deviation][sample-stdev], ignoring `NaN` values. + +```javascript +var accumulator = incrnanmeanstdev(); +``` + +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 = incrnanmeanstdev( new Float64Array( 2 ) ); +``` + +#### 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 = incrmeanstdev(); + +var ms = accumulator(); +// returns null + +var ms = accumulator( NaN ); +// returns null + +ms = accumulator( 2.0 ); +// returns [ 2.0, 0.0 ] + +ms = accumulator( 1.0 ); +// returns [ 1.5, ~0.71 ] + +ms = accumulator( 3.0 ); +// returns [ 2.0, 1.0 ] + +ms = accumulator( -7.0 ); +// returns [ -0.25, ~4.57 ] + +ms = accumulator( -5.0 ); +// returns [ -1.2, ~4.49 ] + +ms = accumulator(); +// returns [ -1.2, ~4.49 ] +``` + +
+ + + +
+ +## 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. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var incrnanmeanstdev = require( '@stdlib/stats/incr/nanmeanstdev' ); + +var offset; +var acc; +var buf; +var out; +var ms; +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( incrmeanstdev( out ) ); +} + +// Simulate data and update the sample means and standard deviations... +for ( i = 0; i < 100; i++ ) { + for ( j = 0; j < N; j++ ) { + if ( randu() < 0.1 ){ + v = NaN; + } else { + v = randu() * 100.0 * (j+1); + } + acc[ j ]( v ); + } +} + +// Print the final results: +console.log( 'Mean\tStDev' ); +for ( i = 0; i < N; i++ ) { + ms = acc[ i ](); + console.log( '%d\t%d', ms[ 0 ].toFixed( 3 ), ms[ 1 ].toFixed( 3 ) ); +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/benchmark/benchmark.js new file mode 100644 index 000000000000..728300a49f95 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/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 incrnanmeanstdev = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmeanstdev(); + 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 = incrnanmeanstdev(); + + 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/nanmeanstdev/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..aea7a5f6687a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,43 @@ + +x overbar equals StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/img/equation_corrected_sample_standard_deviation.svg b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/img/equation_corrected_sample_standard_deviation.svg new file mode 100644 index 000000000000..6af85c9d5732 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/img/equation_corrected_sample_standard_deviation.svg @@ -0,0 +1,73 @@ + +s equals StartRoot StartFraction 1 Over n minus 1 EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts left-parenthesis x Subscript i Baseline minus x overbar right-parenthesis squared EndRoot + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/repl.txt new file mode 100644 index 000000000000..1dd5c54c6fe8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/repl.txt @@ -0,0 +1,40 @@ + +{{alias}}( [out] ) + Returns an accumulator function which incrementally computes an arithmetic + mean and corrected sample standard deviation, ignoring `NaN` values. + + If provided a value, the accumulator function returns updated accumulated + values. If not provided a value, the accumulator function returns the + current accumulated values. + + Parameters + ---------- + out: Array|TypedArray (optional) + Output array. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var ms = accumulator() + null + > ms = accumulator( NaN ) + null + > ms = accumulator( 2.0 ) + [ 2.0, 0.0 ] + > ms = accumulator( -5.0 ) + [ -1.5, ~4.95 ] + > ms = accumulator( 3.0 ) + [ 0.0, ~4.36 ] + > ms = accumulator( 5.0 ) + [ 1.25, ~4.35 ] + > ms = accumulator() + [ 1.25, ~4.35 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/index.d.ts new file mode 100644 index 000000000000..6b632bde2643 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/index.d.ts @@ -0,0 +1,70 @@ +/* +* @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 + +/// + +import { ArrayLike } from '@stdlib/types/array'; + +/** +* If provided a value, the accumulator function returns updated results. If not provided a value, the accumulator function returns the current results. +* +* ## Notes +* +* @param x - input value +* @returns output array or null +*/ +type accumulator = ( x?: number ) => ArrayLike | null; + +/** +* Returns an accumulator function which incrementally computes an arithmetic mean and corrected sample standard deviation, ignoring `NaN` values. +* +* @param out - output array +* @returns accumulator function +* +* @example +* var accumulator = incrmeanstdev(); +* +* var ms = accumulator(); +* // returns null +* +* var ms = accumulator( NaN ); +* // returns null +* +* ms = accumulator( 2.0 ); +* // returns [ 2.0, 0.0 ] +* +* ms = accumulator( -5.0 ); +* // returns [ -1.5, ~4.95 ] +* +* ms = accumulator( 3.0 ); +* // returns [ 0.0, ~4.36 ] +* +* ms = accumulator( 5.0 ); +* // returns [ 1.25, ~4.35 ] +* +* ms = accumulator(); +* // returns [ 1.25, ~4.35 ] +*/ +declare function incrnanmeanstdev( out?: ArrayLike ): accumulator; + + +// EXPORTS // + +export = incrnanmeanstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/test.ts new file mode 100644 index 000000000000..76fc40bb053c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @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 incrnanmeanstdev = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmeanstdev(); // $ExpectType accumulator + const out = [ 0.0, 0.0 ]; + incrnanmeanstdev( out ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not an array-like object of numbers... +{ + incrnanmeanstdev( '5' ); // $ExpectError + incrnanmeanstdev( 5 ); // $ExpectError + incrnanmeanstdev( true ); // $ExpectError + incrnanmeanstdev( false ); // $ExpectError + incrnanmeanstdev( null ); // $ExpectError + incrnanmeanstdev( {} ); // $ExpectError + incrnanmeanstdev( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmeanstdev(); + + 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 = incrnanmeanstdev(); + + 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/nanmeanstdev/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/examples/index.js new file mode 100644 index 000000000000..b65c54f094ef --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/examples/index.js @@ -0,0 +1,72 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var incrnanmeanstdev = require( './../lib' ); + +var offset; +var acc; +var buf; +var out; +var ms; +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( incrnanmeanstdev( out ) ); +} + +// Simulate data and update the sample means and standard deviations... +for ( i = 0; i < 100; i++ ) { + for ( j = 0; j < N; j++ ) { + if ( randu() < 0.1 ){ + v = NaN; + } else { + v = randu() * 100.0 * (j+1); + } + acc[ j ]( v ); + } +} + +// Print the final results: +console.log( 'Mean\tStDev' ); +for ( i = 0; i < N; i++ ) { + ms = acc[ i ](); + console.log( '%d\t%d', ms[ 0 ].toFixed( 3 ), ms[ 1 ].toFixed( 3 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/lib/index.js new file mode 100644 index 000000000000..1d28b88dd731 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/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 an arithmetic mean and corrected sample standard deviation incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanmeanstdev +* +* @example +* var incrnanmeanstdev = require( '@stdlib/stats/incr/nanmeanstdev' ); +* +* var accumulator = incrnanmeanstdev(); +* +* var ms = accumulator(); +* // returns null +* +* var ms = accumulator( NaN ); +* // returns null +* +* ms = accumulator( 2.0 ); +* // returns [ 2.0, 0.0 ] +* +* ms = accumulator( -5.0 ); +* // returns [ -1.5, ~4.95 ] +* +* ms = accumulator( 3.0 ); +* // returns [ 0.0, ~4.36 ] +* +* ms = accumulator( 5.0 ); +* // returns [ 1.25, ~4.35 ] +* +* ms = accumulator(); +* // returns [ 1.25, ~4.35 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/lib/main.js new file mode 100644 index 000000000000..3022132b4197 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/lib/main.js @@ -0,0 +1,74 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 incrmeanstdev = require( '@stdlib/stats/incr/meanstdev' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an arithmetic mean and corrected sample standard deviation, +* while skipping NaN values. +* +* @param {Collection} [out] - output array +* @throws {TypeError} output argument must be array-like +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmeanstdev(); +* +* var ms = accumulator(); +* // returns null +* +* ms = accumulator( 2.0 ); +* // returns [ 2.0, 0.0 ] +* +* ms = accumulator( NaN ); +* // returns [ 2.0, 0.0 ] +* +* ms = accumulator( -5.0 ); +* // returns [ -1.5, ~4.95 ] +* +* ms = accumulator( 3.0 ); +* // returns [ 0.0, ~4.36 ] +* +* ms = accumulator( 5.0 ); +* // returns [ 1.25, ~4.35 ] +* +* ms = accumulator(); +* // returns [ 1.25, ~4.35 ] +*/ +function incrnanmeanstdev( out ) { + var nanmeanstdev = (arguments.length === 0) ? incrmeanstdev() : incrmeanstdev(out); + return accumulator; + + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return nanmeanstdev(); + } + return nanmeanstdev( x ); + }; +} + +// EXPORTS // + +module.exports = incrnanmeanstdev; \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/package.json b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/package.json new file mode 100644 index 000000000000..542f34aa6682 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/stats/incr/meanstdev", + "version": "0.0.0", + "description": "Compute an arithmetic mean and corrected sample standard deviation 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", + "variance", + "sample variance", + "unbiased", + "var", + "dispersion", + "standard deviation", + "stdev", + "central tendency", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/test/test.js new file mode 100644 index 000000000000..22f4b72d3c5d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanstdev/test/test.js @@ -0,0 +1,185 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var incrnanmeanstdev = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmeanstdev, 'function', 'main export is a function' ); + t.end(); +}); + +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() { + incrnanmeanstdev( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmeanstdev(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (output)', function test( t ) { + t.equal( typeof incrnanmeanstdev( [ 0.0, 0.0 ] ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an arithmetic mean and corrected sample standard deviation', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 2.0, NaN, 4.0, NaN, 3.0, 4.0 ]; + N = data.length; + + // Test against Julia: + expected = [ + [ 2.0, 0.0 ], + [ 2.5, sqrt( 0.5 ) ], + [ 7.0/3.0, sqrt( 0.33333333333333337 ) ], + [ 7.0/3.0, sqrt( 0.33333333333333337 ) ], + [ 11.0/4.0, sqrt( 0.9166666666666666 ) ], + [ 11.0/4.0, sqrt( 0.9166666666666666 ) ], + [ 14.0/5.0, sqrt( 0.7 ) ], + [ 18.0/6.0, sqrt( 0.8 ) ] + ]; + + acc = incrnanmeanstdev(); + + for ( i = 0; i < N; i++ ) { + actual = acc( data[ i ] ); + t.deepEqual( actual, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the accumulator function incrementally computes an arithmetic mean and corrected sample standard deviation (output)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var out; + var N; + var i; + + data = [ 2.0, 3.0, 2.0, NaN, 4.0, NaN, 3.0, 4.0 ]; + N = data.length; + + // Test against Julia: + expected = [ + [ 2.0, 0.0 ], + [ 2.5, sqrt( 0.5 ) ], + [ 7.0/3.0, sqrt( 0.33333333333333337 ) ], + [ 7.0/3.0, sqrt( 0.33333333333333337 ) ], + [ 11.0/4.0, sqrt( 0.9166666666666666 ) ], + [ 11.0/4.0, sqrt( 0.9166666666666666 ) ], + [ 14.0/5.0, sqrt( 0.7 ) ], + [ 18.0/6.0, sqrt( 0.8 ) ] + ]; + out = [ 0.0, 0.0 ]; + acc = incrnanmeanstdev( out ); + + for ( i = 0; i < N; i++ ) { + actual = acc( data[ i ] ); + t.equal( actual, out, 'returns output array' ); + t.deepEqual( actual, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current mean and corrected sample standard deviation', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, NaN, 3.0, NaN, 1.0 ]; + acc = incrnanmeanstdev(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.deepEqual( acc(), [ 2.0, 1.0 ], 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmeanstdev(); + t.equal( acc(), null, 'returns null' ); + t.equal( acc(), null, 'returns null' ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'the sample standard deviation is `0` until at least 2 datums have been provided', function test( t ) { + var acc; + var ms; + + acc = incrnanmeanstdev(); + + ms = acc(); + t.equal( ms, null, 'returns null' ); + + ms = acc( 3.0 ); + t.equal( ms[ 1 ], 0.0, 'returns expected value' ); + + ms = acc(); + t.equal( ms[ 1 ], 0.0, 'returns expected value' ); + + ms = acc( 5.0 ); + t.notEqual( ms[ 1 ], 0.0, 'does not return 0' ); + + ms = acc(); + t.notEqual( ms[ 1 ], 0.0, 'does not return 0' ); + + t.end(); +});