ndarray.js (3776B)
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 // MODULES // 22 23 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 24 25 26 // MAIN // 27 28 /** 29 * Computes the variance of a single-precision floating-point strided array using Welford's algorithm. 30 * 31 * ## Method 32 * 33 * - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let 34 * 35 * ```tex 36 * \begin{align*} 37 * S_n &= n \sigma_n^2 \\ 38 * &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ 39 * &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 40 * \end{align*} 41 * ``` 42 * 43 * Accordingly, 44 * 45 * ```tex 46 * \begin{align*} 47 * 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 \\ 48 * &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ 49 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ 50 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ 51 * &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ 52 * &= 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} \\ 53 * &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ 54 * &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ 55 * &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) 56 * \end{align*} 57 * ``` 58 * 59 * where we use the identity 60 * 61 * ```tex 62 * x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) 63 * ``` 64 * 65 * ## References 66 * 67 * - 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). 68 * - 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). 69 * 70 * @param {PositiveInteger} N - number of indexed elements 71 * @param {number} correction - degrees of freedom adjustment 72 * @param {Float32Array} x - input array 73 * @param {integer} stride - stride length 74 * @param {NonNegativeInteger} offset - starting index 75 * @returns {number} variance 76 * 77 * @example 78 * var Float32Array = require( '@stdlib/array/float32' ); 79 * var floor = require( '@stdlib/math/base/special/floor' ); 80 * 81 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 82 * var N = floor( x.length / 2 ); 83 * 84 * var v = svariancewd( N, 1, x, 2, 1 ); 85 * // returns 6.25 86 */ 87 function svariancewd( N, correction, x, stride, offset ) { 88 var delta; 89 var mu; 90 var M2; 91 var ix; 92 var v; 93 var n; 94 var i; 95 96 n = N - correction; 97 if ( N <= 0 || n <= 0.0 ) { 98 return NaN; 99 } 100 if ( N === 1 || stride === 0 ) { 101 return 0.0; 102 } 103 ix = offset; 104 M2 = 0.0; 105 mu = 0.0; 106 for ( i = 0; i < N; i++ ) { 107 v = x[ ix ]; 108 delta = float64ToFloat32( v - mu ); 109 mu = float64ToFloat32( mu + float64ToFloat32( delta / (i+1) ) ); 110 M2 = float64ToFloat32( M2 + float64ToFloat32( delta * float64ToFloat32( v - mu ) ) ); // eslint-disable-line max-len 111 ix += stride; 112 } 113 return float64ToFloat32( M2 / n ); 114 } 115 116 117 // EXPORTS // 118 119 module.exports = svariancewd;