From 2429b29960f9fc373c2fc8b450eb7afd61e84848 Mon Sep 17 00:00:00 2001 From: "nirved.mishra.eee23@itbhu.ac.in" Date: Wed, 5 Mar 2025 18:01:07 +0530 Subject: [PATCH 01/13] feat(lib): added `stats/incr/nanstdev` package This commits add a package nanstdev, which works like stdev in which it calculates corrected sample standard deviation incrementally ignoring NaN values --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/incr/nanstdev/README.md | 184 ++++++++++++ .../incr/nanstdev/benchmark/benchmark.js | 91 ++++++ ...on_corrected_sample_standard_deviation.svg | 73 +++++ .../@stdlib/stats/incr/nanstdev/docs/repl.txt | 36 +++ .../stats/incr/nanstdev/docs/types/index.d.ts | 60 ++++ .../stats/incr/nanstdev/docs/types/test.ts | 60 ++++ .../stats/incr/nanstdev/examples/index.js | 43 +++ .../@stdlib/stats/incr/nanstdev/lib/index.js | 54 ++++ .../@stdlib/stats/incr/nanstdev/lib/main.js | 123 ++++++++ .../@stdlib/stats/incr/nanstdev/package.json | 69 +++++ .../@stdlib/stats/incr/nanstdev/test/test.js | 273 ++++++++++++++++++ 11 files changed, 1066 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md new file mode 100644 index 000000000000..718ccfb30116 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md @@ -0,0 +1,184 @@ + + +# incrnanstdev + +> Compute a [corrected sample standard deviation][sample-stdev] incrementally, ignoring `NaN` values. + +
+ +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 incrnanstdev = require( '@stdlib/stats/incr/nanstdev' ); +``` + +#### incrnanstdev( \[mean] ) + +Returns an accumulator `function` which incrementally computes a [corrected sample standard deviation][sample-stdev], ignoring `NaN` values. + +```javascript +var accumulator = incrnanstdev(); +``` + +If the mean is already known, provide a `mean` argument. + +```javascript +var accumulator = incrnanstdev( 3.0 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [corrected sample standard deviation][sample-stdev]. If not provided an input value `x`, the accumulator function returns the current [corrected sample standard deviation][sample-stdev]. + +```javascript +var accumulator = incrnanstdev(); + +var s = accumulator( 2.0 ); +// returns 0.0 + +s = accumulator( 1.0 ); // => sqrt(((2-1.5)^2+(1-1.5)^2) / (2-1)) +// returns ~0.7071 + +s = accumulator( 3.0 ); // => sqrt(((2-2)^2+(1-2)^2+(3-2)^2) / (3-1)) +// returns 1.0 + +s = accumulator( NaN ); +// returns 1.0 + +s = accumulator(); +// returns 1.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. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanstdev = require( '@stdlib/stats/incr/nanstdev' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanstdev(); + +// For each simulated datum, update the sample standard deviation... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js new file mode 100644 index 000000000000..402e348282af --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js @@ -0,0 +1,91 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrnanstdev = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanstdev(); + 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 = incrnanstdev(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + 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(); +}); + +bench( pkg+'::accumulator,known_mean', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanstdev( 3.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + 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/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/img/equation_corrected_sample_standard_deviation.svg new file mode 100644 index 000000000000..6af85c9d5732 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/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/nanstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/repl.txt new file mode 100644 index 000000000000..2579e6046dba --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( [mean] ) + Returns an accumulator function which incrementally computes a corrected + sample standard deviation, ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated corrected + sample standard deviation. If not provided a value, the accumulator function + returns the current corrected sample standard deviation. + + Parameters + ---------- + mean: number (optional) + Known mean. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var s = accumulator() + null + > s = accumulator( 2.0 ) + 0.0 + > s = accumulator( -5.0 ) + ~4.95 + > s = accumulator( NaN ) + ~4.95 + > s = accumulator() + ~4.95 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts new file mode 100644 index 000000000000..d7feaeaebe6c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 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 corrected sample standard deviation; otherwise, returns the current corrected sample standard deviation. +* +* @param x - value +* @returns corrected sample standard deviation +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a corrected sample standard deviation, ignoring `NaN` values. +* +* @param mu - known mean +* @returns accumulator function +* +* @example +* var accumulator = incrnanstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns ~4.95 +* +* s = accumulator( NaN ); +* // returns ~4.95 +* +* s = accumulator(); +* // returns ~4.95 +*/ +declare function incrnanstdev( mu?: number ): accumulator; + + +// EXPORTS // + +export = incrnanstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts new file mode 100644 index 000000000000..b327d978e5f5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 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 incrnanstdev = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanstdev(); // $ExpectType accumulator + incrnanstdev( 0.0 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided invalid arguments... +{ + incrnanstdev( '5' ); // $ExpectError + incrnanstdev( true ); // $ExpectError + incrnanstdev( false ); // $ExpectError + incrnanstdev( null ); // $ExpectError + incrnanstdev( [] ); // $ExpectError + incrnanstdev( {} ); // $ExpectError + incrnanstdev( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanstdev(); + + 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 = incrnanstdev(); + + 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/nanstdev/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js new file mode 100644 index 000000000000..482aa269e255 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 incrnanstdev = require( './../lib' ); + +var accumulator; +var s; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanstdev(); + +// For each simulated datum, update the corrected sample standard deviation... +console.log( '\nValue\tSigma\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + s = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), s.toFixed( 4 ) ); +} +console.log( '\nFinal standard deviation: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js new file mode 100644 index 000000000000..3b8e2a59d134 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 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 corrected sample standard deviation incrementally. +* +* @module @stdlib/stats/incr/nanstdev +* +* @example +* var incrstdev = require( '@stdlib/stats/incr/nanstdev' ); +* +* var accumulator = incrnanstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns ~4.95 +* +* s = accumulator( NaN ); +* // returns ~4.95 +* +* s = accumulator(); +* // returns ~4.95 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js new file mode 100644 index 000000000000..fb6db1859633 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js @@ -0,0 +1,123 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrstdev = require( '@stdlib/stats/incr/stdev' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a corrected sample standard deviation, ignoring `NaN` values. +* +* ## Method +* +* - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let +* +* ```tex +* \begin{align*} +* S_n &= n \sigma_n^2 \\ +* &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ +* &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 +* \end{align*} +* ``` +* +* Accordingly, +* +* ```tex +* \begin{align*} +* S_n - S_{n-1} &= \sum_{i=1}^{n} x_i^2 - n\mu_n^2 - \sum_{i=1}^{n-1} x_i^2 + (n-1)\mu_{n-1}^2 \\ +* &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ +* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ +* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ +* &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ +* &= x_n^2 - \mu_{n-1}^2 + \mu_{n-1}^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ +* &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ +* &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ +* &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) +* \end{align*} +* ``` +* +* where we use the identity +* +* ```tex +* x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) +* ``` +* +* ## References +* +* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). +* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). +* +* @param {number} [mean] - mean value +* @throws {TypeError} must provide a number primitive +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanstdev(); +* +* var s = accumulator(); +* // returns null +* +* s = accumulator( 2.0 ); +* // returns 0.0 +* +* s = accumulator( -5.0 ); +* // returns ~4.95 +* +* s = accumulator( NaN ); +* // returns ~4.95 +* +* s = accumulator(); +* // returns ~4.95 +* +* @example +* var accumulator = incrnanstdev( 3.0 ); +*/ +function incrnanstdev( mean ) { + var stdev; + if ( arguments.length ) { + stdev = incrstdev( mean ); + } else { + stdev = incrstdev(); + } + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated corrected sample standard deviation. If not provided a value, the accumulator function returns the current corrected sample standard deviation. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} corrected sample standard deviation or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return stdev(); + } + return stdev( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanstdev; diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json new file mode 100644 index 000000000000..d37e1f677677 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/incr/nanstdev", + "version": "0.0.0", + "description": "Compute a corrected sample standard deviation 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", + "variance", + "sample variance", + "var", + "dispersion", + "standard deviation", + "stdev", + "corrected", + "central tendency", + "incremental", + "accumulator" + ] + } diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js new file mode 100644 index 000000000000..93241130f84f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js @@ -0,0 +1,273 @@ +/** +* @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 tape = require( 'tape' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var incrnanstdev = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanstdev, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanstdev(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function (known mean)', function test( t ) { + t.equal( typeof incrnanstdev( 3.0 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a non-numeric value', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanstdev( value ); + }; + } +}); + +tape( 'the accumulator function incrementally computes a corrected sample standard deviation', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + + // Check against Julia: + expected = [ + 0.0, + sqrt( 0.5 ), + sqrt( 0.33333333333333337 ), + sqrt( 0.9166666666666666 ), + sqrt( 0.7 ), + sqrt( 0.8 ) + ]; + + acc = incrnanstdev(); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a corrected sample standard deviation (known mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + + // Test against Julia: + expected =[ + sqrt( 1.0 ), + sqrt( 0.5 ), + sqrt( 0.6666666666666666 ), + sqrt( 0.75 ), + sqrt( 0.6 ), + sqrt( 0.6666666666666666 ) + ]; + + acc = incrnanstdev( 3.0 ); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current corrected sample sample deviation', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = incrnanstdev(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 1.0, 'returns the current accumulated corrected sample standard deviation' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current corrected sample sample deviation (known mean)', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = incrnanstdev( 2.0 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), sqrt( 0.6666666666666666 ), 'returns the current accumulated corrected sample standard deviation' ); + t.end(); +}); + +tape( 'the corrected sample standard deviation is `null` until at least 1 datum has been provided (unknown mean)', function test( t ) { + var acc; + var s; + + acc = incrnanstdev(); + + s = acc(); + t.equal( s, null, 'returns null' ); + + s = acc( 3.0 ); + t.notEqual( s, null, 'does not return null' ); + + s = acc(); + t.notEqual( s, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the corrected sample standard deviation is `null` until at least 1 datum has been provided (known mean)', function test( t ) { + var acc; + var s; + + acc = incrnanstdev( 3.0 ); + + s = acc(); + t.equal( s, null, 'returns null' ); + + s = acc( 3.0 ); + t.notEqual( s, null, 'does not return null' ); + + s = acc(); + t.notEqual( s, null, 'does not return null' ); + + t.end(); +}); + +tape( 'the corrected sample standard deviation is `0` until at least 2 datums have been provided (unknown mean)', function test( t ) { + var acc; + var s; + + acc = incrnanstdev(); + + s = acc( 2.0 ); + t.equal( s, 0.0, 'returns 0' ); + + s = acc(); + t.equal( s, 0.0, 'returns 0' ); + + s = acc( 3.0 ); + t.notEqual( s, 0.0, 'does not return 0' ); + + s = acc(); + t.notEqual( s, 0.0, 'does not return 0' ); + + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation.', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, NaN, 2.0, 4.0, 3.0, 4.0 ]; + + // Check against Julia: + expected = [ + 0.0, + sqrt( 0.5 ), + sqrt( 0.5 ), + sqrt( 0.33333333333333337 ), + sqrt( 0.9166666666666666 ), + sqrt( 0.7 ), + sqrt( 0.8 ) + ]; + + acc = incrnanstdev(); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation (unknown mean)', function test( t ) { + var expected; + var actual; + var data; + var acc; + var i; + + data = [ 2.0, 3.0, NaN, 2.0, 4.0, 3.0, 4.0 ]; + + // Test against Julia: + expected =[ + sqrt( 1.0 ), + sqrt( 0.5 ), + sqrt( 0.5 ), + sqrt( 0.6666666666666666 ), + sqrt( 0.75 ), + sqrt( 0.6 ), + sqrt( 0.6666666666666666 ) + ]; + + acc = incrnanstdev( 3.0 ); + + actual = []; + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); From b13126b43833e90b581638844695fe3a570bb573 Mon Sep 17 00:00:00 2001 From: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Date: Wed, 5 Mar 2025 17:09:36 +0000 Subject: [PATCH 02/13] chore: update copyright years --- lib/node_modules/@stdlib/stats/incr/nanstdev/README.md | 2 +- .../@stdlib/stats/incr/nanstdev/benchmark/benchmark.js | 2 +- .../@stdlib/stats/incr/nanstdev/docs/types/index.d.ts | 2 +- lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts | 2 +- lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md index 718ccfb30116..8e8cca8488ec 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2020 The Stdlib Authors. +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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js index 402e348282af..2271eec6d9e9 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts index d7feaeaebe6c..2ee4067bc90a 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts index b327d978e5f5..6f118274d871 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js index 482aa269e255..9867b8772c72 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js index 3b8e2a59d134..2afba6766b75 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js index fb6db1859633..2226d3f531bd 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js index 93241130f84f..f87067997b6d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. From 9c183500c03021dea78a8fd1efb9615f7085473a Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:10:33 -0700 Subject: [PATCH 03/13] style: remove backticks Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/incr/nanstdev/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md index 8e8cca8488ec..a9ccca5904ee 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md @@ -53,7 +53,7 @@ var incrnanstdev = require( '@stdlib/stats/incr/nanstdev' ); #### incrnanstdev( \[mean] ) -Returns an accumulator `function` which incrementally computes a [corrected sample standard deviation][sample-stdev], ignoring `NaN` values. +Returns an accumulator function which incrementally computes a [corrected sample standard deviation][sample-stdev], ignoring `NaN` values. ```javascript var accumulator = incrnanstdev(); From 03c441f3b883d350d03f07cd8cd3d21d7716bf69 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:13:37 -0700 Subject: [PATCH 04/13] docs: update example Signed-off-by: Athan --- .../@stdlib/stats/incr/nanstdev/README.md | 47 ++----------------- 1 file changed, 5 insertions(+), 42 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md index a9ccca5904ee..ab6bea7dfc01 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/README.md @@ -109,24 +109,17 @@ s = accumulator(); ```javascript -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanstdev = require( '@stdlib/stats/incr/nanstdev' ); -var accumulator; -var v; -var i; - // Initialize an accumulator: -accumulator = incrnanstdev(); +var accumulator = incrnanstdev(); // For each simulated datum, update the sample standard deviation... +var i; for ( i = 0; i < 100; i++ ) { - if ( randu() < 0.2 ) { - v = NaN; - } else { - v = randu() * 100.0; - } - accumulator( v ); + accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ) ); } console.log( accumulator() ); ``` @@ -139,18 +132,6 @@ console.log( accumulator() ); @@ -161,24 +142,6 @@ console.log( accumulator() ); [sample-stdev]: https://en.wikipedia.org/wiki/Standard_deviation - - -[@stdlib/stats/incr/kurtosis]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/kurtosis - -[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean - -[@stdlib/stats/incr/mstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mstdev - -[@stdlib/stats/incr/skewness]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/skewness - -[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary - -[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance - -[@stdlib/stats/incr/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanstdev - - - From 6e5bbe2aec12f918bc668e31bc89f1574fd6aa97 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:15:29 -0700 Subject: [PATCH 05/13] chore: remove triple slash directive Signed-off-by: Athan --- .../@stdlib/stats/incr/nanstdev/docs/types/index.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts index 2ee4067bc90a..8ffed06b436d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/docs/types/index.d.ts @@ -18,8 +18,6 @@ // TypeScript Version: 4.1 -/// - /** * If provided a value, returns an updated corrected sample standard deviation; otherwise, returns the current corrected sample standard deviation. * From 29805edfda0135a2769e150f250c4889802e4318 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:16:41 -0700 Subject: [PATCH 06/13] docs: update description Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js index 2afba6766b75..a8ac1d807f61 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Compute a corrected sample standard deviation incrementally. +* Compute a corrected sample standard deviation incrementally, ignoring `NaN` values. * * @module @stdlib/stats/incr/nanstdev * From f2c9c0ec76a7500489936057117b969e67adfac9 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:18:19 -0700 Subject: [PATCH 07/13] docs: update example Signed-off-by: Athan --- .../stats/incr/nanstdev/examples/index.js | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js index 9867b8772c72..8277cce35734 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/examples/index.js @@ -18,26 +18,19 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanstdev = require( './../lib' ); -var accumulator; -var s; -var v; -var i; - // Initialize an accumulator: -accumulator = incrnanstdev(); +var accumulator = incrnanstdev(); // For each simulated datum, update the corrected sample standard deviation... console.log( '\nValue\tSigma\n' ); +var v; +var i; for ( i = 0; i < 100; i++ ) { - if ( randu() < 0.2 ) { - v = NaN; - } else { - v = randu() * 100.0; - } - s = accumulator( v ); - console.log( '%d\t%d', v.toFixed( 4 ), s.toFixed( 4 ) ); + v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 ); + console.log( '%d\t%d', v.toFixed( 4 ), accumulator( v ).toFixed( 4 ) ); } console.log( '\nFinal standard deviation: %d\n', accumulator() ); From f336640913941bc367c3959c4578193616bdf58a Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:21:23 -0700 Subject: [PATCH 08/13] docs: remove extended comment Signed-off-by: Athan --- .../@stdlib/stats/incr/nanstdev/lib/main.js | 39 ------------------- 1 file changed, 39 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js index 2226d3f531bd..b0a9e5d54c48 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js @@ -29,45 +29,6 @@ var incrstdev = require( '@stdlib/stats/incr/stdev' ); /** * Returns an accumulator function which incrementally computes a corrected sample standard deviation, ignoring `NaN` values. * -* ## Method -* -* - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let -* -* ```tex -* \begin{align*} -* S_n &= n \sigma_n^2 \\ -* &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ -* &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 -* \end{align*} -* ``` -* -* Accordingly, -* -* ```tex -* \begin{align*} -* S_n - S_{n-1} &= \sum_{i=1}^{n} x_i^2 - n\mu_n^2 - \sum_{i=1}^{n-1} x_i^2 + (n-1)\mu_{n-1}^2 \\ -* &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ -* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ -* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ -* &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ -* &= x_n^2 - \mu_{n-1}^2 + \mu_{n-1}^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ -* &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ -* &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ -* &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) -* \end{align*} -* ``` -* -* where we use the identity -* -* ```tex -* x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) -* ``` -* -* ## References -* -* - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). -* - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). -* * @param {number} [mean] - mean value * @throws {TypeError} must provide a number primitive * @returns {Function} accumulator function From 5da7a58181d689e1e6dc1969d09c290f9bc835a0 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:21:54 -0700 Subject: [PATCH 09/13] docs: update description Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js index b0a9e5d54c48..226dcacc8e09 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/lib/main.js @@ -30,7 +30,7 @@ var incrstdev = require( '@stdlib/stats/incr/stdev' ); * Returns an accumulator function which incrementally computes a corrected sample standard deviation, ignoring `NaN` values. * * @param {number} [mean] - mean value -* @throws {TypeError} must provide a number primitive +* @throws {TypeError} must provide a number * @returns {Function} accumulator function * * @example From ee6f11fab66e9a9ca053f466dfd355a35b01a3c3 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:23:07 -0700 Subject: [PATCH 10/13] docs: add missing period Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/incr/nanstdev/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json index d37e1f677677..ad094bc37797 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/stats/incr/nanstdev", "version": "0.0.0", - "description": "Compute a corrected sample standard deviation incrementally, ignoring NaN values", + "description": "Compute a corrected sample standard deviation incrementally, ignoring NaN values.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", From ab13f0ceb86d40880c3e5e7df7ae1c84ccc860fd Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:30:24 -0700 Subject: [PATCH 11/13] test: update test messages Signed-off-by: Athan --- .../@stdlib/stats/incr/nanstdev/test/test.js | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js index f87067997b6d..f1ea5b147e05 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js @@ -34,12 +34,12 @@ tape( 'main export is a function', function test( t ) { }); tape( 'the function returns an accumulator function', function test( t ) { - t.equal( typeof incrnanstdev(), 'function', 'returns a function' ); + t.equal( typeof incrnanstdev(), 'function', 'returns expected value' ); t.end(); }); tape( 'the function returns an accumulator function (known mean)', function test( t ) { - t.equal( typeof incrnanstdev( 3.0 ), 'function', 'returns a function' ); + t.equal( typeof incrnanstdev( 3.0 ), 'function', 'returns expected value' ); t.end(); }); @@ -94,7 +94,7 @@ tape( 'the accumulator function incrementally computes a corrected sample standa for ( i = 0; i < data.length; i++ ) { actual.push( acc( data[ i ] ) ); } - t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.deepEqual( actual, expected, 'returns expected value' ); t.end(); }); @@ -123,11 +123,11 @@ tape( 'the accumulator function incrementally computes a corrected sample standa for ( i = 0; i < data.length; i++ ) { actual.push( acc( data[ i ] ) ); } - t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.deepEqual( actual, expected, 'returns expected value' ); t.end(); }); -tape( 'if not provided an input value, the accumulator function returns the current corrected sample sample deviation', function test( t ) { +tape( 'if not provided an input value, the accumulator function returns the current corrected sample deviation', function test( t ) { var data; var acc; var i; @@ -137,11 +137,11 @@ tape( 'if not provided an input value, the accumulator function returns the curr for ( i = 0; i < data.length; i++ ) { acc( data[ i ] ); } - t.equal( acc(), 1.0, 'returns the current accumulated corrected sample standard deviation' ); + t.equal( acc(), 1.0, 'returns expected value' ); t.end(); }); -tape( 'if not provided an input value, the accumulator function returns the current corrected sample sample deviation (known mean)', function test( t ) { +tape( 'if not provided an input value, the accumulator function returns the current corrected sample deviation (known mean)', function test( t ) { var data; var acc; var i; @@ -151,7 +151,7 @@ tape( 'if not provided an input value, the accumulator function returns the curr for ( i = 0; i < data.length; i++ ) { acc( data[ i ] ); } - t.equal( acc(), sqrt( 0.6666666666666666 ), 'returns the current accumulated corrected sample standard deviation' ); + t.equal( acc(), sqrt( 0.6666666666666666 ), 'returns expected value' ); t.end(); }); @@ -162,13 +162,13 @@ tape( 'the corrected sample standard deviation is `null` until at least 1 datum acc = incrnanstdev(); s = acc(); - t.equal( s, null, 'returns null' ); + t.equal( s, null, 'returns expected value' ); s = acc( 3.0 ); - t.notEqual( s, null, 'does not return null' ); + t.notEqual( s, null, 'returns expected value' ); s = acc(); - t.notEqual( s, null, 'does not return null' ); + t.notEqual( s, null, 'returns expected value' ); t.end(); }); @@ -180,13 +180,13 @@ tape( 'the corrected sample standard deviation is `null` until at least 1 datum acc = incrnanstdev( 3.0 ); s = acc(); - t.equal( s, null, 'returns null' ); + t.equal( s, null, 'returns expected value' ); s = acc( 3.0 ); - t.notEqual( s, null, 'does not return null' ); + t.notEqual( s, null, 'returns expected value' ); s = acc(); - t.notEqual( s, null, 'does not return null' ); + t.notEqual( s, null, 'returns expected value' ); t.end(); }); @@ -198,21 +198,21 @@ tape( 'the corrected sample standard deviation is `0` until at least 2 datums ha acc = incrnanstdev(); s = acc( 2.0 ); - t.equal( s, 0.0, 'returns 0' ); + t.equal( s, 0.0, 'returns expected value' ); s = acc(); - t.equal( s, 0.0, 'returns 0' ); + t.equal( s, 0.0, 'returns expected value' ); s = acc( 3.0 ); - t.notEqual( s, 0.0, 'does not return 0' ); + t.notEqual( s, 0.0, 'returns expected value' ); s = acc(); - t.notEqual( s, 0.0, 'does not return 0' ); + t.notEqual( s, 0.0, 'returns expected value' ); t.end(); }); -tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation.', function test( t ) { +tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation', function test( t ) { var expected; var actual; var data; @@ -238,7 +238,7 @@ tape( 'if provided a `NaN`, the accumulator function returns the current correct for ( i = 0; i < data.length; i++ ) { actual.push( acc( data[ i ] ) ); } - t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.deepEqual( actual, expected, 'returns expected value' ); t.end(); }); @@ -268,6 +268,6 @@ tape( 'if provided a `NaN`, the accumulator function returns the current correct for ( i = 0; i < data.length; i++ ) { actual.push( acc( data[ i ] ) ); } - t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.deepEqual( actual, expected, 'returns expected value' ); t.end(); }); From 94a40658f1f0ccb680b72c5860b9a77f6434cdf1 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:32:17 -0700 Subject: [PATCH 12/13] test: fix descriptions Signed-off-by: Athan --- lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js index f1ea5b147e05..f27d7d1e5b83 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/test/test.js @@ -212,7 +212,7 @@ tape( 'the corrected sample standard deviation is `0` until at least 2 datums ha t.end(); }); -tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation', function test( t ) { +tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation (unknown mean)', function test( t ) { var expected; var actual; var data; @@ -242,7 +242,7 @@ tape( 'if provided a `NaN`, the accumulator function returns the current correct t.end(); }); -tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation (unknown mean)', function test( t ) { +tape( 'if provided a `NaN`, the accumulator function returns the current corrected sample standard deviation (known mean)', function test( t ) { var expected; var actual; var data; From 3b0ca7054426bce3f37c8346fd93dc2c5d8174f0 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Apr 2025 02:35:59 -0700 Subject: [PATCH 13/13] style: fix indentation Signed-off-by: Athan --- .../@stdlib/stats/incr/nanstdev/package.json | 132 +++++++++--------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json index ad094bc37797..774c5d911bcf 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanstdev/package.json @@ -1,69 +1,69 @@ { - "name": "@stdlib/stats/incr/nanstdev", - "version": "0.0.0", - "description": "Compute a corrected sample standard deviation incrementally, ignoring NaN values.", - "license": "Apache-2.0", - "author": { + "name": "@stdlib/stats/incr/nanstdev", + "version": "0.0.0", + "description": "Compute a corrected sample standard deviation 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" - }, - "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", - "variance", - "sample variance", - "var", - "dispersion", - "standard deviation", - "stdev", - "corrected", - "central tendency", - "incremental", - "accumulator" - ] - } + } + ], + "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", + "variance", + "sample variance", + "var", + "dispersion", + "standard deviation", + "stdev", + "corrected", + "central tendency", + "incremental", + "accumulator" + ] +}