Skip to content

Commit 6162260

Browse files
committed
Auto-generated commit
1 parent 478ad61 commit 6162260

File tree

16 files changed

+1928
-0
lines changed

16 files changed

+1928
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`0d237ed`](https://github.com/stdlib-js/stdlib/commit/0d237ed24ae4362f4d31ebdf742b0b3901ada59b) - add `stats/strided/nanmidrange` [(#9323)](https://github.com/stdlib-js/stdlib/pull/9323)
1314
- [`c17e5f2`](https://github.com/stdlib-js/stdlib/commit/c17e5f26a44218e7cc295e5360f681c1d36fee13) - add `stats/base/ndarray/stdev` [(#9248)](https://github.com/stdlib-js/stdlib/pull/9248)
1415
- [`595918c`](https://github.com/stdlib-js/stdlib/commit/595918c57777524dd8533b80a429c7d331f87444) - add `stats/strided/midrange` [(#9298)](https://github.com/stdlib-js/stdlib/pull/9298)
1516
- [`76d6b8f`](https://github.com/stdlib-js/stdlib/commit/76d6b8f5bc1907fd716c2ddb93406bba25bc8789) - add `stats/nanrange` [(#8956)](https://github.com/stdlib-js/stdlib/pull/8956)
@@ -3526,6 +3527,7 @@ A total of 559 issues were closed in this release:
35263527

35273528
<details>
35283529

3530+
- [`0d237ed`](https://github.com/stdlib-js/stdlib/commit/0d237ed24ae4362f4d31ebdf742b0b3901ada59b) - **feat:** add `stats/strided/nanmidrange` [(#9323)](https://github.com/stdlib-js/stdlib/pull/9323) _(by Sachin Pangal, Athan Reines)_
35293531
- [`038e74d`](https://github.com/stdlib-js/stdlib/commit/038e74da7817997d6de072cf614f32e930136fa9) - **docs:** revert back to using 'constructor' instead of 'function' _(by Philipp Burckhardt)_
35303532
- [`c216219`](https://github.com/stdlib-js/stdlib/commit/c2162190116f0953296c56c41b7480c8c050ab7e) - **docs:** fix JSDoc parameter type in `stats/base/ztest/one-sample/results` constructors _(by Philipp Burckhardt)_
35313533
- [`518e88e`](https://github.com/stdlib-js/stdlib/commit/518e88ef1c3de77e44a760fcabb1aa7ebd4ee72d) - **chore:** use literal since setter accepts strings directly _(by Philipp Burckhardt)_

strided/nanmidrange/README.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
# nanmidrange
22+
23+
> Calculate the [mid-range][mid-range] of a strided array, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [**mid-range**][mid-range], or **mid-extreme**, is 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 nanmidrange = require( '@stdlib/stats/strided/nanmidrange' );
39+
```
40+
41+
#### nanmidrange( N, x, strideX )
42+
43+
Computes the [mid-range][mid-range] of a strided array, ignoring `NaN` values.
44+
45+
```javascript
46+
var x = [ 1.0, -2.0, NaN, 2.0 ];
47+
var N = x.length;
48+
49+
var v = nanmidrange( N, x, 1 );
50+
// returns 0.0
51+
```
52+
53+
The function has the following parameters:
54+
55+
- **N**: number of indexed elements.
56+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
57+
- **strideX**: stride length for `x`.
58+
59+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [mid-range][mid-range] of every other element in `x`,
60+
61+
```javascript
62+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
63+
64+
var v = nanmidrange( 5, x, 2 );
65+
// returns 1.0
66+
```
67+
68+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
69+
70+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
71+
72+
```javascript
73+
var Float64Array = require( '@stdlib/array/float64' );
74+
75+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 4.0 ] );
76+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
77+
78+
var v = nanmidrange( 4, x1, 2 );
79+
// returns 1.0
80+
```
81+
82+
#### nanmidrange.ndarray( N, x, strideX, offsetX )
83+
84+
Computes the [mid-range][mid-range] of a strided array, ignoring `NaN` values and using alternative indexing semantics.
85+
86+
```javascript
87+
var x = [ 1.0, -2.0, NaN, 2.0 ];
88+
var N = x.length;
89+
90+
var v = nanmidrange.ndarray( N, x, 1, 0 );
91+
// returns 0.0
92+
```
93+
94+
The function has the following additional parameters:
95+
96+
- **offsetX**: starting index for `x`.
97+
98+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [mid-range][mid-range] for every other element in the strided array starting from the second element
99+
100+
```javascript
101+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 2.0, 3.0, 4.0 ];
102+
103+
var v = nanmidrange.ndarray( 5, x, 2, 1 );
104+
// returns 1.0
105+
```
106+
107+
</section>
108+
109+
<!-- /.usage -->
110+
111+
<section class="notes">
112+
113+
## Notes
114+
115+
- If `N <= 0`, both functions return `NaN`.
116+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
117+
- Depending on the environment, the typed versions ([`dnanmidrange`][@stdlib/stats/strided/dnanmidrange], [`snanmidrange`][@stdlib/stats/strided/snanmidrange], etc.) are likely to be significantly more performant.
118+
119+
</section>
120+
121+
<!-- /.notes -->
122+
123+
<section class="examples">
124+
125+
## Examples
126+
127+
<!-- eslint no-undef: "error" -->
128+
129+
```javascript
130+
var uniform = require( '@stdlib/random/base/uniform' );
131+
var filledarrayBy = require( '@stdlib/array/filled-by' );
132+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
133+
var nanmidrange = require( '@stdlib/stats/strided/nanmidrange' );
134+
135+
function rand() {
136+
if ( bernoulli( 0.8 ) < 1 ) {
137+
return NaN;
138+
}
139+
return uniform( -50.0, 50.0 );
140+
}
141+
142+
var x = filledarrayBy( 10, 'float64', rand );
143+
console.log( x );
144+
145+
var v = nanmidrange( x.length, x, 1 );
146+
console.log( v );
147+
```
148+
149+
</section>
150+
151+
<!-- /.examples -->
152+
153+
<!-- Section for related `stdlib` packages. -->
154+
155+
<section class="related">
156+
157+
</section>
158+
159+
<!-- /.related -->
160+
161+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
162+
163+
<section class="links">
164+
165+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
166+
167+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
168+
169+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
170+
171+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/array-base-accessor
172+
173+
[@stdlib/stats/strided/dnanmidrange]: https://github.com/stdlib-js/stats/tree/main/strided/dnanmidrange
174+
175+
[@stdlib/stats/strided/snanmidrange]: https://github.com/stdlib-js/stats/tree/main/strided/snanmidrange
176+
177+
</section>
178+
179+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 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 nanmidrange = require( './../lib/main.js' );
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 x = filledarrayBy( len, 'float64', rand );
58+
return benchmark;
59+
60+
function benchmark( b ) {
61+
var v;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
v = nanmidrange( x.length, x, 1 );
67+
if ( isnan( v ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( v ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( format( '%s:len=%d', pkg, len ), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)