From 0c1b1ea7f164f987c66deea2f588218aeedbaa59 Mon Sep 17 00:00:00 2001 From: Abhishek Jain Date: Thu, 13 Mar 2025 19:41:38 +0530 Subject: [PATCH 1/3] feat: add --- .../@stdlib/stats/incr/nanhmean/README.md | 169 ++++++++++++++++++ .../incr/nanhmean/benchmark/benchmark.js | 69 +++++++ .../docs/img/equation_harmonic_mean.svg | 148 +++++++++++++++ .../@stdlib/stats/incr/nanhmean/docs/repl.txt | 31 ++++ .../stats/incr/nanhmean/docs/types/index.d.ts | 59 ++++++ .../stats/incr/nanhmean/docs/types/test.ts | 61 +++++++ .../stats/incr/nanhmean/examples/index.js | 43 +++++ .../@stdlib/stats/incr/nanhmean/lib/index.js | 54 ++++++ .../@stdlib/stats/incr/nanhmean/lib/main.js | 93 ++++++++++ .../@stdlib/stats/incr/nanhmean/package.json | 67 +++++++ .../@stdlib/stats/incr/nanhmean/test/test.js | 100 +++++++++++ 11 files changed, 894 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/docs/img/equation_harmonic_mean.svg create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanhmean/test/test.js 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..05d08d54fa85 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md @@ -0,0 +1,169 @@ + + +# 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 randu = require( '@stdlib/random/base/randu' ); +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 ( randu() < 0.2 ) { + v = NaN; + } else { + v = ( randu()*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..d3d2868ac625 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/examples/index.js @@ -0,0 +1,43 @@ +/** +* @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 incrnanhmean = require( './../lib' ); + +var accumulator; +var mu; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanhmean(); + +// For each simulated datum, update the harmonic mean... +console.log( '\nValue\tHarmonic Mean\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = ( randu()*100.0 ); + } + mu = accumulator( v ); + console.log( '%d\t%d', ( v === null) ? NaN : 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..0b1439f219ef --- /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..3a8fe3b33dc7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/incr/nanhmean", + "version": "0.0.0", + "description": "Compute a harmonic mean incrementally.", + "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" + ] +} 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..c03c72c917b6 --- /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( '@stdlib/stats/incr/nanhmean/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(); +}); From ae9a8a7eab520162be84823007541dae30065b6d Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 27 Sep 2025 13:49:53 -0500 Subject: [PATCH 2/3] chore: minor clean-up --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/incr/nanhmean/README.md | 7 ++++--- .../stats/incr/nanhmean/examples/index.js | 21 +++++++------------ .../@stdlib/stats/incr/nanhmean/lib/main.js | 4 ++-- .../@stdlib/stats/incr/nanhmean/package.json | 6 ++++-- .../@stdlib/stats/incr/nanhmean/test/test.js | 6 +++--- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md b/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md index 05d08d54fa85..d783d0cf417c 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md @@ -103,7 +103,8 @@ v = accumulator(); ```javascript -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanhmean = require( '@stdlib/stats/incr/nanhmean' ); var accumulator; @@ -115,10 +116,10 @@ accumulator = incrnanhmean(); // For each simulated datum, update the harmonic mean... for ( i = 0; i < 100; i++ ) { - if ( randu() < 0.2 ) { + if ( bernoulli( 0.2 ) ) { v = NaN; } else { - v = ( randu()*100.0 ); + v = uniform( 1.0, 100.0 ); } accumulator( v ); } diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanhmean/examples/index.js index d3d2868ac625..5d7b31dcde6d 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanhmean/examples/index.js +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/examples/index.js @@ -18,26 +18,21 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ); +var bernoulli = require( '@stdlib/random/base/bernoulli' ); var incrnanhmean = require( './../lib' ); -var accumulator; -var mu; -var v; -var i; - // Initialize an accumulator: -accumulator = incrnanhmean(); +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++ ) { - if ( randu() < 0.2 ) { - v = NaN; - } else { - v = ( randu()*100.0 ); - } + v = ( bernoulli( 0.2 ) ) ? NaN : uniform( 1.0, 100.0 ); mu = accumulator( v ); - console.log( '%d\t%d', ( v === null) ? NaN : v.toFixed( 4 ), ( mu === null) ? NaN : mu.toFixed( 4 ) ); + 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/main.js b/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/main.js index 0b1439f219ef..c4ba751b3024 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/main.js +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/lib/main.js @@ -80,10 +80,10 @@ function incrnanhmean() { * @returns {(number|null)} harmonic mean or null */ function accumulator( x ) { - if ( arguments.length === 0 || isnan( x )) { + if ( arguments.length === 0 || isnan( x ) ) { return hmean(); } - return hmean(x); + return hmean( x ); } } diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json b/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json index 3a8fe3b33dc7..526c721a0f13 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/stats/incr/nanhmean", "version": "0.0.0", - "description": "Compute a harmonic mean incrementally.", + "description": "Compute a harmonic mean incrementally, ignoring NaN values.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -62,6 +62,8 @@ "harmonic mean", "central tendency", "incremental", - "accumulator" + "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 index c03c72c917b6..25be0045440a 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanhmean/test/test.js +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/test/test.js @@ -24,7 +24,7 @@ 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( '@stdlib/stats/incr/nanhmean/lib' ); +var incrnanhmean = require( './../lib' ); // TESTS // @@ -68,10 +68,10 @@ tape( 'the accumulator function incrementally computes a harmonic mean', functio j = 0; for ( i = 0; i < N; i++ ) { d = data[ i ]; - if ( !isnan(d) ) { + if ( !isnan( d ) ) { sum += 1.0 / d; expected = (j+1) / sum; - j+=1; + j += 1; } actual = acc( d ); if ( actual === expected ) { From 85aed5fc013f0104b3d42f5bb21e8f908c38cc15 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Sat, 27 Sep 2025 14:17:46 -0500 Subject: [PATCH 3/3] chore: remove auto-generated content --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/incr/nanhmean/README.md | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md b/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md index d783d0cf417c..389ef7df9502 100644 --- a/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md +++ b/lib/node_modules/@stdlib/stats/incr/nanhmean/README.md @@ -32,10 +32,6 @@ The [harmonic mean][harmonic-mean] of positive real numbers `x_0, x_1, ..., x_{n \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} ``` - @@ -134,15 +130,6 @@ console.log( accumulator() ); @@ -155,14 +142,6 @@ console.log( accumulator() ); -[@stdlib/stats/incr/gmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/gmean - -[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean - -[@stdlib/stats/incr/mnanhmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mnanhmean - -[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary -