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
* Computes the variance of a strided array using a one-pass trial mean algorithm.
222
292
*
293
+
* ## Method
294
+
*
295
+
* - This implementation uses a one-pass trial mean approach, as suggested by Chan et al (1983).
296
+
*
297
+
* ## References
298
+
*
299
+
* - 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).
300
+
* - 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).
301
+
* - 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).
302
+
* - 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).
303
+
*
304
+
* @private
223
305
* @param {PositiveInteger} N - number of indexed elements
224
306
* @param {number} correction - degrees of freedom adjustment
225
307
* @param {Object} x - input array object
226
-
* @param {Function} x.get - accessor function
227
-
* @param {integer} stride - stride length
228
-
* @param {NonNegativeInteger} offset - starting index
308
+
* @param {Collection} x.data - input array data
309
+
* @param {Array<Function>} x.accessors - array element accessors
310
+
* @param {integer} strideX - stride length
311
+
* @param {NonNegativeInteger} offsetX - starting index
229
312
* @returns {number} variance
313
+
*
314
+
* @example
315
+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
316
+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
317
+
*
318
+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
319
+
*
320
+
* var v = variancech( 4, 1, arraylike2object( x ), 2, 1 );
321
+
* // returns 6.25
230
322
*/
231
-
<spanclass="cstat-no" title="statement not covered" ><spanclass="fstat-no" title="function not covered" >function variancech(N, correction, x, stride, offset) {</span></span>
232
-
<spanclass="cstat-no" title="statement not covered" > var sum;</span>
233
-
<spanclass="cstat-no" title="statement not covered" > var ix;</span>
234
-
<spanclass="cstat-no" title="statement not covered" > var i;</span>
323
+
<spanclass="cstat-no" title="statement not covered" ><spanclass="fstat-no" title="function not covered" >function variancech( N, correction, x, strideX, offsetX ) {</span></span>
324
+
<spanclass="cstat-no" title="statement not covered" > var xbuf;</span>
325
+
<spanclass="cstat-no" title="statement not covered" > var get;</span>
326
+
<spanclass="cstat-no" title="statement not covered" > var mu;</span>
327
+
<spanclass="cstat-no" title="statement not covered" > var ix;</span>
328
+
<spanclass="cstat-no" title="statement not covered" > var M2;</span>
329
+
<spanclass="cstat-no" title="statement not covered" > var M;</span>
330
+
<spanclass="cstat-no" title="statement not covered" > var d;</span>
331
+
<spanclass="cstat-no" title="statement not covered" > var n;</span>
332
+
<spanclass="cstat-no" title="statement not covered" > var i;</span>
333
+
<spanclass="cstat-no" title="statement not covered" ></span>
334
+
<spanclass="cstat-no" title="statement not covered" > // Cache reference to array data:</span>
335
+
<spanclass="cstat-no" title="statement not covered" > xbuf = x.data;</span>
336
+
<spanclass="cstat-no" title="statement not covered" ></span>
337
+
<spanclass="cstat-no" title="statement not covered" > // Cache a reference to the element accessor:</span>
338
+
<spanclass="cstat-no" title="statement not covered" > get = x.accessors[ 0 ];</span>
235
339
<spanclass="cstat-no" title="statement not covered" ></span>
236
-
<spanclass="cstat-no" title="statement not covered" > if (N <= 0) {</span>
237
-
<spanclass="cstat-no" title="statement not covered" > return NaN;</span>
238
-
<spanclass="cstat-no" title="statement not covered" > }</span>
239
-
<spanclass="cstat-no" title="statement not covered" > if (N === 1 || stride === 0) {</span>
240
-
<spanclass="cstat-no" title="statement not covered" > return 0.0;</span>
241
-
<spanclass="cstat-no" title="statement not covered" > }</span>
242
-
<spanclass="cstat-no" title="statement not covered" > ix = offset;</span>
243
-
<spanclass="cstat-no" title="statement not covered" > sum = 0.0;</span>
340
+
<spanclass="cstat-no" title="statement not covered" > n = N - correction;</span>
341
+
<spanclass="cstat-no" title="statement not covered" > if ( N <= 0 || n <= 0.0 ) {</span>
342
+
<spanclass="cstat-no" title="statement not covered" > return NaN;</span>
343
+
<spanclass="cstat-no" title="statement not covered" > }</span>
344
+
<spanclass="cstat-no" title="statement not covered" > if ( N === 1 || strideX === 0 ) {</span>
345
+
<spanclass="cstat-no" title="statement not covered" > return 0.0;</span>
346
+
<spanclass="cstat-no" title="statement not covered" > }</span>
347
+
<spanclass="cstat-no" title="statement not covered" > ix = offsetX;</span>
244
348
<spanclass="cstat-no" title="statement not covered" ></span>
245
-
<spanclass="cstat-no" title="statement not covered" > // Calculate mean</span>
246
-
<spanclass="cstat-no" title="statement not covered" > for (i = 0; i < N; i++) {</span>
247
-
<spanclass="cstat-no" title="statement not covered" > sum += x.get(ix);</span>
248
-
<spanclass="cstat-no" title="statement not covered" > ix += stride;</span>
249
-
<spanclass="cstat-no" title="statement not covered" > }</span>
250
-
<spanclass="cstat-no" title="statement not covered" > var mean = sum / N;</span>
349
+
<spanclass="cstat-no" title="statement not covered" > // Use an estimate for the mean:</span>
350
+
<spanclass="cstat-no" title="statement not covered" > mu = get( xbuf, ix );</span>
351
+
<spanclass="cstat-no" title="statement not covered" > ix += strideX;</span>
251
352
<spanclass="cstat-no" title="statement not covered" ></span>
252
-
<spanclass="cstat-no" title="statement not covered" > // Calculate variance</span>
253
-
<spanclass="cstat-no" title="statement not covered" > ix = offset;</span>
254
-
<spanclass="cstat-no" title="statement not covered" > var sum2 = 0.0;</span>
255
-
<spanclass="cstat-no" title="statement not covered" > for (i = 0; i < N; i++) {</span>
256
-
<spanclass="cstat-no" title="statement not covered" > var delta = x.get(ix) - mean;</span>
257
-
<spanclass="cstat-no" title="statement not covered" > sum2 += delta * delta;</span>
258
-
<spanclass="cstat-no" title="statement not covered" > ix += stride;</span>
259
-
<spanclass="cstat-no" title="statement not covered" > }</span>
0 commit comments