Skip to content

Commit 9abfda5

Browse files
committed
Auto-generated commit
1 parent 62336f3 commit 9abfda5

File tree

17 files changed

+2229
-0
lines changed

17 files changed

+2229
-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+
- [`ffee9eb`](https://github.com/stdlib-js/stdlib/commit/ffee9eb167e2f3b1750163bf9f9937f7e9db48d3) - add `stats/strided/mskmidrange` [(#9331)](https://github.com/stdlib-js/stdlib/pull/9331)
1314
- [`b6b70c7`](https://github.com/stdlib-js/stdlib/commit/b6b70c7fd270b902706e1870933f97f1ae95a0e1) - add `stats/base/ndarray/midrange` [(#9332)](https://github.com/stdlib-js/stdlib/pull/9332)
1415
- [`0d237ed`](https://github.com/stdlib-js/stdlib/commit/0d237ed24ae4362f4d31ebdf742b0b3901ada59b) - add `stats/strided/nanmidrange` [(#9323)](https://github.com/stdlib-js/stdlib/pull/9323)
1516
- [`c17e5f2`](https://github.com/stdlib-js/stdlib/commit/c17e5f26a44218e7cc295e5360f681c1d36fee13) - add `stats/base/ndarray/stdev` [(#9248)](https://github.com/stdlib-js/stdlib/pull/9248)
@@ -3528,6 +3529,7 @@ A total of 559 issues were closed in this release:
35283529

35293530
<details>
35303531

3532+
- [`ffee9eb`](https://github.com/stdlib-js/stdlib/commit/ffee9eb167e2f3b1750163bf9f9937f7e9db48d3) - **feat:** add `stats/strided/mskmidrange` [(#9331)](https://github.com/stdlib-js/stdlib/pull/9331) _(by Sachin Pangal, Athan Reines)_
35313533
- [`b6b70c7`](https://github.com/stdlib-js/stdlib/commit/b6b70c7fd270b902706e1870933f97f1ae95a0e1) - **feat:** add `stats/base/ndarray/midrange` [(#9332)](https://github.com/stdlib-js/stdlib/pull/9332) _(by Sachin Pangal, Athan Reines)_
35323534
- [`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)_
35333535
- [`038e74d`](https://github.com/stdlib-js/stdlib/commit/038e74da7817997d6de072cf614f32e930136fa9) - **docs:** revert back to using 'constructor' instead of 'function' _(by Philipp Burckhardt)_

strided/mskmidrange/README.md

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

0 commit comments

Comments
 (0)