diff --git a/lib/node_modules/@stdlib/stats/incr/nanewmean/README.md b/lib/node_modules/@stdlib/stats/incr/nanewmean/README.md new file mode 100644 index 000000000000..2e2eb84ffdbe --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/README.md @@ -0,0 +1,159 @@ + + +# incrnanewmean + +> Compute an [exponentially weighted mean][moving-average] incrementally, ignoring `NaN` values. + +
+ +An [exponentially weighted mean][moving-average] can be defined recursively as + + + +```math +\mu_t = \begin{cases} x_0 & \textrm{if}\ t = 0 \\ \alpha x_t + (1-\alpha) \mu_{t-1} & \textrm{if}\ t > 0 \end{cases} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanewmean = require( '@stdlib/stats/incr/nanewmean' ); +``` + +#### incrnanewmean( alpha ) + +Returns an accumulator `function` which incrementally computes an [exponentially weighted mean][moving-average], where `alpha` is a smoothing factor between `0` and `1`, ignoring `NaN` values. + +```javascript +var accumulator = incrnanewmean( 0.5 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated mean. If not provided an input value `x`, the accumulator function returns the current mean. + +```javascript +var accumulator = incrnanewmean( 0.5 ); + +var v = accumulator(); +// returns null + +v = accumulator( 2.0 ); +// returns 2.0 + +v = accumulator( 1.0 ); +// returns 1.5 + +v = accumulator( NaN ); +// returns 1.5 + +v = accumulator( 3.0 ); +// returns 2.25 + +v = accumulator(); +// returns 2.25 +``` + +
+ + + +
+ +## 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 incrnanewmean = require( '@stdlib/stats/incr/nanewmean' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanewmean( 0.5 ); + +// For each simulated datum, update the exponentially weighted mean... +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/nanewmean/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanewmean/benchmark/benchmark.js new file mode 100644 index 000000000000..5cc2fecefdc3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/benchmark/benchmark.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var incrnanewmean = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanewmean( 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( format( '%s::accumulator', pkg ), function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanewmean( 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/nanewmean/docs/img/equation_exponentially_weighted_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/img/equation_exponentially_weighted_mean.svg new file mode 100644 index 000000000000..b89cfc9aaae1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/img/equation_exponentially_weighted_mean.svg @@ -0,0 +1,74 @@ + +mu Subscript t Baseline equals StartLayout Enlarged left-brace 1st Row 1st Column x 0 2nd Column if t equals 0 2nd Row 1st Column alpha x Subscript t Baseline plus left-parenthesis 1 minus alpha right-parenthesis mu Subscript t minus 1 Baseline 2nd Column if t greater-than 0 EndLayout + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/repl.txt new file mode 100644 index 000000000000..10bb20079f92 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( α ) + Returns an accumulator function which incrementally computes an + exponentially weighted mean, where α is a smoothing factor between 0 and 1, + ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated mean. If + not provided a value, the accumulator function returns the current mean. + + 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 ) + 2.0 + > v = accumulator( NaN ) + 2.0 + > v = accumulator( -5.0 ) + -1.5 + > v = accumulator() + -1.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/types/index.d.ts new file mode 100644 index 000000000000..2aea8a3aff4b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/types/index.d.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided a value, the accumulator function returns an updated mean. If not provided a value, the accumulator function returns the current mean. +* +* @param x - value +* @returns mean value +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an exponentially weighted mean, ignoring `NaN` values. +* +* @param alpha - smoothing factor +* @throws must be on the interval `[0,1]` +* @returns accumulator function +* +* @example +* var accumulator = incrnanewmean( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( -5.0 ); +* // returns -1.5 +* +* v = accumulator(); +* // returns -1.5 +*/ +declare function incrnanewmean( alpha: number ): accumulator; + + +// EXPORTS // + +export = incrnanewmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/types/test.ts new file mode 100644 index 000000000000..a18e75baac78 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanewmean = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanewmean( 0.5 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument that is not a number... +{ + incrnanewmean( '5' ); // $ExpectError + incrnanewmean( true ); // $ExpectError + incrnanewmean( false ); // $ExpectError + incrnanewmean( null ); // $ExpectError + incrnanewmean( undefined ); // $ExpectError + incrnanewmean( [] ); // $ExpectError + incrnanewmean( {} ); // $ExpectError + incrnanewmean( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanewmean( 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 = incrnanewmean( 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/nanewmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanewmean/examples/index.js new file mode 100644 index 000000000000..c72b3ee82ac5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var incrnanewmean = require( './../lib' ); + +var accumulator; +var mu; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanewmean( 0.5 ); + +// For each simulated datum, update the exponentially weighted mean... +console.log( '\nValue\tMean\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + mu = accumulator( v ); + console.log( '%s\t%s', ( v === null ) ? v : v.toFixed( 4 ), ( mu === null ) ? mu : mu.toFixed( 4 ) ); +} +console.log( '\nFinal mean: %s\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanewmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanewmean/lib/index.js new file mode 100644 index 000000000000..9d20d2c5ad79 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute an exponentially weighted mean incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanewmean +* +* @example +* var incrnanewmean = require( '@stdlib/stats/incr/nanewmean' ); +* +* var accumulator = incrnanewmean( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.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/nanewmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanewmean/lib/main.js new file mode 100644 index 000000000000..95dfc9963df5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/lib/main.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var increwmean = require( '@stdlib/stats/incr/ewmean' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an exponentially weighted mean, ignoring `NaN` values. +* +* @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 +* var accumulator = incrnanewmean( 0.5 ); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( -5.0 ); +* // returns -1.5 +* +* v = accumulator(); +* // returns -1.5 +*/ +function incrnanewmean( alpha ) { + var ewmean = increwmean( alpha ); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated mean. If not provided a value, the accumulator function returns the current mean. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} mean value or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return ewmean(); + } + return ewmean( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanewmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanewmean/package.json b/lib/node_modules/@stdlib/stats/incr/nanewmean/package.json new file mode 100644 index 000000000000..f35f24e8c535 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/incr/nanewmean", + "version": "0.0.0", + "description": "Compute an exponentially weighted mean incrementally, ignoring NaN values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "average", + "avg", + "mean", + "weighted", + "exponential", + "moving average", + "ema", + "ewma", + "incremental", + "accumulator" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanewmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanewmean/test/test.js new file mode 100644 index 000000000000..7313f531ba20 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanewmean/test/test.js @@ -0,0 +1,133 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var incrnanewmean = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanewmean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a nonnegative number', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + NaN, + 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() { + incrnanewmean( value ); + }; + } +}); + +tape( 'the function throws an error if provided a nonnegative number which does not reside on the interval `[0,1]`', function test( t ) { + var values; + var i; + + values = [ + 1.5, + 3.14, + 1.0001, + 1.0e300 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanewmean( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanewmean( 0.5 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnanewmean( 0.5 ); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an exponentially weighted mean', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 2.0, NaN, 4.0, NaN, 3.0, 4.0 ]; + N = data.length; + + expected = [ 2.0, 2.5, 2.25, 2.25, 3.125, 3.125, 3.0625, 3.53125 ]; + actual = []; + + acc = incrnanewmean( 0.5 ); + + for ( i = 0; i < N; 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 mean', function test( t ) { + var data; + var acc; + var i; + + data = [ NaN, 3.0, 1.0 ]; + acc = incrnanewmean( 0.5 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 2.0, 'returns the current accumulated mean' ); + t.end(); +});