variancewd.js (3273B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MAIN // 22 23 /** 24 * Computes the variance of a strided array using Welford's algorithm. 25 * 26 * ## Method 27 * 28 * - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let 29 * 30 * ```tex 31 * \begin{align*} 32 * S_n &= n \sigma_n^2 \\ 33 * &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ 34 * &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 35 * \end{align*} 36 * ``` 37 * 38 * Accordingly, 39 * 40 * ```tex 41 * \begin{align*} 42 * S_n - S_{n-1} &= \sum_{i=1}^{n} x_i^2 - n\mu_n^2 - \sum_{i=1}^{n-1} x_i^2 + (n-1)\mu_{n-1}^2 \\ 43 * &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ 44 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ 45 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ 46 * &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ 47 * &= x_n^2 - \mu_{n-1}^2 + \mu_{n-1}^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ 48 * &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ 49 * &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ 50 * &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) 51 * \end{align*} 52 * ``` 53 * 54 * where we use the identity 55 * 56 * ```tex 57 * x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) 58 * ``` 59 * 60 * ## References 61 * 62 * - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022). 63 * - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961). 64 * 65 * @param {PositiveInteger} N - number of indexed elements 66 * @param {number} correction - degrees of freedom adjustment 67 * @param {NumericArray} x - input array 68 * @param {integer} stride - stride length 69 * @returns {number} variance 70 * 71 * @example 72 * var x = [ 1.0, -2.0, 2.0 ]; 73 * 74 * var v = variancewd( x.length, 1, x, 1 ); 75 * // returns ~4.3333 76 */ 77 function variancewd( N, correction, x, stride ) { 78 var delta; 79 var mu; 80 var M2; 81 var ix; 82 var v; 83 var n; 84 var i; 85 86 n = N - correction; 87 if ( N <= 0 || n <= 0.0 ) { 88 return NaN; 89 } 90 if ( N === 1 || stride === 0 ) { 91 return 0.0; 92 } 93 if ( stride < 0 ) { 94 ix = (1-N) * stride; 95 } else { 96 ix = 0; 97 } 98 M2 = 0.0; 99 mu = 0.0; 100 for ( i = 0; i < N; i++ ) { 101 v = x[ ix ]; 102 delta = v - mu; 103 mu += delta / (i+1); 104 M2 += delta * ( v - mu ); 105 ix += stride; 106 } 107 return M2 / n; 108 } 109 110 111 // EXPORTS // 112 113 module.exports = variancewd;