|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 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 | +# nanvariance |
| 22 | + |
| 23 | +> Calculate the [variance][variance] of a strided array ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The population [variance][variance] of a finite size population of size `N` is given by |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:population_variance" align="center" raw="\sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2" alt="Equation for the population variance."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2 |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\sigma^2 = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu)^2" data-equation="eq:population_variance"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@86d4717094f2f0e1d5850ad3208e8591d15792ba/lib/node_modules/@stdlib/stats/strided/nanvariance/docs/img/equation_population_variance.svg" alt="Equation for the population variance."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +where the population mean is given by |
| 43 | + |
| 44 | +<!-- <equation class="equation" label="eq:population_mean" align="center" raw="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" alt="Equation for the population mean."> --> |
| 45 | + |
| 46 | +```math |
| 47 | +\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i |
| 48 | +``` |
| 49 | + |
| 50 | +<!-- <div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:population_mean"> |
| 51 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@86d4717094f2f0e1d5850ad3208e8591d15792ba/lib/node_modules/@stdlib/stats/strided/nanvariance/docs/img/equation_population_mean.svg" alt="Equation for the population mean."> |
| 52 | + <br> |
| 53 | +</div> --> |
| 54 | + |
| 55 | +<!-- </equation> --> |
| 56 | + |
| 57 | +Often in the analysis of data, the true population [variance][variance] is not known _a priori_ and must be estimated from a sample drawn from the population distribution. If one attempts to use the formula for the population [variance][variance], the result is biased and yields a **biased sample variance**. To compute an **unbiased sample variance** for a sample of size `n`, |
| 58 | + |
| 59 | +<!-- <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 computing an unbiased sample variance."> --> |
| 60 | + |
| 61 | +```math |
| 62 | +s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 |
| 63 | +``` |
| 64 | + |
| 65 | +<!-- <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"> |
| 66 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@86d4717094f2f0e1d5850ad3208e8591d15792ba/lib/node_modules/@stdlib/stats/strided/nanvariance/docs/img/equation_unbiased_sample_variance.svg" alt="Equation for computing an unbiased sample variance."> |
| 67 | + <br> |
| 68 | +</div> --> |
| 69 | + |
| 70 | +<!-- </equation> --> |
| 71 | + |
| 72 | +where the sample mean is given by |
| 73 | + |
| 74 | +<!-- <equation class="equation" label="eq:sample_mean" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the sample mean."> --> |
| 75 | + |
| 76 | +```math |
| 77 | +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i |
| 78 | +``` |
| 79 | + |
| 80 | +<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" data-equation="eq:sample_mean"> |
| 81 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@86d4717094f2f0e1d5850ad3208e8591d15792ba/lib/node_modules/@stdlib/stats/strided/nanvariance/docs/img/equation_sample_mean.svg" alt="Equation for the sample mean."> |
| 82 | + <br> |
| 83 | +</div> --> |
| 84 | + |
| 85 | +<!-- </equation> --> |
| 86 | + |
| 87 | +The use of the term `n-1` is commonly referred to as Bessel's correction. Note, however, that applying Bessel's correction can increase the mean squared error between the sample variance and population variance. Depending on the characteristics of the population distribution, other correction factors (e.g., `n-1.5`, `n+1`, etc) can yield better estimators. |
| 88 | + |
| 89 | +</section> |
| 90 | + |
| 91 | +<!-- /.intro --> |
| 92 | + |
| 93 | +<section class="usage"> |
| 94 | + |
| 95 | +## Usage |
| 96 | + |
| 97 | +```javascript |
| 98 | +var nanvariance = require( '@stdlib/stats/strided/nanvariance' ); |
| 99 | +``` |
| 100 | + |
| 101 | +#### nanvariance( N, correction, x, strideX ) |
| 102 | + |
| 103 | +Computes the [variance][variance] of a strided array ignoring `NaN` values. |
| 104 | + |
| 105 | +```javascript |
| 106 | +var x = [ 1.0, -2.0, NaN, 2.0 ]; |
| 107 | + |
| 108 | +var v = nanvariance( x.length, 1, x, 1 ); |
| 109 | +// returns ~4.3333 |
| 110 | +``` |
| 111 | + |
| 112 | +The function has the following parameters: |
| 113 | + |
| 114 | +- **N**: number of indexed elements. |
| 115 | +- **correction**: degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [variance][variance] according to `n-c` where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements. When computing the [variance][variance] of a population, setting this parameter to `0` is the standard choice (i.e., the provided array contains data constituting an entire population). When computing the unbiased sample [variance][variance], setting this parameter to `1` is the standard choice (i.e., the provided array contains data sampled from a larger population; this is commonly referred to as Bessel's correction). |
| 116 | +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. |
| 117 | +- **strideX**: stride length for `x`. |
| 118 | + |
| 119 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [variance][variance] of every other element in `x`, |
| 120 | + |
| 121 | +```javascript |
| 122 | +var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ]; |
| 123 | + |
| 124 | +var v = nanvariance( 5, 1, x, 2 ); |
| 125 | +// returns 6.25 |
| 126 | +``` |
| 127 | + |
| 128 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 129 | + |
| 130 | +<!-- eslint-disable stdlib/capitalized-comments, max-len --> |
| 131 | + |
| 132 | +```javascript |
| 133 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 134 | + |
| 135 | +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); |
| 136 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 137 | + |
| 138 | +var v = nanvariance( 5, 1, x1, 2 ); |
| 139 | +// returns 6.25 |
| 140 | +``` |
| 141 | + |
| 142 | +#### nanvariance.ndarray( N, correction, x, strideX, offsetX ) |
| 143 | + |
| 144 | +Computes the [variance][variance] of a strided array ignoring `NaN` values and using alternative indexing semantics. |
| 145 | + |
| 146 | +```javascript |
| 147 | +var x = [ 1.0, -2.0, NaN, 2.0 ]; |
| 148 | + |
| 149 | +var v = nanvariance.ndarray( x.length, 1, x, 1, 0 ); |
| 150 | +// returns ~4.33333 |
| 151 | +``` |
| 152 | + |
| 153 | +The function has the following additional parameters: |
| 154 | + |
| 155 | +- **offsetX**: starting index for `x`. |
| 156 | + |
| 157 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [variance][variance] for every other element in `x` starting from the second element |
| 158 | + |
| 159 | +```javascript |
| 160 | +var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ]; |
| 161 | + |
| 162 | +var v = nanvariance.ndarray( 5, 1, x, 2, 1 ); |
| 163 | +// returns 6.25 |
| 164 | +``` |
| 165 | + |
| 166 | +</section> |
| 167 | + |
| 168 | +<!-- /.usage --> |
| 169 | + |
| 170 | +<section class="notes"> |
| 171 | + |
| 172 | +## Notes |
| 173 | + |
| 174 | +- If `N <= 0`, both functions return `NaN`. |
| 175 | +- If `n - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment and `n` corresponds to the number of non-`NaN` indexed elements), both functions return `NaN`. |
| 176 | +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 177 | +- Depending on the environment, the typed versions ([`dnanvariance`][@stdlib/stats/strided/dnanvariance], [`snanvariance`][@stdlib/stats/base/snanvariance], etc.) are likely to be significantly more performant. |
| 178 | + |
| 179 | +</section> |
| 180 | + |
| 181 | +<!-- /.notes --> |
| 182 | + |
| 183 | +<section class="examples"> |
| 184 | + |
| 185 | +## Examples |
| 186 | + |
| 187 | +<!-- eslint no-undef: "error" --> |
| 188 | + |
| 189 | +```javascript |
| 190 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 191 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 192 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 193 | +var nanvariance = require( '@stdlib/stats/strided/nanvariance' ); |
| 194 | + |
| 195 | +function rand() { |
| 196 | + if ( bernoulli( 0.8 ) < 1 ) { |
| 197 | + return NaN; |
| 198 | + } |
| 199 | + return uniform( -50.0, 50.0 ); |
| 200 | +} |
| 201 | + |
| 202 | +var x = filledarrayBy( 10, 'float64', rand ); |
| 203 | +console.log( x ); |
| 204 | + |
| 205 | +var v = nanvariance( x.length, 1, x, 1 ); |
| 206 | +console.log( v ); |
| 207 | +``` |
| 208 | + |
| 209 | +</section> |
| 210 | + |
| 211 | +<!-- /.examples --> |
| 212 | + |
| 213 | +<section class="references"> |
| 214 | + |
| 215 | +</section> |
| 216 | + |
| 217 | +<!-- /.references --> |
| 218 | + |
| 219 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 220 | + |
| 221 | +<section class="related"> |
| 222 | + |
| 223 | +* * * |
| 224 | + |
| 225 | +## See Also |
| 226 | + |
| 227 | +- <span class="package-name">[`@stdlib/stats/strided/dnanvariance`][@stdlib/stats/strided/dnanvariance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a double-precision floating-point strided array ignoring NaN values.</span> |
| 228 | +- <span class="package-name">[`@stdlib/stats/base/nanstdev`][@stdlib/stats/base/nanstdev]</span><span class="delimiter">: </span><span class="description">calculate the standard deviation of a strided array ignoring NaN values.</span> |
| 229 | +- <span class="package-name">[`@stdlib/stats/base/snanvariance`][@stdlib/stats/base/snanvariance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a single-precision floating-point strided array ignoring NaN values.</span> |
| 230 | +- <span class="package-name">[`@stdlib/stats/strided/variance`][@stdlib/stats/strided/variance]</span><span class="delimiter">: </span><span class="description">calculate the variance of a strided array.</span> |
| 231 | + |
| 232 | +</section> |
| 233 | + |
| 234 | +<!-- /.related --> |
| 235 | + |
| 236 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 237 | + |
| 238 | +<section class="links"> |
| 239 | + |
| 240 | +[variance]: https://en.wikipedia.org/wiki/Variance |
| 241 | + |
| 242 | +[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array |
| 243 | + |
| 244 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 245 | + |
| 246 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 247 | + |
| 248 | +<!-- <related-links> --> |
| 249 | + |
| 250 | +[@stdlib/stats/strided/dnanvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanvariance |
| 251 | + |
| 252 | +[@stdlib/stats/base/nanstdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanstdev |
| 253 | + |
| 254 | +[@stdlib/stats/base/snanvariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanvariance |
| 255 | + |
| 256 | +[@stdlib/stats/strided/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/variance |
| 257 | + |
| 258 | +<!-- </related-links> --> |
| 259 | + |
| 260 | +</section> |
| 261 | + |
| 262 | +<!-- /.links --> |
0 commit comments