|
| 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 | +# variancepn |
| 22 | + |
| 23 | +> Calculate the [variance][variance] of an array using a two-pass algorithm. |
| 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 | +<!-- </equation> --> |
| 36 | + |
| 37 | +where the population mean is given by |
| 38 | + |
| 39 | +<!-- <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."> --> |
| 40 | + |
| 41 | +```math |
| 42 | +\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i |
| 43 | +``` |
| 44 | + |
| 45 | +<!-- </equation> --> |
| 46 | + |
| 47 | +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`, |
| 48 | + |
| 49 | +<!-- <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."> --> |
| 50 | + |
| 51 | +```math |
| 52 | +s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 |
| 53 | +``` |
| 54 | + |
| 55 | +<!-- </equation> --> |
| 56 | + |
| 57 | +where the sample mean is given by |
| 58 | + |
| 59 | +<!-- <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."> --> |
| 60 | + |
| 61 | +```math |
| 62 | +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i |
| 63 | +``` |
| 64 | + |
| 65 | +<!-- </equation> --> |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +</section> |
| 70 | + |
| 71 | +<!-- /.intro --> |
| 72 | + |
| 73 | +<section class="usage"> |
| 74 | + |
| 75 | +## Usage |
| 76 | + |
| 77 | +```javascript |
| 78 | +var variancepn = require( '@stdlib/stats/array/variancepn' ); |
| 79 | +``` |
| 80 | + |
| 81 | +#### variancepn( x\[, correction] ) |
| 82 | + |
| 83 | +Computes the variance of an array. |
| 84 | + |
| 85 | +```javascript |
| 86 | +var x = [ 1.0, -2.0, 2.0 ]; |
| 87 | + |
| 88 | +var v = variancepn( x ); |
| 89 | +// returns ~4.3333 |
| 90 | +``` |
| 91 | + |
| 92 | +The function has the following parameters: |
| 93 | + |
| 94 | +- **x**: input array. |
| 95 | +- **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 `N` corresponds to the number of array elements and `c` corresponds to the provided degrees of freedom adjustment. 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). Default: `1.0`. |
| 96 | + |
| 97 | +By default, the function computes the sample [variance][variance]. To adjust the degrees of freedom when computing the [variance][variance], provide a `correction` argument. |
| 98 | + |
| 99 | +```javascript |
| 100 | +var x = [ 1.0, -2.0, 2.0 ]; |
| 101 | + |
| 102 | +var v = variancepn( x, 0.0 ); |
| 103 | +// returns ~2.8889 |
| 104 | +``` |
| 105 | + |
| 106 | +</section> |
| 107 | + |
| 108 | +<!-- /.usage --> |
| 109 | + |
| 110 | +<section class="notes"> |
| 111 | + |
| 112 | +## Notes |
| 113 | + |
| 114 | +- If provided an empty array, the function returns `NaN`. |
| 115 | +- If provided a `correction` argument which is greater than or equal to the number of elements in a provided input array, the function returns `NaN`. |
| 116 | +- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 117 | + |
| 118 | +</section> |
| 119 | + |
| 120 | +<!-- /.notes --> |
| 121 | + |
| 122 | +<section class="examples"> |
| 123 | + |
| 124 | +## Examples |
| 125 | + |
| 126 | +<!-- eslint no-undef: "error" --> |
| 127 | + |
| 128 | +```javascript |
| 129 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 130 | +var variancepn = require( '@stdlib/stats/array/variancepn' ); |
| 131 | + |
| 132 | +var x = discreteUniform( 10, -50, 50, { |
| 133 | + 'dtype': 'float64' |
| 134 | +}); |
| 135 | +console.log( x ); |
| 136 | + |
| 137 | +var v = variancepn( x ); |
| 138 | +console.log( v ); |
| 139 | +``` |
| 140 | + |
| 141 | +</section> |
| 142 | + |
| 143 | +<!-- /.examples --> |
| 144 | + |
| 145 | +* * * |
| 146 | + |
| 147 | +<section class="references"> |
| 148 | + |
| 149 | +## References |
| 150 | + |
| 151 | +- Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958][@neely:1966a]. |
| 152 | +- Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036][@schubert:2018a]. |
| 153 | + |
| 154 | +</section> |
| 155 | + |
| 156 | +<!-- /.references --> |
| 157 | + |
| 158 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 159 | + |
| 160 | +<section class="related"> |
| 161 | + |
| 162 | +</section> |
| 163 | + |
| 164 | +<!-- /.related --> |
| 165 | + |
| 166 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 167 | + |
| 168 | +<section class="links"> |
| 169 | + |
| 170 | +[variance]: https://en.wikipedia.org/wiki/Variance |
| 171 | + |
| 172 | +[@neely:1966a]: https://doi.org/10.1145/365719.365958 |
| 173 | + |
| 174 | +[@schubert:2018a]: https://doi.org/10.1145/3221269.3223036 |
| 175 | + |
| 176 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 177 | + |
| 178 | +</section> |
| 179 | + |
| 180 | +<!-- /.links --> |
0 commit comments