diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md b/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md new file mode 100644 index 000000000000..389ef7df9502 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md @@ -0,0 +1,149 @@ + + +# incrnanhmean + +> Compute a [harmonic mean][harmonic-mean] incrementally, ignoring `NaN` values. + +
+ +The [harmonic mean][harmonic-mean] of positive real numbers `x_0, x_1, ..., x_{n-1}` is defined as + + + +```math +\begin{align}H &= \frac{n}{\frac{1}{x_0} + \frac{1}{x_1} + \cdots + \frac{1}{x_{n-1}}} \\ &= \frac{\displaystyle n}{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}} \\ &= \biggl( \frac{\displaystyle\sum_{i=0}^{n-1} \frac{1}{x_i}}{\displaystyle n} \biggr)^{-1}\end{align} +``` + + + + +
+ + + +
+ +## Usage + +```javascript +var incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); +``` + +#### incrnanhmean() + +Returns an accumulator `function` which incrementally computes a [harmonic mean][harmonic-mean], ignoring `NaN` values. + +```javascript +var accumulator = incrnanhmean(); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [harmonic mean][harmonic-mean]. If not provided an input value `x`, the accumulator function returns the current [harmonic mean][harmonic-mean]. + +```javascript +var accumulator = incrnanhmean(); + +var v = accumulator( 2.0 ); +// returns 2.0 + +v = accumulator( 1.0 ); +// returns ~1.33 + +v = accumulator( NaN ); +// returns ~1.33 + +v = accumulator( 3.0 ); +// returns ~1.64 + +v = accumulator(); +// returns ~1.64 +``` + +
+ + + +
+ +## 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 incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanhmean(); + +// For each simulated datum, update the harmonic mean... +for ( i = 0; i < 100; i++ ) { + if ( bernoulli( 0.2 ) ) { + v = NaN; + } else { + v = uniform( 1.0, 100.0 ); + } + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanhmean/benchmark/benchmark.js new file mode 100644 index 000000000000..08776697bdaf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/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 incrnanhmean = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanhmean(); + 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 = incrnanhmean(); + + 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/nanhmean/docs/img/equation_harmonic_mean.svg b/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/img/equation_harmonic_mean.svg new file mode 100644 index 000000000000..495851a8151e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/img/equation_harmonic_mean.svg @@ -0,0 +1,148 @@ + +StartLayout 1st Row 1st Column upper H 2nd Column equals StartStartFraction n OverOver StartFraction 1 Over x 0 EndFraction plus StartFraction 1 Over x 1 EndFraction plus midline-horizontal-ellipsis plus StartFraction 1 Over x Subscript n minus 1 Baseline EndFraction EndEndFraction 2nd Row 1st Column Blank 2nd Column equals StartStartFraction n OverOver sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts StartFraction 1 Over x Subscript i Baseline EndFraction EndEndFraction 3rd Row 1st Column Blank 2nd Column equals left-parenthesis StartStartFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts StartFraction 1 Over x Subscript i Baseline EndFraction OverOver n EndEndFraction right-parenthesis Superscript negative 1 EndLayout + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/repl.txt new file mode 100644 index 000000000000..3fc4195375d7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes a harmonic + mean, ignoring NaN values. + + If provided a value, the accumulator function returns an updated harmonic + mean. If not provided a value, the accumulator function returns the current + harmonic mean. + + 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 ) + ~2.86 + > v = accumulator() + ~2.86 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/types/index.d.ts new file mode 100644 index 000000000000..492995c009a4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/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 harmonic mean; otherwise, returns the current harmonic mean. +* +* @param x - value +* @returns harmonic mean +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a harmonic mean, ignoring `NaN` values. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanhmean(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( 5.0 ); +* // returns ~2.86 +* +* v = accumulator(); +* // returns ~2.86 +*/ +declare function incrnanhmean(): accumulator; + + +// EXPORTS // + +export = incrnanhmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanhmean/docs/types/test.ts new file mode 100644 index 000000000000..0e7329c96ac7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/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 incrnanhmean = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanhmean(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnanhmean( '5' ); // $ExpectError + incrnanhmean( 5 ); // $ExpectError + incrnanhmean( true ); // $ExpectError + incrnanhmean( false ); // $ExpectError + incrnanhmean( null ); // $ExpectError + incrnanhmean( undefined ); // $ExpectError + incrnanhmean( [] ); // $ExpectError + incrnanhmean( {} ); // $ExpectError + incrnanhmean( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanhmean(); + + 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 = incrnanhmean(); + + 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/nanhmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanhmean/examples/index.js new file mode 100644 index 000000000000..5d7b31dcde6d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/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 incrnanhmean = require( './../lib' ); + +// Initialize an accumulator: +var accumulator = incrnanhmean(); + +// For each simulated datum, update the harmonic mean... +console.log( '\nValue\tHarmonic Mean\n' ); +var mu; +var v; +var i; +for ( i = 0; i < 100; i++ ) { + v = ( bernoulli( 0.2 ) ) ? NaN : uniform( 1.0, 100.0 ); + mu = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), ( mu === null ) ? NaN : mu.toFixed( 4 ) ); +} +console.log( '\nFinal harmonic mean: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/index.js new file mode 100644 index 000000000000..141bdf557f1c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/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 harmonic mean incrementally, ignoring `NaN` values. +* +* @module @stdlib/stats/incr/nanhmean +* +* @example +* var incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); +* +* var accumulator = incrnanhmean(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( 5.0 ); +* // returns ~2.86 +* +* v = accumulator(); +* // returns ~2.86 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/main.js new file mode 100644 index 000000000000..c4ba751b3024 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/main.js @@ -0,0 +1,93 @@ +/** +* @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 incrhmean = require( '@stdlib/stats/incr/hmean' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a harmonic mean, ignoring `NaN` values. +* +* ## Method +* +* - The harmonic mean of positive real numbers \\(x_0, x_1, \ldots, x_{n-1}\\) is defined as +* +* ```tex +* \begin{align*} +* H &= \frac{n}{\frac{1}{x_0} + \frac{1}{x_1} + \cdots + \frac{1}{x_{n-1}}} \\ +* &= \frac{n}{\sum_{i=0}^{n-1} \frac{1}{x_i}} +* \end{align*} +* ``` +* +* which may be expressed +* +* ```tex +* H = \biggl( \frac{\sum_{i=0}^{n-1} \frac{1}{x_i}}{n} \biggr)^{-1} +* ``` +* +* - Accordingly, to compute the harmonic mean incrementally, we can simply compute the arithmetic mean of reciprocal values and then compute the reciprocal of the result. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanhmean(); +* +* var v = accumulator(); +* // returns null +* +* v = accumulator( 2.0 ); +* // returns 2.0 +* +* v = accumulator( NaN ); +* // returns 2.0 +* +* v = accumulator( 5.0 ); +* // returns ~2.86 +* +* v = accumulator(); +* // returns ~2.86 +*/ +function incrnanhmean() { + var hmean = incrhmean(); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated harmonic mean. If not provided a value, the accumulator function returns the current harmonic mean. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} harmonic mean or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return hmean(); + } + return hmean( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanhmean; diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json b/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json new file mode 100644 index 000000000000..526c721a0f13 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/stats/incr/nanhmean", + "version": "0.0.0", + "description": "Compute a harmonic 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", + "harmonic", + "mean", + "harmonic mean", + "central tendency", + "incremental", + "accumulator", + "nan", + "ignore" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanhmean/test/test.js new file mode 100644 index 000000000000..25be0045440a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/test/test.js @@ -0,0 +1,100 @@ +/** +* @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 EPSILON = require( '@stdlib/constants/float64/eps' ); +var incrnanhmean = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanhmean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanhmean(), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the initial accumulated value is null', function test( t ) { + var acc = incrnanhmean(); + t.equal( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a harmonic mean', function test( t ) { + var expected; + var actual; + var delta; + var data; + var tol; + var acc; + var sum; + 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 = incrnanhmean(); + + sum = 0; + j = 0; + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + if ( !isnan( d ) ) { + sum += 1.0 / d; + expected = (j+1) / sum; + j += 1; + } + actual = acc( d ); + if ( actual === expected ) { + t.strictEqual( actual, expected, 'returns expected result' ); + } else { + delta = abs( expected - actual ); + tol = EPSILON * expected; + t.strictEqual( delta <= tol, true, 'within tolerance. Expected: '+expected+'. Actual: '+actual+'. Delta: '+delta+'. Tol: '+tol+'.' ); + } + } + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current harmonic mean', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 1.0 ]; + acc = incrnanhmean(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 1.6363636363636365, 'returns the current accumulated harmonic mean' ); + t.end(); +});