diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/README.md b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/README.md new file mode 100644 index 000000000000..dbef25bded83 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/README.md @@ -0,0 +1,141 @@ + + +# incrnanmeanabs + +> Compute an [arithmetic mean][arithmetic-mean] of absolute values incrementally, ignoring `NaN` values. + +
+ +The [arithmetic mean][arithmetic-mean] of absolute values is defined as + + + +```math +\mu = \frac{1}{n} \sum_{i=0}^{n-1} |x_i| +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' ); +``` + +#### incrnanmeanabs() + +Returns an accumulator function which incrementally computes an [arithmetic mean][arithmetic-mean] of absolute values, ignoring `NaN` values. + +```javascript +var accumulator = incrnanmeanabs(); +``` + +#### 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 = incrnanmeanabs(); + +var mu = accumulator( 2.0 ); +// returns 2.0 + +mu = accumulator( NaN ); +// returns 2.0 + +mu = accumulator( -1.0 ); +// returns 1.5 + +mu = accumulator( -3.0 ); +// returns 2.0 + +mu = accumulator(); +// returns 2.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 uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' ); + +// Initialize an accumulator: +var accumulator = incrnanmeanabs(); + +// For each simulated datum, update the mean... +var i; +for ( i = 0; i < 100; i++ ) { + accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -100.0, 100.0 ) ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/benchmark/benchmark.js new file mode 100644 index 000000000000..9e67b90ecb26 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/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 incrnanmeanabs = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmeanabs(); + 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 = incrnanmeanabs(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu()-0.5 ); + 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/nanmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg new file mode 100644 index 000000000000..257657a0f016 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/img/equation_arithmetic_mean_absolute_values.svg @@ -0,0 +1,45 @@ + +mu equals StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/repl.txt new file mode 100644 index 000000000000..eed62415b149 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes an arithmetic + mean of absolute values, 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. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var mu = accumulator() + null + > mu = accumulator( 2.0 ) + 2.0 + > mu = accumulator( NaN ) + 2.0 + > mu = accumulator( -5.0 ) + 3.5 + > mu = accumulator() + 3.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/types/index.d.ts new file mode 100644 index 000000000000..89a6ecaaf3d6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/** +* If provided a value, returns an updated arithmetic mean; otherwise, returns the current arithmetic mean. +* +* @param x - value +* @returns arithmetic mean of absolute values +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes an arithmetic mean of absolute values, ignoring `NaN` values. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanmeanabs(); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( NaN ); +* // returns 2.0 +* +* mu = accumulator( -3.0 ); +* // returns 2.5 +* +* mu = accumulator( -4.0 ); +* // returns 3.0 +* +* mu = accumulator(); +* // returns 3.0 +*/ +declare function incrnanmeanabs(): accumulator; + + +// EXPORTS // + +export = incrnanmeanabs; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/types/test.ts new file mode 100644 index 000000000000..f127ba3950a6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanmeanabs = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmeanabs(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnanmeanabs( '5' ); // $ExpectError + incrnanmeanabs( 5 ); // $ExpectError + incrnanmeanabs( true ); // $ExpectError + incrnanmeanabs( false ); // $ExpectError + incrnanmeanabs( null ); // $ExpectError + incrnanmeanabs( undefined ); // $ExpectError + incrnanmeanabs( [] ); // $ExpectError + incrnanmeanabs( {} ); // $ExpectError + incrnanmeanabs( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmeanabs(); + + 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 = incrnanmeanabs(); + + 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/nanmeanabs/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/examples/index.js new file mode 100644 index 000000000000..835617e1b915 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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 uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); +var incrnanmeanabs = require( './../lib' ); + +// Initialize an accumulator: +var accumulator = incrnanmeanabs(); + +// For each simulated datum, update the mean... +console.log( '\nValue\tMean\n' ); +var mu; +var v; +var i; +for ( i = 0; i < 100; i++ ) { + v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -100.0, 100.0 ); + mu = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 3 ), ( mu === null ) ? NaN : mu.toFixed( 3 ) ); +} +console.log( '\nFinal mean: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/lib/index.js new file mode 100644 index 000000000000..7f3da39488b7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/lib/index.js @@ -0,0 +1,54 @@ +/** +* @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 of absolute values incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanmeanabs +* +* @example +* var incrnanmeanabs = require( '@stdlib/stats/incr/nanmeanabs' ); +* +* var accumulator = incrnanmeanabs(); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( NaN ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns 3.5 +* +* mu = accumulator(); +* // returns 3.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/lib/main.js new file mode 100644 index 000000000000..85e23c72b8bf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/lib/main.js @@ -0,0 +1,74 @@ +/** +* @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 incrmeanabs = require( '@stdlib/stats/incr/meanabs' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes an arithmetic mean of absolute values, ignoring `NaN` values. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmeanabs(); +* +* var mu = accumulator(); +* // returns null +* +* mu = accumulator( 2.0 ); +* // returns 2.0 +* +* mu = accumulator( NaN ); +* // returns 2.0 +* +* mu = accumulator( -5.0 ); +* // returns 3.5 +* +* mu = accumulator(); +* // returns 3.5 +*/ +function incrnanmeanabs() { + var meanabs = incrmeanabs(); + 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 meanabs(); + } + return meanabs( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanmeanabs; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/package.json b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/package.json new file mode 100644 index 000000000000..b4d0f75662f7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/incr/nanmeanabs", + "version": "0.0.0", + "description": "Compute an arithmetic mean of absolute values 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", + "arithmetic mean", + "central tendency", + "incremental", + "accumulator", + "absolute", + "value", + "abs", + "magnitude", + "math.abs" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmeanabs/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/test/test.js new file mode 100644 index 000000000000..1252eb13e12c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmeanabs/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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var incrnanmeanabs = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmeanabs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmeanabs(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is `null`', function test( t ) { + var acc = incrnanmeanabs(); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes an arithmetic mean of absolute values', function test( t ) { + var expected; + var actual; + var count; + var data; + var sum; + var acc; + var i; + var d; + var N; + + data = [ 2.0, -3.0, 2.0, NaN, -4.0, NaN, 3.0, -4.0 ]; + N = data.length; + count = 0; + sum = 0; + + expected = []; + actual = []; + + acc = incrnanmeanabs(); + + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + if ( isnan( d ) === false ) { + sum += abs( d ); + count += 1; + } + expected.push( ( count === 0 ) ? null : sum / count ); + actual.push( acc( d ) ); + } + t.deepEqual( actual, expected, 'returns expected value' ); + 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 = [ 2.0, NaN, -3.0, NaN, -1.0 ]; + acc = incrnanmeanabs(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 2.0, 'returns expected value' ); + t.end(); +});