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");
262
+
* you may not use this file except in compliance with the License.
263
+
* You may obtain a copy of the License at
264
+
*
265
+
* http://www.apache.org/licenses/LICENSE-2.0
266
+
*
267
+
* Unless required by applicable law or agreed to in writing, software
268
+
* distributed under the License is distributed on an "AS IS" BASIS,
269
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
270
+
* See the License for the specific language governing permissions and
271
+
* limitations under the License.
272
+
*/
273
+
274
+
'use strict';
275
+
276
+
// MAIN //
277
+
278
+
/**
279
+
* Computes the variance of a strided array using a one-pass trial mean algorithm.
280
+
*
281
+
* ## Method
282
+
*
283
+
* - This implementation uses a one-pass trial mean approach, as suggested by Chan et al (1983).
284
+
*
285
+
* ## References
286
+
*
287
+
* - 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).
288
+
* - Ling, Robert F. 1974. "Comparison of Several Algorithms for Computing Sample Means and Variances." _Journal of the American Statistical Association_ 69 (348). American Statistical Association, Taylor & Francis, Ltd.: 859–66. doi:[10.2307/2286154](https://doi.org/10.2307/2286154).
289
+
* - Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. 1983. "Algorithms for Computing the Sample Variance: Analysis and Recommendations." _The American Statistician_ 37 (3). American Statistical Association, Taylor & Francis, Ltd.: 242–47. doi:[10.1080/00031305.1983.10483115](https://doi.org/10.1080/00031305.1983.10483115).
290
+
* - 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).
291
+
*
292
+
* @private
293
+
* @param {PositiveInteger} N - number of indexed elements
294
+
* @param {number} correction - degrees of freedom adjustment
295
+
* @param {Object} x - input array object
296
+
* @param {Collection} x.data - input array data
297
+
* @param {Array<Function>} x.accessors - array element accessors
298
+
* @param {integer} strideX - stride length
299
+
* @param {NonNegativeInteger} offsetX - starting index
300
+
* @returns {number} variance
301
+
*
302
+
* @example
303
+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
304
+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
305
+
*
306
+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
307
+
*
308
+
* var v = variancech( 4, 1, arraylike2object( x ), 2, 1 );
309
+
* // returns 6.25
310
+
*/
311
+
function variancech( N, correction, x, strideX, offsetX ) {
312
+
var xbuf;
313
+
var get;
314
+
var mu;
315
+
var ix;
316
+
var M2;
317
+
var M;
318
+
var d;
319
+
var n;
320
+
var i;
321
+
322
+
// Cache reference to array data:
323
+
xbuf = x.data;
324
+
325
+
// Cache a reference to the element accessor:
326
+
get = x.accessors[ 0 ];
327
+
328
+
n = N - correction;
329
+
ix = offsetX;
330
+
331
+
// Use an estimate for the mean:
332
+
mu = get( xbuf, ix );
333
+
ix += strideX;
334
+
335
+
// Compute the variance...
336
+
M2 = 0.0;
337
+
M = 0.0;
338
+
for ( i = 1; i < N; i++ ) {
339
+
d = get( xbuf, ix ) - mu;
340
+
M2 += d * d;
341
+
M += d;
342
+
ix += strideX;
343
+
}
344
+
return (M2/n) - ((M/N)*(M/n));
345
+
}
346
+
347
+
348
+
// EXPORTS //
349
+
350
+
module.exports = variancech;
351
+
</pre></td></tr></table></pre>
352
+
353
+
<divclass='push'></div><!-- for sticky footer -->
354
+
</div><!-- /wrapper -->
355
+
<divclass='footer quiet pad2 space-top1 center small'>
0 commit comments