time-to-botec

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

main.js (4252B)


      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 var sqrt = require( '@stdlib/math/base/special/sqrt' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an accumulator function which incrementally computes the coefficient of variation (CV).
     32 *
     33 * ## Method
     34 *
     35 * -   This implementation uses [Welford's method][algorithms-variance] 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 * [algorithms-variance]: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
     68 *
     69 * @param {number} [mean] - mean value
     70 * @throws {TypeError} must provide a number primitive
     71 * @returns {Function} accumulator function
     72 *
     73 * @example
     74 * var accumulator = incrcv();
     75 *
     76 * var cv = accumulator();
     77 * // returns null
     78 *
     79 * cv = accumulator( 2.0 );
     80 * // returns 0.0
     81 *
     82 * cv = accumulator( 1.0 );
     83 * // returns ~0.47
     84 *
     85 * cv = accumulator();
     86 * // returns ~0.47
     87 *
     88 * @example
     89 * var accumulator = incrcv( 3.14 );
     90 */
     91 function incrcv( mean ) {
     92 	var delta;
     93 	var mu;
     94 	var M2;
     95 	var N;
     96 
     97 	M2 = 0.0;
     98 	N = 0;
     99 	if ( arguments.length ) {
    100 		if ( !isNumber( mean ) ) {
    101 			throw new TypeError( 'invalid argument. Must provide a number primitive. Value: `' + mean + '`.' );
    102 		}
    103 		mu = mean;
    104 		return accumulator2;
    105 	}
    106 	mu = 0.0;
    107 	return accumulator1;
    108 
    109 	/**
    110 	* 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.
    111 	*
    112 	* @private
    113 	* @param {number} [x] - new value
    114 	* @returns {(number|null)} accumulated value or null
    115 	*/
    116 	function accumulator1( x ) {
    117 		if ( arguments.length === 0 ) {
    118 			if ( N === 0 ) {
    119 				return null;
    120 			}
    121 			if ( N === 1 ) {
    122 				return ( isnan( M2 ) ) ? NaN : 0.0/mu;
    123 			}
    124 			return sqrt( M2/(N-1) ) / mu;
    125 		}
    126 		N += 1;
    127 		delta = x - mu;
    128 		mu += delta / N;
    129 		M2 += delta * ( x - mu );
    130 		if ( N < 2 ) {
    131 			return ( isnan( M2 ) ) ? NaN : 0.0/mu;
    132 		}
    133 		return sqrt( M2/(N-1) ) / mu;
    134 	}
    135 
    136 	/**
    137 	* 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.
    138 	*
    139 	* @private
    140 	* @param {number} [x] - new value
    141 	* @returns {(number|null)} accumulated value or null
    142 	*/
    143 	function accumulator2( x ) {
    144 		if ( arguments.length === 0 ) {
    145 			if ( N === 0 ) {
    146 				return null;
    147 			}
    148 			return sqrt( M2/N ) / mu;
    149 		}
    150 		N += 1;
    151 		delta = x - mu;
    152 		M2 += delta * delta;
    153 		return sqrt( M2/N ) / mu;
    154 	}
    155 }
    156 
    157 
    158 // EXPORTS //
    159 
    160 module.exports = incrcv;