# Finding the variance, summing up the difference between the mean and each value of X is not outputting zero as it should

I am new to stats, I am trying to find the variance of a very simple set of numbers, Z = { -2, 4, 7 }, I calculated the mean and I Got 9, the sum of each Z(i) the Iteration and the mean not 0, but 4 instead as per my calculations, could someone tell me what could have gone wrong, plus, I would appreciate if somebody put a link in a comment on how to use mathematical symbols in questions and comments, thanks in advance.

• How could you reasonably assert that the mean--the best-known form of average--lies outside the range of the data?? – whuber Nov 7 '18 at 20:02

## 1 Answer

As you begin to understand how to compute the sample variance, I suggest you make a table that corresponds with the formula $$S^2 = \frac{\sum_{i=1}^n(X_i - \bar X)^2}{n-1}.$$

To start you know $$n = 3.$$ Also $$\bar X = \frac{\sum_{i=1}^3 X_i}{n} = \frac 9 3 = 3.$$

  i     X
-------
1    -2
2     4
n=3     7
-------
Total   9


Now to finish the formula for the variance, you need a column for 'deviations from the mean', which are $$D_i = X_i = \bar X.$$ That column will always sum to $$0.$$

Also, you need a column for 'squared deviations from the mean', which are $$Q_i = D_i^2 = (X_i - \bar X)^2.$$ The total of that column will be the numerator of the sample variance.

  i     X            D         Q
-------------------------------
1    -2    -2-3 = -5        25
2     4     4-3 =  1         1
n=3     7     7-3 =  4        16
-------------------------------
Total   9            0        42


So $$S^2 = \frac{42}{3-1} = 21.$$

Note: In R statistical software, the computation of the sample variance looks like this, where x is a vector (list) of the three data values:

x = c(-2, 4, 7)
var(x)
 21


I don't know whether you're interested in learning something about R statistical software. If you want to illustrate the individual steps performed by the function var, then you might do something like what is shown below, where I use a as a symbol for the sample mean (or average) $$\bar X.$$ (For now, ignore the numbers in brackets [ ]'s.)

x = c(-2, 4, 7)
a = mean(x);  a
 3
x-a;  sum(x-a)
 -5  1  4
 0
sum((x-a)^2)
 42
sum((x-a)^2)/(length(x)-1)
 21
cbind(x, d=x-a, q=(x-a)^2)
x  d  q
[1,] -2 -5 25
[2,]  4  1  1
[3,]  7  4 16