Skip to content

Commit 6edfaf5

Browse files
committed
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
1 parent 72be5b1 commit 6edfaf5

10 files changed

Lines changed: 790 additions & 0 deletions

File tree

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# incrnanmidrange
22+
23+
> Compute a [mid-range][mid-range] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
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.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var incrnanmidrange = require( '@stdlib/stats/incr/nanmidrange' );
39+
```
40+
41+
#### incrnanmidrange()
42+
43+
Returns an accumulator `function` which incrementally computes a [mid-range][mid-range], ignoring `NaN` values.
44+
45+
```javascript
46+
var accumulator = incrnanmidrange();
47+
```
48+
49+
#### accumulator( \[x] )
50+
51+
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].
52+
53+
```javascript
54+
var accumulator = incrnanmidrange();
55+
56+
var midrange = accumulator();
57+
// returns null
58+
59+
midrange = accumulator( -2.0 );
60+
// returns -2.0
61+
62+
midrange = accumulator( 1.0 );
63+
// returns -0.5
64+
65+
midrange = accumulator( NaN );
66+
// returns -0.5
67+
68+
midrange = accumulator( 3.0 );
69+
// returns 0.5
70+
71+
midrange = accumulator();
72+
// returns 0.5
73+
```
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<section class="notes">
80+
81+
## Notes
82+
83+
- 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.
84+
85+
</section>
86+
87+
<!-- /.notes -->
88+
89+
<section class="examples">
90+
91+
## Examples
92+
93+
<!-- eslint no-undef: "error" -->
94+
95+
```javascript
96+
var randu = require( '@stdlib/random/base/randu' );
97+
var incrnanmidrange = require( '@stdlib/stats/incr/nanmidrange' );
98+
99+
var accumulator;
100+
var v;
101+
var i;
102+
103+
// Initialize an accumulator:
104+
accumulator = incrnanmidrange();
105+
106+
// For each simulated datum, update the mid-range...
107+
for ( i = 0; i < 100; i++ ) {
108+
if ( randu() < 0.2 ) {
109+
v = NaN;
110+
} else {
111+
v = randu() * 100.0;
112+
}
113+
accumulator( v );
114+
}
115+
console.log( accumulator() );
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
* * *
127+
128+
## See Also
129+
130+
- <span class="package-name">[`@stdlib/stats/incr/midrange`][@stdlib/stats/incr/midrange]</span><span class="delimiter">: </span><span class="description">compute a mid-range incrementally.</span>
131+
- <span class="package-name">[`@stdlib/stats/incr/nanmean`][@stdlib/stats/incr/nanmean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally, ignoring NaN values.</span>
132+
- <span class="package-name">[`@stdlib/stats/incr/nanmax`][@stdlib/stats/incr/nanmax]</span><span class="delimiter">: </span><span class="description">compute a maximum value incrementally, ignoring NaN values.</span>
133+
- <span class="package-name">[`@stdlib/stats/incr/nanmin`][@stdlib/stats/incr/nanmin]</span><span class="delimiter">: </span><span class="description">compute a minimum value incrementally, ignoring NaN values.</span>
134+
- <span class="package-name">[`@stdlib/stats/incr/nanrange`][@stdlib/stats/incr/nanrange]</span><span class="delimiter">: </span><span class="description">compute a range incrementally, ignoring NaN values.</span>
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
145+
146+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
147+
148+
<!-- <related-links> -->
149+
150+
[@stdlib/stats/incr/midrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/midrange
151+
152+
[@stdlib/stats/incr/nanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmean
153+
154+
[@stdlib/stats/incr/nanmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmax
155+
156+
[@stdlib/stats/incr/nanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmin
157+
158+
[@stdlib/stats/incr/nanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanrange
159+
160+
<!-- </related-links> -->
161+
162+
</section>
163+
164+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanmidrange = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanmidrange();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanmidrange();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a mid-range,
4+
ignoring NaN values.
5+
6+
The mid-range is the arithmetic mean of maximum and minimum values.
7+
Accordingly, the mid-range is the midpoint of the range and a measure of
8+
central tendency.
9+
10+
If provided a value, the accumulator function returns an updated mid-range.
11+
If not provided a value, the accumulator function returns the current mid-
12+
range.
13+
14+
NaN values are ignored.
15+
16+
Returns
17+
-------
18+
acc: Function
19+
Accumulator function.
20+
21+
Examples
22+
--------
23+
> var accumulator = {{alias}}();
24+
> var v = accumulator()
25+
null
26+
> v = accumulator( 3.14 )
27+
3.14
28+
> v = accumulator( -5.0 )
29+
~-0.93
30+
> v = accumulator( NaN )
31+
~-0.93
32+
> v = accumulator( 10.1 )
33+
2.55
34+
> v = accumulator()
35+
2.55
36+
37+
See Also
38+
--------
39+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided a value, returns an updated mid-range; otherwise, returns the current mid-range.
25+
*
26+
* ## Notes
27+
*
28+
* - 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.
29+
* - NaN values are ignored.
30+
*
31+
* @param x - value
32+
* @returns mid-range
33+
*/
34+
type accumulator = ( x?: number ) => number | null;
35+
36+
/**
37+
* Returns an accumulator function which incrementally computes a mid-range, ignoring NaN values.
38+
*
39+
* @returns accumulator function
40+
*
41+
* @example
42+
* var accumulator = incrnanmidrange();
43+
*
44+
* var midrange = accumulator();
45+
* // returns null
46+
*
47+
* midrange = accumulator( 3.14 );
48+
* // returns 3.14
49+
*
50+
* midrange = accumulator( -5.0 );
51+
* // returns ~-0.93
52+
*
53+
* midrange = accumulator( NaN );
54+
* // returns ~-0.93
55+
*
56+
* midrange = accumulator( 10.1 );
57+
* // returns 2.55
58+
*
59+
* midrange = accumulator();
60+
* // returns 2.55
61+
*/
62+
declare function incrnanmidrange(): accumulator;
63+
64+
65+
// EXPORTS //
66+
67+
export = incrnanmidrange;

0 commit comments

Comments
 (0)