Skip to content

feat: add stats/incr/nanmaxabs #6116

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

Merged
merged 6 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
chore: clean-up
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: passed
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: passed
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
  • Loading branch information
kgryte committed Apr 29, 2025
commit 2d894812cdbd35e543c8c0899eaf29fb9f322346
48 changes: 13 additions & 35 deletions lib/node_modules/@stdlib/stats/incr/nanmaxabs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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
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,
Expand All @@ -20,7 +20,7 @@ limitations under the License.

# incrnanmaxabs

> Compute a maximum absolute value incrementally.
> Compute a maximum absolute value incrementally, ignoring `NaN` values.

<section class="usage">

Expand All @@ -32,7 +32,7 @@ var incrnanmaxabs = require( '@stdlib/stats/incr/nanmaxabs' );

#### incrnanmaxabs()

Returns an accumulator `function` which incrementally computes a maximum absolute value.
Returns an accumulator function which incrementally computes a maximum absolute value, ignoring `NaN` values.

```javascript
var accumulator = incrnanmaxabs();
Expand All @@ -51,10 +51,10 @@ var max = accumulator( 2.0 );
max = accumulator( 1.0 );
// returns 2.0

max = accumulator( -3.0 );
// returns 3.0
max = accumulator( NaN );
// returns 2.0

max = accumulator( NaN ); //Ignored
max = accumulator( -3.0 );
// returns 3.0

max = accumulator();
Expand All @@ -69,7 +69,7 @@ max = accumulator();

## Notes

- Input values are type checked. If provided `NaN` or any value which results in `NaN`, it will be ignored but you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
- 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.

</section>

Expand All @@ -82,21 +82,17 @@ max = accumulator();
<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var incrnanmaxabs = require( './../lib' );

var accumulator;
var max;
var v;
var i;
var uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var incrnanmaxabs = require( '@stdlib/stats/incr/nanmaxabs' );

// Initialize an accumulator:
accumulator = incrnanmaxabs();
var accumulator = incrnanmaxabs();

// For each simulated datum, update the max absolute value...
var i;
for ( i = 0; i < 100; i++ ) {
v = ( randu() < 0.2 ) ? NaN : (randu() * 100.0) - 50.0;
max = accumulator( v );
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ) );
}
console.log( accumulator() );
```
Expand All @@ -109,14 +105,6 @@ console.log( accumulator() );

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/stats/incr/max`][@stdlib/stats/incr/max]</span><span class="delimiter">: </span><span class="description">compute a maximum 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/mmaxabs`][@stdlib/stats/incr/mmaxabs]</span><span class="delimiter">: </span><span class="description">compute a moving maximum absolute value incrementally.</span>

</section>

<!-- /.related -->
Expand All @@ -125,16 +113,6 @@ console.log( accumulator() );

<section class="links">

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

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

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

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

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// MODULES //

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

Expand Down Expand Up @@ -55,7 +54,7 @@ bench( pkg+'::accumulator', function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu()-0.5 );
v = acc( i );
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
Expand Down
8 changes: 3 additions & 5 deletions lib/node_modules/@stdlib/stats/incr/nanmaxabs/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

{{alias}}()
Returns an accumulator function which incrementally computes a maximum
absolute value.
absolute value, ignoring `NaN` values.

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

If provided `NaN`, it will be ignored.

