From 370ba17ba857d9d543926d45826fbe891e4f1164 Mon Sep 17 00:00:00 2001 From: Jayhari Aditiya Date: Fri, 14 Mar 2025 13:31:43 +0530 Subject: [PATCH 1/7] test(stats/incr): update test cases for nanewvariance3 --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../stats/incr/nanewvariance/README.md | 148 ++++++++++++++++++ .../incr/nanewvariance/benchmark/benchmark.js | 67 ++++++++ ...uation_exponentially_weighted_variance.svg | 89 +++++++++++ .../stats/incr/nanewvariance/docs/repl.txt | 42 +++++ .../incr/nanewvariance/docs/types/index.d.ts | 77 +++++++++ .../incr/nanewvariance/docs/types/test.ts | 57 +++++++ .../incr/nanewvariance/examples/index.js | 39 +++++ .../stats/incr/nanewvariance/lib/index.js | 57 +++++++ .../stats/incr/nanewvariance/lib/main.js | 81 ++++++++++ .../stats/incr/nanewvariance/package.json | 66 ++++++++ .../stats/incr/nanewvariance/test/test.js | 94 +++++++++++ 11 files changed, 817 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/img/equation_exponentially_weighted_variance.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md new file mode 100644 index 000000000000..6bd61f99e65d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md @@ -0,0 +1,148 @@ + + +# nanewvariance + +> Compute an [exponentially weighted variance][exponentially weighted variance] incrementally, ignoring `NaN` values. + +
+ +The [exponentially weighted variance][exponentially weighted variance] is defined as + + + +```math +S_n = \begin{cases} 0 & \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n - \mu_{n-1})^2) & \textrm{if}\ n > 0 \end{cases} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var nanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +``` + +#### nanewvariance() + +Returns an accumulator function which incrementally computes an [exponentially weighted variance][exponentially weighted variance], ignoring `NaN` values. + +```javascript +var accumulator = nanewvariance( 0.5 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated exponentially weighted variance. If not provided an input value `x`, the accumulator function returns the current exponentially weighted variance. + +```javascript +var accumulator = nanewvariance( 0.5 ); + +var v = accumulator(); +// returns null + +v = accumulator( 2.0 ); +// returns 0 + +v = accumulator( 4.0 ); +// returns 1 + +v = accumulator( NaN ); +// returns 1 + +v = accumulator( 5.0 ); +// returns 1.5 + +v = accumulator(); +// returns 1.5 +``` + +
+ + + +
+ +## 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 nanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator with smoothing factor α = 0.5: +accumulator = nanewvariance( 0.5 ); + +// For each simulated datum, update the exponentially weighted variance... +for ( i = 0; i < 100; i++ ) { + // With a 20% chance, provide NaN to test that invalid inputs are ignored: + v = ( randu() < 0.2 ) ? NaN : randu() * 100.0; + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js new file mode 100644 index 000000000000..cb5158f07032 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js @@ -0,0 +1,67 @@ +/** +* @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 nanewvariance = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = nanewvariance( 0.5 ); + 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 = nanewvariance( 0.5 ); + 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/nanewvariance/docs/img/equation_exponentially_weighted_variance.svg b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/img/equation_exponentially_weighted_variance.svg new file mode 100644 index 000000000000..ec759179e837 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/img/equation_exponentially_weighted_variance.svg @@ -0,0 +1,89 @@ + +upper S Subscript n Baseline equals StartLayout Enlarged left-brace 1st Row 1st Column 0 2nd Column if n equals 0 2nd Row 1st Column left-parenthesis 1 minus alpha right-parenthesis left-parenthesis upper S Subscript n minus 1 Baseline plus alpha left-parenthesis x Subscript n Baseline minus mu Subscript n minus 1 Baseline right-parenthesis squared right-parenthesis 2nd Column if n greater-than 0 EndLayout + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/repl.txt new file mode 100644 index 000000000000..8a7fe4f004c9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( α ) + Returns an accumulator function which incrementally computes an + exponentially weighted variance, where α is a smoothing factor between 0 and + 1. This accumulator ignores any input values that are `NaN`. + + If provided a value, the accumulator function returns an updated variance. + If not provided a value, the accumulator function returns the current + variance. + + If provided `NaN` or a value which, when used in computations, results in + `NaN`, that input is ignored and the accumulated variance remains unchanged. + + Parameters + ---------- + α: number + Smoothing factor (value between 0 and 1). + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 0.5 ); + > var v = accumulator() + null + > v = accumulator( 2.0 ) + 0 + > v = accumulator( 4.0 ) + 1 + > v = accumulator( NaN ) + 1 + > v = accumulator( 5.0 ) + 1.5 + > v = accumulator() + 1.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts new file mode 100644 index 000000000000..bd2b613e0557 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts @@ -0,0 +1,77 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided a value, the accumulator function returns an updated variance. If not provided a value, the accumulator function returns the current variance. +* +* ## Notes +* +* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the input is ignored and the accumulated value remains unchanged. +* +* @param x - value +* @returns variance or null +*/ +type Accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an exponentially weighted variance, +* ignoring `NaN` values. +* +* @param alpha - smoothing factor +* @throws must provide a nonnegative number +* @throws must be on the interval `[0,1]` +* @returns accumulator function +* +* @example +* var accumulator = nanewvariance( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns null +* +* v = accumulator( 4.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( 5.0 ); +* // returns 2.3 +* +* v = accumulator(); +* // returns 2.3 +*/ +declare namespace incrnanewvariance { + /** + * If provided a value, returns an updated exponentially weighted variance; otherwise, returns the current variance. + * + * @param x - input value + * @returns exponentially weighted variance or null + */ + type Accumulator = ( x?: number ) => number | null; +} + +// EXPORTS // + +export = incrnanewvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts new file mode 100644 index 000000000000..ba2061094141 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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 nanewvariance = require( './index' ); + +// TESTS // + +// The function returns an accumulator function... +{ + nanewvariance( 0.5 ); // $ExpectType Accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + nanewvariance( '5' ); // $ExpectError + nanewvariance( true ); // $ExpectError + nanewvariance( false ); // $ExpectError + nanewvariance( null ); // $ExpectError + nanewvariance( undefined ); // $ExpectError + nanewvariance( [] ); // $ExpectError + nanewvariance( {} ); // $ExpectError + nanewvariance( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = nanewvariance( 0.5 ); + 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 = nanewvariance( 0.5 ); + 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/nanewvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js new file mode 100644 index 000000000000..c8bcd6d0b2b1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js @@ -0,0 +1,39 @@ +/** +* @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 nanewvariance = require( './..' ); + +var accumulator; +var s2; +var v; +var i; + +// Initialize an accumulator: +accumulator = nanewvariance( 0.5 ); + +// For each simulated datum, update the exponentially weighted variance... +console.log('\nValue\tVariance\n'); +for ( i = 0; i < 100; i++ ) { + v = (randu() < 0.2) ? NaN : randu() * 100.0; + s2 = accumulator( v ); + console.log('%s\t%s', ((isNaN(v)) ? 'NaN' : v.toFixed(4)), ((s2 === null) ? 'null' : s2.toFixed(4))); +} +console.log('\nFinal variance: %s\n', ((accumulator() === null) ? 'null' : accumulator().toFixed(4))); diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js new file mode 100644 index 000000000000..089f4f0623eb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js @@ -0,0 +1,57 @@ +/** +* @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 incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanewvariance +* +* @example +* var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +* +* var accumulator = incrnanewvariance(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0 +* +* v = accumulator( 4.0 ); +* // returns 1.0 +* +* v = accumulator( NaN ); +* // returns 1.0 +* +* v = accumulator( 5.0 ); +* // returns 1.5 +* +* v = accumulator(); +* // returns 1.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js new file mode 100644 index 000000000000..be27ad9df361 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js @@ -0,0 +1,81 @@ +/** +* @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 increwvariance = require( '@stdlib/stats/incr/ewvariance' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an arithmetic mean, ignoring `NaN` values. +* +* @param {number} alpha - smoothing factor +* @returns {Function} accumulator function +* +* @example +* var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +* +* var accumulator = incrnanewvariance( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 0 +* +* v = accumulator( 4.0 ); +* // returns 1.0 +* +* v = accumulator( NaN ); +* // returns 1.0 +* +* v = accumulator( 5.0 ); +* // returns 1.5 +* +* v = accumulator(); +* // returns 1.5 +*/ +function nanewvariance(alpha) { + var acc = increwvariance( alpha ); + return accumulator; + + /** + * Updates and returns the exponentially weighted variance when provided a numeric input. + * If no input is provided, returns the current exponentially weighted variance. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} updated exponentially weighted variance or null + */ + function accumulator(x) { + if ( arguments.length === 0 || isnan( x ) ) { + return acc(); + } + return acc( x ); + } +} + + +// EXPORTS // + +module.exports = nanewvariance; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json b/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json new file mode 100644 index 000000000000..2e754d96f185 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/stats/incr/nanewvariance", + "version": "0.0.0", + "description": "Compute an exponentially weighted 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", + "weighted variance", + "ewvariance", + "variance", + "exponential", + "smoothing factor", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js new file mode 100644 index 000000000000..d1867e78f9f1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js @@ -0,0 +1,94 @@ +/** +* @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 nanewvariance = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = nanewvariance( 0.5 ); + t.strictEqual( acc(), null, 'returns null initially' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an exponentially weighted variance', 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 ]; + expected = [ + 0.0, // after 1st input + 0.25, // after 2nd input + 0.1875, // after 3rd input + 0.859375, // after 4th input + 0.43359375, // after 5th input + 0.4365234375 // after 6th input + ]; + + actual = []; + acc = nanewvariance( 0.5 ); + for ( i = 0; i < data.length; i++ ) { + actual.push( acc( data[ i ] ) ); + } + t.deepEqual( actual, expected, 'returns expected values' ); + t.end(); +}); + +tape( 'if not provided an input value, accumulator returns current variance', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = nanewvariance( 0.5 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.strictEqual( acc(), 0.6875, 'returns expected variance' ); + t.end(); +}); + +tape( 'the accumulator function skips NaN inputs', function test( t ) { + var acc = nanewvariance( 0.5 ); + var v; + + v = acc( 2.0 ); + t.strictEqual( v, 0.0, 'returns 0.0 with one valid value' ); + + v = acc( NaN ); + t.strictEqual( v, 0.0, 'still returns 0.0 after NaN input' ); + + v = acc( 4.0 ); + t.strictEqual( v, 1.0, 'returns correct variance after second valid input' ); + + t.end(); +}); From a901b83592269fd983a7bbb5b73962577139f2cb Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 15 Jul 2026 00:35:50 -0500 Subject: [PATCH 2/7] fix: address review feedback Correct JSDoc and package descriptions to describe an exponentially weighted variance, add a callable `declare function` signature and remove the unused standalone type in the TypeScript declarations, and use `@stdlib/math/base/assert/is-nan` with stdlib spacing conventions in the example. --- .../incr/nanewvariance/docs/types/index.d.ts | 30 +++++++------------ .../incr/nanewvariance/docs/types/test.ts | 2 +- .../incr/nanewvariance/examples/index.js | 9 +++--- .../stats/incr/nanewvariance/lib/index.js | 4 +-- .../stats/incr/nanewvariance/lib/main.js | 10 ++++--- .../stats/incr/nanewvariance/package.json | 2 +- 6 files changed, 26 insertions(+), 31 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts index bd2b613e0557..acf40c852f3d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts @@ -25,16 +25,15 @@ * * ## Notes * -* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the input is ignored and the accumulated value remains unchanged. +* - If provided `NaN`, the input is ignored and the accumulated value remains unchanged. * * @param x - value * @returns variance or null */ -type Accumulator = ( x?: number ) => number | null; +type accumulator = ( x?: number ) => number | null; /** -* Returns an accumulator function which incrementally computes an exponentially weighted variance, -* ignoring `NaN` values. +* Returns an accumulator function which incrementally computes an exponentially weighted variance, ignoring `NaN` values. * * @param alpha - smoothing factor * @throws must provide a nonnegative number @@ -42,35 +41,28 @@ type Accumulator = ( x?: number ) => number | null; * @returns accumulator function * * @example -* var accumulator = nanewvariance( 0.5 ); +* var accumulator = incrnanewvariance( 0.5 ); * * var v = accumulator(); * // returns null * * v = accumulator( 2.0 ); -* // returns null +* // returns 0 * * v = accumulator( 4.0 ); -* // returns 2.0 +* // returns 1.0 * * v = accumulator( NaN ); -* // returns 2.0 +* // returns 1.0 * * v = accumulator( 5.0 ); -* // returns 2.3 +* // returns 1.5 * * v = accumulator(); -* // returns 2.3 +* // returns 1.5 */ -declare namespace incrnanewvariance { - /** - * If provided a value, returns an updated exponentially weighted variance; otherwise, returns the current variance. - * - * @param x - input value - * @returns exponentially weighted variance or null - */ - type Accumulator = ( x?: number ) => number | null; -} +declare function incrnanewvariance( alpha: number ): accumulator; + // EXPORTS // diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts index ba2061094141..b7dd9f7fc17d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts @@ -22,7 +22,7 @@ import nanewvariance = require( './index' ); // The function returns an accumulator function... { - nanewvariance( 0.5 ); // $ExpectType Accumulator + nanewvariance( 0.5 ); // $ExpectType accumulator } // The compiler throws an error if the function is provided an argument that is not a number... diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js index c8bcd6d0b2b1..d5e663091fe2 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js @@ -19,6 +19,7 @@ 'use strict'; var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); var nanewvariance = require( './..' ); var accumulator; @@ -30,10 +31,10 @@ var i; accumulator = nanewvariance( 0.5 ); // For each simulated datum, update the exponentially weighted variance... -console.log('\nValue\tVariance\n'); +console.log( '\nValue\tVariance\n' ); for ( i = 0; i < 100; i++ ) { - v = (randu() < 0.2) ? NaN : randu() * 100.0; + v = ( randu() < 0.2 ) ? NaN : randu() * 100.0; s2 = accumulator( v ); - console.log('%s\t%s', ((isNaN(v)) ? 'NaN' : v.toFixed(4)), ((s2 === null) ? 'null' : s2.toFixed(4))); + console.log( '%s\t%s', ( isnan( v ) ) ? 'NaN' : v.toFixed( 4 ), ( s2 === null ) ? 'null' : s2.toFixed( 4 ) ); } -console.log('\nFinal variance: %s\n', ((accumulator() === null) ? 'null' : accumulator().toFixed(4))); +console.log( '\nFinal variance: %s\n', ( accumulator() === null ) ? 'null' : accumulator().toFixed( 4 ) ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js index 089f4f0623eb..fd28a8ad62de 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js @@ -19,14 +19,14 @@ 'use strict'; /** -* Compute an arithmetic mean incrementally, ignoring `NaN` values. +* Compute an exponentially weighted variance incrementally, ignoring `NaN` values. * * @module @stdlib/stats/incr/nanewvariance * * @example * var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); * -* var accumulator = incrnanewvariance(); +* var accumulator = incrnanewvariance( 0.5 ); * * var v = accumulator(); * // returns null diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js index be27ad9df361..be5a32cdd110 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js @@ -27,9 +27,11 @@ var increwvariance = require( '@stdlib/stats/incr/ewvariance' ); // MAIN // /** -* Returns an accumulator function which incrementally computes an arithmetic mean, ignoring `NaN` values. +* Returns an accumulator function which incrementally computes an exponentially weighted variance, ignoring `NaN` values. * -* @param {number} alpha - smoothing factor +* @param {NonNegativeNumber} alpha - smoothing factor +* @throws {TypeError} must provide a nonnegative number +* @throws {RangeError} must be on the interval `[0,1]` * @returns {Function} accumulator function * * @example @@ -55,7 +57,7 @@ var increwvariance = require( '@stdlib/stats/incr/ewvariance' ); * v = accumulator(); * // returns 1.5 */ -function nanewvariance(alpha) { +function nanewvariance( alpha ) { var acc = increwvariance( alpha ); return accumulator; @@ -67,7 +69,7 @@ function nanewvariance(alpha) { * @param {number} [x] - new value * @returns {(number|null)} updated exponentially weighted variance or null */ - function accumulator(x) { + function accumulator( x ) { if ( arguments.length === 0 || isnan( x ) ) { return acc(); } diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json b/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json index 2e754d96f185..fb7d368454f2 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/stats/incr/nanewvariance", "version": "0.0.0", - "description": "Compute an exponentially weighted variance incrementally, ignoring `NaN` values.", + "description": "Compute an exponentially weighted variance incrementally, ignoring NaN values.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", From c5b529ea9382f4197ab3b589695fe90eb14c2e86 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 15 Jul 2026 00:43:11 -0500 Subject: [PATCH 3/7] chore: update copyright years --- lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md | 2 +- .../@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js | 2 +- .../@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts | 2 +- .../@stdlib/stats/incr/nanewvariance/docs/types/test.ts | 2 +- .../@stdlib/stats/incr/nanewvariance/examples/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js | 2 +- lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md index 6bd61f99e65d..039991ceed70 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2025 The Stdlib Authors. +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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js index cb5158f07032..9e86b090aa7f 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts index acf40c852f3d..d352ce759321 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts index b7dd9f7fc17d..29761e9fb8a8 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js index d5e663091fe2..e690b056e08d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js index fd28a8ad62de..2470b910ecf1 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js index be5a32cdd110..480cf917fb7a 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js index d1867e78f9f1..87bb602bec78 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. From 829a5584a34bc1463e0f090282003191de9f785d Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 15 Jul 2026 01:14:03 -0500 Subject: [PATCH 4/7] refactor: rename accumulator factory to incrnanewvariance Aligns the function and module export with the incr-prefixed naming convention used across stats/incr packages and the package's own docs/types. --- lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js index 480cf917fb7a..8df03ffe3dfc 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/lib/main.js @@ -57,7 +57,7 @@ var increwvariance = require( '@stdlib/stats/incr/ewvariance' ); * v = accumulator(); * // returns 1.5 */ -function nanewvariance( alpha ) { +function incrnanewvariance( alpha ) { var acc = increwvariance( alpha ); return accumulator; @@ -80,4 +80,4 @@ function nanewvariance( alpha ) { // EXPORTS // -module.exports = nanewvariance; +module.exports = incrnanewvariance; From 2d845af815da9037d46ed609403d12de34e214d2 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 15 Jul 2026 01:14:04 -0500 Subject: [PATCH 5/7] style: use format for benchmark name Replace string concatenation with format( '%s::accumulator', pkg ) and add the @stdlib/string/format require. --- .../@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js index 9e86b090aa7f..31302d039195 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/benchmark/benchmark.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench' ); var randu = require( '@stdlib/random/base/randu' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var nanewvariance = require( './../lib' ); @@ -46,7 +47,7 @@ bench( pkg, function benchmark( b ) { b.end(); }); -bench( pkg+'::accumulator', function benchmark( b ) { +bench( format( '%s::accumulator', pkg ), function benchmark( b ) { var acc; var v; var i; From 3991754ee60d72826c80bc33b24d322b95327174 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 15 Jul 2026 01:14:04 -0500 Subject: [PATCH 6/7] docs: fix equation annotations and rename to incrnanewvariance Correct the equation label/raw text and SVG image source, and rename README references to the incr-prefixed identifier. --- .../stats/incr/nanewvariance/README.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md index 039991ceed70..c65abc49c7c3 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# nanewvariance +# incrnanewvariance > Compute an [exponentially weighted variance][exponentially weighted variance] incrementally, ignoring `NaN` values. @@ -26,14 +26,14 @@ limitations under the License. The [exponentially weighted variance][exponentially weighted variance] is defined as - + ```math S_n = \begin{cases} 0 & \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n - \mu_{n-1})^2) & \textrm{if}\ n > 0 \end{cases} ``` - @@ -48,15 +48,15 @@ S_n = \begin{cases} 0 & \textrm{if}\ n = 0 \\ (1 - \alpha) (S_{n-1} + \alpha(x_n ## Usage ```javascript -var nanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); ``` -#### nanewvariance() +#### incrnanewvariance( alpha ) Returns an accumulator function which incrementally computes an [exponentially weighted variance][exponentially weighted variance], ignoring `NaN` values. ```javascript -var accumulator = nanewvariance( 0.5 ); +var accumulator = incrnanewvariance( 0.5 ); ``` #### accumulator( \[x] ) @@ -64,7 +64,7 @@ var accumulator = nanewvariance( 0.5 ); If provided an input value `x`, the accumulator function returns an updated exponentially weighted variance. If not provided an input value `x`, the accumulator function returns the current exponentially weighted variance. ```javascript -var accumulator = nanewvariance( 0.5 ); +var accumulator = incrnanewvariance( 0.5 ); var v = accumulator(); // returns null @@ -107,14 +107,14 @@ v = accumulator(); ```javascript var randu = require( '@stdlib/random/base/randu' ); -var nanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); +var incrnanewvariance = require( '@stdlib/stats/incr/nanewvariance' ); var accumulator; var v; var i; // Initialize an accumulator with smoothing factor α = 0.5: -accumulator = nanewvariance( 0.5 ); +accumulator = incrnanewvariance( 0.5 ); // For each simulated datum, update the exponentially weighted variance... for ( i = 0; i < 100; i++ ) { From 210527bcc35958bd7a2b5e6cd9ac43e2fbd8a223 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 15 Jul 2026 01:16:56 -0500 Subject: [PATCH 7/7] style: add blank line before test section separator Match the two-blank-line convention preceding the `// TESTS //` separator used across sibling packages. --- .../@stdlib/stats/incr/nanewvariance/docs/types/test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts index 29761e9fb8a8..ae83d18dc3d5 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts +++ b/lib/node_modules/@stdlib/stats/incr/nanewvariance/docs/types/test.ts @@ -18,6 +18,7 @@ import nanewvariance = require( './index' ); + // TESTS // // The function returns an accumulator function...