|
| 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 | +# incrnanmmeanvar |
| 22 | + |
| 23 | +> Compute a moving [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance] incrementally, **skipping `NaN` values**. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +For a window of size `W`, the [arithmetic mean][arithmetic-mean] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i" alt="Equation for the arithmetic mean."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{W} \sum_{i=0}^{W-1} x_i" data-equation="eq:arithmetic_mean"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@c8c3c87eeab590bfdff924ec0fb269fb33a7de2b/lib/node_modules/@stdlib/stats/incr/mmeanvar/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +and the [unbiased sample variance][sample-variance] is defined as |
| 43 | + |
| 44 | +<!-- <equation class="equation" label="eq:unbiased_sample_variance" align="center" raw="s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2" alt="Equation for the unbiased sample variance."> --> |
| 45 | + |
| 46 | +```math |
| 47 | +s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2 |
| 48 | +``` |
| 49 | + |
| 50 | +<!-- <div class="equation" align="center" data-raw-text="s^2 = \frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2" data-equation="eq:unbiased_sample_variance"> |
| 51 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@563a8587d936008c82db675be84f8ce1474fee27/lib/node_modules/@stdlib/stats/incr/mmeanvar/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for the unbiased sample variance."> |
| 52 | + <br> |
| 53 | +</div> --> |
| 54 | + |
| 55 | +<!-- </equation> --> |
| 56 | + |
| 57 | +</section> |
| 58 | + |
| 59 | +<!-- /.intro --> |
| 60 | + |
| 61 | +<section class="usage"> |
| 62 | + |
| 63 | +## Usage |
| 64 | + |
| 65 | +```javascript |
| 66 | +var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); |
| 67 | +``` |
| 68 | + |
| 69 | +#### incrnanmmeanvar( \[out,] window ) |
| 70 | + |
| 71 | +Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance] **while skipping `NaN` values**. The `window` parameter defines the maximum number of most recent **non-NaN** values used to compute the moving statistics. |
| 72 | + |
| 73 | +```javascript |
| 74 | +var accumulator = incrnanmmeanvar( 3 ); |
| 75 | +``` |
| 76 | + |
| 77 | +By default, the returned accumulator `function` returns the accumulated values as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object. Unlike `incrmmeanvar`, this accumulator ignores (skips) any `NaN` values and does not allow them to propagate into the moving mean and variance. |
| 78 | + |
| 79 | +```javascript |
| 80 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 81 | + |
| 82 | +var accumulator = incrnanmmeanvar( new Float64Array( 2 ), 3 ); |
| 83 | +``` |
| 84 | + |
| 85 | +#### accumulator( \[x] ) |
| 86 | + |
| 87 | +If provided an input value `x`, the accumulator function updates and returns the current moving arithmetic mean and unbiased sample variance. If `x` is `NaN`, the value is ignored (i.e., it does not affect the window). If not provided an input value `x`, the accumulator function returns the current accumulated values without updating. |
| 88 | + |
| 89 | +```javascript |
| 90 | +var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); |
| 91 | + |
| 92 | +var accumulator = incrnanmmeanvar( 3 ); |
| 93 | + |
| 94 | +var out = accumulator(); |
| 95 | +// returns null |
| 96 | + |
| 97 | +// Fill the window (no NaNs yet)... |
| 98 | +out = accumulator( 2.0 ); // [2.0] |
| 99 | +// returns [ 2.0, 0.0 ] |
| 100 | + |
| 101 | +out = accumulator( NaN ); // NaN is ignored [2.0] |
| 102 | +// returns [ 2.0, 0.0 ] |
| 103 | + |
| 104 | +out = accumulator( 1.0 ); // [2.0, 1.0] |
| 105 | +// returns [ 1.5, 0.5 ] |
| 106 | + |
| 107 | +out = accumulator( 3.0 ); // [2.0, 1.0, 3.0] |
| 108 | +// returns [ 2.0, 1.0 ] |
| 109 | + |
| 110 | +// Window begins sliding... |
| 111 | +out = accumulator( -7.0 ); // [1.0, 3.0, -7.0] |
| 112 | +// returns [ -1.0, 28.0 ] |
| 113 | + |
| 114 | +out = accumulator( NaN ); // NaN ignored [1.0, 3.0, -7.0] |
| 115 | +// returns [ -1.0, 28.0 ] |
| 116 | + |
| 117 | +out = accumulator( -5.0 ); // [3.0, -7.0, -5.0] |
| 118 | +// returns [ -3.0, 28.0 ] |
| 119 | + |
| 120 | +out = accumulator(); |
| 121 | +// returns [ -3.0, 28.0 ] |
| 122 | +``` |
| 123 | + |
| 124 | +</section> |
| 125 | + |
| 126 | +<!-- /.usage --> |
| 127 | + |
| 128 | +<section class="notes"> |
| 129 | + |
| 130 | +## Notes |
| 131 | + |
| 132 | +- Input values are **not** type checked. If provided `NaN`, the value is ignored and does not affect the moving window or the accumulated statistics. If non-numeric inputs are possible, you are advised to type check and handle them **before** passing values to the accumulator function. |
| 133 | +- As `W` valid (non-`NaN`) values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window has received `W` valid values, each returned value is calculated from all valid inputs seen so far. |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.notes --> |
| 138 | + |
| 139 | +<section class="examples"> |
| 140 | + |
| 141 | +## Examples |
| 142 | + |
| 143 | +<!-- eslint no-undef: "error" --> |
| 144 | + |
| 145 | +```javascript |
| 146 | +var randu = require( '@stdlib/random/base/randu' ); |
| 147 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 148 | +var ArrayBuffer = require( '@stdlib/array/buffer' ); |
| 149 | +var incrnanmmeanvar = require( '@stdlib/stats/incr/nanmmeanvar' ); |
| 150 | + |
| 151 | +var offset; |
| 152 | +var acc; |
| 153 | +var buf; |
| 154 | +var out; |
| 155 | +var mv; |
| 156 | +var N; |
| 157 | +var v; |
| 158 | +var i; |
| 159 | +var j; |
| 160 | + |
| 161 | +// Define the number of accumulators: |
| 162 | +N = 5; |
| 163 | + |
| 164 | +// Create an array buffer for storing accumulator output: |
| 165 | +buf = new ArrayBuffer( N*2*8 ); |
| 166 | + |
| 167 | +// Initialize accumulators: |
| 168 | +acc = []; |
| 169 | +for ( i = 0; i < N; i++ ) { |
| 170 | + // Compute the byte offset for the ith accumulator: |
| 171 | + offset = i * 2 * 8; |
| 172 | + |
| 173 | + // Create a typed array view over the correct section of the buffer: |
| 174 | + out = new Float64Array( buf, offset, 2 ); |
| 175 | + |
| 176 | + // Create a moving mean/variance accumulator with window W = 5: |
| 177 | + acc.push( incrnanmmeanvar( out, 5 ) ); |
| 178 | +} |
| 179 | + |
| 180 | +// Simulate streaming data updates: |
| 181 | +for ( i = 0; i < 100; i++ ) { |
| 182 | + for ( j = 0; j < N; j++ ) { |
| 183 | + |
| 184 | + // Generate random values, but occasionally insert NaN. |
| 185 | + // Any NaNs are ignored by the accumulator. |
| 186 | + v = ( randu() > 0.1 ) ? randu() * 100 : NaN; |
| 187 | + |
| 188 | + // Update accumulator j: |
| 189 | + acc[ j ]( v ); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +// Display final moving means and variances: |
| 194 | +console.log( 'Mean\tVariance' ); |
| 195 | +for ( i = 0; i < N; i++ ) { |
| 196 | + mv = acc[ i ](); // Get the current result |
| 197 | + console.log( mv[ 0 ].toFixed( 3 ) + '\t' + mv[ 1 ].toFixed( 3 ) ); |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +</section> |
| 202 | + |
| 203 | +<!-- /.examples --> |
| 204 | + |
| 205 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 206 | + |
| 207 | +<section class="related"> |
| 208 | + |
| 209 | +* * * |
| 210 | + |
| 211 | +## See Also |
| 212 | + |
| 213 | +- <span class="package-name">[`@stdlib/stats/incr/mmeanvar`][@stdlib/stats/incr/mmeanvar]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean and unbiased sample variance incrementally (**propagates NaN values**).</span> |
| 214 | +- <span class="package-name">[`@stdlib/stats/incr/meanvar`][@stdlib/stats/incr/meanvar]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean and unbiased sample variance incrementally.</span> |
| 215 | +- <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> |
| 216 | +- <span class="package-name">[`@stdlib/stats/incr/mmeanstdev`][@stdlib/stats/incr/mmeanstdev]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean and corrected sample standard deviation incrementally.</span> |
| 217 | +- <span class="package-name">[`@stdlib/stats/incr/mvariance`][@stdlib/stats/incr/mvariance]</span><span class="delimiter">: </span><span class="description">compute a moving unbiased sample variance incrementally.</span> |
| 218 | + |
| 219 | +</section> |
| 220 | + |
| 221 | +<!-- /.related --> |
| 222 | + |
| 223 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 224 | + |
| 225 | +<section class="links"> |
| 226 | + |
| 227 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 228 | + |
| 229 | +[sample-variance]: https://en.wikipedia.org/wiki/Variance |
| 230 | + |
| 231 | +<!-- <related-links> --> |
| 232 | + |
| 233 | +[@stdlib/stats/incr/mmeanvar]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmeanvar |
| 234 | + |
| 235 | +[@stdlib/stats/incr/meanvar]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/meanvar |
| 236 | + |
| 237 | +[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean |
| 238 | + |
| 239 | +[@stdlib/stats/incr/mmeanstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmeanstdev |
| 240 | + |
| 241 | +[@stdlib/stats/incr/mvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mvariance |
| 242 | + |
| 243 | +<!-- </related-links> --> |
| 244 | + |
| 245 | +</section> |
| 246 | + |
| 247 | +<!-- /.links --> |
0 commit comments