Calculating Variance with Constant memory/Without using arrays

Viewed 444

I was wondering whether there is any way to calculate variance without storing the values in an array or something. I was able to do it for mean and was able to approximately calculate variance also but was not very accurate. here is the code i used:

#include <stdio.h>
#include <math.h>
int main()
{
    int n, temp;
    float sum = 0, crm = 0, var = 0;

    printf("Enter number of elements : ");
    scanf("%d", &n);

    for (int i = 0; i < n; i++)
    {
        printf("Enter number %d :", i + 1);
        scanf("%d", &temp);
        sum = sum + temp;
        crm = sum / ((float)i + 1);
        var = (var*i + (temp - crm) * (temp - crm) )/ ((float)i + 1);
    }
    printf("\naverage : %f,appprox variance :  %f\n", sum / (float)n, var);
}
2 Answers

In the loop you only need to calculate the sum and the sum of squared values. Then calculate the variance afterwards.

Like:

for (int i = 0; i < n; i++)
{
    printf("Enter number %d :", i + 1);
    scanf("%d", &temp);
    sum = sum + temp;
    ssum = ssum + temp * temp;
}
crm = sum / n;
var = ssum/n - crm * crm;

Note: The above code does not handle possible overflows

The variance of values xi is defined to be E((xi − E(xi))2), where E is the mean of its arguments1 (the “expected value”).

Then:

  • E((xi − E(xi))2) = E(xi2 − 2•xi•E(xi) + E(xi)2)
  • = E(xi2) − 2•E(xi)•E(xi) + E(xi)2
  • = E(xi2) − E(xi)2.

Therefore, to calculate the variance, you need to know the mean of the squares of the values and the square of the mean of the values. In turn, these can be calculated from the number of values, the sum of the values, and the sum of the squares of the values. Let these be n, s, and S, respectively.

Then E(xi2) − E(xi)2 = S/n − (s/n)2.

It is easy to accumulate n, s, and S incrementally after they are initialized to zero. As each value x is seen:

n += 1;
s += x;
S += x*x;

Footnote

1 E(yi) = the sum of the yi divided by the number of the yi.

Related