Skip to content

feat(stats): add nanminmaxabs - incremental min/max absolute values ignoring NaNs #6387

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

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

-->

# incrnanminmaxabs

> Compute minimum and maximum absolute values incrementally, ignoring `NaN` values.

<section class="usage">

## Usage

```javascript
var incrnanminmaxabs = require( '@stdlib/stats/incr/nanminmaxabs' );
```

#### incrnanminmaxabs( \[out] )

Returns an accumulator `function` which incrementally computes minimum and maximum absolute values while ignoring `NaN` values.

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

By default, the returned accumulator `function` returns the minimum and maximum absolute values as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.

```javascript
var Float64Array = require( '@stdlib/array/float64' );

var accumulator = incrnanminmaxabs( new Float64Array( 2 ) );
```

#### accumulator( \[x] )

If provided an input value `x`, the accumulator function returns updated minimum and maximum absolute values, ignoring `NaN`. If not provided an input value `x`, the accumulator function returns the current minimum and maximum absolute values.

```javascript
var accumulator = incrnanminmaxabs();

var mm = accumulator();
// returns null

mm = accumulator( 2.0 );
// returns [ 2.0, 2.0 ]

mm = accumulator( NaN );
// returns [ 2.0, 2.0 ] (ignores NaN)

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

mm = accumulator( 3.0 );
// returns [ 1.0, 3.0 ]

mm = accumulator( -7.0 );
// returns [ 1.0, 7.0 ]

mm = accumulator();
// returns [ 1.0, 7.0 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- `NaN` values are ignored in calculations. This ensures `NaN` does not contaminate future results.
- If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

var accumulator;
var v;
var i;

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

// For each simulated datum, update the minimum and maximum absolute values...
for ( i = 0; i < 100; i++ ) {
v = ( randu()*100.0 ) - 50.0;
if ( randu() < 0.1 ) {
v = 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/maxabs`][@stdlib/stats/incr/maxabs]</span><span class="delimiter">: </span><span class="description">compute a maximum absolute value incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/minabs`][@stdlib/stats/incr/minabs]</span><span class="delimiter">: </span><span class="description">compute a minimum absolute value incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/minmax`][@stdlib/stats/incr/minmax]</span><span class="delimiter">: </span><span class="description">compute a minimum and maximum incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mminmaxabs`][@stdlib/stats/incr/mminmaxabs]</span><span class="delimiter">: </span><span class="description">compute moving minimum and maximum absolute values incrementally.</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">

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

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

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

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

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

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @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 isnan = require("@stdlib/math/base/assert/is-nan");
var pkg = require("./../package.json").name;
var incrnanminmaxabs = require("./../lib");

// MAIN //

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

b.tic();
for (i = 0; i < b.iterations; i++) {
v = randu() > 0.1 ? randu() - 0.5 : NaN; // Introduce some NaNs
acc(v);
}
v = acc();
b.toc();

// Ensure NaN values are ignored unless all inputs were NaN
if (isnan(v[0]) && isnan(v[1]) && i > 0) {
b.fail("should not return NaN unless all inputs were NaN");
}

b.pass("benchmark finished");
b.end();
});
39 changes: 39 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanminmaxabs/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{alias}}( [out] )
Returns an accumulator function which incrementally computes a minimum and
maximum absolute value.

If provided a value, the accumulator function returns an updated minimum and
maximum. If not provided a value, the accumulator function returns the
current minimum and maximum.

If provided `NaN`, the accumulator ignores it and does not update the
minimum or maximum values.

Parameters
----------
out: Array|TypedArray (optional)
Output array.

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

Examples
--------
> var accumulator = {{alias}}();
> var mm = accumulator();
null
> mm = accumulator( 2.0 );
[ 2, 2 ]
> mm = accumulator( -5.0 );
[ 2, 5 ]
> mm = accumulator( 3.0 );
[ 2, 5 ]
> mm = accumulator( 5.0 );
[ 2, 5 ]
> mm = accumulator();
[ 2, 5 ]

See Also
--------
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.
*/

// TypeScript Version: 4.1

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

import { ArrayLike } from '@stdlib/types/array';

/**
* If provided a value, the accumulator function returns updated absolute minimum and maximum values. If not provided a value, the accumulator function returns the current minimum and maximum values.
*
* ## Notes
*
* - If provided `NaN`, the minimum and maximum values are equal to `NaN` for all future invocations.
*
* @param x - input value
* @returns output array or null
*/
type accumulator = ( x?: number ) => ArrayLike<number> | null;

/**
* Returns an accumulator function which incrementally computes minimum and maximum absolute values.
*
* @param out - output array
* @returns accumulator function
*
* @example
* var accumulator = incrnanminmaxabs();
*
* var mm = accumulator();
* // returns null
*
* mm = accumulator( 2.0 );
* // returns [ 2.0, 2.0 ]
*
* mm = accumulator( -5.0 );
* // returns [ 2.0, 5.0 ]
*
* mm = accumulator( 3.0 );
* // returns [ 2.0, 5.0 ]
*
* mm = accumulator( 5.0 );
* // returns [ 2.0, 5.0 ]
*
* mm = accumulator();
* // returns [ 2.0, 5.0 ]
*/
declare function incrnanminmaxabs( out?: ArrayLike<number> ): accumulator;


// EXPORTS //

export = incrnanminmaxabs;
Loading
Loading