time-to-botec

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

snanvariancepn.js (3241B)


      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 var snansumpw = require( './snansumpw.js' );
     25 
     26 
     27 // VARIABLES //
     28 
     29 var WORKSPACE = [ 0.0, 0 ];
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Computes the variance of a single-precision floating-point strided array ignoring `NaN` values and using a two-pass algorithm.
     36 *
     37 * ## Method
     38 *
     39 * -   This implementation uses a two-pass approach, as suggested by Neely (1966).
     40 *
     41 * ## References
     42 *
     43 * -   Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958).
     44 * -   Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036).
     45 *
     46 * @param {PositiveInteger} N - number of indexed elements
     47 * @param {number} correction - degrees of freedom adjustment
     48 * @param {Float32Array} x - input array
     49 * @param {integer} stride - stride length
     50 * @returns {number} variance
     51 *
     52 * @example
     53 * var Float32Array = require( '@stdlib/array/float32' );
     54 *
     55 * var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
     56 * var N = x.length;
     57 *
     58 * var v = snanvariancepn( N, 1, x, 1 );
     59 * // returns ~4.3333
     60 */
     61 function snanvariancepn( N, correction, x, stride ) {
     62 	var mu;
     63 	var ix;
     64 	var M2;
     65 	var nc;
     66 	var M;
     67 	var d;
     68 	var v;
     69 	var n;
     70 	var i;
     71 
     72 	if ( N <= 0 ) {
     73 		return NaN;
     74 	}
     75 	if ( N === 1 || stride === 0 ) {
     76 		v = x[ 0 ];
     77 		if ( v === v && N-correction > 0.0 ) {
     78 			return 0.0;
     79 		}
     80 		return NaN;
     81 	}
     82 	if ( stride < 0 ) {
     83 		ix = (1-N) * stride;
     84 	} else {
     85 		ix = 0;
     86 	}
     87 	// Compute an estimate for the mean...
     88 	WORKSPACE[ 0 ] = 0.0;
     89 	WORKSPACE[ 1 ] = 0;
     90 	snansumpw( N, WORKSPACE, x, stride, ix );
     91 	n = WORKSPACE[ 1 ];
     92 	nc = n - correction;
     93 	if ( nc <= 0.0 ) {
     94 		return NaN;
     95 	}
     96 	mu = float64ToFloat32( WORKSPACE[ 0 ] / n );
     97 
     98 	// Compute the variance...
     99 	M2 = 0.0;
    100 	M = 0.0;
    101 	for ( i = 0; i < N; i++ ) {
    102 		v = x[ ix ];
    103 		if ( v === v ) {
    104 			d = float64ToFloat32( v - mu );
    105 			M2 = float64ToFloat32( M2 + float64ToFloat32( d*d ) );
    106 			M = float64ToFloat32( M + d );
    107 			n += 1;
    108 		}
    109 		ix += stride;
    110 	}
    111 	return float64ToFloat32( float64ToFloat32(M2/nc) - float64ToFloat32(float64ToFloat32(M/n)*float64ToFloat32(M/nc)) ); // eslint-disable-line max-len
    112 }
    113 
    114 
    115 // EXPORTS //
    116 
    117 module.exports = snanvariancepn;