time-to-botec

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

svariancewd.js (3668B)


      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 // MODULES //
     22 
     23 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Computes the variance of a single-precision floating-point strided array using Welford's algorithm.
     30 *
     31 * ## Method
     32 *
     33 * -   This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let
     34 *
     35 *     ```tex
     36 *     \begin{align*}
     37 *     S_n &= n \sigma_n^2 \\
     38 *         &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\
     39 *         &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2
     40 *     \end{align*}
     41 *     ```
     42 *
     43 *     Accordingly,
     44 *
     45 *     ```tex
     46 *     \begin{align*}
     47 *     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 \\
     48 *                   &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\
     49 *                   &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\
     50 *                   &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\
     51 *                   &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\
     52 *                   &= 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} \\
     53 *                   &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
     54 *                   &= (x_n - \mu_{n-1})(x_n - \mu_n) \\
     55 *                   &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n)
     56 *     \end{align*}
     57 *     ```
     58 *
     59 *     where we use the identity
     60 *
     61 *     ```tex
     62 *     x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1})
     63 *     ```
     64 *
     65 * ## References
     66 *
     67 * -   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).
     68 * -   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).
     69 *
     70 * @param {PositiveInteger} N - number of indexed elements
     71 * @param {number} correction - degrees of freedom adjustment
     72 * @param {Float32Array} x - input array
     73 * @param {integer} stride - stride length
     74 * @returns {number} variance
     75 *
     76 * @example
     77 * var Float32Array = require( '@stdlib/array/float32' );
     78 *
     79 * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
     80 * var N = x.length;
     81 *
     82 * var v = svariancewd( N, 1, x, 1 );
     83 * // returns ~4.3333
     84 */
     85 function svariancewd( N, correction, x, stride ) {
     86 	var delta;
     87 	var mu;
     88 	var M2;
     89 	var ix;
     90 	var v;
     91 	var n;
     92 	var i;
     93 
     94 	n = N - correction;
     95 	if ( N <= 0 || n <= 0.0 ) {
     96 		return NaN;
     97 	}
     98 	if ( N === 1 || stride === 0 ) {
     99 		return 0.0;
    100 	}
    101 	if ( stride < 0 ) {
    102 		ix = (1-N) * stride;
    103 	} else {
    104 		ix = 0;
    105 	}
    106 	M2 = 0.0;
    107 	mu = 0.0;
    108 	for ( i = 0; i < N; i++ ) {
    109 		v = x[ ix ];
    110 		delta = float64ToFloat32( v - mu );
    111 		mu = float64ToFloat32( mu + float64ToFloat32( delta / (i+1) ) );
    112 		M2 = float64ToFloat32( M2 + float64ToFloat32( delta * float64ToFloat32( v - mu ) ) ); // eslint-disable-line max-len
    113 		ix += stride;
    114 	}
    115 	return float64ToFloat32( M2 / n );
    116 }
    117 
    118 
    119 // EXPORTS //
    120 
    121 module.exports = svariancewd;