|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# incrnanmeanvar |
| 22 | + |
| 23 | +> Compute an [arithmetic mean][arithmetic-mean] and an [unbiased sample variance][sample-variance] incrementally, ignoring `NaN` value. |
| 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 | +```math |
| 32 | +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <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"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b2df03cb2a582cf1df289c3ddca6922c0db854b4/lib/node_modules/@stdlib/stats/incr/meanvar/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}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2" alt="Equation for the unbiased sample variance."> --> |
| 45 | + |
| 46 | +```math |
| 47 | +s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2 |
| 48 | +``` |
| 49 | + |
| 50 | +<!-- <div class="equation" align="center" data-raw-text="s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2" data-equation="eq:unbiased_sample_variance"> |
| 51 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@eafa6e61d15b7c712c9288d59d8e0e3f0aa6c011/lib/node_modules/@stdlib/stats/incr/meanvar/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for the unbiased sample variance."> |
| 52 | + <br> |
| 53 | +</div> --> |
| 54 | + |
| 55 | +<!-- </equation> --> |
| 56 | + |
| 57 | +<section class="usage"> |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +```javascript |
| 62 | +var incrnanmeanvar = require( '@stdlib/stats/incr/nanmeanvar' ); |
| 63 | +``` |
| 64 | + |
| 65 | +#### incrnanmeanvar( \[out] ) |
| 66 | + |
| 67 | +Returns an accumulator `function` which incrementally computes an [arithmetic mean][arithmetic-mean] and [unbiased sample variance][sample-variance], ignoring `NaN` value. |
| 68 | + |
| 69 | +```javascript |
| 70 | +var accumulator = incrnanmeanvar(); |
| 71 | +``` |
| 72 | + |
| 73 | +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. |
| 74 | + |
| 75 | +```javascript |
| 76 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 77 | + |
| 78 | +var accumulator = incrnanmeanvar( new Float64Array( 2 ) ); |
| 79 | +``` |
| 80 | + |
| 81 | +#### accumulator( \[x] ) |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +```javascript |
| 86 | +var accumulator = incrnanmeanvar(); |
| 87 | + |
| 88 | +var mv = accumulator(); |
| 89 | +// returns null |
| 90 | + |
| 91 | +mv = accumulator( 2.0 ); |
| 92 | +// returns [ 2.0, 0.0 ] |
| 93 | + |
| 94 | +mv = accumulator( 1.0 ); |
| 95 | +// returns [ 1.5, 0.5 ] |
| 96 | + |
| 97 | +mv = accumulator( 3.0 ); |
| 98 | +// returns [ 2.0, 1.0 ] |
| 99 | + |
| 100 | +mv = accumulator( NaN ); |
| 101 | +// returns [ 2.0, 1.0 ] |
| 102 | + |
| 103 | +mv = accumulator( -7.0 ); |
| 104 | +// returns [ -0.25, ~20.92 ] |
| 105 | + |
| 106 | +mv = accumulator( -5.0 ); |
| 107 | +// returns [ -1.2, 20.2 ] |
| 108 | + |
| 109 | +mv = accumulator(); |
| 110 | +// returns [ -1.2, 20.2 ] |
| 111 | +``` |
| 112 | + |
| 113 | +</section> |
| 114 | + |
| 115 | +<!-- /.usage --> |
| 116 | + |
| 117 | +<section class="notes"> |
| 118 | + |
| 119 | +## Notes |
| 120 | + |
| 121 | +- Input values are type checked; if a NaN value is provided, the accumulator skips the update and continues to return the previous state. If non-numeric inputs are possible, you are advised to type check and handle them accordingly **before** passing the value to the accumulator function. |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.notes --> |
| 126 | + |
| 127 | +<section class="examples"> |
| 128 | + |
| 129 | +## Examples |
| 130 | + |
| 131 | +<!-- eslint no-undef: "error" --> |
| 132 | + |
| 133 | +```javascript |
| 134 | +var randu = require( '@stdlib/random/base/randu' ); |
| 135 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 136 | +var ArrayBuffer = require( '@stdlib/array/buffer' ); |
| 137 | +var incrnanmeanvar = require( '@stdlib/stats/incr/nanmeanvar' ); |
| 138 | + |
| 139 | +var offset; |
| 140 | +var acc; |
| 141 | +var buf; |
| 142 | +var out; |
| 143 | +var mv; |
| 144 | +var N; |
| 145 | +var v; |
| 146 | +var i; |
| 147 | +var j; |
| 148 | + |
| 149 | +// Define the number of accumulators: |
| 150 | +N = 5; |
| 151 | + |
| 152 | +// Create an array buffer for storing accumulator output: |
| 153 | +buf = new ArrayBuffer( N*2*8 ); // 8 bytes per element |
| 154 | + |
| 155 | +// Initialize accumulators: |
| 156 | +acc = []; |
| 157 | +for ( i = 0; i < N; i++ ) { |
| 158 | + // Compute the byte offset: |
| 159 | + offset = i * 2 * 8; // stride=2, bytes_per_element=8 |
| 160 | + |
| 161 | + // Create a new view for storing accumulated values: |
| 162 | + out = new Float64Array( buf, offset, 2 ); |
| 163 | + |
| 164 | + // Initialize an accumulator which will write results to the view: |
| 165 | + acc.push( incrnanmeanvar( out ) ); |
| 166 | +} |
| 167 | + |
| 168 | +// Simulate data and update the sample means and variances... |
| 169 | +for ( i = 0; i < 100; i++ ) { |
| 170 | + for ( j = 0; j < N; j++ ) { |
| 171 | + if ( randu() < 0.2 ) { |
| 172 | + v = NaN; |
| 173 | + } else { |
| 174 | + v = randu() * 100.0 * (j+1); |
| 175 | + } |
| 176 | + acc[ j ]( v ); |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +// Print the final results: |
| 181 | +console.log( 'Mean\tVariance' ); |
| 182 | +for ( i = 0; i < N; i++ ) { |
| 183 | + mv = acc[ i ](); |
| 184 | + console.log( '%d\t%d', mv[ 0 ].toFixed( 3 ), mv[ 1 ].toFixed( 3 ) ); |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +</section> |
| 189 | + |
| 190 | +<!-- /.examples --> |
| 191 | + |
| 192 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 193 | + |
| 194 | +<section class="related"> |
| 195 | + |
| 196 | +* * * |
| 197 | + |
| 198 | +## See Also |
| 199 | + |
| 200 | +- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span> |
| 201 | +- <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> |
| 202 | +- <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> |
| 203 | +- <span class="package-name">[`@stdlib/stats/incr/variance`][@stdlib/stats/incr/variance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample variance incrementally.</span> |
| 204 | + |
| 205 | +</section> |
| 206 | + |
| 207 | +<!-- /.related --> |
| 208 | + |
| 209 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 210 | + |
| 211 | +<section class="links"> |
| 212 | + |
| 213 | +[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean |
| 214 | + |
| 215 | +[sample-variance]: https://en.wikipedia.org/wiki/Variance |
| 216 | + |
| 217 | +<!-- <related-links> --> |
| 218 | + |
| 219 | +[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean |
| 220 | + |
| 221 | +[@stdlib/stats/incr/meanstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/meanstdev |
| 222 | + |
| 223 | +[@stdlib/stats/incr/mmeanvar]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmeanvar |
| 224 | + |
| 225 | +[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance |
| 226 | + |
| 227 | +<!-- </related-links> --> |
| 228 | + |
| 229 | +</section> |
| 230 | + |
| 231 | +<!-- /.links --> |
0 commit comments