|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 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 | +# incrnanvariance |
| 22 | + |
| 23 | +> Compute an [unbiased sample variance][sample-variance] incrementally, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [unbiased sample variance][sample-variance] is defined as |
| 28 | + |
| 29 | +<!-- <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."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} ( x_i - \bar{x} )^2 |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- </equation> --> |
| 36 | + |
| 37 | +</section> |
| 38 | + |
| 39 | +<!-- /.intro --> |
| 40 | + |
| 41 | +<section class="usage"> |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +```javascript |
| 46 | +var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' ); |
| 47 | +``` |
| 48 | + |
| 49 | +#### incrnanvariance( \[mean] ) |
| 50 | + |
| 51 | +Returns an accumulator `function` which incrementally computes an [unbiased sample variance][sample-variance], ignoring `NaN` values. |
| 52 | + |
| 53 | +```javascript |
| 54 | +var accumulator = incrnanvariance(); |
| 55 | +``` |
| 56 | + |
| 57 | +If the mean is already known, provide a `mean` argument. |
| 58 | + |
| 59 | +```javascript |
| 60 | +var accumulator = incrnanvariance( 3.0 ); |
| 61 | +``` |
| 62 | + |
| 63 | +#### accumulator( \[x] ) |
| 64 | + |
| 65 | +If provided an input value `x`, the accumulator function returns an updated [unbiased sample variance][sample-variance]. If not provided an input value `x`, the accumulator function returns the current [unbiased sample variance][sample-variance]. |
| 66 | + |
| 67 | +```javascript |
| 68 | +var accumulator = incrnanvariance(); |
| 69 | + |
| 70 | +var s2 = accumulator( 2.0 ); |
| 71 | +// returns 0.0 |
| 72 | + |
| 73 | +s2 = accumulator( 1.0 ); // ((2-1.5)^2+(1-1.5)^2) / (2-1) |
| 74 | +// returns 0.5 |
| 75 | + |
| 76 | +s2 = accumulator( 3.0 ); // ((2-2)^2+(1-2)^2+(3-2)^2) / (3-1) |
| 77 | +// returns 1.0 |
| 78 | + |
| 79 | +s2 = accumulator( NaN ); |
| 80 | +// returns 1.0 |
| 81 | + |
| 82 | +s2 = accumulator(); |
| 83 | +// returns 1.0 |
| 84 | +``` |
| 85 | + |
| 86 | +</section> |
| 87 | + |
| 88 | +<!-- /.usage --> |
| 89 | + |
| 90 | +<section class="notes"> |
| 91 | + |
| 92 | +## Notes |
| 93 | + |
| 94 | +- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.notes --> |
| 99 | + |
| 100 | +<section class="examples"> |
| 101 | + |
| 102 | +## Examples |
| 103 | + |
| 104 | +<!-- eslint no-undef: "error" --> |
| 105 | + |
| 106 | +```javascript |
| 107 | +var randu = require( '@stdlib/random/base/randu' ); |
| 108 | +var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' ); |
| 109 | + |
| 110 | +var accumulator; |
| 111 | +var v; |
| 112 | +var i; |
| 113 | + |
| 114 | +// Initialize an accumulator: |
| 115 | +accumulator = incrnanvariance(); |
| 116 | + |
| 117 | +// For each simulated datum, update the unbiased sample variance... |
| 118 | +for ( i = 0; i < 100; i++ ) { |
| 119 | + if ( randu() < 0.2 ) { |
| 120 | + v = NaN; |
| 121 | + } else { |
| 122 | + v = randu() * 100.0; |
| 123 | + } |
| 124 | + accumulator( v ); |
| 125 | +} |
| 126 | +console.log( accumulator() ); |
| 127 | +``` |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.examples --> |
| 132 | + |
| 133 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 134 | + |
| 135 | +<section class="related"> |
| 136 | + |
| 137 | +* * * |
| 138 | + |
| 139 | +## See Also |
| 140 | + |
| 141 | +- <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> |
| 142 | +- <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> |
| 143 | +- <span class="package-name">[`@stdlib/stats/incr/nansum`][@stdlib/stats/incr/nansum]</span><span class="delimiter">: </span><span class="description">compute a sum incrementally, ignoring NaN values.</span> |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.related --> |
| 148 | + |
| 149 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 150 | + |
| 151 | +<section class="links"> |
| 152 | + |
| 153 | +[sample-variance]: https://en.wikipedia.org/wiki/Variance |
| 154 | + |
| 155 | +<!-- <related-links> --> |
| 156 | + |
| 157 | +[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance |
| 158 | + |
| 159 | +[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean |
| 160 | + |
| 161 | +[@stdlib/stats/incr/nansum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/nansum |
| 162 | + |
| 163 | +<!-- </related-links> --> |
| 164 | + |
| 165 | +</section> |
| 166 | + |
| 167 | +<!-- /.links --> |
0 commit comments