time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

dvariancewd.c (3277B)


      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 #include "stdlib/stats/base/dvariancewd.h"
     20 #include <stdint.h>
     21 
     22 /**
     23 * Computes the variance of a double-precision floating-point strided array using Welford's algorithm.
     24 *
     25 * ## Method
     26 *
     27 * -   This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let
     28 *
     29 *     ```tex
     30 *     \begin{align*}
     31 *     S_n &= n \sigma_n^2 \\
     32 *         &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\
     33 *         &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2
     34 *     \end{align*}
     35 *     ```
     36 *
     37 *     Accordingly,
     38 *
     39 *     ```tex
     40 *     \begin{align*}
     41 *     S_n - S_{n-1} &= \sum_{i=1}^{n} x_i^2 - n\mu_n^2 - \sum_{i=1}^{n-1} x_i^2 + (n-1)\mu_{n-1}^2 \\
     42 *                   &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\
     43 *                   &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\
     44 *                   &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\
     45 *                   &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\
     46 *                   &= x_n^2 - \mu_{n-1}^2 + \mu_{n-1}^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
     47 *                   &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
     48 *                   &= (x_n - \mu_{n-1})(x_n - \mu_n) \\
     49 *                   &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n)
     50 *     \end{align*}
     51 *     ```
     52 *
     53 *     where we use the identity
     54 *
     55 *     ```tex
     56 *     x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1})
     57 *     ```
     58 *
     59 * ## References
     60 *
     61 * -   Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022).
     62 * -   van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961).
     63 *
     64 * @param N           number of indexed elements
     65 * @param correction  degrees of freedom adjustment
     66 * @param X           input array
     67 * @param stride      stride length
     68 * @return            output value
     69 */
     70 double stdlib_strided_dvariancewd( const int64_t N, const double correction, const double *X, const int64_t stride ) {
     71 	double delta;
     72 	int64_t ix;
     73 	int64_t i;
     74 	double mu;
     75 	double M2;
     76 	double n;
     77 	double v;
     78 
     79 	n = (double)N - correction;
     80 	if ( N <= 0 || n <= 0.0 ) {
     81 		return 0.0 / 0.0; // NaN
     82 	}
     83 	if ( N == 1 || stride == 0 ) {
     84 		return 0.0;
     85 	}
     86 	if ( stride < 0 ) {
     87 		ix = (1-N) * stride;
     88 	} else {
     89 		ix = 0;
     90 	}
     91 	M2 = 0.0;
     92 	mu = 0.0;
     93 	for ( i = 0; i < N; i++ ) {
     94 		v = X[ ix ];
     95 		delta = v - mu;
     96 		mu += delta / (double)(i+1);
     97 		M2 += delta * ( v - mu );
     98 		ix += stride;
     99 	}
    100 	return M2 / n;
    101 }