diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/README.md b/lib/node_modules/@stdlib/stats/incr/nangmean/README.md new file mode 100644 index 000000000000..f2a8deb08ac0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/README.md @@ -0,0 +1,149 @@ + + +# incrnangmean + +> Compute a [geometric mean][geometric-mean] incrementally, ignoring `NaN` values. + +
+ +The [geometric mean][geometric-mean] is defined as the nth root of a product of _n_ numbers. + + + +```math +\biggl( \prod_{i=0}^{n-1} x_i \biggr)^{\frac{1}{n}} = \sqrt[n]{x_0 x_1 \cdots x_{n-1}} +``` + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnangmean = require( '@stdlib/stats/incr/nangmean' ); +``` + +#### incrnangmean() + +Returns an accumulator `function` which incrementally computes a [geometric mean][geometric-mean], ignoring `NaN` values. + +```javascript +var accumulator = incrnangmean(); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [geometric mean][geometric-mean]. If not provided an input value `x`, the accumulator function returns the current [geometric mean][geometric-mean]. + +```javascript +var accumulator = incrnangmean(); + +var prod = accumulator( 2.0 ); +// returns 2.0 + +prod = accumulator( 1.0 ); +// returns ~1.414 + +prod = accumulator( NaN ); +// returns ~1.414 + +prod = accumulator( 3.0 ); +// returns ~1.817 + +prod = accumulator(); +// returns ~1.817 +``` + +
+ + + +
+ +## 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 bernoulli = require( '@stdlib/random/base/bernoulli' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var incrnangmean = require( '@stdlib/stats/incr/nangmean' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnangmean(); + +// For each simulated value, update the geometric mean... +for ( i = 0; i < 100; i++ ) { + if ( bernoulli( 0.2 ) ) { + v = NaN; + } else { + v = uniform( 0.0, 100.0 ); + } + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nangmean/benchmark/benchmark.js new file mode 100644 index 000000000000..3462392366f7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/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 incrnangmean = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnangmean(); + 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 = incrnangmean(); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu()*10.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/nangmean/docs/img/equation_geometric_mean.svg b/lib/node_modules/@stdlib/stats/incr/nangmean/docs/img/equation_geometric_mean.svg new file mode 100644 index 000000000000..09660f01bb97 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/docs/img/equation_geometric_mean.svg @@ -0,0 +1,68 @@ + +left-parenthesis product Underscript i equals 0 Overscript n minus 1 Endscripts right-parenthesis Superscript StartFraction 1 Over n EndFraction Baseline equals RootIndex n StartRoot x 0 x 1 midline-horizontal-ellipsis x Subscript n minus 1 Baseline EndRoot + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nangmean/docs/repl.txt new file mode 100644 index 000000000000..21d21fa2b667 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes a geometric + mean, ignoring `NaN` values. + + If provided a value, the accumulator function returns an updated geometric + mean. If not provided a value, the accumulator function returns the current + geometric mean. + + If provided a negative value, the accumulated value is `NaN` for all future + invocations. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var v = accumulator() + null + > v = accumulator( 2.0 ) + 2.0 + > v = accumulator( NaN ) + 2.0 + > v = accumulator( 5.0 ) + ~3.16 + > v = accumulator() + ~3.16 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nangmean/docs/types/index.d.ts new file mode 100644 index 000000000000..1841ab45e572 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/docs/types/index.d.ts @@ -0,0 +1,59 @@ +/* +* @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 geometric mean; otherwise, returns the current geometric mean. +* +* @param x - value +* @returns geometric mean +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a geometric mean, ignoring `NaN` values. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnangmean(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( 5.0 ); +* // returns ~3.16 +* +* v = accumulator(); +* // returns ~3.16 +*/ +declare function incrnangmean(): accumulator; + + +// EXPORTS // + +export = incrnangmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nangmean/docs/types/test.ts new file mode 100644 index 000000000000..7fe1b490faf0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/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 incrnangmean = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnangmean(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnangmean( '5' ); // $ExpectError + incrnangmean( 5 ); // $ExpectError + incrnangmean( true ); // $ExpectError + incrnangmean( false ); // $ExpectError + incrnangmean( null ); // $ExpectError + incrnangmean( undefined ); // $ExpectError + incrnangmean( [] ); // $ExpectError + incrnangmean( {} ); // $ExpectError + incrnangmean( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnangmean(); + + 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 = incrnangmean(); + + 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/nangmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nangmean/examples/index.js new file mode 100644 index 000000000000..bf7a20fc3d5f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/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 bernoulli = require( '@stdlib/random/base/bernoulli' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var incrnangmean = require( './../lib' ); + +// Initialize an accumulator: +var accumulator = incrnangmean(); + +// For each simulated value, update the geometric mean... +console.log( '\nValue\tGeometric mean\n' ); +var prod; +var v; +var i; +for ( i = 0; i < 100; i++ ) { + v = ( bernoulli( 0.2 ) ) ? NaN : uniform( 0.0, 100.0 ); + prod = accumulator( v ); + console.log( '%s\t%s', ( v === v ) ? v.toFixed( 4 ) : 'NaN', ( prod === null ) ? 'null' : prod.toFixed( 4 ) ); +} +console.log( '\nFinal geometric mean: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nangmean/lib/index.js new file mode 100644 index 000000000000..8e4abf172d0a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/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 a geometric mean incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nangmean +* +* @example +* var incrnangmean = require( '@stdlib/stats/incr/nangmean' ); +* +* var accumulator = incrnangmean(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( 5.0 ); +* // returns ~3.16 +* +* v = accumulator(); +* // returns ~3.16 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nangmean/lib/main.js new file mode 100644 index 000000000000..496206308bed --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/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 incrgmean = require( '@stdlib/stats/incr/gmean' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a geometric mean, ignoring `NaN` values. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnangmean(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( 5.0 ); +* // returns ~3.16 +* +* v = accumulator(); +* // returns ~3.16 +*/ +function incrnangmean() { + var gmean = incrgmean(); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated geometric mean. If not provided a value, the accumulator function returns the current geometric mean. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} geometric mean or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return gmean(); + } + return gmean( x ); + } +} + + +// EXPORTS // + +module.exports = incrnangmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/package.json b/lib/node_modules/@stdlib/stats/incr/nangmean/package.json new file mode 100644 index 000000000000..e28f7a0e591a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/stats/incr/nangmean", + "version": "0.0.0", + "description": "Compute a geometric 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", + "geometric", + "mean", + "average", + "avg", + "central tendency", + "product", + "prod", + "incremental", + "accumulator", + "nan", + "ignore" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nangmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/nangmean/test/test.js new file mode 100644 index 000000000000..b4d942a1915b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nangmean/test/test.js @@ -0,0 +1,150 @@ +/** +* @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 ln = require( '@stdlib/math/base/special/ln' ); +var exp = require( '@stdlib/math/base/special/exp' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPSILON = require( '@stdlib/constants/float64/eps' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var incrnangmean = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnangmean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.strictEqual( typeof incrnangmean(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a geometric mean', function test( t ) { + var expected; + var actual; + var delta; + var data; + var prod; + var tol; + var acc; + var N; + var d; + var i; + var j; + + data = [ 2.0, 3.0, NaN, 2.0, 4.0, NaN, 3.0, 4.0 ]; + N = data.length; + + acc = incrnangmean(); + + prod = 1.0; + j = 0; + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + if ( !isnan( d ) ) { + prod *= d; + expected = pow( prod, 1.0/(j+1) ); + j += 1; + } + actual = acc( d ); + if ( actual === expected ) { + t.strictEqual( actual, expected, 'returns expected value. d: '+d+'.' ); + } else { + delta = abs( expected - actual ); + tol = 1.5 * EPSILON * abs( expected ); + t.strictEqual( delta <= tol, true, 'within tolerance. d: '+d+'. Expected: '+expected+'. Actual: '+actual+'. Delta: '+delta+'. Tol: '+tol+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current geometric mean', function test( t ) { + var expected; + var delta; + var data; + var acc; + var tol; + var i; + + data = [ 2.0, NaN, 3.0, 1.0 ]; + + acc = incrnangmean(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + expected = 1.8171205928321397; + delta = abs( expected - acc() ); + tol = EPSILON * abs( expected ); + + t.strictEqual( delta <= tol, true, 'within tolerance. Expected: '+expected+'. Actual: '+acc()+'. Delta: '+delta+'. Tol: '+tol+'.' ); + t.end(); +}); + +tape( 'the accumulator function can return a result which overflows', function test( t ) { + var acc = incrnangmean(); + var i; + + for ( i = 0; i < 10000; i++ ) { + acc( 1.7976931348623157e+308 ); + } + t.strictEqual( acc(), PINF, 'returns infinity' ); + t.end(); +}); + +tape( 'the accumulator function can return a result which underflows', function test( t ) { + var acc = incrnangmean(); + var i; + + for ( i = 0; i < 10000; i++ ) { + acc( 5.0e-324 ); + } + t.strictEqual( acc(), 0.0, 'returns 0' ); + t.end(); +}); + +tape( 'if provided a `NaN`, the accumulator function ignores it', function test( t ) { + var acc; + + acc = incrnangmean(); + acc( 5.0 ); + + t.strictEqual( acc(), 5.0, 'returns expected value' ); + + acc( NaN ); + t.strictEqual( isnan( acc() ), false, 'does not return NaN' ); + + acc( 3.14 ); + t.strictEqual( acc(), exp( ( ln( 5.0 ) + ln( 3.14 ) ) / 2 ), 'correctly updates after NaN'); + t.end(); +}); + +tape( 'if the accumulator function has not been provided any data, the accumulator function returns `null`', function test( t ) { + var acc = incrnangmean(); + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +});