README.md (6066B)
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 # incrmmeanstdev 22 23 > Compute a moving [arithmetic mean][arithmetic-mean] and [corrected sample standard deviation][standard-deviation] incrementally. 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 <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"> 32 <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."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 and the [corrected sample standard deviation][standard-deviation] is defined as 39 40 <!-- <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."> --> 41 42 <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"> 43 <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."> 44 <br> 45 </div> 46 47 <!-- </equation> --> 48 49 </section> 50 51 <!-- /.intro --> 52 53 <section class="usage"> 54 55 ## Usage 56 57 ```javascript 58 var incrmmeanstdev = require( '@stdlib/stats/incr/mmeanstdev' ); 59 ``` 60 61 #### incrmmeanstdev( \[out,] window ) 62 63 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]. 64 65 ```javascript 66 var accumulator = incrmmeanstdev( 3 ); 67 ``` 68 69 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. 70 71 ```javascript 72 var Float64Array = require( '@stdlib/array/float64' ); 73 74 var accumulator = incrmmeanstdev( new Float64Array( 2 ), 3 ); 75 ``` 76 77 #### accumulator( \[x] ) 78 79 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. 80 81 ```javascript 82 var accumulator = incrmmeanstdev( 3 ); 83 84 var out = accumulator(); 85 // returns null 86 87 // Fill the window... 88 out = accumulator( 2.0 ); // [2.0] 89 // returns [ 2.0, 0.0 ] 90 91 out = accumulator( 1.0 ); // [2.0, 1.0] 92 // returns [ 1.5, ~0.71 ] 93 94 out = accumulator( 3.0 ); // [2.0, 1.0, 3.0] 95 // returns [ 2.0, 1.0 ] 96 97 // Window begins sliding... 98 out = accumulator( -7.0 ); // [1.0, 3.0, -7.0] 99 // returns [ -1.0, ~5.29 ] 100 101 out = accumulator( -5.0 ); // [3.0, -7.0, -5.0] 102 // returns [ -3.0, ~5.29 ] 103 104 out = accumulator(); 105 // returns [ -3.0, ~5.29 ] 106 ``` 107 108 </section> 109 110 <!-- /.usage --> 111 112 <section class="notes"> 113 114 ## Notes 115 116 - Input values are **not** type checked. If provided `NaN`, the accumulated values are `NaN` for **at least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. 117 - 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. 118 119 </section> 120 121 <!-- /.notes --> 122 123 <section class="examples"> 124 125 ## Examples 126 127 <!-- eslint no-undef: "error" --> 128 129 ```javascript 130 var randu = require( '@stdlib/random/base/randu' ); 131 var Float64Array = require( '@stdlib/array/float64' ); 132 var ArrayBuffer = require( '@stdlib/array/buffer' ); 133 var incrmmeanstdev = require( '@stdlib/stats/incr/mmeanstdev' ); 134 135 var offset; 136 var acc; 137 var buf; 138 var out; 139 var ms; 140 var N; 141 var v; 142 var i; 143 var j; 144 145 // Define the number of accumulators: 146 N = 5; 147 148 // Create an array buffer for storing accumulator output: 149 buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element 150 151 // Initialize accumulators: 152 acc = []; 153 for ( i = 0; i < N; i++ ) { 154 // Compute the byte offset: 155 offset = i * 2 * 8; // stride=2, bytes_per_element=8 156 157 // Create a new view for storing accumulated values: 158 out = new Float64Array( buf, offset, 2 ); 159 160 // Initialize an accumulator which will write results to the view: 161 acc.push( incrmmeanstdev( out, 5 ) ); 162 } 163 164 // Simulate data and update the moving sample means and standard deviations... 165 for ( i = 0; i < 100; i++ ) { 166 for ( j = 0; j < N; j++ ) { 167 v = randu() * 100.0 * (j+1); 168 acc[ j ]( v ); 169 } 170 } 171 172 // Print the final results: 173 console.log( 'Mean\tStDev' ); 174 for ( i = 0; i < N; i++ ) { 175 ms = acc[ i ](); 176 console.log( '%d\t%d', ms[ 0 ].toFixed( 3 ), ms[ 1 ].toFixed( 3 ) ); 177 } 178 ``` 179 180 </section> 181 182 <!-- /.examples --> 183 184 <section class="links"> 185 186 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean 187 188 [standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation 189 190 </section> 191 192 <!-- /.links -->