ndarray.js (3360B)
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 * @param {NonNegativeInteger} offset - starting index 51 * @returns {number} variance 52 * 53 * @example 54 * var Float32Array = require( '@stdlib/array/float32' ); 55 * var floor = require( '@stdlib/math/base/special/floor' ); 56 * 57 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] ); 58 * var N = floor( x.length / 2 ); 59 * 60 * var v = snanvariancepn( N, 1, x, 2, 1 ); 61 * // returns 6.25 62 */ 63 function snanvariancepn( N, correction, x, stride, offset ) { 64 var mu; 65 var ix; 66 var M2; 67 var nc; 68 var M; 69 var d; 70 var v; 71 var n; 72 var i; 73 74 if ( N <= 0 ) { 75 return NaN; 76 } 77 if ( N === 1 || stride === 0 ) { 78 v = x[ offset ]; 79 if ( v === v && N-correction > 0.0 ) { 80 return 0.0; 81 } 82 return NaN; 83 } 84 ix = offset; 85 86 // Compute an estimate for the mean... 87 WORKSPACE[ 0 ] = 0.0; 88 WORKSPACE[ 1 ] = 0; 89 snansumpw( N, WORKSPACE, x, stride, ix ); 90 n = WORKSPACE[ 1 ]; 91 nc = n - correction; 92 if ( nc <= 0.0 ) { 93 return NaN; 94 } 95 mu = float64ToFloat32( WORKSPACE[ 0 ] / n ); 96 97 // Compute the variance... 98 M2 = 0.0; 99 M = 0.0; 100 for ( i = 0; i < N; i++ ) { 101 v = x[ ix ]; 102 if ( v === v ) { 103 d = float64ToFloat32( v - mu ); 104 M2 = float64ToFloat32( M2 + float64ToFloat32( d*d ) ); 105 M = float64ToFloat32( M + d ); 106 n += 1; 107 } 108 ix += stride; 109 } 110 return float64ToFloat32( float64ToFloat32(M2/nc) - float64ToFloat32(float64ToFloat32(M/n)*float64ToFloat32(M/nc)) ); // eslint-disable-line max-len 111 } 112 113 114 // EXPORTS // 115 116 module.exports = snanvariancepn;