Skip to content

Commit 0271a9e

Browse files
committed
added nanrange
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 934c483 commit 0271a9e

File tree

10 files changed

+753
-0
lines changed

10 files changed

+753
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
# incrnanrange
22+
23+
> Compute a [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. This implementation ignores `NaN` values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var incrnanrange = require( '@stdlib/stats/incr/nanrange' );
39+
```
40+
41+
#### incrnanrange()
42+
43+
Returns an accumulator `function` which incrementally computes a [range][range], ignoring `NaN` values.
44+
45+
```javascript
46+
var accumulator = incrnanrange();
47+
```
48+
49+
#### accumulator( \[x] )
50+
51+
If provided an input value `x`, the accumulator function returns an updated [range][range], ignoring `NaN` values. If not provided an input value `x`, the accumulator function returns the current [range][range].
52+
53+
```javascript
54+
var accumulator = incrnanrange();
55+
56+
var range = accumulator( -2.0 );
57+
// returns 0.0
58+
59+
range = accumulator( NaN );
60+
// returns 0.0 (ignores NaN)
61+
62+
range = accumulator( 1.0 );
63+
// returns 3.0
64+
65+
range = accumulator( 3.0 );
66+
// returns 5.0
67+
68+
range = accumulator();
69+
// returns 5.0
70+
```
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<section class="notes">
77+
78+
## Notes
79+
80+
- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
81+
- Unlike `incrrange`, `incrnanrange` ignores `NaN` values, ensuring they do not affect the computed range.
82+
83+
</section>
84+
85+
<!-- /.notes -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var randu = require( '@stdlib/random/base/randu' );
95+
var incrnanrange = require( '@stdlib/stats/incr/nanrange' );
96+
97+
var accumulator;
98+
var v;
99+
var i;
100+
101+
// Initialize an accumulator:
102+
accumulator = incrnanrange();
103+
104+
// For each simulated datum, update the range...
105+
for ( i = 0; i < 100; i++ ) {
106+
v = randu() > 0.1 ? randu() * 100.0 : NaN; // Introduce some NaNs
107+
accumulator( v );
108+
}
109+
console.log( accumulator() );
110+
```
111+
112+
</section>
113+
114+
<!-- /.examples -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
* * *
121+
122+
## See Also
123+
124+
- <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>
125+
- <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>
126+
127+
</section>
128+
129+
<!-- /.related -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
136+
137+
<!-- <related-links> -->
138+
139+
[@stdlib/stats/incr/max]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/max
140+
141+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
142+
143+
[@stdlib/stats/incr/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/min
144+
145+
[@stdlib/stats/incr/mrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mrange
146+
147+
[@stdlib/stats/incr/nanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmean
148+
149+
<!-- </related-links> -->
150+
151+
</section>
152+
153+
<!-- /.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 incrnanrange = 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 = incrnanrange();
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 = incrnanrange();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()*10.0 );
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{{alias}}()
2+
Returns an accumulator function which incrementally computes a range, ignoring
3+
`NaN` values.
4+
5+
If provided a value, the accumulator function returns an updated range. If
6+
not provided a value, the accumulator function returns the current range.
7+
8+
Returns
9+
-------
10+
acc: Function
11+
Accumulator function.
12+
13+
Examples
14+
--------
15+
> var accumulator = {{alias}}();
16+
> var v = accumulator()
17+
null
18+
> v = accumulator( -2.0 )
19+
0.0
20+
> v = accumulator( NaN )
21+
0.0
22+
> v = accumulator( 3.0 )
23+
5.0
24+
> v = accumulator( 1.0 )
25+
5.0
26+
> v = accumulator()
27+
5.0
28+
29+
See Also
30+
--------
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
29+
*
30+
* @param x - value
31+
* @returns range
32+
*/
33+
type accumulator = ( x?: number ) => number | null;
34+
35+
/**
36+
* Returns an accumulator function which incrementally computes a range.
37+
*
38+
* @returns accumulator function
39+
*
40+
* @example
41+
* var accumulator = incrnanrange();
42+
*
43+
* var v = accumulator();
44+
* // returns null
45+
*
46+
* v = accumulator( 2.0 );
47+
* // returns 0.0
48+
*
49+
* v = accumulator( -1.0 );
50+
* // returns 3.0
51+
*
52+
* v = accumulator( NaN );
53+
* // returns 3.0
54+
*
55+
* v = accumulator( -3.0 );
56+
* // returns 5.0
57+
*
58+
* v = accumulator();
59+
* // returns 5.0
60+
*/
61+
declare function incrnanrange(): accumulator;
62+
63+
64+
// EXPORTS //
65+
66+
export = incrnanrange;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
import incrnanrange = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanrange(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
incrnanrange( '5' ); // $ExpectError
32+
incrnanrange( 5 ); // $ExpectError
33+
incrnanrange( true ); // $ExpectError
34+
incrnanrange( false ); // $ExpectError
35+
incrnanrange( null ); // $ExpectError
36+
incrnanrange( undefined ); // $ExpectError
37+
incrnanrange( [] ); // $ExpectError
38+
incrnanrange( {} ); // $ExpectError
39+
incrnanrange( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanrange();
45+
46+
acc(); // $ExpectType number | null
47+
acc( 3.14 ); // $ExpectType number | null
48+
}
49+
50+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
51+
{
52+
const acc = incrnanrange();
53+
54+
acc( '5' ); // $ExpectError
55+
acc( true ); // $ExpectError
56+
acc( false ); // $ExpectError
57+
acc( null ); // $ExpectError
58+
acc( [] ); // $ExpectError
59+
acc( {} ); // $ExpectError
60+
acc( ( x: number ): number => x ); // $ExpectError
61+
}

0 commit comments

Comments
 (0)