Skip to content

feat: add stats/incr/nanrange #5635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanrange/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# incrnanrange

> Compute a [range][range] incrementally, ignoring `NaN` values.

<section class="intro">

The [**range**][range] is defined as the difference between the maximum and minimum values. This implementation ignores `NaN` values.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var incrnanrange = require( '@stdlib/stats/incr/nanrange' );
```

#### incrnanrange()

Returns an accumulator `function` which incrementally computes a [range][range], ignoring `NaN` values.

```javascript
var accumulator = incrnanrange();
```

#### accumulator( \[x] )

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].

```javascript
var accumulator = incrnanrange();

var range = accumulator( -2.0 );
// returns 0.0

range = accumulator( NaN );
// returns 0.0 (ignores NaN)

range = accumulator( 1.0 );
// returns 3.0

range = accumulator( 3.0 );
// returns 5.0

range = accumulator();
// returns 5.0
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- 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.
- Unlike `incrrange`, `incrnanrange` ignores `NaN` values, ensuring they do not affect the computed range.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var incrnanrange = require( '@stdlib/stats/incr/nanrange' );

var accumulator;
var v;
var i;

// Initialize an accumulator:
accumulator = incrnanrange();

// For each simulated datum, update the range...
for ( i = 0; i < 100; i++ ) {
v = ( randu() > 0.1 ) ? ( randu() * 100.0 ) : NaN;
accumulator( v );
}
console.log( accumulator() );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <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>
- <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>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29

<!-- <related-links> -->

[@stdlib/stats/incr/mrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mrange

[@stdlib/stats/incr/nanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nanmean

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var pkg = require( './../package.json' ).name;
var incrnanrange = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var f;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
f = incrnanrange();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
}
b.toc();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::accumulator', function benchmark( b ) {
var acc;
var v;
var i;

acc = incrnanrange();

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu()*10.0 );
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanrange/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{alias}}()
Returns an accumulator function which incrementally computes a range,
ignoring `NaN` values.

If provided a value, the accumulator function returns an updated range. If
not provided a value, the accumulator function returns the current range.

Returns
-------
acc: Function
Accumulator function.

Examples
--------
> var accumulator = {{alias}}();
> var v = accumulator()
null
> v = accumulator( -2.0 )
0.0
> v = accumulator( NaN )
0.0
> v = accumulator( 3.0 )
5.0
> v = accumulator( 1.0 )
5.0
> v = accumulator()
5.0

See Also
--------
66 changes: 66 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

/**
* If provided a value, returns an updated range; otherwise, returns the current range.
*
* ## Notes
*
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
*
* @param x - value
* @returns range
*/
type accumulator = ( x?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a range.
*
* @returns accumulator function
*
* @example
* var accumulator = incrnanrange();
*
* var v = accumulator();
* // returns null
*
* v = accumulator( 2.0 );
* // returns 0.0
*
* v = accumulator( -1.0 );
* // returns 3.0
*
* v = accumulator( NaN );
* // returns 3.0
*
* v = accumulator( -3.0 );
* // returns 5.0
*
* v = accumulator();
* // returns 5.0
*/
declare function incrnanrange(): accumulator;


// EXPORTS //

export = incrnanrange;
61 changes: 61 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanrange/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import incrnanrange = require( './index' );


// TESTS //

// The function returns an accumulator function...
{
incrnanrange(); // $ExpectType accumulator
}

// The compiler throws an error if the function is provided arguments...
{
incrnanrange( '5' ); // $ExpectError
incrnanrange( 5 ); // $ExpectError
incrnanrange( true ); // $ExpectError
incrnanrange( false ); // $ExpectError
incrnanrange( null ); // $ExpectError
incrnanrange( undefined ); // $ExpectError
incrnanrange( [] ); // $ExpectError
incrnanrange( {} ); // $ExpectError
incrnanrange( ( x: number ): number => x ); // $ExpectError
}

// The function returns an accumulator function which returns an accumulated result...
{
const acc = incrnanrange();

acc(); // $ExpectType number | null
acc( 3.14 ); // $ExpectType number | null
}

// The compiler throws an error if the returned accumulator function is provided invalid arguments...
{
const acc = incrnanrange();

acc( '5' ); // $ExpectError
acc( true ); // $ExpectError
acc( false ); // $ExpectError
acc( null ); // $ExpectError
acc( [] ); // $ExpectError
acc( {} ); // $ExpectError
acc( ( x: number ): number => x ); // $ExpectError
}
Loading