Skip to content

Commit 5329a11

Browse files
author
Tejas-963
committed
Adding a new package to the stats/incr/* namespace: @stdlib/stats/incr/nanmprod.
1 parent 60310a4 commit 5329a11

File tree

11 files changed

+1255
-0
lines changed

11 files changed

+1255
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
# incrnanmprod
22+
23+
> Compute a moving product incrementally.
24+
25+
<section class="intro">
26+
27+
For a window of size `W`, the moving product is defined as
28+
29+
<!-- <equation class="equation" label="eq:moving_product" align="center" raw="\prod_{i=0}^{W-1} x_i" alt="Equation for the moving product."> -->
30+
31+
```math
32+
\prod_{i=0}^{W-1} x_i
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\prod_{i=0}^{W-1} x_i" data-equation="eq:moving_product">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mprod/docs/img/equation_moving_product.svg" alt="Equation for the moving product.">
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 incrnanmprod = require( '@stdlib/stats/incr/nanmprod' );
52+
```
53+
54+
#### incrnanmprod
55+
56+
Returns an accumulator `function` which incrementally computes a moving product. The `window` parameter defines the number of values over which to compute the moving product, ignoring `NaN` values.
57+
58+
```javascript
59+
var accumulator = incrnanmprod( 3 );
60+
```
61+
62+
#### accumulator( \[x] )
63+
64+
If provided an input value `x`, the accumulator function returns an updated product. If not provided an input value `x`, the accumulator function returns the current product.
65+
66+
```javascript
67+
var accumulator = incrnanmprod( 3 );
68+
69+
var p = accumulator();
70+
// returns null
71+
72+
// Fill the window...
73+
p = accumulator( 2.0 ); // [2.0]
74+
// returns 2.0
75+
76+
p = accumulator( 1.0 ); // [2.0, 1.0]
77+
// returns 2.0
78+
79+
p = accumulator( 3.0 ); // [2.0, 1.0, 3.0]
80+
// returns 6.0
81+
82+
// Window begins sliding...
83+
p = accumulator( -7.0 ); // [1.0, 3.0, -7.0]
84+
// returns -21.0
85+
86+
p = accumulator( NaN );
87+
// returns -21.0
88+
89+
p = accumulator( -5.0 ); // [3.0, -7.0, -5.0]
90+
// returns 105.0
91+
92+
p = accumulator();
93+
// returns 105.0
94+
```
95+
96+
Under certain conditions, overflow may be transient.
97+
98+
```javascript
99+
// Large values:
100+
var x = 5.0e+300;
101+
var y = 1.0e+300;
102+
103+
// Tiny value:
104+
var z = 2.0e-302;
105+
106+
// Initialize an accumulator:
107+
var accumulator = incrnanmprod( 3 );
108+
109+
var p = accumulator( x );
110+
// returns 5.0e+300
111+
112+
// Transient overflow:
113+
p = accumulator( y );
114+
// returns Infinity
115+
116+
// Recover a finite result:
117+
p = accumulator( z );
118+
// returns 1.0e+299
119+
```
120+
121+
Similarly, under certain conditions, underflow may be transient.
122+
123+
```javascript
124+
// Tiny values:
125+
var x = 4.0e-302;
126+
var y = 9.0e-303;
127+
128+
// Large value:
129+
var z = 2.0e+300;
130+
131+
// Initialize an accumulator:
132+
var accumulator = incrnanmprod( 3 );
133+
134+
var p = accumulator( x );
135+
// returns 4.0e-302
136+
137+
// Transient underflow:
138+
p = accumulator( y );
139+
// returns 0.0
140+
141+
// Recover a non-zero result:
142+
p = accumulator( z );
143+
// returns 7.2e-304
144+
```
145+
146+
</section>
147+
148+
<!-- /.usage -->
149+
150+
<section class="notes">
151+
152+
## Notes
153+
154+
- 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.
155+
- 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.
156+
- For large accumulation windows or accumulations of either large or small numbers, care should be taken to prevent overflow and underflow. Note, however, that overflow/underflow may be transient, as the accumulator does not use a double-precision floating-point number to store an accumulated product. Instead, the accumulator splits an accumulated product into a normalized **fraction** and **exponent** and updates each component separately. Doing so guards against a loss in precision.
157+
158+
</section>
159+
160+
<!-- /.notes -->
161+
162+
<section class="examples">
163+
164+
## Examples
165+
166+
<!-- eslint no-undef: "error" -->
167+
168+
```javascript
169+
var randu = require( '@stdlib/random/base/randu' );
170+
var incrnanmprod = require( '@stdlib/stats/incr/mprod' );
171+
172+
var accumulator;
173+
var v;
174+
var i;
175+
176+
// Initialize an accumulator:
177+
accumulator = incrnanmprod( 5 );
178+
179+
// For each simulated datum, update the moving product...
180+
for ( i = 0; i < 100; i++ ) {
181+
if ( randu() < 0.2 ) {
182+
v = NaN;
183+
} else {
184+
v = (randu() * 10.0) - 5.0;
185+
}
186+
accumulator( v );
187+
}
188+
console.log( accumulator() );
189+
```
190+
191+
</section>
192+
193+
<!-- /.examples -->
194+
195+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
196+
197+
<section class="related">
198+
199+
* * *
200+
201+
## See Also
202+
203+
- <span class="package-name">[`@stdlib/stats/incr/nansum`][@stdlib/stats/incr/nansum]</span><span class="delimiter">: </span><span class="description">compute a sum incrementally, ignoring NaN values.</span>
204+
- <span class="package-name">[`@stdlib/stats/incr/msumprod`][@stdlib/stats/incr/msumprod]</span><span class="delimiter">: </span><span class="description">compute a moving sum of products incrementally.</span>
205+
206+
</section>
207+
208+
<!-- /.related -->
209+
210+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
211+
212+
<section class="links">
213+
214+
<!-- <related-links> -->
215+
216+
[@stdlib/stats/incr/nansum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nansum
217+
218+
[@stdlib/stats/incr/msumprod]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/msumprod
219+
220+
<!-- </related-links> -->
221+
222+
</section>
223+
224+
<!-- /.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 incrnanmprod = 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 = incrnanmprod( (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 = incrnanmprod( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
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: 30 additions & 0 deletions
Loading
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving
4+
product, ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving product.
8+
9+
If provided a value, the accumulator function returns an updated moving
10+
product. If not provided a value or provided NaN value, the accumulator function returns the
11+
current moving product.
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+
For accumulations over large windows or accumulations of large numbers, care
18+
should be taken to prevent overflow. Note, however, that overflow/underflow
19+
may be transient, as the accumulator does not use a double-precision
20+
floating-point number to store an accumulated product. Instead, the
21+
accumulator splits an accumulated product into a normalized fraction and
22+
exponent and updates each component separately. Doing so guards against a
23+
loss in precision.
24+
25+
Parameters
26+
----------
27+
W: integer
28+
Window size.
29+
30+
Returns
31+
-------
32+
acc: Function
33+
Accumulator function.
34+
35+
Examples
36+
--------
37+
> var accumulator = {{alias}}( 3 );
38+
> var p = accumulator()
39+
null
40+
> p = accumulator( 2.0 )
41+
2.0
42+
> p = accumulator( -5.0 )
43+
-10.0
44+
> p = accumulator( 3.0 )
45+
-30.0
46+
> p = accumulator( NaN )
47+
-30.0
48+
> p = accumulator( 5.0 )
49+
-75.0
50+
> p = accumulator()
51+
-75.0
52+
53+
See Also
54+
--------
55+

0 commit comments

Comments
 (0)