Skip to content

Commit 31bd756

Browse files
committed
feat: add incrnanmsumprod accumulator for computing moving sum of products while ignoring NaN values
1 parent f29381d commit 31bd756

File tree

11 files changed

+913
-0
lines changed

11 files changed

+913
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# incrnanmsumprod
22+
23+
> Compute a moving sum of products incrementally.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the moving sum of products is defined as
28+
29+
<!-- <equation class="equation" label="eq:moving_sum_product" align="center" raw="s = \sum_{i=0}^{W-1} x_i y_i" alt="Equation for the moving sum of products."> -->
30+
31+
```math
32+
s = \sum_{i=0}^{W-1} x_i y_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="s = \sum_{i=0}^{W-1} x_i y_i" data-equation="eq:moving_sum_product">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/nanmsumprod/docs/img/equation_moving_sum_product.svg" alt="Equation for the moving sum of products.">
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 incrnanmsumprod = require( '@stdlib/stats/incr/nanmsumprod' );
52+
```
53+
54+
#### incrnanmsumprod( window )
55+
56+
Returns an accumulator `function` which incrementally computes a moving sum of products. The `window` parameter defines the number of values over which to compute the moving sum of products.
57+
58+
```javascript
59+
var accumulator = incrnanmsumprod( 4 );
60+
```
61+
62+
#### accumulator( \[x, y] )
63+
64+
If provided input values `x` and `y`, the accumulator function returns an updated sum. If not provided input values `x` and `y`, the accumulator function returns the current sum.
65+
66+
```javascript
67+
var accumulator = incrnanmsumprod( 3 );
68+
69+
var sum = accumulator();
70+
// returns null
71+
72+
// Fill the window...
73+
sum = accumulator( 2.0, 3.0 ); // [(2.0,3.0)]
74+
// returns 6.0
75+
76+
sum = accumulator( 1.0, -1.0 ); // [(2.0,3.0), (1.0,-1.0)]
77+
// returns 5.0
78+
79+
sum = accumulator( NaN, -1.0 ); //Ignored
80+
// returns 5.0
81+
82+
sum = accumulator( -3.0, 5.0 ); // [(2.0,3.0), (1.0,-1.0), (-3.0,5.0)]
83+
// returns -10.0
84+
85+
// Window begins sliding...
86+
sum = accumulator( -7.0, -1.0 ); // [(1.0,-1.0), (-3.0,5.0), (-7.0,-1.0)]
87+
// returns -9.0
88+
89+
sum = accumulator( 5.0, 4.0 ); // [(-3.0,5.0), (-7.0,-1.0), (5.0,4.0)]
90+
// returns 12.0
91+
92+
sum = accumulator( 5.0, NaN ); //ignored
93+
// returns 12.0
94+
95+
sum = accumulator();
96+
// returns 12.0
97+
```
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- If provided `NaN` or a value which, when used in computations, 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.
108+
- As `W` (x,y) pairs 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.
109+
110+
</section>
111+
112+
<!-- /.notes -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
```javascript
121+
var randu = require( '@stdlib/random/base/randu' );
122+
var incrnanmsumprod = require( './../lib' );
123+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
124+
125+
var accumulator;
126+
var sum;
127+
var v1;
128+
var v2;
129+
var i;
130+
131+
// Initialize an accumulator:
132+
accumulator = incrnanmsumprod( 5 );
133+
134+
// For each simulated datum, update the moving sum-product...
135+
console.log( '\nValue\tValue\tSum\n' );
136+
for ( i = 0; i < 100; i++ ) {
137+
v1 = (randu()<0.2) ? NaN : randu() * 100.0;
138+
v2 = (randu()<0.2) ? NaN : randu() * 100.0;
139+
sum = accumulator( v1, v2 );
140+
}
141+
consol.log(accumulator())
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149+
150+
<section class="related">
151+
152+
* * *
153+
154+
## See Also
155+
156+
- <span class="package-name">[`@stdlib/stats/incr/mprod`][@stdlib/stats/incr/mprod]</span><span class="delimiter">: </span><span class="description">compute a moving product incrementally.</span>
157+
- <span class="package-name">[`@stdlib/stats/incr/msum`][@stdlib/stats/incr/msum]</span><span class="delimiter">: </span><span class="description">compute a moving sum incrementally.</span>
158+
- <span class="package-name">[`@stdlib/stats/incr/sumprod`][@stdlib/stats/incr/sumprod]</span><span class="delimiter">: </span><span class="description">compute a sum of products incrementally.</span>
159+
160+
</section>
161+
162+
<!-- /.related -->
163+
164+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
165+
166+
<section class="links">
167+
168+
<!-- <related-links> -->
169+
170+
[@stdlib/stats/incr/mprod]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mprod
171+
172+
[@stdlib/stats/incr/msum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msum
173+
174+
[@stdlib/stats/incr/sumprod]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/sumprod
175+
176+
<!-- </related-links> -->
177+
178+
</section>
179+
180+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 incrmsumprod = 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 = incrmsumprod( (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 = incrmsumprod( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()*10.0, randu()*10.0 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 40 additions & 0 deletions
Loading
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving sum of
4+
products while ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of (x,y) pairs over which to compute
7+
the moving sum of products.
8+
9+
If provided input values, the accumulator function returns an updated moving
10+
sum. If not provided input values, the accumulator function returns the
11+
current moving sum.
12+
13+
As `W` (x,y) pairs are needed to fill the window buffer, the first `W-1`
14+
returned values are calculated from smaller sample sizes. Until the window
15+
is full, 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, 3.0 )
33+
6.0
34+
> s = accumulator( NaN, 3.0 )
35+
6.0
36+
> s = accumulator( -5.0, 2.0 )
37+
-4.0
38+
> s = accumulator( -5.0, NaN )
39+
-4.0
40+
> s = accumulator( 3.0, -2.0 )
41+
-10.0
42+
> s = accumulator( NaN, NaN )
43+
-10.0
44+
> s = accumulator( 5.0, 3.0 )
45+
-1.0
46+
> s = accumulator()
47+
-1.0
48+
49+
See Also
50+
--------
51+

0 commit comments

Comments
 (0)