time-to-botec

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

main.js (3044B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2019 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 /**
     22 * Returns an accumulator function which incrementally computes a weighted arithmetic mean.
     23 *
     24 * ## Method
     25 *
     26 * -   The weighted arithmetic mean is defined as
     27 *
     28 *     ```tex
     29 *     \mu = \frac{\sum_{i=0}^{n-1} w_i x_i}{\sum_{i=0}^{n-1} w_i}
     30 *     ```
     31 *
     32 *     where \\( w_i \\) are the weights.
     33 *
     34 * -   The weighted arithmetic mean is equivalent to the simple arithmetic mean when all weights are equal.
     35 *
     36 *     ```tex
     37 *     \begin{align*}
     38 *     \mu &= \frac{\sum_{i=0}^{n-1} w x_i}{\sum_{i=0}^{n-1} w} \\
     39 *         &= \frac{w\sum_{i=0}^{n-1} x_i}{nw} \\
     40 *         &= \frac{1}{n} \sum_{i=0}^{n-1}
     41 *     \end{align*}
     42 *     ```
     43 *
     44 * -   If the weights are different, then one can view weights either as sample frequencies or as a means to calculate probabilities where \\( p_i = w_i / \sum w_i \\).
     45 *
     46 * -   To derive an incremental formula for computing a weighted arithmetic mean, let
     47 *
     48 *     ```tex
     49 *     W_n = \sum_{i=1}^{n} w_i
     50 *     ```
     51 *
     52 * -   Accordingly,
     53 *
     54 *     ```tex
     55 *     \begin{align*}
     56 *     \mu_n &= \frac{1}{W_n} \sum_{i=1}^{n} w_i x_i \\
     57 *         &= \frac{1}{W_n} \biggl(w_n x_n + \sum_{i=1}^{n-1} w_i x_i \biggr) \\
     58 *         &= \frac{1}{W_n} (w_n x_n + W_{n-1} \mu_{n-1}) \\
     59 *         &= \frac{1}{W_n} (w_n x_n + (W_n - w_n) \mu_{n-1}) \\
     60 *         &= \frac{1}{W_n} (W_n \mu_{n-1} + w_n x_n - w_n\mu_{n-1}) \\
     61 *         &= \mu_{n-1} + \frac{w_n}{W_n} (x_n - \mu_{n-1})
     62 *     \end{align*}
     63 *     ```
     64 *
     65 * @returns {Function} accumulator function
     66 *
     67 * @example
     68 * var accumulator = incrwmean();
     69 *
     70 * var mu = accumulator();
     71 * // returns null
     72 *
     73 * mu = accumulator( 2.0, 1.0 );
     74 * // returns 2.0
     75 *
     76 * mu = accumulator( 2.0, 0.5 );
     77 * // returns 2.0
     78 *
     79 * mu = accumulator( 3.0, 1.5 );
     80 * // returns 2.5
     81 *
     82 * mu = accumulator();
     83 * // returns 2.5
     84 */
     85 function incrwmean() {
     86 	var wsum;
     87 	var FLG;
     88 	var mu;
     89 
     90 	wsum = 0.0;
     91 	mu = 0.0;
     92 
     93 	return accumulator;
     94 
     95 	/**
     96 	* If provided arguments, the accumulator function returns an updated weighted mean. If not provided arguments, the accumulator function returns the current weighted mean.
     97 	*
     98 	* @private
     99 	* @param {number} [x] - value
    100 	* @param {number} [w] - weight
    101 	* @returns {(number|null)} weighted mean or null
    102 	*/
    103 	function accumulator( x, w ) {
    104 		if ( arguments.length === 0 ) {
    105 			if ( FLG === void 0 ) {
    106 				return null;
    107 			}
    108 			return mu;
    109 		}
    110 		FLG = true;
    111 		wsum += w;
    112 		mu += ( w/wsum ) * ( x-mu );
    113 		return mu;
    114 	}
    115 }
    116 
    117 
    118 // EXPORTS //
    119 
    120 module.exports = incrwmean;