main.js (4488B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 isArrayLike = require( '@stdlib/assert/is-array-like-object' ); 24 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns an accumulator function which incrementally computes an arithmetic mean and unbiased sample variance. 31 * 32 * ## Method 33 * 34 * 35 * - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let 36 * 37 * ```tex 38 * \begin{align*} 39 * S_n &= n \sigma_n^2 \\ 40 * &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ 41 * &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 42 * \end{align*} 43 * ``` 44 * 45 * Accordingly, 46 * 47 * ```tex 48 * \begin{align*} 49 * 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 \\ 50 * &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ 51 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ 52 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ 53 * &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ 54 * &= 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} \\ 55 * &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ 56 * &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ 57 * &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) 58 * \end{align*} 59 * ``` 60 * 61 * where we use the identity 62 * 63 * ```tex 64 * x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) 65 * ``` 66 * 67 * ## References 68 * 69 * - 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). 70 * - 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). 71 * 72 * @param {Collection} [out] - output array 73 * @throws {TypeError} output argument must be array-like 74 * @returns {Function} accumulator function 75 * 76 * @example 77 * var accumulator = incrmeanvar(); 78 * 79 * var mv = accumulator(); 80 * // returns null 81 * 82 * mv = accumulator( 2.0 ); 83 * // returns [ 2.0, 0.0 ] 84 * 85 * mv = accumulator( -5.0 ); 86 * // returns [ -1.5, 24.5 ] 87 * 88 * mv = accumulator( 3.0 ); 89 * // returns [ 0.0, 19.0 ] 90 * 91 * mv = accumulator( 5.0 ); 92 * // returns [ 1.25, ~18.92 ] 93 * 94 * mv = accumulator(); 95 * // returns [ 1.25, ~18.92 ] 96 */ 97 function incrmeanvar( out ) { 98 var meanvar; 99 var delta; 100 var mu; 101 var M2; 102 var N; 103 if ( arguments.length === 0 ) { 104 meanvar = [ 0.0, 0.0 ]; 105 } else { 106 if ( !isArrayLike( out ) ) { 107 throw new TypeError( 'invalid argument. Output argument must be an array-like object. Value: `' + out + '`.' ); 108 } 109 meanvar = out; 110 } 111 M2 = 0.0; 112 mu = 0.0; 113 N = 0; 114 return accumulator; 115 116 /** 117 * If provided a value, the accumulator function returns updated results. If not provided a value, the accumulator function returns the current results. 118 * 119 * @private 120 * @param {number} [x] - input value 121 * @returns {(ArrayLikeObject|null)} output array or null 122 */ 123 function accumulator( x ) { 124 if ( arguments.length === 0 ) { 125 if ( N === 0 ) { 126 return null; 127 } 128 meanvar[ 0 ] = mu; // Why? Because we cannot guarantee someone hasn't mutated the output array 129 if ( N === 1 ) { 130 if ( isnan( M2 ) ) { 131 meanvar[ 1 ] = NaN; 132 } else { 133 meanvar[ 1 ] = 0.0; 134 } 135 return meanvar; 136 } 137 meanvar[ 1 ] = M2 / (N-1); 138 return meanvar; 139 } 140 N += 1; 141 delta = x - mu; 142 mu += delta / N; 143 M2 += delta * ( x - mu ); 144 145 meanvar[ 0 ] = mu; 146 if ( N < 2 ) { 147 if ( isnan( M2 ) ) { 148 meanvar[ 1 ] = NaN; 149 } else { 150 meanvar[ 1 ] = 0.0; 151 } 152 return meanvar; 153 } 154 meanvar[ 1 ] = M2 / (N-1); 155 return meanvar; 156 } 157 } 158 159 160 // EXPORTS // 161 162 module.exports = incrmeanvar;