Skip to content

Commit 7ef3bae

Browse files
feat: add stats/array/nanmskmidrange
PR-URL: stdlib-js#10224 Co-authored-by: stdlib-bot <noreply@stdlib.io> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent ea860cb commit 7ef3bae

10 files changed

Lines changed: 962 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# nanmskmidrange
22+
23+
> Calculate the [midrange][midrange] of an array according to a mask, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [**midrange**][midrange] is defined as the arithmetic mean of the maximum and minimum values in a data set. The measure is the midpoint of the 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 nanmskmidrange = require( '@stdlib/stats/array/nanmskmidrange' );
39+
```
40+
41+
#### nanmskmidrange( x, mask )
42+
43+
Computes the [midrange][midrange] of an array according to a mask, ignoring `NaN` values.
44+
45+
```javascript
46+
var x = [ 1.0, -2.0, 4.0, 2.0, NaN, NaN ];
47+
var mask = [ 0, 0, 1, 0, 0, 0 ];
48+
49+
var v = nanmskmidrange( x, mask );
50+
// returns 0.0
51+
```
52+
53+
The function has the following parameters:
54+
55+
- **x**: input array.
56+
- **mask**: mask array. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation.
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- If provided an empty array, the function returns `NaN`.
67+
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var uniform = require( '@stdlib/random/base/uniform' );
81+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
82+
var filledarrayBy = require( '@stdlib/array/filled-by' );
83+
var nanmskmidrange = require( '@stdlib/stats/array/nanmskmidrange' );
84+
85+
function rand() {
86+
if ( bernoulli( 0.8 ) < 1 ) {
87+
return NaN;
88+
}
89+
return uniform( -50.0, 50.0 );
90+
}
91+
92+
var x = filledarrayBy( 10, 'float64', rand );
93+
console.log( x );
94+
95+
var mask = filledarrayBy( x.length, 'uint8', bernoulli.factory( 0.2 ) );
96+
console.log( mask );
97+
98+
var v = nanmskmidrange( x, mask );
99+
console.log( v );
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
107+
108+
<section class="related">
109+
110+
</section>
111+
112+
<!-- /.related -->
113+
114+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
115+
116+
<section class="links">
117+
118+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
119+
120+
[midrange]: https://en.wikipedia.org/wiki/Mid-range
121+
122+
</section>
123+
124+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var nanmskmidrange = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Returns a random value or `NaN`.
38+
*
39+
* @private
40+
* @returns {number} random number or `NaN`
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.8 ) < 1 ) {
44+
return NaN;
45+
}
46+
return uniform( -10.0, 10.0 );
47+
}
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveInteger} len - array length
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( len ) {
57+
var mask;
58+
var x;
59+
60+
x = filledarrayBy( len, 'generic', rand );
61+
mask = filledarrayBy( len, 'uint8', bernoulli.factory( 0.2 ) );
62+
return benchmark;
63+
64+
/**
65+
* Benchmark function.
66+
*
67+
* @private
68+
* @param {Benchmark} b - benchmark instance
69+
*/
70+
function benchmark( b ) {
71+
var v;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
v = nanmskmidrange( x, mask );
77+
if ( isnan( v ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( v ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var f;
103+
var i;
104+
105+
min = 1; // 10^min
106+
max = 6; // 10^max
107+
108+
for ( i = min; i <= max; i++ ) {
109+
len = pow( 10, i );
110+
f = createBenchmark( len );
111+
bench( format( '%s:len=%d', pkg, len ), f );
112+
}
113+
}
114+
115+
main();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( x, mask )
3+
Computes the midrange of an array according to a mask, ignoring `NaN`
4+
values.
5+
6+
If a `mask` array element is `0`, the corresponding element in `x` is
7+
considered valid and included in computation.
8+
9+
If a `mask` array element is `1`, the corresponding element in `x` is
10+
considered invalid/missing and excluded from computation.
11+
12+
If provided an empty array, the function returns `NaN`.
13+
14+
Parameters
15+
----------
16+
x: Array<number>|TypedArray
17+
Input array.
18+
19+
mask: Array<number>|TypedArray
20+
Mask array.
21+
22+
Returns
23+
-------
24+
out: number
25+
Midrange.
26+
27+
Examples
28+
--------
29+
> var x = [ 1.0, -2.0, 4.0, 2.0, NaN ];
30+
> var mask = [ 0, 0, 1, 0, 0 ];
31+
> {{alias}}( x, mask )
32+
0.0
33+
34+
See Also
35+
--------
36+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
30+
/**
31+
* Computes the midrange of an array according to a mask, ignoring `NaN` values.
32+
*
33+
* @param x - input array
34+
* @param mask - mask array
35+
* @returns midrange
36+
*
37+
* @example
38+
* var x = [ 1.0, -2.0, 4.0, 2.0, NaN, NaN ];
39+
* var mask = [ 0, 0, 1, 0, 0, 0 ];
40+
*
41+
* var v = nanmskmidrange( x, mask );
42+
* // returns 0.0
43+
*/
44+
declare function nanmskmidrange( x: InputArray, mask: InputArray ): number;
45+
46+
47+
// EXPORTS //
48+
49+
export = nanmskmidrange;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 AccessorArray = require( '@stdlib/array/base/accessor' );
20+
import nanmskmidrange = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a number...
26+
{
27+
const x = new Float64Array( 10 );
28+
const mask = new Uint8Array( 10 );
29+
30+
nanmskmidrange( x, mask ); // $ExpectType number
31+
nanmskmidrange( new AccessorArray( x ), mask ); // $ExpectType number
32+
}
33+
34+
// The compiler throws an error if the function is provided a first argument which is not a numeric array...
35+
{
36+
const mask = new Uint8Array( 10 );
37+
38+
nanmskmidrange( 10, mask ); // $ExpectError
39+
nanmskmidrange( '10', mask ); // $ExpectError
40+
nanmskmidrange( true, mask ); // $ExpectError
41+
nanmskmidrange( false, mask ); // $ExpectError
42+
nanmskmidrange( null, mask ); // $ExpectError
43+
nanmskmidrange( undefined, mask ); // $ExpectError
44+
nanmskmidrange( {}, mask ); // $ExpectError
45+
nanmskmidrange( ( x: number ): number => x, mask ); // $ExpectError
46+
}
47+
48+
// The compiler throws an error if the function is provided a second argument which is not a numeric array...
49+
{
50+
const x = new Float64Array( 10 );
51+
52+
nanmskmidrange( x, 10 ); // $ExpectError
53+
nanmskmidrange( x, '10' ); // $ExpectError
54+
nanmskmidrange( x, true ); // $ExpectError
55+
nanmskmidrange( x, false ); // $ExpectError
56+
nanmskmidrange( x, null ); // $ExpectError
57+
nanmskmidrange( x, undefined ); // $ExpectError
58+
nanmskmidrange( x, {} ); // $ExpectError
59+
nanmskmidrange( x, ( x: number ): number => x ); // $ExpectError
60+
}
61+
62+
// The compiler throws an error if the function is provided an unsupported number of arguments...
63+
{
64+
const x = new Float64Array( 10 );
65+
const mask = new Uint8Array( 10 );
66+
67+
nanmskmidrange(); // $ExpectError
68+
nanmskmidrange( x ); // $ExpectError
69+
nanmskmidrange( x, mask, {} ); // $ExpectError
70+
}

0 commit comments

Comments
 (0)