Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(lib): added stats/incr/nanmmin package #5849

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 8 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
161 changes: 78 additions & 83 deletions lib/node_modules/@stdlib/stats/incr/mmin/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,94 +58,89 @@ var format = require( '@stdlib/string/format' );
* m = accumulator();
* // returns -5.0
*/
function incrmmin( W ) {
var buf;
var min;
var N;
var i;
if ( !isPositiveInteger( W ) ) {
throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) );
}
buf = new Float64Array( W );
min = PINF;
i = -1;
N = 0;
function incrnanmmin( W ) {
var buf;
var min;
var N;
var i;
if ( !isPositiveInteger( W ) ) {
throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) );
}
buf = new Float64Array( W );
min = PINF;
i = -1;
N = 0;

return accumulator;
return accumulator;

/**
* If provided a value, the accumulator function returns an updated minimum. If not provided a value, the accumulator function returns the current minimum.
*
* @private
* @param {number} [x] - input value
* @returns {(number|null)} minimum value or null
*/
function accumulator( x ) {
var v;
var k;
if ( arguments.length === 0 ) {
if ( N === 0 ) {
return null;
}
return min;
}
// Update the index for managing the circular buffer:
i = (i+1) % W;
/**
* If provided a value, the accumulator function returns an updated minimum, ignoring NaN values. If not provided a value, the accumulator function returns the current minimum.
*
* @private
* @param {number} [x] - input value
* @returns {(number|null)} minimum value or null
*/
function accumulator( x ) {
var v;
var k;
if ( arguments.length === 0 ) {
if ( N === 0 ) {
return null;
}
return min;
}
if ( isnan( x ) ) {
return min; // Ignore NaN values and return current minimum
}

// Case: update initial window...
if ( N < W ) {
N += 1;
if (
isnan( x ) ||
x < min ||
( x === min && isNegativeZero( x ) )
) {
min = x;
}
}
// Case: incoming value is NaN or less than current minimum value...
else if ( isnan( x ) || x < min ) {
min = x;
}
// Case: outgoing value is the current minimum and the new value is greater than the minimum, and, thus, we need to find a new minimum among the current values...
else if ( ( buf[ i ] === min && x > min ) || isnan( buf[ i ] ) ) {
min = x;
for ( k = 0; k < W; k++ ) {
if ( k !== i ) {
v = buf[ k ];
if ( isnan( v ) ) {
min = v;
break; // no need to continue searching
}
if ( v < min || ( v === min && isNegativeZero( v ) ) ) {
min = v;
}
}
}
}
// Case: outgoing value is the current minimum, which is zero, and the new value is also zero, and, thus, we need to correctly handle signed zeros...
else if ( buf[ i ] === min && x === min && x === 0.0 ) {
if ( isNegativeZero( x ) ) {
min = x;
} else if ( isNegativeZero( buf[ i ] ) ) {
// Because the outgoing and incoming are different signs (-,+), we need to search the buffer to see if it contains a negative zero. If so, the minimum value remains negative zero; otherwise, the minimum value is incoming value...
min = x;
for ( k = 0; k < W; k++ ) {
if ( k !== i && isNegativeZero( buf[ k ] ) ) {
min = buf[ k ];
break;
}
}
}
// Case: the outgoing and incoming values are both positive zero, so nothing changes
}
// Case: updating existing window; however, the minimum value does not change so nothing to do but update our buffer...
// Update the index for managing the circular buffer:
i = (i + 1) % W;

buf[ i ] = x;
return min;
}
}
// Case: update initial window...
if ( N < W ) {
N += 1;
if ( x < min || ( x === min && isNegativeZero( x ) ) ) {
min = x;
}
}
// Case: incoming value is less than current minimum value...
else if ( x < min ) {
min = x;
}
// Case: outgoing value is the current minimum and the new value is greater than the minimum, requiring a new minimum search...
else if ( buf[ i ] === min && x > min ) {
min = x;
for ( k = 0; k < W; k++ ) {
if ( k !== i ) {
v = buf[ k ];
if ( isnan( v ) ) {
continue; // Ignore NaN values
}
if ( v < min || ( v === min && isNegativeZero( v ) ) ) {
min = v;
}
}
}
}
// Case: handling signed zero differences...
else if ( buf[ i ] === min && x === min && x === 0.0 ) {
if ( isNegativeZero( x ) ) {
min = x;
} else if ( isNegativeZero( buf[ i ] ) ) {
min = x;
for ( k = 0; k < W; k++ ) {
if ( k !== i && isNegativeZero( buf[ k ] ) ) {
min = buf[ k ];
break;
}
}
}
}

buf[ i ] = x;
return min;
}
}

// EXPORTS //
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// EXPORTS //
// EXPORTS //
module.exports = incrnanmmin


Expand Down
157 changes: 157 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanmmin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!--

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

-->

# incrnanmmin

> Compute a moving minimum value incrementally, ignoring `NaN` values.

<section class="usage">

## Usage

```javascript
var incrnanmmin = require( '@stdlib/stats/incr/nanmmin' );
```

#### incrnanmmin( window )

Returns an accumulator `function` which incrementally computes a moving minimum value. The `window` parameter defines the number of values over which to compute the moving minimum, ignoring `NaN` values.

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

#### accumulator( \[x] )

If provided an input value `x`, the accumulator function returns an updated minimum value. If not provided an input value `x`, the accumulator function returns the current minimum value.

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

var m = accumulator();
// returns null

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

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

m = accumulator( NaN );
// returns 1.0

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

// Window begins sliding...
m = accumulator( -7.0 ); // [1.0, 3.0, -7.0]
// returns -7.0

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

m = accumulator();
// returns -7.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.
- 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 incrnanmmin = require( '@stdlib/stats/incr/nanmmin' );

var accumulator;
var v;
var i;

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

// For each simulated datum, update the moving minimum...
for ( i = 0; i < 100; i++ ) {
v = ( randu() < 0.1 ) ? NaN : 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">

* * *

## See Also

- <span class="package-name">[`@stdlib/stats/incr/min`][@stdlib/stats/incr/min]</span><span class="delimiter">: </span><span class="description">compute a minimum value incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mmax`][@stdlib/stats/incr/mmax]</span><span class="delimiter">: </span><span class="description">compute a moving maximum incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mmidrange`][@stdlib/stats/incr/mmidrange]</span><span class="delimiter">: </span><span class="description">compute a moving mid-range incrementally.</span>
- <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/msummary`][@stdlib/stats/incr/msummary]</span><span class="delimiter">: </span><span class="description">compute a moving statistical summary 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/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/min

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

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

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

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

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

</section>

<!-- /.links -->
Loading
Loading