Skip to content

Commit 76d6b8f

Browse files
Sachinn-64kgrytestdlib-bot
authored
feat: add stats/nanrange
PR-URL: stdlib-js#8956 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 0adbf48 commit 76d6b8f

File tree

13 files changed

+2749
-0
lines changed

13 files changed

+2749
-0
lines changed
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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+
# nanrange
22+
23+
> Compute the [range][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var nanrange = require( '@stdlib/stats/nanrange' );
31+
```
32+
33+
#### nanrange( x\[, options] )
34+
35+
Computes the [range][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions, ignoring `NaN` values.
36+
37+
```javascript
38+
var array = require( '@stdlib/ndarray/array' );
39+
40+
var x = array( [ -1.0, 2.0, NaN ] );
41+
42+
var y = nanrange( x );
43+
// returns <ndarray>[ 3.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
49+
- **options**: function options (_optional_).
50+
51+
The function accepts the following options:
52+
53+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
54+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
55+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
56+
57+
By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option.
58+
59+
```javascript
60+
var array = require( '@stdlib/ndarray/array' );
61+
62+
var x = array( [ -1.0, 2.0, NaN, 4.0 ], {
63+
'shape': [ 2, 2 ],
64+
'order': 'row-major'
65+
});
66+
// returns <ndarray>[ [ -1.0, 2.0 ], [ NaN, 4.0 ] ]
67+
68+
var y = nanrange( x, {
69+
'dims': [ 0 ]
70+
});
71+
// returns <ndarray>[ 0.0, 2.0 ]
72+
73+
y = nanrange( x, {
74+
'dims': [ 1 ]
75+
});
76+
// returns <ndarray>[ 3.0, 0.0 ]
77+
78+
y = nanrange( x, {
79+
'dims': [ 0, 1 ]
80+
});
81+
// returns <ndarray>[ 5.0 ]
82+
```
83+
84+
By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`.
85+
86+
```javascript
87+
var array = require( '@stdlib/ndarray/array' );
88+
89+
var x = array( [ -1.0, 2.0, NaN, 4.0 ], {
90+
'shape': [ 2, 2 ],
91+
'order': 'row-major'
92+
});
93+
// returns <ndarray>[ [ -1.0, 2.0 ], [ NaN, 4.0 ] ]
94+
95+
var y = nanrange( x, {
96+
'dims': [ 0 ],
97+
'keepdims': true
98+
});
99+
// returns <ndarray>[ [ 0.0, 2.0 ] ]
100+
101+
y = nanrange( x, {
102+
'dims': [ 1 ],
103+
'keepdims': true
104+
});
105+
// returns <ndarray>[ [ 3.0 ], [ 0.0 ] ]
106+
107+
y = nanrange( x, {
108+
'dims': [ 0, 1 ],
109+
'keepdims': true
110+
});
111+
// returns <ndarray>[ [ 5.0 ] ]
112+
```
113+
114+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
115+
116+
```javascript
117+
var getDType = require( '@stdlib/ndarray/dtype' );
118+
var array = require( '@stdlib/ndarray/array' );
119+
120+
var x = array( [ -1.0, 2.0, NaN ], {
121+
'dtype': 'generic'
122+
});
123+
124+
var y = nanrange( x, {
125+
'dtype': 'float64'
126+
});
127+
// returns <ndarray>
128+
129+
var dt = String( getDType( y ) );
130+
// returns 'float64'
131+
```
132+
133+
#### nanrange.assign( x, out\[, options] )
134+
135+
Computes the [range][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions, ignoring `NaN` values, and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
136+
137+
```javascript
138+
var array = require( '@stdlib/ndarray/array' );
139+
var zeros = require( '@stdlib/ndarray/zeros' );
140+
141+
var x = array( [ -1.0, 2.0, NaN ] );
142+
var y = zeros( [] );
143+
144+
var out = nanrange.assign( x, y );
145+
// returns <ndarray>[ 3.0 ]
146+
147+
var bool = ( out === y );
148+
// returns true
149+
```
150+
151+
The method has the following parameters:
152+
153+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes].
154+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
155+
- **options**: function options (_optional_).
156+
157+
The method accepts the following options:
158+
159+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
160+
161+
</section>
162+
163+
<!-- /.usage -->
164+
165+
<section class="notes">
166+
167+
## Notes
168+
169+
- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor].
170+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
171+
172+
</section>
173+
174+
<!-- /.notes -->
175+
176+
<section class="examples">
177+
178+
## Examples
179+
180+
<!-- eslint no-undef: "error" -->
181+
182+
```javascript
183+
var uniform = require( '@stdlib/random/base/uniform' );
184+
var filledarrayBy = require( '@stdlib/array/filled-by' );
185+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
186+
var getDType = require( '@stdlib/ndarray/dtype' );
187+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
188+
var ndarray = require( '@stdlib/ndarray/ctor' );
189+
var nanrange = require( '@stdlib/stats/nanrange' );
190+
191+
function rand() {
192+
if ( bernoulli( 0.8 ) < 1 ) {
193+
return NaN;
194+
}
195+
return uniform( -50.0, 50.0 );
196+
}
197+
198+
// Generate an array of random numbers:
199+
var xbuf = filledarrayBy( 25, 'generic', rand );
200+
201+
// Wrap in an ndarray:
202+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
203+
console.log( ndarray2array( x ) );
204+
205+
// Perform a reduction:
206+
var y = nanrange( x, {
207+
'dims': [ 0 ]
208+
});
209+
210+
// Resolve the output array data type:
211+
var dt = getDType( y );
212+
console.log( dt );
213+
214+
// Print the results:
215+
console.log( ndarray2array( y ) );
216+
```
217+
218+
</section>
219+
220+
<!-- /.examples -->
221+
222+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
223+
224+
<section class="related">
225+
226+
</section>
227+
228+
<!-- /.related -->
229+
230+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
231+
232+
<section class="links">
233+
234+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
235+
236+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
237+
238+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
239+
240+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
241+
242+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
243+
244+
</section>
245+
246+
<!-- /.links -->
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
29+
var zeros = require( '@stdlib/array/zeros' );
30+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
31+
var pkg = require( './../package.json' ).name;
32+
var nanrange = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var options = {
38+
'dtype': 'float64'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Returns a random number.
46+
*
47+
* @private
48+
* @returns {number} random number
49+
*/
50+
function rand() {
51+
if ( bernoulli( 0.8 ) < 1 ) {
52+
return NaN;
53+
}
54+
return uniform( -10.0, 10.0 );
55+
}
56+
57+
/**
58+
* Creates a benchmark function.
59+
*
60+
* @private
61+
* @param {PositiveInteger} len - array length
62+
* @returns {Function} benchmark function
63+
*/
64+
function createBenchmark( len ) {
65+
var out;
66+
var x;
67+
68+
x = filledarrayBy( len, options.dtype, rand );
69+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
70+
71+
out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' );
72+
73+
return benchmark;
74+
75+
/**
76+
* Benchmark function.
77+
*
78+
* @private
79+
* @param {Benchmark} b - benchmark instance
80+
*/
81+
function benchmark( b ) {
82+
var o;
83+
var i;
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
o = nanrange.assign( x, out );
88+
if ( typeof o !== 'object' ) {
89+
b.fail( 'should return an ndarray' );
90+
}
91+
}
92+
b.toc();
93+
if ( isnan( o.get() ) ) {
94+
b.fail( 'should not return NaN' );
95+
}
96+
b.pass( 'benchmark finished' );
97+
b.end();
98+
}
99+
}
100+
101+
102+
// MAIN //
103+
104+
/**
105+
* Main execution sequence.
106+
*
107+
* @private
108+
*/
109+
function main() {
110+
var len;
111+
var min;
112+
var max;
113+
var f;
114+
var i;
115+
116+
min = 1; // 10^min
117+
max = 6; // 10^max
118+
119+
for ( i = min; i <= max; i++ ) {
120+
len = pow( 10, i );
121+
f = createBenchmark( len );
122+
bench( pkg+':assign:dtype='+options.dtype+',len='+len, f );
123+
}
124+
}
125+
126+
main();

0 commit comments

Comments
 (0)