From 6edfaf5778bc2a04edb3fb4c289dbd802fc518e7 Mon Sep 17 00:00:00 2001 From: shanky Date: Tue, 30 Dec 2025 10:07:13 +0530 Subject: [PATCH] feat: add stats/incr/nanmidrange - Add incremental accumulator for computing mid-range while ignoring NaN values - Based on stats/incr/midrange with NaN handling pattern from nanmin/nansum - Includes tests, examples, benchmarks, and TypeScript definitions Closes #5578 --- .../@stdlib/stats/incr/nanmidrange/README.md | 164 ++++++++++++++++++ .../incr/nanmidrange/benchmark/benchmark.js | 69 ++++++++ .../stats/incr/nanmidrange/docs/repl.txt | 39 +++++ .../incr/nanmidrange/docs/types/index.d.ts | 67 +++++++ .../stats/incr/nanmidrange/docs/types/test.ts | 63 +++++++ .../stats/incr/nanmidrange/examples/index.js | 43 +++++ .../stats/incr/nanmidrange/lib/index.js | 57 ++++++ .../stats/incr/nanmidrange/lib/main.js | 77 ++++++++ .../stats/incr/nanmidrange/package.json | 73 ++++++++ .../stats/incr/nanmidrange/test/test.js | 138 +++++++++++++++ 10 files changed, 790 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/README.md create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/package.json create mode 100644 lib/node_modules/@stdlib/stats/incr/nanmidrange/test/test.js diff --git a/lib/node_modules/@stdlib/stats/incr/nanmidrange/README.md b/lib/node_modules/@stdlib/stats/incr/nanmidrange/README.md new file mode 100644 index 000000000000..61a7fdd6092d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/README.md @@ -0,0 +1,164 @@ + + +# incrnanmidrange + +> Compute a [mid-range][mid-range] incrementally, ignoring `NaN` values. + +
+ +The [**mid-range**][mid-range], or **mid-extreme**, is the arithmetic mean of maximum and minimum values. Accordingly, the [mid-range][mid-range] is the midpoint of the [range][range] and a measure of central tendency. + +
+ + + +
+ +## Usage + +```javascript +var incrnanmidrange = require( '@stdlib/stats/incr/nanmidrange' ); +``` + +#### incrnanmidrange() + +Returns an accumulator `function` which incrementally computes a [mid-range][mid-range], ignoring `NaN` values. + +```javascript +var accumulator = incrnanmidrange(); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [mid-range][mid-range]. If not provided an input value `x`, the accumulator function returns the current [mid-range][mid-range]. + +```javascript +var accumulator = incrnanmidrange(); + +var midrange = accumulator(); +// returns null + +midrange = accumulator( -2.0 ); +// returns -2.0 + +midrange = accumulator( 1.0 ); +// returns -0.5 + +midrange = accumulator( NaN ); +// returns -0.5 + +midrange = accumulator( 3.0 ); +// returns 0.5 + +midrange = accumulator(); +// returns 0.5 +``` + +
+ + + +
+ +## 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 incrnanmidrange = require( '@stdlib/stats/incr/nanmidrange' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmidrange(); + +// For each simulated datum, update the mid-range... +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/nanmidrange/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmidrange/benchmark/benchmark.js new file mode 100644 index 000000000000..97671f982eb6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/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 incrnanmidrange = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmidrange(); + 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 = incrnanmidrange(); + + 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/nanmidrange/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/repl.txt new file mode 100644 index 000000000000..ebc7a964c107 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}() + Returns an accumulator function which incrementally computes a mid-range, + ignoring NaN values. + + The mid-range is the arithmetic mean of maximum and minimum values. + Accordingly, the mid-range is the midpoint of the range and a measure of + central tendency. + + If provided a value, the accumulator function returns an updated mid-range. + If not provided a value, the accumulator function returns the current mid- + range. + + NaN values are ignored. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}(); + > var v = accumulator() + null + > v = accumulator( 3.14 ) + 3.14 + > v = accumulator( -5.0 ) + ~-0.93 + > v = accumulator( NaN ) + ~-0.93 + > v = accumulator( 10.1 ) + 2.55 + > v = accumulator() + 2.55 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/index.d.ts new file mode 100644 index 000000000000..ad6ff143549f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/index.d.ts @@ -0,0 +1,67 @@ +/* +* @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 mid-range; otherwise, returns the current mid-range. +* +* ## Notes +* +* - The mid-range is the arithmetic mean of maximum and minimum values. Accordingly, the mid-range is the midpoint of the range and a measure of central tendency. +* - NaN values are ignored. +* +* @param x - value +* @returns mid-range +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a mid-range, ignoring NaN values. +* +* @returns accumulator function +* +* @example +* var accumulator = incrnanmidrange(); +* +* var midrange = accumulator(); +* // returns null +* +* midrange = accumulator( 3.14 ); +* // returns 3.14 +* +* midrange = accumulator( -5.0 ); +* // returns ~-0.93 +* +* midrange = accumulator( NaN ); +* // returns ~-0.93 +* +* midrange = accumulator( 10.1 ); +* // returns 2.55 +* +* midrange = accumulator(); +* // returns 2.55 +*/ +declare function incrnanmidrange(): accumulator; + + +// EXPORTS // + +export = incrnanmidrange; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/test.ts new file mode 100644 index 000000000000..b888b86e2bdf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/docs/types/test.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. +*/ + +import incrnanmidrange = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmidrange(); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided arguments... +{ + incrnanmidrange( '5' ); // $ExpectError + incrnanmidrange( 5 ); // $ExpectError + incrnanmidrange( true ); // $ExpectError + incrnanmidrange( false ); // $ExpectError + incrnanmidrange( null ); // $ExpectError + incrnanmidrange( undefined ); // $ExpectError + incrnanmidrange( [] ); // $ExpectError + incrnanmidrange( {} ); // $ExpectError + incrnanmidrange( ( x: number ): number => x ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrmidrange(); + + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null + acc( 5 ); // $ExpectType number | null + acc( -2 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrmidrange(); + + 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/nanmidrange/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmidrange/examples/index.js new file mode 100644 index 000000000000..84ea37409fac --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/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 incrnanmidrange = require( './../lib' ); + +var accumulator; +var midrange; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmidrange(); + +// For each simulated datum, update the mid-range... +console.log( '\nValue\tMid-range\n' ); +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + midrange = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), midrange ? midrange.toFixed( 4 ) : 'null' ); +} +console.log( '\nFinal mid-range: %d\n', accumulator() ); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/index.js new file mode 100644 index 000000000000..b4b9f8b91f9f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/index.js @@ -0,0 +1,57 @@ +/** +* @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 mid-range incrementally, ignoring NaN values. +* +* @module @stdlib/stats/incr/nanmidrange +* +* @example +* var incrnanmidrange = require( '@stdlib/stats/incr/nanmidrange' ); +* +* var accumulator = incrnanmidrange(); +* +* var midrange = accumulator(); +* // returns null +* +* midrange = accumulator( 3.14 ); +* // returns 3.14 +* +* midrange = accumulator( -5.0 ); +* // returns ~-0.93 +* +* midrange = accumulator( NaN ); +* // returns ~-0.93 +* +* midrange = accumulator( 10.1 ); +* // returns 2.55 +* +* midrange = accumulator(); +* // returns 2.55 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/main.js new file mode 100644 index 000000000000..ecd3cb1857d3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/lib/main.js @@ -0,0 +1,77 @@ +/** +* @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 incrmidrange = require( '@stdlib/stats/incr/midrange' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a mid-range, ignoring `NaN` values. +* +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmidrange(); +* +* var midrange = accumulator(); +* // returns null +* +* midrange = accumulator( 3.14 ); +* // returns 3.14 +* +* midrange = accumulator( -5.0 ); +* // returns ~-0.93 +* +* midrange = accumulator( NaN ); +* // returns ~-0.93 +* +* midrange = accumulator( 10.1 ); +* // returns 2.55 +* +* midrange = accumulator(); +* // returns 2.55 +*/ +function incrnanmidrange() { + var midrange = incrmidrange(); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated mid-range. If not provided a value, the accumulator function returns the current mid-range. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} mid-range or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return midrange(); + } + return midrange( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanmidrange; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmidrange/package.json b/lib/node_modules/@stdlib/stats/incr/nanmidrange/package.json new file mode 100644 index 000000000000..135a071d71ee --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/stats/incr/nanmidrange", + "version": "0.0.0", + "description": "Compute a mid-range 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", + "maximum", + "max", + "minimum", + "min", + "mid", + "range", + "mid-range", + "mid-extreme", + "mean", + "central tendency", + "incremental", + "accumulator", + "nan", + "ignore" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmidrange/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmidrange/test/test.js new file mode 100644 index 000000000000..4362d00eab91 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmidrange/test/test.js @@ -0,0 +1,138 @@ +/** +* @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 PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrnanmidrange = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmidrange, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.strictEqual( typeof incrnanmidrange(), 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'if not provided any values, the initial returned mid-range is `null`', function test( t ) { + var acc = incrnanmidrange(); + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function incrementally computes a mid-range, ignoring NaN values', function test( t ) { + var expected; + var actual; + var data; + var acc; + var max; + var min; + var N; + var d; + var i; + + data = [ 2.0, 3.0, NaN, -2.0, 4.0, NaN, -3.0, 4.0 ]; + N = data.length; + + expected = zeros( N ); + actual = zeros( N ); + + acc = incrnanmidrange(); + + max = NINF; + min = PINF; + for ( i = 0; i < N; i++ ) { + d = data[ i ]; + if ( isnan( d ) === false ) { + if ( d > max ) { + max = d; + } + if ( d < min ) { + min = d; + } + } + expected[ i ] = (max + min) / 2.0; + actual[ i ] = acc( d ); + } + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current mid-range', function test( t ) { + var data; + var acc; + var i; + + data = [ -2.0, NaN, 3.0, NaN, 1.0 ]; + acc = incrnanmidrange(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.strictEqual( acc(), 0.5, 'returns the current accumulated mid-range' ); + t.end(); +}); + +tape( 'if provided only `NaN` values, the accumulator function returns `null`', function test( t ) { + var data; + var acc; + var i; + + data = [ NaN, NaN, NaN ]; + acc = incrnanmidrange(); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.strictEqual( acc(), null, 'returns expected value' ); + t.end(); +}); + +tape( 'the accumulator function correctly handles NaN values', function test( t ) { + var acc; + var v; + + acc = incrnanmidrange(); + + v = acc( 2.0 ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + v = acc( NaN ); + t.strictEqual( v, 2.0, 'returns expected value' ); + + v = acc( 3.0 ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + v = acc( NaN ); + t.strictEqual( v, 2.5, 'returns expected value' ); + + v = acc( -5.0 ); + t.strictEqual( v, -1.0, 'returns expected value' ); + + t.end(); +});