Skip to content

Commit d9213cf

Browse files
committed
Added nanmrange
1 parent 551a3e9 commit d9213cf

File tree

10 files changed

+1093
-0
lines changed

10 files changed

+1093
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# incrnanmrange
22+
23+
> Compute a moving [range][range] incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [**range**][range] is defined as the difference between the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var incrnanmrange = require( '@stdlib/stats/incr/nanmrange' );
39+
```
40+
41+
#### incrnanmrange( window )
42+
43+
Returns an accumulator `function` which incrementally computes a moving [range][range], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [range][range].
44+
45+
```javascript
46+
var accumulator = incrnanmrange( 3 );
47+
```
48+
49+
#### accumulator( \[x] )
50+
51+
If provided an input value `x`, the accumulator function returns an updated [range][range]. If not provided an input value `x`, the accumulator function returns the current [range][range].
52+
53+
```javascript
54+
var accumulator = incrnanmrange( 3 );
55+
56+
var r = accumulator();
57+
// returns null
58+
59+
// Fill the window...
60+
r = accumulator( 2.0 ); // [2.0]
61+
// returns 0.0
62+
63+
r = accumulator( 1.0 ); // [2.0, 1.0]
64+
// returns 1.0
65+
66+
r = accumulator( NaN ); // [2.0, 1.0, NaN]
67+
// returns 1.0
68+
69+
r = accumulator( 3.0 ); // [1.0, NaN, 3.0]
70+
// returns 2.0
71+
72+
// Window begins sliding...
73+
r = accumulator( -7.0 ); // [NaN, 3.0, -7.0]
74+
// returns 10.0
75+
76+
r = accumulator( -5.0 ); // [3.0, -7.0, -5.0]
77+
// returns 10.0
78+
79+
r = accumulator();
80+
// returns 10.0
81+
```
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<section class="notes">
88+
89+
## Notes
90+
91+
- Input values are **not** type checked. If provided `NaN`, the value is ignored. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
92+
- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
93+
94+
</section>
95+
96+
<!-- /.notes -->
97+
98+
<section class="examples">
99+
100+
## Examples
101+
102+
<!-- eslint no-undef: "error" -->
103+
104+
```javascript
105+
var randu = require( '@stdlib/random/base/randu' );
106+
var incrnanmrange = require( '@stdlib/stats/incr/nanmrange' );
107+
108+
var accumulator;
109+
var v;
110+
var i;
111+
112+
// Initialize an accumulator:
113+
accumulator = incrnanmrange( 5 );
114+
115+
// For each simulated datum, update the moving range...
116+
for ( i = 0; i < 100; i++ ) {
117+
if ( randu() < 0.2 ) {
118+
v = NaN;
119+
} else {
120+
v = randu() * 100.0;
121+
}
122+
accumulator( v );
123+
}
124+
console.log( accumulator() );
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
* * *
136+
137+
## See Also
138+
139+
- <span class="package-name">[`@stdlib/stats/incr/mrange`][@stdlib/stats/incr/mrange]</span><span class="delimiter">: </span><span class="description">compute a moving range incrementally.</span>
140+
- <span class="package-name">[`@stdlib/stats/incr/nanmean`][@stdlib/stats/incr/nanmean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally, ignoring NaN values.</span>
141+
- <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>
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
152+
153+
<!-- <related-links> -->
154+
155+
[@stdlib/stats/incr/mrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mrange
156+
157+
[@stdlib/stats/incr/nanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmean
158+
159+
[@stdlib/stats/incr/range]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/range
160+
161+
<!-- </related-links> -->
162+
163+
</section>
164+
165+
<!-- /.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) 2018 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 incrnanmrange = 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 = incrnanmrange( (i%5)+1 );
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 = incrnanmrange( 5 );
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving range.
4+
5+
The `W` parameter defines the number of values over which to compute the
6+
moving range.
7+
8+
If provided a value, the accumulator function returns an updated moving
9+
range. If not provided a value, the accumulator function returns the current
10+
moving range.
11+
12+
As `W` values are needed to fill the window buffer, the first `W-1` returned
13+
values are calculated from smaller sample sizes. Until the window is full,
14+
each returned value is calculated from all provided values.
15+
16+
Parameters
17+
----------
18+
W: integer
19+
Window size.
20+
21+
Returns
22+
-------
23+
acc: Function
24+
Accumulator function.
25+
26+
Examples
27+
--------
28+
> var accumulator = {{alias}}( 3 );
29+
> var r = accumulator()
30+
null
31+
> r = accumulator( 2.0 )
32+
0.0
33+
> r = accumulator( -5.0 )
34+
7.0
35+
> r = accumulator( NaN )
36+
7.0
37+
> r = accumulator( 5.0 )
38+
10.0
39+
> r = accumulator()
40+
10.0
41+
42+
See Also
43+
--------
44+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 range; otherwise, returns the current range.
25+
*
26+
* ## Notes
27+
*
28+
* - If provided `NaN`, the `NaN` value is ignored.
29+
* - If the number of non-NaN values in the window is less than two, the function returns `null`.
30+
*
31+
* @param x - value
32+
* @returns range
33+
*/
34+
type accumulator = ( x?: number ) => number | null;
35+
36+
/**
37+
* Returns an accumulator function which incrementally computes a moving range, ignoring `NaN` values.
38+
*
39+
* ## Notes
40+
*
41+
* - The `W` parameter defines the number of values over which to compute the moving range.
42+
* - As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
43+
* - `NaN` values are ignored.
44+
*
45+
* @param W - window size
46+
* @throws must provide a positive integer
47+
* @returns accumulator function
48+
*
49+
* @example
50+
* var accumulator = incrnanmrange( 3 );
51+
*
52+
* var r = accumulator();
53+
* // returns null
54+
*
55+
* r = accumulator( 2.0 );
56+
* // returns 0.0
57+
*
58+
* r = accumulator( -5.0 );
59+
* // returns 7.0
60+
*
61+
* r = accumulator( NaN );
62+
* // returns 7.0
63+
*
64+
* r = accumulator( 3.0 );
65+
* // returns 8.0
66+
*
67+
* r = accumulator( 5.0 );
68+
* // returns 10.0
69+
*
70+
* r = accumulator();
71+
* // returns 10.0
72+
*/
73+
declare function incrnanmrange( W: number ): accumulator;
74+
75+
76+
// EXPORTS //
77+
78+
export = incrnanmrange;

0 commit comments

Comments
 (0)