time-to-botec

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

main.js (4692B)


      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 sqrt = require( '@stdlib/math/base/special/sqrt' );
     25 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an accumulator function which incrementally computes a corrected sample standard deviation.
     32 *
     33 * ## Method
     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 {number} [mean] - mean value
     73 * @throws {TypeError} must provide a number primitive
     74 * @returns {Function} accumulator function
     75 *
     76 * @example
     77 * var accumulator = incrstdev();
     78 *
     79 * var s = accumulator();
     80 * // returns null
     81 *
     82 * s = accumulator( 2.0 );
     83 * // returns 0.0
     84 *
     85 * s = accumulator( -5.0 );
     86 * // returns ~4.95
     87 *
     88 * s = accumulator();
     89 * // returns ~4.95
     90 *
     91 * @example
     92 * var accumulator = incrstdev( 3.0 );
     93 */
     94 function incrstdev( mean ) {
     95 	var delta;
     96 	var mu;
     97 	var M2;
     98 	var N;
     99 
    100 	M2 = 0.0;
    101 	N = 0;
    102 	if ( arguments.length ) {
    103 		if ( !isNumber( mean ) ) {
    104 			throw new TypeError( 'invalid argument. Must provide a number primitive. Value: `' + mean + '`.' );
    105 		}
    106 		mu = mean;
    107 		return accumulator2;
    108 	}
    109 	mu = 0.0;
    110 	return accumulator1;
    111 
    112 	/**
    113 	* If provided a value, the accumulator function returns an updated corrected sample standard deviation. If not provided a value, the accumulator function returns the current corrected sample standard deviation.
    114 	*
    115 	* @private
    116 	* @param {number} [x] - new value
    117 	* @returns {(number|null)} corrected sample standard deviation or null
    118 	*/
    119 	function accumulator1( x ) {
    120 		if ( arguments.length === 0 ) {
    121 			if ( N === 0 ) {
    122 				return null;
    123 			}
    124 			if ( N === 1 ) {
    125 				return ( isnan( M2 ) ) ? NaN : 0.0;
    126 			}
    127 			return sqrt( M2/(N-1) );
    128 		}
    129 		N += 1;
    130 		delta = x - mu;
    131 		mu += delta / N;
    132 		M2 += delta * ( x-mu );
    133 		if ( N < 2 ) {
    134 			return ( isnan( M2 ) ) ? NaN : 0.0;
    135 		}
    136 		return sqrt( M2/(N-1) );
    137 	}
    138 
    139 	/**
    140 	* If provided a value, the accumulator function returns an updated corrected sample standard deviation. If not provided a value, the accumulator function returns the current corrected sample standard deviation.
    141 	*
    142 	* @private
    143 	* @param {number} [x] - new value
    144 	* @returns {(number|null)} corrected sample standard deviation or null
    145 	*/
    146 	function accumulator2( x ) {
    147 		if ( arguments.length === 0 ) {
    148 			if ( N === 0 ) {
    149 				return null;
    150 			}
    151 			return sqrt( M2/N );
    152 		}
    153 		N += 1;
    154 		delta = x - mu;
    155 		M2 += delta * delta;
    156 		return sqrt( M2/N );
    157 	}
    158 }
    159 
    160 
    161 // EXPORTS //
    162 
    163 module.exports = incrstdev;