|
| 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 --> |
0 commit comments