Skip to content

Commit 76f65a5

Browse files
Girish-Gargkgrytestdlib-bot
authored
feat: add stats/incr/nanmaxabs
PR-URL: #6116 Closes: #5566 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 9415971 commit 76f65a5

File tree

10 files changed

+683
-0
lines changed

10 files changed

+683
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# incrnanmaxabs
22+
23+
> Compute a maximum absolute value incrementally, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var incrnanmaxabs = require( '@stdlib/stats/incr/nanmaxabs' );
31+
```
32+
33+
#### incrnanmaxabs()
34+
35+
Returns an accumulator function which incrementally computes a maximum absolute value, ignoring `NaN` values.
36+
37+
```javascript
38+
var accumulator = incrnanmaxabs();
39+
```
40+
41+
#### accumulator( \[x] )
42+
43+
If provided an input value `x`, the accumulator function returns an updated maximum absolute value. If not provided an input value `x`, the accumulator function returns the current maximum absolute value.
44+
45+
```javascript
46+
var accumulator = incrnanmaxabs();
47+
48+
var max = accumulator( 2.0 );
49+
// returns 2.0
50+
51+
max = accumulator( 1.0 );
52+
// returns 2.0
53+
54+
max = accumulator( NaN );
55+
// returns 2.0
56+
57+
max = accumulator( -3.0 );
58+
// returns 3.0
59+
60+
max = accumulator();
61+
// returns 3.0
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- 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.
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var uniform = require( '@stdlib/random/base/uniform' );
86+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
87+
var incrnanmaxabs = require( '@stdlib/stats/incr/nanmaxabs' );
88+
89+
// Initialize an accumulator:
90+
var accumulator = incrnanmaxabs();
91+
92+
// For each simulated datum, update the max absolute value...
93+
var i;
94+
for ( i = 0; i < 100; i++ ) {
95+
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ) );
96+
}
97+
console.log( accumulator() );
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
105+
106+
<section class="related">
107+
108+
</section>
109+
110+
<!-- /.related -->
111+
112+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
113+
114+
<section class="links">
115+
116+
</section>
117+
118+
<!-- /.links -->
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pkg = require( './../package.json' ).name;
25+
var incrnanmaxabs = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg, function benchmark( b ) {
31+
var f;
32+
var i;
33+
b.tic();
34+
for ( i = 0; i < b.iterations; i++ ) {
35+
f = incrnanmaxabs();
36+
if ( typeof f !== 'function' ) {
37+
b.fail( 'should return a function' );
38+
}
39+
}
40+
b.toc();
41+
if ( typeof f !== 'function' ) {
42+
b.fail( 'should return a function' );
43+
}
44+
b.pass( 'benchmark finished' );
45+
b.end();
46+
});
47+
48+
bench( pkg+'::accumulator', function benchmark( b ) {
49+
var acc;
50+
var v;
51+
var i;
52+
53+
acc = incrnanmaxabs();
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = acc( i );
58+
if ( v !== v ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( v !== v ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a maximum
4+
absolute value, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated maximum
7+
absolute value. If not provided a value, the accumulator function returns
8+
the current maximum absolute value.
9+
10+
Returns
11+
-------
12+
acc: Function
13+
Accumulator function.
14+
15+
Examples
16+
--------
17+
> var accumulator = {{alias}}();
18+
> var m = accumulator()
19+
null
20+
> m = accumulator( 3.14 )
21+
3.14
22+
> m = accumulator( -5.0 )
23+
5.0
24+
> m = accumulator( NaN )
25+
5.0
26+
> m = accumulator( 10.1 )
27+
10.1
28+
> m = accumulator()
29+
10.1
30+
31+
See Also
32+
--------
33+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided a value, returns an updated maximum absolute value; otherwise, returns the current maximum absolute value.
25+
*
26+
* @param x - value
27+
* @returns maximum absolute value
28+
*/
29+
type accumulator = ( x?: number ) => number | null;
30+
31+
/**
32+
* Returns an accumulator function which incrementally computes a maximum absolute value, ignoring `NaN` values.
33+
*
34+
* @returns accumulator function
35+
*
36+
* @example
37+
* var accumulator = incrmaxabs();
38+
*
39+
* var v = accumulator();
40+
* // returns null
41+
*
42+
* v = accumulator( 2.0 );
43+
* // returns 2.0
44+
*
45+
* v = accumulator( -3.0 );
46+
* // returns 3.0
47+
*
48+
* v = accumulator( NaN );
49+
* // returns 3.0
50+
*
51+
* v = accumulator( 1.0 );
52+
* // returns 3.0
53+
*
54+
* v = accumulator();
55+
* // returns 3.0
56+
*/
57+
declare function incrmaxabs(): accumulator;
58+
59+
60+
// EXPORTS //
61+
62+
export = incrmaxabs;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import incrnanmaxabs = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanmaxabs(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
incrnanmaxabs( '5' ); // $ExpectError
32+
incrnanmaxabs( 5 ); // $ExpectError
33+
incrnanmaxabs( true ); // $ExpectError
34+
incrnanmaxabs( false ); // $ExpectError
35+
incrnanmaxabs( null ); // $ExpectError
36+
incrnanmaxabs( undefined ); // $ExpectError
37+
incrnanmaxabs( [] ); // $ExpectError
38+
incrnanmaxabs( {} ); // $ExpectError
39+
incrnanmaxabs( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanmaxabs();
45+
46+
acc(); // $ExpectType number | null
47+
acc( 3.14 ); // $ExpectType number | null
48+
}
49+
50+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
51+
{
52+
const acc = incrnanmaxabs();
53+
54+
acc( '5' ); // $ExpectError
55+
acc( true ); // $ExpectError
56+
acc( false ); // $ExpectError
57+
acc( null ); // $ExpectError
58+
acc( [] ); // $ExpectError
59+
acc( {} ); // $ExpectError
60+
acc( ( x: number ): number => x ); // $ExpectError
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var uniform = require( '@stdlib/random/base/uniform' );
22+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
23+
var incrnanmaxabs = require( './../lib' );
24+
25+
// Initialize an accumulator:
26+
var accumulator = incrnanmaxabs();
27+
28+
// For each simulated datum, update the max absolute value...
29+
console.log( '\nValue\tMaxAbs\n' );
30+
var max;
31+
var v;
32+
var i;
33+
for ( i = 0; i < 100; i++ ) {
34+
v = ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 );
35+
max = accumulator( v );
36+
console.log( '%d\t%d', v.toFixed( 4 ), ( max === null ) ? NaN : max.toFixed( 3 ) );
37+
}
38+
console.log( '\nFinal maximum absolute value: %d\n', accumulator() );

0 commit comments

Comments
 (0)