Skip to content

Commit eee8409

Browse files
committed
feat(lib): added stats/incr/nanmsum package
--- 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: passed - task: lint_license_headers status: passed ---
1 parent 528116b commit eee8409

File tree

11 files changed

+811
-0
lines changed

11 files changed

+811
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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 randu = require( '@stdlib/random/base/randu' );
114+
var incrnanmsum = require( '@stdlib/stats/incr/nanmsum' );
115+
116+
var accumulator;
117+
var v;
118+
var i;
119+
120+
// Initialize an accumulator:
121+
accumulator = incrnanmsum( 5 );
122+
123+
// For each simulated datum, update the moving sum...
124+
for ( i = 0; i < 100; i++ ) {
125+
if ( randu() < 0.2 ) {
126+
v = NaN;
127+
} else {
128+
v = randu() * 100.0;
129+
}
130+
accumulator( v );
131+
}
132+
console.log( accumulator() );
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
* * *
144+
145+
## See Also
146+
147+
- <span class="package-name">[`@stdlib/stats/incr/mmean`][@stdlib/stats/incr/mmean]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean incrementally.</span>
148+
- <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>
149+
- <span class="package-name">[`@stdlib/stats/incr/sum`][@stdlib/stats/incr/sum]</span><span class="delimiter">: </span><span class="description">compute a sum incrementally.</span>
150+
151+
</section>
152+
153+
<!-- /.related -->
154+
155+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="links">
158+
159+
<!-- <related-links> -->
160+
161+
[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean
162+
163+
[@stdlib/stats/incr/msummary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msummary
164+
165+
[@stdlib/stats/incr/sum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/sum
166+
167+
<!-- </related-links> -->
168+
169+
</section>
170+
171+
<!-- /.links -->
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanmsum = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanmsum( (i%5)+1 );
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanmsum( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = randu();
59+
if ( v < 0.1 ) {
60+
v = NaN;
61+
}
62+
v = acc( v );
63+
if ( v !== v ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( v !== v ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
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+

0 commit comments

Comments
 (0)