Returns
-------
acc: Function
Expand All @@ -23,9 +21,9 @@
3.14
> m = accumulator( -5.0 )
5.0
> m = accumulator( 10.1 )
10.1
> m = accumulator( NaN )
5.0
> m = accumulator( 10.1 )
10.1
> m = accumulator()
10.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@
/**
* If provided a value, returns an updated maximum absolute value; otherwise, returns the current maximum absolute value.
*
* ## Notes
*
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, it will be ignored.
*
* @param x - value
* @returns maximum absolute value
*/
type accumulator = ( x?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a maximum absolute value.
* Returns an accumulator function which incrementally computes a maximum absolute value, ignoring `NaN` values.
*
* @returns accumulator function
*
Expand All @@ -49,10 +45,10 @@ type accumulator = ( x?: number ) => number | null;
* v = accumulator( -3.0 );
* // returns 3.0
*
* v = accumulator( 1.0 );
* v = accumulator( NaN );
* // returns 3.0
*
* v = accumulator( NaN );
* v = accumulator( 1.0 );
* // returns 3.0
*
* v = accumulator();
Expand Down
23 changes: 8 additions & 15 deletions lib/node_modules/@stdlib/stats/incr/nanmaxabs/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,21 @@

'use strict';

var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var uniform = require( '@stdlib/random/base/uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var incrnanmaxabs = require( './../lib' );

var accumulator;
var max;
var v;
var i;

// Initialize an accumulator:
accumulator = incrnanmaxabs();
var accumulator = incrnanmaxabs();

// For each simulated datum, update the max absolute value...
console.log( '\nValue\tMaxAbs\n' );
var max;
var v;
var i;
for ( i = 0; i < 100; i++ ) {
if ( randu() < 0.2 ) {
v = NaN;
}
else {
v = ( randu() * 100.0 ) - 50.0;
}
v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 );
max = accumulator( v );
console.log( '%d\t%d', ( isnan(v) ) ? 'NaN' : v.toFixed( 3 ), max.toFixed( 3 ) );
console.log( '%d\t%d', v.toFixed( 4 ), ( max === null ) ? NaN : max.toFixed( 3 ) );
}
console.log( '\nFinal maximum absolute value: %d\n', accumulator() );
5 changes: 4 additions & 1 deletion lib/node_modules/@stdlib/stats/incr/nanmaxabs/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'use strict';

/**
* Compute a maximum absolute value incrementally.
* Compute a maximum absolute value incrementally, ignoring `NaN` values.
*
* @module @stdlib/stats/incr/nanmaxabs
*
Expand All @@ -37,6 +37,9 @@
* max = accumulator( -5.0 );
* // returns 5.0
*
* max = accumulator( NaN );
* // returns 5.0
*
* max = accumulator( 10.1 );
* // returns 10.1
*
Expand Down
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/stats/incr/nanmaxabs/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
// MAIN //

/**
* Returns an accumulator function which incrementally computes a maximum absolute value, ignoring `NaN` value.
* Returns an accumulator function which incrementally computes a maximum absolute value, ignoring `NaN` values.
*
* @returns {Function} accumulator function
*
Expand All @@ -43,10 +43,10 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
* max = accumulator( -5.0 );
* // returns 5.0
*
* max = accumulator( 10.1 );
* // returns 10.1
* max = accumulator( NaN );
* // returns 5.0
*
* max = accumulator( NaN ); //Ignored
* max = accumulator( 10.1 );
* // returns 10.1
*
* max = accumulator();
Expand All @@ -64,7 +64,7 @@ function incrnanmaxabs() {
* @returns {(number|null)} maximum absolute value or null
*/
function accumulator( x ) {
if ( arguments.length === 0 || isnan(x) ) {
if ( arguments.length === 0 || isnan( x ) ) {
return max();
}
return max( x );
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/stats/incr/nanmaxabs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@stdlib/stats/incr/nanmaxabs",
"version": "0.0.0",
"description": "Compute a maximum absolute value incrementally.",
"description": "Compute a maximum absolute value incrementally, ignoring NaN values.",
"license": "Apache-2.0",
"author": {
"name": "The Stdlib Authors",
Expand Down
19 changes: 4 additions & 15 deletions lib/node_modules/@stdlib/stats/incr/nanmaxabs/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

var tape = require( 'tape' );
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
var abs = require( '@stdlib/math/base/special/abs' );
var incrmaxabs = require( '@stdlib/stats/incr/nanmaxabs/lib' );
var incrmaxabs = require( './../lib' );


// TESTS //
Expand All @@ -39,7 +38,7 @@ tape( 'the function returns an accumulator function', function test( t ) {
t.end();
});

tape( 'if not provided any values, the initial returned maximum absolute value is `null`', function test( t ) {
tape( 'the initial accumulated value is `null`', function test( t ) {
var acc = incrmaxabs();
t.equal( acc(), null, 'returns null' );
t.end();
Expand All @@ -50,29 +49,19 @@ tape( 'the accumulator function incrementally computes a maximum absolute value'
var actual;
var data;
var acc;
var max;
var ad;
var N;
var d;
var i;

data = [ 2.0, -3.0, NaN, 2.0, -4.0, NaN, 3.0, 4.0 ];
N = data.length;

expected = [];
expected = [ 2.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0 ];
actual = [];

acc = incrmaxabs();

max = data[ 0 ];
for ( i = 0; i < N; i++ ) {
d = data[ i ];
ad = abs( d );
if ( ad > max ) {
max = ad;
}
expected.push(max);
actual.push(acc( d ));
actual.push( acc( data[ i ] ) );
}
t.deepEqual( actual, expected, 'returns expected incremental results' );
t.end();
Expand Down