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");
266
+
* you may not use this file except in compliance with the License.
267
+
* You may obtain a copy of the License at
268
+
*
269
+
* http://www.apache.org/licenses/LICENSE-2.0
270
+
*
271
+
* Unless required by applicable law or agreed to in writing, software
272
+
* distributed under the License is distributed on an "AS IS" BASIS,
273
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
274
+
* See the License for the specific language governing permissions and
275
+
* limitations under the License.
276
+
*/
277
+
278
+
'use strict';
279
+
280
+
// MODULES //
281
+
282
+
var gsumpw = require( '@stdlib/blas/ext/base/gsumpw' ).ndarray;
283
+
284
+
285
+
// MAIN //
286
+
287
+
/**
288
+
* Computes the variance of a strided array using a two-pass algorithm.
289
+
*
290
+
* ## Method
291
+
*
292
+
* - This implementation uses a two-pass approach, as suggested by Neely (1966).
293
+
*
294
+
* ## References
295
+
*
296
+
* - 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).
297
+
* - 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).
298
+
*
299
+
* @param {PositiveInteger} N - number of indexed elements
300
+
* @param {number} correction - degrees of freedom adjustment
301
+
* @param {Object} x - input array object
302
+
* @param {Collection} x.data - input array data
303
+
* @param {Array<Function>} x.accessors - array element accessors
304
+
* @param {integer} strideX - stride length
305
+
* @param {NonNegativeInteger} offsetX - starting index
306
+
* @returns {number} variance
307
+
*
308
+
* @example
309
+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
310
+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
311
+
*
312
+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
313
+
*
314
+
* var v = variancepn( 4, 1, arraylike2object( x ), 2, 1 );
315
+
* // returns 6.25
316
+
*/
317
+
function variancepn( N, correction, x, strideX, offsetX ) {
318
+
var xbuf;
319
+
var get;
320
+
var mu;
321
+
var ix;
322
+
var M2;
323
+
var M;
324
+
var d;
325
+
var n;
326
+
var i;
327
+
328
+
// Cache reference to array data:
329
+
xbuf = x.data;
330
+
331
+
// Cache a reference to the element accessor:
332
+
get = x.accessors[ 0 ];
333
+
334
+
// Compute an estimate for the mean:
335
+
mu = gsumpw( N, xbuf, strideX, offsetX ) / N;
336
+
337
+
n = N - correction;
338
+
ix = offsetX;
339
+
340
+
// Compute the variance...
341
+
M2 = 0.0;
342
+
M = 0.0;
343
+
344
+
for ( i = 0; i < N; i++ ) {
345
+
d = get( xbuf, ix ) - mu;
346
+
M2 += d * d;
347
+
M += d;
348
+
ix += strideX;
349
+
}
350
+
return (M2/n) - ((M/N)*(M/n));
351
+
}
352
+
353
+
354
+
// EXPORTS //
355
+
356
+
module.exports = variancepn;
357
+
</pre></td></tr></table></pre>
358
+
359
+
<divclass='push'></div><!-- for sticky footer -->
360
+
</div><!-- /wrapper -->
361
+
<divclass='footer quiet pad2 space-top1 center small'>
0 commit comments