README.md (5397B)
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 # incrmeanstdev 22 23 > Compute an [arithmetic mean][arithmetic-mean] and a [corrected sample standard deviation][sample-stdev] incrementally. 24 25 <section class="intro"> 26 27 The [arithmetic mean][arithmetic-mean] is defined as 28 29 <!-- <equation class="equation" label="eq:arithmetic_mean" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the arithmetic mean."> --> 30 31 <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:arithmetic_mean"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5ca648efa8739942a21b07ad7df1947cdd01b662/lib/node_modules/@stdlib/stats/incr/meanstdev/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][sample-stdev] is defined as 39 40 <!-- <equation class="equation" label="eq:corrected_sample_standard_deviation" align="center" raw="s = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-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}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2}" data-equation="eq:corrected_sample_standard_deviation"> 43 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5ca648efa8739942a21b07ad7df1947cdd01b662/lib/node_modules/@stdlib/stats/incr/meanstdev/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 class="usage"> 50 51 ## Usage 52 53 ```javascript 54 var incrmeanstdev = require( '@stdlib/stats/incr/meanstdev' ); 55 ``` 56 57 #### incrmeanstdev( \[out] ) 58 59 Returns an accumulator `function` which incrementally computes an [arithmetic mean][arithmetic-mean] and [corrected sample standard deviation][sample-stdev]. 60 61 ```javascript 62 var accumulator = incrmeanstdev(); 63 ``` 64 65 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. 66 67 ```javascript 68 var Float64Array = require( '@stdlib/array/float64' ); 69 70 var accumulator = incrmeanstdev( new Float64Array( 2 ) ); 71 ``` 72 73 #### accumulator( \[x] ) 74 75 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. 76 77 ```javascript 78 var accumulator = incrmeanstdev(); 79 80 var ms = accumulator(); 81 // returns null 82 83 ms = accumulator( 2.0 ); 84 // returns [ 2.0, 0.0 ] 85 86 ms = accumulator( 1.0 ); 87 // returns [ 1.5, ~0.71 ] 88 89 ms = accumulator( 3.0 ); 90 // returns [ 2.0, 1.0 ] 91 92 ms = accumulator( -7.0 ); 93 // returns [ -0.25, ~4.57 ] 94 95 ms = accumulator( -5.0 ); 96 // returns [ -1.2, ~4.49 ] 97 98 ms = accumulator(); 99 // returns [ -1.2, ~4.49 ] 100 ``` 101 102 </section> 103 104 <!-- /.usage --> 105 106 <section class="notes"> 107 108 ## Notes 109 110 - Input values are **not** type checked. If provided `NaN`, the accumulated values are equal to `NaN` for **all** 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. 111 112 </section> 113 114 <!-- /.notes --> 115 116 <section class="examples"> 117 118 ## Examples 119 120 <!-- eslint no-undef: "error" --> 121 122 ```javascript 123 var randu = require( '@stdlib/random/base/randu' ); 124 var Float64Array = require( '@stdlib/array/float64' ); 125 var ArrayBuffer = require( '@stdlib/array/buffer' ); 126 var incrmeanstdev = require( '@stdlib/stats/incr/meanstdev' ); 127 128 var offset; 129 var acc; 130 var buf; 131 var out; 132 var ms; 133 var N; 134 var v; 135 var i; 136 var j; 137 138 // Define the number of accumulators: 139 N = 5; 140 141 // Create an array buffer for storing accumulator output: 142 buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element 143 144 // Initialize accumulators: 145 acc = []; 146 for ( i = 0; i < N; i++ ) { 147 // Compute the byte offset: 148 offset = i * 2 * 8; // stride=2, bytes_per_element=8 149 150 // Create a new view for storing accumulated values: 151 out = new Float64Array( buf, offset, 2 ); 152 153 // Initialize an accumulator which will write results to the view: 154 acc.push( incrmeanstdev( out ) ); 155 } 156 157 // Simulate data and update the sample means and standard deviations... 158 for ( i = 0; i < 100; i++ ) { 159 for ( j = 0; j < N; j++ ) { 160 v = randu() * 100.0 * (j+1); 161 acc[ j ]( v ); 162 } 163 } 164 165 // Print the final results: 166 console.log( 'Mean\tStDev' ); 167 for ( i = 0; i < N; i++ ) { 168 ms = acc[ i ](); 169 console.log( '%d\t%d', ms[ 0 ].toFixed( 3 ), ms[ 1 ].toFixed( 3 ) ); 170 } 171 ``` 172 173 </section> 174 175 <!-- /.examples --> 176 177 <section class="links"> 178 179 [arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean 180 181 [sample-stdev]: https://en.wikipedia.org/wiki/Standard_deviation 182 183 </section> 184 185 <!-- /.links -->