|
| 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 | +# incrnanmmeanstdev |
| 22 | + |
| 23 | +> Compute a moving [arithmetic mean][arithmetic-mean] and [corrected sample standard deviation][standard-deviation] incrementally, ignoring `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@10877053965ff3d0149611583ee50714bb64a8ea/lib/node_modules/@stdlib/stats/incr/mmeanstdev/docs/img/equation_arithmetic_mean.svg" alt="Equation for the arithmetic mean."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +and the [corrected sample standard deviation][standard-deviation] is defined as |
| 43 | + |
| 44 | +<!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2}" alt="Equation for the corrected sample standard deviation."> --> |
| 45 | + |
| 46 | +```math |
| 47 | +s = \sqrt{\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 = \sqrt{\frac{1}{W-1} \sum_{i=0}^{W-1} ( x_i - \bar{x} )^2}" data-equation="eq:corrected_sample_standard_deviation"> |
| 51 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@8284daf692badf90996becd5080db0dabf438411/lib/node_modules/@stdlib/stats/incr/mmeanstdev/docs/img/equation_corrected_sample_standard_deviation.svg" alt="Equation for the corrected sample standard deviation."> |
| 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 incrnanmmeanstdev = require( '@stdlib/stats/incr/nanmmeanstdev' ); |
| 67 | +``` |
| 68 | + |
| 69 | +#### incrnanmmeanstdev( \[out,] window ) |
| 70 | + |
| 71 | +Returns an accumulator `function` which incrementally computes a moving [arithmetic mean][arithmetic-mean] and [corrected sample standard deviation][standard-deviation]. The `window` parameter defines the number of values over which to compute the moving [arithmetic mean][arithmetic-mean] and [corrected sample standard deviation][standard-deviation]. |
| 72 | + |
| 73 | +```javascript |
| 74 | +var accumulator = incrnanmmeanstdev( 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. |
| 78 | + |
| 79 | +```javascript |
| 80 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 81 | + |
| 82 | +var accumulator = incrnanmmeanstdev( new Float64Array( 2 ), 3 ); |
| 83 | +``` |
| 84 | + |
| 85 | +#### accumulator( \[x] ) |
| 86 | + |
| 87 | +If provided an input value `x`, the accumulator function returns updated accumulated values. If not provided an input value `x`, the accumulator function returns the current accumulated values. |
| 88 | + |
| 89 | +```javascript |
| 90 | +var accumulator = incrnanmmeanstdev( 3 ); |
| 91 | + |
| 92 | +var out = accumulator(); |
| 93 | +// returns null |
| 94 | + |
| 95 | +// Fill the window... |
| 96 | +out = accumulator( 2.0 ); // [2.0] |
| 97 | +// returns [ 2.0, 0.0 ] |
| 98 | + |
| 99 | +out = accumulator( 1.0 ); // [2.0, 1.0] |
| 100 | +// returns [ 1.5, ~0.71 ] |
| 101 | + |
| 102 | +out = accumulator( 3.0 ); // [2.0, 1.0, 3.0] |
| 103 | +// returns [ 2.0, 1.0 ] |
| 104 | + |
| 105 | +out = accumulator( NaN ); // [2.0, 1.0, 3.0] |
| 106 | +// returns [ 2.0, 1.0 ] |
| 107 | + |
| 108 | +// Window begins sliding... |
| 109 | +out = accumulator( -7.0 ); // [1.0, 3.0, -7.0] |
| 110 | +// returns [ -1.0, ~5.29 ] |
| 111 | + |
| 112 | +out = accumulator( -5.0 ); // [3.0, -7.0, -5.0] |
| 113 | +// returns [ -3.0, ~5.29 ] |
| 114 | + |
| 115 | +out = accumulator(); |
| 116 | +// returns [ -3.0, ~5.29 ] |
| 117 | +``` |
| 118 | + |
| 119 | +</section> |
| 120 | + |
| 121 | +<!-- /.usage --> |
| 122 | + |
| 123 | +<section class="notes"> |
| 124 | + |
| 125 | +## Notes |
| 126 | + |
| 127 | +- 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. |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.notes --> |
| 132 | + |
| 133 | +<section class="examples"> |
| 134 | + |
| 135 | +## Examples |
| 136 | + |
| 137 | +<!-- eslint no-undef: "error" --> |
| 138 | + |
| 139 | +```javascript |
| 140 | +var randu = require( '@stdlib/random/base/randu' ); |
| 141 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 142 | +var ArrayBuffer = require( '@stdlib/array/buffer' ); |
| 143 | +var incrnanmmeanstdev = require( '@stdlib/stats/incr/nanmmeanstdev' ); |
| 144 | + |
| 145 | +var offset; |
| 146 | +var acc; |
| 147 | +var buf; |
| 148 | +var out; |
| 149 | +var ms; |
| 150 | +var N; |
| 151 | +var v; |
| 152 | +var i; |
| 153 | +var j; |
| 154 | + |
| 155 | +// Define the number of accumulators: |
| 156 | +N = 5; |
| 157 | + |
| 158 | +// Create an array buffer for storing accumulator output: |
| 159 | +buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element |
| 160 | + |
| 161 | +// Initialize accumulators: |
| 162 | +acc = []; |
| 163 | +for ( i = 0; i < N; i++ ) { |
| 164 | + // Compute the byte offset: |
| 165 | + offset = i * 2 * 8; // stride=2, bytes_per_element=8 |
| 166 | + |
| 167 | + // Create a new view for storing accumulated values: |
| 168 | + out = new Float64Array( buf, offset, 2 ); |
| 169 | + |
| 170 | + // Initialize an accumulator which will write results to the view: |
| 171 | + acc.push( incrnanmmeanstdev( out, 5 ) ); |
| 172 | +} |
| 173 | + |
| 174 | +// Simulate data and update the moving sample means and standard deviations... |
| 175 | +for ( i = 0; i < 100; i++ ) { |
| 176 | + for ( j = 0; j < N; j++ ) { |
| 177 | + v = randu() * 100.0 * (j+1); |
| 178 | + acc[ j ]( v ); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// Print the final results: |
| 183 | +console.log( 'Mean\tStDev' ); |
| 184 | +for ( i = 0; i < N; i++ ) { |
| 185 | + ms = acc[ i ](); |
| 186 | + console.log( '%d\t%d', ms[ 0 ].toFixed( 3 ), ms[ 1 ].toFixed( 3 ) ); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +</section> |
| 191 | + |
| 192 | +<!-- /.examples --> |
| 193 | + |
| 194 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 195 | + |
| 196 | +<section class="related"> |
| 197 | + |
| 198 | +* * * |
| 199 | + |
| 200 | +## See Also |
| 201 | + |
| 202 | +- <span class="package-name">[`@stdlib/stats/incr/mmeanstdev`][@stdlib/stats/incr/meanstdev]</span><span class="delimiter">: </span><span class="description">compute a moving arithmetic mean and corrected sample standard deviation incrementally.</span> |
| 203 | +- <span class="package-name">[`@stdlib/stats/incr/meanstdev`][@stdlib/stats/incr/meanstdev]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean and corrected sample standard deviation incrementally.</span> |
| 204 | +- <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> |
| 205 | +- <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.</span> |
| 206 | +- <span class="package-name">[`@stdlib/stats/incr/mstdev`][@stdlib/stats/incr/mstdev]</span><span class="delimiter">: </span><span class="description">compute a moving corrected sample standard deviation incrementally.</span> |
| 207 | + |
| 208 | +</section> |
| 209 | + |
| 210 | +<!-- /.related --> |
| 211 | + |
| 212 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 213 | + |
| 214 | +<section class="links"> |
| 215 | + |
| 216 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 217 | + |
| 218 | +[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation |
| 219 | + |
| 220 | +<!-- <related-links> --> |
| 221 | + |
| 222 | +[@stdlib/stats/incr/meanstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/meanstdev |
| 223 | + |
| 224 | +[@stdlib/stats/incr/mmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmean |
| 225 | + |
| 226 | +[@stdlib/stats/incr/mmeanvar]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmeanvar |
| 227 | + |
| 228 | +[@stdlib/stats/incr/mstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mstdev |
| 229 | + |
| 230 | +<!-- </related-links> --> |
| 231 | + |
| 232 | +</section> |
| 233 | + |
| 234 | +<!-- /.links --> |
0 commit comments