Skip to content

Commit 7024a89

Browse files
committed
added nanmidrange
1 parent a0bf2f0 commit 7024a89

File tree

10 files changed

+748
-0
lines changed

10 files changed

+748
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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( NaN );
63+
// returns -2.0
64+
65+
midrange = accumulator( 1.0 );
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 uniform = require( '@stdlib/random/base/uniform' );
97+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
98+
var incrnanmidrange = require( '@stdlib/stats/incr/nanmidrange' );
99+
100+
var accumulator;
101+
var v;
102+
var i;
103+
104+
// Initialize an accumulator:
105+
accumulator = incrnanmidrange();
106+
107+
// For each simulated datum, update the mid-range...
108+
for ( i = 0; i < 100; i++ ) {
109+
v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( 0.0, 100.0 );
110+
accumulator( v );
111+
}
112+
console.log( accumulator() );
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
120+
121+
<section class="related">
122+
123+
* * *
124+
125+
## See Also
126+
127+
- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span>
128+
- <span class="package-name">[`@stdlib/stats/incr/max`][@stdlib/stats/incr/max]</span><span class="delimiter">: </span><span class="description">compute a maximum value incrementally.</span>
129+
- <span class="package-name">[`@stdlib/stats/incr/min`][@stdlib/stats/incr/min]</span><span class="delimiter">: </span><span class="description">compute a minimum value incrementally.</span>
130+
- <span class="package-name">[`@stdlib/stats/incr/range`][@stdlib/stats/incr/range]</span><span class="delimiter">: </span><span class="description">compute a range incrementally.</span>
131+
- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span>
132+
133+
</section>
134+
135+
<!-- /.related -->
136+
137+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="links">
140+
141+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
142+
143+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
144+
145+
<!-- <related-links> -->
146+
147+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
148+
149+
[@stdlib/stats/incr/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/max
150+
151+
[@stdlib/stats/incr/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/min
152+
153+
[@stdlib/stats/incr/range]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/range
154+
155+
[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary
156+
157+
<!-- </related-links> -->
158+
159+
</section>
160+
161+
<!-- /.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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
Returns
15+
-------
16+
acc: Function
17+
Accumulator function.
18+
19+
Examples
20+
--------
21+
> var accumulator = {{alias}}();
22+
> var v = accumulator()
23+
null
24+
> v = accumulator( 3.14 )
25+
3.14
26+
> v = accumulator( NaN )
27+
3.14
28+
> v = accumulator( -5.0 )
29+
~-0.93
30+
> v = accumulator( 10.1 )
31+
2.55
32+
> v = accumulator()
33+
2.55
34+
35+
See Also
36+
--------
37+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import incrnanmidrange = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanmidrange(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
incrnanmidrange( '5' ); // $ExpectError
32+
incrnanmidrange( 5 ); // $ExpectError
33+
incrnanmidrange( true ); // $ExpectError
34+
incrnanmidrange( false ); // $ExpectError
35+
incrnanmidrange( null ); // $ExpectError
36+
incrnanmidrange( undefined ); // $ExpectError
37+
incrnanmidrange( [] ); // $ExpectError
38+
incrnanmidrange( {} ); // $ExpectError
39+
incrnanmidrange( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanmidrange();
45+
46+
acc(); // $ExpectType number | null
47+
acc( 3.14 ); // $ExpectType number | null
48+
acc( 5 ); // $ExpectType number | null
49+
acc( -2 ); // $ExpectType number | null
50+
}
51+
52+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
53+
{
54+
const acc = incrnanmidrange();
55+
56+
acc( '5' ); // $ExpectError
57+
acc( true ); // $ExpectError
58+
acc( false ); // $ExpectError
59+
acc( null ); // $ExpectError
60+
acc( [] ); // $ExpectError
61+
acc( {} ); // $ExpectError
62+
acc( ( x: number ): number => x ); // $ExpectError
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
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( 10.1 );
54+
* // returns 2.55
55+
*
56+
* midrange = accumulator();
57+
* // returns 2.55
58+
*/
59+
declare function incrnanmidrange(): accumulator;
60+
61+
62+
// EXPORTS //
63+
64+
export = incrnanmidrange;

0 commit comments

Comments
 (0)