time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (4553B)


      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 an unbiased sample variance.
     31 *
     32 * ## Method
     33 *
     34 * -   This implementation uses Welford's algorithm 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 * ## References
     67 *
     68 * -   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).
     69 * -   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).
     70 *
     71 * @param {number} [mean] - mean value
     72 * @throws {TypeError} must provide a number primitive
     73 * @returns {Function} accumulator function
     74 *
     75 * @example
     76 * var accumulator = incrvariance();
     77 *
     78 * var s2 = accumulator();
     79 * // returns null
     80 *
     81 * s2 = accumulator( 2.0 );
     82 * // returns 0.0
     83 *
     84 * s2 = accumulator( -5.0 );
     85 * // returns 24.5
     86 *
     87 * s2 = accumulator();
     88 * // returns 24.5
     89 *
     90 * @example
     91 * var accumulator = incrvariance( 3.14 );
     92 */
     93 function incrvariance( mean ) {
     94 	var delta;
     95 	var mu;
     96 	var M2;
     97 	var N;
     98 
     99 	M2 = 0.0;
    100 	N = 0;
    101 	if ( arguments.length ) {
    102 		if ( !isNumber( mean ) ) {
    103 			throw new TypeError( 'invalid argument. Must provide a number primitive. Value: `' + mean + '`.' );
    104 		}
    105 		mu = mean;
    106 		return accumulator2;
    107 	}
    108 	mu = 0.0;
    109 	return accumulator1;
    110 
    111 	/**
    112 	* If provided a value, the accumulator function returns an updated unbiased sample variance. If not provided a value, the accumulator function returns the current unbiased sample variance.
    113 	*
    114 	* @private
    115 	* @param {number} [x] - new value
    116 	* @returns {(number|null)} unbiased sample variance or null
    117 	*/
    118 	function accumulator1( x ) {
    119 		if ( arguments.length === 0 ) {
    120 			if ( N === 0 ) {
    121 				return null;
    122 			}
    123 			if ( N === 1 ) {
    124 				return ( isnan( M2 ) ) ? NaN : 0.0;
    125 			}
    126 			return M2 / (N-1);
    127 		}
    128 		N += 1;
    129 		delta = x - mu;
    130 		mu += delta / N;
    131 		M2 += delta * ( x - mu );
    132 		if ( N < 2 ) {
    133 			return ( isnan( M2 ) ) ? NaN : 0.0;
    134 		}
    135 		return M2 / (N-1);
    136 	}
    137 
    138 	/**
    139 	* If provided a value, the accumulator function returns an updated unbiased sample variance. If not provided a value, the accumulator function returns the current unbiased sample variance.
    140 	*
    141 	* @private
    142 	* @param {number} [x] - new value
    143 	* @returns {(number|null)} unbiased sample variance or null
    144 	*/
    145 	function accumulator2( x ) {
    146 		if ( arguments.length === 0 ) {
    147 			if ( N === 0 ) {
    148 				return null;
    149 			}
    150 			return M2 / N;
    151 		}
    152 		N += 1;
    153 		delta = x - mu;
    154 		M2 += delta * delta;
    155 		return M2 / N;
    156 	}
    157 }
    158 
    159 
    160 // EXPORTS //
    161 
    162 module.exports = incrvariance;