Skip to content

Commit d67b212

Browse files
jsai28kgrytestdlib-bot
authored
feat: add stats/incr/nanmsum
PR-URL: #5842 Closes: #5610 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 48f7e10 commit d67b212

File tree

11 files changed

+776
-0
lines changed

11 files changed

+776
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
# incrnanmsum
22+
23+
> Compute a moving sum incrementally, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the moving sum is defined as
28+
29+
<!-- <equation class="equation" label="eq:moving_sum" align="center" raw="s = \sum_{i=0}^{W-1} x_i" alt="Equation for the moving sum."> -->
30+
31+
```math
32+
s = \sum_{i=0}^{W-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="s = \sum_{i=0}^{W-1} x_i" data-equation="eq:moving_sum">
36+
<img src="./docs/img/equation_moving_sum.svg" alt="Equation for the moving sum.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var incrnanmsum = require( '@stdlib/stats/incr/nanmsum' );
52+
```
53+
54+
#### incrnanmsum( window )
55+
56+
Returns an accumulator function which incrementally computes a moving sum. The `window` parameter defines the number of values over which to compute the moving sum.
57+
58+
```javascript
59+
var accumulator = incrnanmsum( 3 );
60+
```
61+
62+
#### accumulator( \[x] )
63+
64+
If provided an input value `x`, the accumulator function returns an updated sum. If not provided an input value `x`, the accumulator function returns the current sum.
65+
66+
```javascript
67+
var accumulator = incrnanmsum( 3 );
68+
69+
var sum = accumulator();
70+
// returns null
71+
72+
sum = accumulator( 2.0 );
73+
// returns 2.0
74+
75+
sum = accumulator( NaN );
76+
// returns 2.0
77+
78+
sum = accumulator( -5.0 );
79+
// returns -3.0
80+
81+
sum = accumulator( 3.0 );
82+
// returns 0.0
83+
84+
sum = accumulator( 5.0 );
85+
// returns 3.0
86+
87+
sum = accumulator();
88+
// returns 3.0
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- 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.
100+
- 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.
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var uniform = require( '@stdlib/random/base/uniform' );
114+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
115+
var incrnanmsum = require( '@stdlib/stats/incr/nanmsum' );
116+
117+
// Initialize an accumulator:
118+
var accumulator = incrnanmsum( 5 );
119+
120+
// For each simulated datum, update the moving sum...
121+
var i;
122+
for ( i = 0; i < 100; i++ ) {
123+
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -50.0, 50.0 ) );
124+
}
125+
console.log( accumulator() );
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
</section>
145+
146+
<!-- /.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 incrnanmsum = 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 = incrnanmsum( (i%5)+1 );
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 = incrnanmsum( 5 );
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = acc( (i%5) ? i : NaN );
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: 35 additions & 0 deletions
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving sum,
4+
ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving sum.
8+
9+
If provided a value, the accumulator function returns an updated moving sum.
10+
If not provided a value, the accumulator function returns the current moving
11+
sum.
12+
13+
As `W` values are needed to fill the window buffer, the first `W-1` returned
14+
values are calculated from smaller sample sizes. Until the window is full,
15+
each returned value is calculated from all provided values.
16+
17+
Parameters
18+
----------
19+
W: integer
20+
Window size.
21+
22+
Returns
23+
-------
24+
acc: Function
25+
Accumulator function.
26+
27+
Examples
28+
--------
29+
> var accumulator = {{alias}}( 3 );
30+
> var s = accumulator()
31+
null
32+
> s = accumulator( 2.0 )
33+
2.0
34+
> s = accumulator( NaN )
35+
2.0
36+
> s = accumulator( -5.0 )
37+
-3.0
38+
> s = accumulator( 3.0 )
39+
0.0
40+
> s = accumulator()
41+
0.0
42+
43+
See Also
44+
--------
45+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 sum; otherwise, returns the current sum.
25+
*
26+
* @param x - value
27+
* @returns sum
28+
*/
29+
type accumulator = ( x?: number ) => number | null;
30+
31+
/**
32+
* Returns an accumulator function which incrementally computes a moving sum , ignoring `NaN` values.
33+
*
34+
* ## Notes
35+
*
36+
* - The `W` parameter defines the number of values over which to compute the moving sum.
37+
* - 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.
38+
*
39+
* @param W - window size
40+
* @returns accumulator function
41+
*
42+
* @example
43+
* var nanmsum = require( '@stdlib/stats/incr/nanmsum' );
44+
*
45+
* var accumulator = incrnanmsum( 3 );
46+
*
47+
* var v = accumulator();
48+
* // returns null
49+
*
50+
* v = accumulator( 2.0 );
51+
* // returns 2.0
52+
*
53+
* v = accumulator( NaN );
54+
* // returns 2.0
55+
*
56+
* v = accumulator( -5.0 );
57+
* // returns -3.0
58+
*
59+
* v = accumulator( 3.0 );
60+
* // returns 0.0
61+
*
62+
* v = accumulator( 5.0 );
63+
* // returns 3.0
64+
*
65+
* v = accumulator();
66+
* // returns 3.0
67+
*/
68+
declare function incrnanmsum( W: number ): accumulator;
69+
70+
71+
// EXPORTS //
72+
73+
export = incrnanmsum;

0 commit comments

Comments
 (0)