Skip to content

feat: add stats/incr/nanmrange #6310

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 7 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
151 changes: 151 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanmrange/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<!--

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

-->

# incrnanmrange

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

<section class="intro">

The [**range**][range] is defined as the difference between the maximum and minimum values.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var incrnanmrange = require( '@stdlib/stats/incr/nanmrange' );
```

#### incrnanmrange( window )

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

```javascript
var accumulator = incrnanmrange( 3 );
```

#### accumulator( \[x] )

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

```javascript
var accumulator = incrnanmrange( 3 );

var r = accumulator();
// returns null

// Fill the window...
r = accumulator( 2.0 ); // [2.0]
// returns 0.0

r = accumulator( 1.0 ); // [2.0, 1.0]
// returns 1.0

r = accumulator( NaN ); // [2.0, 1.0, NaN]
// returns 1.0

r = accumulator( 3.0 ); // [1.0, NaN, 3.0]
// returns 2.0

// Window begins sliding...
r = accumulator( -7.0 ); // [NaN, 3.0, -7.0]
// returns 10.0

r = accumulator( -5.0 ); // [3.0, -7.0, -5.0]
// returns 10.0

r = accumulator();
// returns 10.0
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

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

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

var accumulator;
var v;
var i;

// Initialize an accumulator:
accumulator = incrnanmrange( 5 );

// For each simulated datum, update the moving range...
for ( i = 0; i < 100; i++ ) {
if ( randu() < 0.2 ) {
v = NaN;
} else {
v = randu() * 100.0;
}
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">

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

<!-- </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 incrnanmrange = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var f;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
f = incrnanmrange( (i%5)+1 );
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 = incrnanmrange( 5 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu() );
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();
});
44 changes: 44 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanmrange/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

{{alias}}( W )
Returns an accumulator function which incrementally computes a moving range.

The `W` parameter defines the number of values over which to compute the
moving range.

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

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.

Parameters
----------
W: integer
Window size.

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

Examples
--------
> var accumulator = {{alias}}( 3 );
> var r = accumulator()
null
> r = accumulator( 2.0 )
0.0
> r = accumulator( -5.0 )
7.0
> r = accumulator( NaN )
7.0
> r = accumulator( 5.0 )
10.0
> r = accumulator()
10.0

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* @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`, the `NaN` value is ignored.
* - If the number of non-NaN values in the window is less than two, the function returns `null`.
*
* @param x - value
* @returns range
*/
type accumulator = ( x?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a moving range, ignoring `NaN` values.
*
* ## Notes
*
* - The `W` parameter defines the number of values over which to compute the moving range.
* - 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.
* - `NaN` values are ignored.
*
* @param W - window size
* @throws must provide a positive integer
* @returns accumulator function
*
* @example
* var accumulator = incrnanmrange( 3 );
*
* var r = accumulator();
* // returns null
*
* r = accumulator( 2.0 );
* // returns 0.0
*
* r = accumulator( -5.0 );
* // returns 7.0
*
* r = accumulator( NaN );
* // returns 7.0
*
* r = accumulator( 3.0 );
* // returns 8.0
*
* r = accumulator( 5.0 );
* // returns 10.0
*
* r = accumulator();
* // returns 10.0
*/
declare function incrnanmrange( W: number ): accumulator;


// EXPORTS //

export = incrnanmrange;
Loading