time-to-botec

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

snanvariancewd.js (2621B)


      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 ignoring `NaN` values and using Welford's algorithm.
     30 *
     31 * ## References
     32 *
     33 * -   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).
     34 * -   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).
     35 *
     36 * @param {PositiveInteger} N - number of indexed elements
     37 * @param {number} correction - degrees of freedom adjustment
     38 * @param {Float32Array} x - input array
     39 * @param {integer} stride - stride length
     40 * @returns {number} variance
     41 *
     42 * @example
     43 * var Float32Array = require( '@stdlib/array/float32' );
     44 *
     45 * var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
     46 * var N = x.length;
     47 *
     48 * var v = snanvariancewd( N, 1, x, 1 );
     49 * // returns ~4.3333
     50 */
     51 function snanvariancewd( N, correction, x, stride ) {
     52 	var delta;
     53 	var mu;
     54 	var M2;
     55 	var ix;
     56 	var nc;
     57 	var v;
     58 	var n;
     59 	var i;
     60 
     61 	if ( N <= 0 ) {
     62 		return NaN;
     63 	}
     64 	if ( N === 1 || stride === 0 ) {
     65 		v = x[ 0 ];
     66 		if ( v === v && N-correction > 0.0 ) {
     67 			return 0.0;
     68 		}
     69 		return NaN;
     70 	}
     71 	if ( stride < 0 ) {
     72 		ix = (1-N) * stride;
     73 	} else {
     74 		ix = 0;
     75 	}
     76 	M2 = 0.0;
     77 	mu = 0.0;
     78 	n = 0;
     79 	for ( i = 0; i < N; i++ ) {
     80 		v = x[ ix ];
     81 		if ( v === v ) {
     82 			delta = float64ToFloat32( v - mu );
     83 			n += 1;
     84 			mu = float64ToFloat32( mu + float64ToFloat32( delta/n ) );
     85 			M2 = float64ToFloat32( M2 + float64ToFloat32( delta*float64ToFloat32( v-mu ) ) ); // eslint-disable-line max-len
     86 		}
     87 		ix += stride;
     88 	}
     89 	nc = n - correction;
     90 	if ( nc <= 0.0 ) {
     91 		return NaN;
     92 	}
     93 	return float64ToFloat32( M2 / nc );
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = snanvariancewd;