time-to-botec

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

meanwd.js (2302B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 // MAIN //
     22 
     23 /**
     24 * Computes the arithmetic mean of a strided array using Welford's algorithm.
     25 *
     26 * ## Method
     27 *
     28 * -   This implementation uses Welford's algorithm for efficient computation, which can be derived as follows
     29 *
     30 *     ```tex
     31 *     \begin{align*}
     32 *     \mu_n &= \frac{1}{n} \sum_{i=0}^{n-1} x_i \\
     33 *           &= \frac{1}{n} \biggl(x_{n-1} + \sum_{i=0}^{n-2} x_i \biggr) \\
     34 *           &= \frac{1}{n} (x_{n-1} + (n-1)\mu_{n-1}) \\
     35 *           &= \mu_{n-1} + \frac{1}{n} (x_{n-1} - \mu_{n-1})
     36 *     \end{align*}
     37 *     ```
     38 *
     39 * ## References
     40 *
     41 * -   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).
     42 * -   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).
     43 *
     44 * @param {PositiveInteger} N - number of indexed elements
     45 * @param {NumericArray} x - input array
     46 * @param {integer} stride - stride length
     47 * @returns {number} arithmetic mean
     48 *
     49 * @example
     50 * var x = [ 1.0, -2.0, 2.0 ];
     51 * var N = x.length;
     52 *
     53 * var v = meanwd( N, x, 1 );
     54 * // returns ~0.3333
     55 */
     56 function meanwd( N, x, stride ) {
     57 	var mu;
     58 	var ix;
     59 	var n;
     60 	var i;
     61 
     62 	if ( N <= 0 ) {
     63 		return NaN;
     64 	}
     65 	if ( N === 1 || stride === 0 ) {
     66 		return x[ 0 ];
     67 	}
     68 	if ( stride < 0 ) {
     69 		ix = (1-N) * stride;
     70 	} else {
     71 		ix = 0;
     72 	}
     73 	mu = 0.0;
     74 	n = 0;
     75 	for ( i = 0; i < N; i++ ) {
     76 		n += 1;
     77 		mu += ( x[ix]-mu ) / n;
     78 		ix += stride;
     79 	}
     80 	return mu;
     81 }
     82 
     83 
     84 // EXPORTS //
     85 
     86 module.exports = meanwd;