main.js (4177B)
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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; 24 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns an accumulator function which incrementally computes a variance-to-mean ratio (VMR). 31 * 32 * ## Method 33 * 34 * - This implementation uses [Welford's method][algorithms-variance] for efficient computation, which can be derived as follows. Let 35 * 36 * ```tex 37 * \begin{align*} 38 * S_n &= n \sigma_n^2 \\ 39 * &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\ 40 * &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2 41 * \end{align*} 42 * ``` 43 * 44 * Accordingly, 45 * 46 * ```tex 47 * \begin{align*} 48 * 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 \\ 49 * &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\ 50 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\ 51 * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\ 52 * &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\ 53 * &= 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} \\ 54 * &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\ 55 * &= (x_n - \mu_{n-1})(x_n - \mu_n) \\ 56 * &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n) 57 * \end{align*} 58 * ``` 59 * 60 * where we use the identity 61 * 62 * ```tex 63 * x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1}) 64 * ``` 65 * 66 * [algorithms-variance]: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance 67 * 68 * @param {number} [mean] - mean value 69 * @throws {TypeError} must provide a number primitive 70 * @returns {Function} accumulator function 71 * 72 * @example 73 * var accumulator = incrvmr(); 74 * 75 * var D = accumulator(); 76 * // returns null 77 * 78 * D = accumulator( 2.0 ); 79 * // returns 0.0 80 * 81 * D = accumulator( 1.0 ); 82 * // returns ~0.33 83 * 84 * D = accumulator(); 85 * // returns ~0.33 86 * 87 * @example 88 * var accumulator = incrvmr( 3.14 ); 89 */ 90 function incrvmr( mean ) { 91 var delta; 92 var mu; 93 var M2; 94 var N; 95 96 M2 = 0.0; 97 N = 0; 98 if ( arguments.length ) { 99 if ( !isNumber( mean ) ) { 100 throw new TypeError( 'invalid argument. Must provide a number primitive. Value: `' + mean + '`.' ); 101 } 102 mu = mean; 103 return accumulator2; 104 } 105 mu = 0.0; 106 return accumulator1; 107 108 /** 109 * If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value. 110 * 111 * @private 112 * @param {number} [x] - new value 113 * @returns {(number|null)} accumulated value or null 114 */ 115 function accumulator1( x ) { 116 if ( arguments.length === 0 ) { 117 if ( N === 0 ) { 118 return null; 119 } 120 if ( N === 1 ) { 121 return ( isnan( M2 ) ) ? NaN : 0.0/mu; 122 } 123 return ( M2/(N-1) ) / mu; 124 } 125 N += 1; 126 delta = x - mu; 127 mu += delta / N; 128 M2 += delta * ( x - mu ); 129 if ( N < 2 ) { 130 return ( isnan( M2 ) ) ? NaN : 0.0/mu; 131 } 132 return ( M2/(N-1) ) / mu; 133 } 134 135 /** 136 * If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value. 137 * 138 * @private 139 * @param {number} [x] - new value 140 * @returns {(number|null)} accumulated value or null 141 */ 142 function accumulator2( x ) { 143 if ( arguments.length === 0 ) { 144 if ( N === 0 ) { 145 return null; 146 } 147 return ( M2/N ) / mu; 148 } 149 N += 1; 150 delta = x - mu; 151 M2 += delta * delta; 152 return ( M2/N ) / mu; 153 } 154 } 155 156 157 // EXPORTS // 158 159 module.exports = incrvmr;