|
| 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 | +# svarianceyc |
| 22 | + |
| 23 | +> Calculate the [variance][variance] of a one-dimensional single-precision floating-point ndarray using a one-pass algorithm proposed by Youngs and Cramer. |
| 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@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/svarianceyc/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@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/svarianceyc/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 an **uncorrected sample variance**. To compute a **corrected sample variance** for a sample of size `n`, |
| 58 | + |
| 59 | +<!-- <equation class="equation" label="eq:corrected_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 a corrected 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:corrected_sample_variance"> |
| 66 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/svarianceyc/docs/img/equation_corrected_sample_variance.svg" alt="Equation for computing a corrected 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@08ca32895957967bd760a4fe02d61762432a0b72/lib/node_modules/@stdlib/stats/strided/svarianceyc/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 svarianceyc = require( '@stdlib/stats/base/ndarray/svarianceyc' ); |
| 99 | +``` |
| 100 | + |
| 101 | +#### svarianceyc( arrays ) |
| 102 | + |
| 103 | +Computes the [variance][variance] of a one-dimensional single-precision floating-point ndarray using a one-pass algorithm proposed by Youngs and Cramer. |
| 104 | + |
| 105 | +```javascript |
| 106 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 107 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 108 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 109 | + |
| 110 | +var opts = { |
| 111 | + 'dtype': 'float32' |
| 112 | +}; |
| 113 | + |
| 114 | +var xbuf = new Float32Array( [ 1.0, -2.0, 2.0 ] ); |
| 115 | +var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); |
| 116 | +var correction = scalar2ndarray( 1.0, opts ); |
| 117 | + |
| 118 | +var v = svarianceyc( [ x, correction ] ); |
| 119 | +// returns ~4.3333 |
| 120 | +``` |
| 121 | + |
| 122 | +The function has the following parameters: |
| 123 | + |
| 124 | +- **arrays**: array-like object containing two elements: a one-dimensional input ndarray and a zero-dimensional ndarray specifying the degrees of freedom adjustment. Providing a non-zero degrees of freedom adjustment has the effect of adjusting the divisor during the calculation of the [variance][variance] according to `N-c` where `N` is the number of elements in the input ndarray 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 corrected 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). |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.usage --> |
| 129 | + |
| 130 | +<section class="notes"> |
| 131 | + |
| 132 | +## Notes |
| 133 | + |
| 134 | +- If provided an empty one-dimensional ndarray, the function returns `NaN`. |
| 135 | +- If `N - c` is less than or equal to `0` (where `N` corresponds to the number of elements in the input ndarray and `c` corresponds to the provided degrees of freedom adjustment), the function returns `NaN`. |
| 136 | + |
| 137 | +</section> |
| 138 | + |
| 139 | +<!-- /.notes --> |
| 140 | + |
| 141 | +<section class="examples"> |
| 142 | + |
| 143 | +## Examples |
| 144 | + |
| 145 | +<!-- eslint no-undef: "error" --> |
| 146 | + |
| 147 | +```javascript |
| 148 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 149 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 150 | +var ndarray = require( '@stdlib/ndarray/base/ctor' ); |
| 151 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 152 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 153 | +var svarianceyc = require( '@stdlib/stats/base/ndarray/svarianceyc' ); |
| 154 | + |
| 155 | +var opts = { |
| 156 | + 'dtype': 'float32' |
| 157 | +}; |
| 158 | + |
| 159 | +var xbuf = discreteUniform( 10, -50, 50, opts ); |
| 160 | +var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); |
| 161 | +console.log( ndarray2array( x ) ); |
| 162 | + |
| 163 | +var correction = scalar2ndarray( 1.0, opts ); |
| 164 | +var v = svarianceyc( [ x, correction ] ); |
| 165 | +console.log( v ); |
| 166 | +``` |
| 167 | + |
| 168 | +</section> |
| 169 | + |
| 170 | +<!-- /.examples --> |
| 171 | + |
| 172 | +* * * |
| 173 | + |
| 174 | +<section class="references"> |
| 175 | + |
| 176 | +## References |
| 177 | + |
| 178 | +- Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826][@youngs:1971a]. |
| 179 | + |
| 180 | +</section> |
| 181 | + |
| 182 | +<!-- /.references --> |
| 183 | + |
| 184 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 185 | + |
| 186 | +<section class="related"> |
| 187 | + |
| 188 | +</section> |
| 189 | + |
| 190 | +<!-- /.related --> |
| 191 | + |
| 192 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 193 | + |
| 194 | +<section class="links"> |
| 195 | + |
| 196 | +[variance]: https://en.wikipedia.org/wiki/Variance |
| 197 | + |
| 198 | +[@youngs:1971a]: https://doi.org/10.1080/00401706.1971.10488826 |
| 199 | + |
| 200 | +</section> |
| 201 | + |
| 202 | +<!-- /.links --> |
0 commit comments