time-to-botec

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

ndarray.js (3396B)


      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 variance 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. Let
     29 *
     30 *     ```tex
     31 *     \begin{align*}
     32 *     S_n &= n \sigma_n^2 \\
     33 *         &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\
     34 *         &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2
     35 *     \end{align*}
     36 *     ```
     37 *
     38 *     Accordingly,
     39 *
     40 *     ```tex
     41 *     \begin{align*}
     42 *     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 \\
     43 *                   &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\
     44 *                   &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\
     45 *                   &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\
     46 *                   &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\
     47 *                   &= 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} \\
     48 *                   &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
     49 *                   &= (x_n - \mu_{n-1})(x_n - \mu_n) \\
     50 *                   &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n)
     51 *     \end{align*}
     52 *     ```
     53 *
     54 *     where we use the identity
     55 *
     56 *     ```tex
     57 *     x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1})
     58 *     ```
     59 *
     60 * ## References
     61 *
     62 * -   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).
     63 * -   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).
     64 *
     65 * @param {PositiveInteger} N - number of indexed elements
     66 * @param {number} correction - degrees of freedom adjustment
     67 * @param {NumericArray} x - input array
     68 * @param {integer} stride - stride length
     69 * @param {NonNegativeInteger} offset - starting index
     70 * @returns {number} variance
     71 *
     72 * @example
     73 * var floor = require( '@stdlib/math/base/special/floor' );
     74 *
     75 * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
     76 * var N = floor( x.length / 2 );
     77 *
     78 * var v = variancewd( N, 1, x, 2, 1 );
     79 * // returns 6.25
     80 */
     81 function variancewd( N, correction, x, stride, offset ) {
     82 	var delta;
     83 	var mu;
     84 	var M2;
     85 	var ix;
     86 	var v;
     87 	var n;
     88 	var i;
     89 
     90 	n = N - correction;
     91 	if ( N <= 0 || n <= 0.0 ) {
     92 		return NaN;
     93 	}
     94 	if ( N === 1 || stride === 0 ) {
     95 		return 0.0;
     96 	}
     97 	ix = offset;
     98 	M2 = 0.0;
     99 	mu = 0.0;
    100 	for ( i = 0; i < N; i++ ) {
    101 		v = x[ ix ];
    102 		delta = v - mu;
    103 		mu += delta / (i+1);
    104 		M2 += delta * ( v - mu );
    105 		ix += stride;
    106 	}
    107 	return M2 / n;
    108 }
    109 
    110 
    111 // EXPORTS //
    112 
    113 module.exports = variancewd;