diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/README.md b/lib/node_modules/@stdlib/stats/incr/nanwmean/README.md new file mode 100644 index 000000000000..909f53c13caa --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/README.md @@ -0,0 +1,176 @@ + + +# incrwmean + +> Compute a [weighted arithmetic mean][weighted-arithmetic-mean] incrementally, ignoring `NaN` values. + +
+ +The [weighted arithmetic mean][weighted-arithmetic-mean] is defined as + + + +```math +\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanwmean = require( '@stdlib/stats/incr/nanwmean' ); +``` + +#### incrnanwmean() + +Returns an accumulator `function` which incrementally computes a [weighted arithmetic mean][weighted-arithmetic-mean], ignoring `NaN` values. + +```javascript +var accumulator = incrnanwmean(); +``` + +#### accumulator( \[x, w] ) + +If provided an input value `x` and a weight `w`, the accumulator function returns an updated weighted mean. If not provided any input values, the accumulator function returns the current mean. + +```javascript +var accumulator = incrnanwmean(); + +var mu = accumulator( 2.0, 1.0 ); +// returns 2.0 + +mu = accumulator( 2.0, 0.5 ); +// returns 2.0 + +mu = accumulator(2.0, NaN ); +// returns 2.0 + +mu = accumulator( 3.0, 1.5 ); +// returns 2.5 + +mu = accumulator(2.0, NaN ); +// returns 2.5 + +mu = accumulator(); +// returns 2.5 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. 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 incrnanwmean = require( '@stdlib/stats/incr/nanwmean' ); + +var accumulator; +var v; +var w; +var i; + +// Initialize an accumulator: +accumulator = incrnanwmean(); + +// For each simulated datum, update the weighted mean... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + if ( randu() < 0.2 ) { + w = NaN; + } + else { + w = randu() * 100.0; + } + accumulator( v, w ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanwmean/benchmark/benchmark.js new file mode 100644 index 000000000000..0cfb3fde34fc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrnanwmean = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanwmean(); + 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 = incrnanwmean(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu(), 1.0 ); + 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/nanwmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/index.d.ts new file mode 100644 index 000000000000..91b7e11bfb95 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/index.d.ts @@ -0,0 +1,63 @@ +/* +* @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 both arguments, returns an updated weighted arithmetic mean; otherwise, returns the current weighted arithmetic mean. +* +* @param x - value +* @param w - weight +* @returns weighted arithmetic mean +*/ +type accumulator = ( x?: number, w?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a weighted arithmetic mean. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanwmean(); +* +* var mu = accumulator( 2.0, 1.0 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, 0.5 ); +* // returns 2.0 +* +* mu = accumulator (2.0, NaN ); +* // returns 2.0 +* +* mu = accumulator( 3.0, 1.5 ); +* // returns 2.5 +* +* mu = accumulator (2.0, NaN ); +* // returns 2.5 +* +* mu = accumulator(); +* // returns 2.5 +*/ +declare function incrnanwmean(): accumulator; + + +// EXPORTS // + +export = incrnanwmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/test.ts new file mode 100644 index 000000000000..8e939d88c8a2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanwmean = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanwmean(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnanwmean( '5' ); // $ExpectError + incrnanwmean( 5 ); // $ExpectError + incrnanwmean( true ); // $ExpectError + incrnanwmean( false ); // $ExpectError + incrnanwmean( null ); // $ExpectError + incrnanwmean( undefined ); // $ExpectError + incrnanwmean( [] ); // $ExpectError + incrnanwmean( {} ); // $ExpectError + incrnanwmean( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanwmean(); + + acc(); // $ExpectType number | null + acc( 3.14, 1.0 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanwmean(); + + acc( '5', 1.0 ); // $ExpectError + acc( true, 1.0 ); // $ExpectError + acc( false, 1.0 ); // $ExpectError + acc( null, 1.0 ); // $ExpectError + acc( [], 1.0 ); // $ExpectError + acc( {}, 1.0 ); // $ExpectError + acc( ( x: number ): number => x, 1.0 ); // $ExpectError + + acc( 3.14, '5' ); // $ExpectError + acc( 3.14, true ); // $ExpectError + acc( 3.14, false ); // $ExpectError + acc( 3.14, null ); // $ExpectError + acc( 3.14, [] ); // $ExpectError + acc( 3.14, {} ); // $ExpectError + acc( 3.14, ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanwmean/examples/index.js new file mode 100644 index 000000000000..77edb6c66735 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/examples/index.js @@ -0,0 +1,50 @@ +/** +* @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 incrnanwmean = require( './../lib' ); + +var accumulator; +var mu; +var x; +var w; +var i; + +// Initialize an accumulator: +accumulator = incrnanwmean(); + +// For each simulated datum, update the weighted mean... +console.log( '\nValue\tWeight\tWeighted Mean\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + x = NaN; + } else { + x = randu() * 100.0; + } + if ( randu() < 0.2 ) { + w = NaN; + } + else { + w = randu() * 100.0; + } + mu = accumulator( x, w ); + console.log( '%d\t%d\t%d', x.toFixed( 4 ), w.toFixed( 4 ), ( mu === null ) ? NaN: mu.toFixed( 4 ) ); +} +console.log( '\nFinal weighted mean: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanwmean/lib/index.js new file mode 100644 index 000000000000..186a6b7a3e9e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/lib/index.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a weighted arithmetic mean incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanwmean +* +* @example +* var incrnanwmean = require( '@stdlib/stats/incr/nanwmean' ); +* +* var accumulator = incrnanwmean(); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0, 1.0 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, 0.5 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, NaN ); +* // returns 2.0 +* +* mu = accumulator( 3.0, 1.5 ); +* // returns 2.5 +* +* mu = accumulator (2.0, NaN ); +* // returns 2.5 +* +* mu = accumulator(); +* // returns 2.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanwmean/lib/main.js new file mode 100644 index 000000000000..acaccac7caf4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/lib/main.js @@ -0,0 +1,81 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrwmean = require( '@stdlib/stats/incr/wmean' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a sum, ignoring `NaN` values. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanwmean(); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0, 1.0 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, 0.5 ); +* // returns 2.0 +* +* mu = accumulator( 2.0, NaN ); +* // returns 2.0 +* +* mu = accumulator( 3.0, 1.5 ); +* // returns 2.5 +* +* mu = accumulator (2.0, NaN ); +* // returns 2.5 +* +* mu = accumulator(); +* // returns 2.5 +*/ +function incrnanwmean() { + var wmean = incrwmean(); + return accumulator; + + /** + * If provided arguments, the accumulator function returns an updated weighted mean. If not provided arguments, the accumulator function returns the current weighted mean. + * + * @private + * @param {number} [x] - value + * @param {number} [w] - weight + * @returns {(number|null)} weighted mean or null + */ + function accumulator( x, w ) { + if ( arguments.length === 0 || isnan( x ) || isnan( w ) ) { + return wmean(); + } + return wmean( x, w ); + } +} + + +// EXPORTS // + +module.exports = incrnanwmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/package.json b/lib/node_modules/@stdlib/stats/incr/nanwmean/package.json new file mode 100644 index 000000000000..6f358b2d3315 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/stats/incr/nanwmean", + "version": "0.0.0", + "description": "Compute a weighted arithmetic 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", + "summation", + "sum", + "total", + "incremental", + "accumulator" + ] + } diff --git a/lib/node_modules/@stdlib/stats/incr/nanwmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanwmean/test/test.js new file mode 100644 index 000000000000..879e49a2ac5b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanwmean/test/test.js @@ -0,0 +1,119 @@ +/** +* @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 EPS = require( '@stdlib/constants/float64/eps' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var incrnanwmean = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanwmean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanwmean(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnanwmean(); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a weighted arithmetic mean', function test( t ) { + var expected; + var actual; + var delta; + var dataX; + var dataW; + var xwSum; + var wSum; + var acc; + var tol; + var N; + var x; + var w; + var i; + + dataX = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]; + dataW = [ 1.0, 2.0, 0.1, 1.8, 9.9, 3.6 ]; + N = dataX.length; + + acc = incrnanwmean(); + + xwSum = 0.0; + wSum = 0.0; + for ( i = 0; i < N; i++ ) { + x = dataX[ i ]; + w = dataW[ i ]; + xwSum += x * w; + wSum += w; + expected = xwSum / wSum; + actual = acc( x, w ); + delta = abs( actual - expected ); + tol = EPS * abs( expected ); + t.ok( delta <= tol, 'within tolerance. x: ' + x + '. Value: ' + actual + '. Expected: ' + expected + '. Tolerance: ' + tol + '.' ); + } + t.end(); +}); + +tape( 'if not provided arguments, the accumulator function returns the current weighted mean', function test( t ) { + var dataX; + var dataW; + var acc; + var i; + + dataX = [ 2.0, NaN, 3.0, 2.0, 1.0 ]; + dataW = [ 2.0, 2.0, 2.0, NaN, 1.0 ]; + acc = incrnanwmean(); + for ( i = 0; i < dataX.length; i++ ) { + acc( dataX[ i ], dataW[ i ] ); + } + t.equal( acc(), 2.2, 'returns the current accumulated mean' ); + t.end(); +}); + +tape( 'if provided `NaN` for either a value or a weight, the accumulator function returns the current weighted mean', function test( t ) { + var dataX; + var dataW; + var acc; + var i; + + dataX = [ 2.0, NaN, 3.0, 2.0, 1.0 ]; + dataW = [ 2.0, 2.0, 2.0, NaN, 1.0 ]; + acc = incrnanwmean(); + for ( i = 0; i < dataX.length; i++ ) { + acc( dataX[ i ], dataW[ i ] ); + } + + t.equal( acc( 2.0, NaN ), 2.2, 'returns the current accumulated mean' ); + t.equal( acc( NaN, 1.0 ), 2.2, 'returns the current accumulated mean' ); + t.equal( acc( NaN, NaN ), 2.2, 'returns the current accumulated mean' ); + + t.end(); +});