You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Licensed under the Apache License, Version 2.0 (the "License");
260
+
* you may not use this file except in compliance with the License.
261
+
* You may obtain a copy of the License at
262
+
*
263
+
* http://www.apache.org/licenses/LICENSE-2.0
264
+
*
265
+
* Unless required by applicable law or agreed to in writing, software
266
+
* distributed under the License is distributed on an "AS IS" BASIS,
267
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
268
+
* See the License for the specific language governing permissions and
269
+
* limitations under the License.
270
+
*/
271
+
272
+
'use strict';
273
+
274
+
// MODULES //
275
+
276
+
var dsumpw = require( '@stdlib/blas/ext/base/dsumpw' );
277
+
278
+
279
+
// MAIN //
280
+
281
+
/**
282
+
* Computes the variance of a double-precision floating-point strided array using a two-pass algorithm.
283
+
*
284
+
* ## Method
285
+
*
286
+
* - This implementation uses a two-pass approach, as suggested by Neely (1966).
287
+
*
288
+
* ## References
289
+
*
290
+
* - 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).
291
+
* - 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).
292
+
*
293
+
* @param {PositiveInteger} N - number of indexed elements
294
+
* @param {number} correction - degrees of freedom adjustment
295
+
* @param {Float64Array} x - input array
296
+
* @param {integer} stride - stride length
297
+
* @returns {number} variance
298
+
*
299
+
* @example
300
+
* var Float64Array = require( '@stdlib/array/float64' );
301
+
*
302
+
* var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
303
+
* var N = x.length;
304
+
*
305
+
* var v = dvariancepn( N, 1, x, 1 );
306
+
* // returns ~4.3333
307
+
*/
308
+
function dvariancepn( N, correction, x, stride ) {
309
+
var mu;
310
+
var ix;
311
+
var M2;
312
+
var M;
313
+
var d;
314
+
var n;
315
+
var i;
316
+
317
+
n = N - correction;
318
+
if ( N <= 0 || n <= 0.0 ) {
319
+
return NaN;
320
+
}
321
+
if ( N === 1 || stride === 0 ) {
322
+
return 0.0;
323
+
}
324
+
// Compute an estimate for the mean:
325
+
mu = dsumpw( N, x, stride ) / N;
326
+
327
+
if ( stride < 0 ) {
328
+
ix = (1-N) * stride;
329
+
} else {
330
+
ix = 0;
331
+
}
332
+
// Compute the variance...
333
+
M2 = 0.0;
334
+
M = 0.0;
335
+
for ( i = 0; i < N; i++ ) {
336
+
d = x[ ix ] - mu;
337
+
M2 += d * d;
338
+
M += d;
339
+
ix += stride;
340
+
}
341
+
return (M2/n) - ((M/N)*(M/n));
342
+
}
343
+
344
+
345
+
// EXPORTS //
346
+
347
+
module.exports = dvariancepn;
348
+
</pre></td></tr></table></pre>
349
+
350
+
<divclass='push'></div><!-- for sticky footer -->
351
+
</div><!-- /wrapper -->
352
+
<divclass='footer quiet pad2 space-top1 center small'>
0 commit comments