# Problem understanding Standard Deviation calculation

For a given dataset wm_t, the example for calculating standard deviation of the "gain" column is represented as:

sd_diff <- sqrt((sum(wm_t$gain^2) - ((sum(wm_t$gain))^2/n))/(n-1))


My problem is with the calculation in the numerator. Let's take away everything except for the numerator, leaving:

sum(wm_t$gain^2) - ((sum(wm_t$gain))^2/n)


So the definition for standard deviation of a sample is:

Sum(X-M)^2 / (n-1)


where X a given value in a dataset, M is the mean, and n is the sample size. Correct?

What they seem to be saying here in that above R excerpt, though, is that the sum(X-M)^2 can be expanded into:

X^2 - (sum(X)^2 / n)


How can you expand the (X-M)^2? I'm not understanding how this is possible, and especially cannot wrap my head around why you are only distributing the square to the numerator of the mean and not the denominator.

To explain via R, on the original definition of standard deviation above, why is the following code not correct?

mean_diff <- sum(wm_t$gain) / n sd <- sqrt(sum(wm_t$gain - mean_diff)^2 / (n-1))


This follows the original equation, whereas the example that they give (which is correct) seems to expand (X-M)^2. I cannot understand what is wrong with my approach.

I hope this is clear. Thank you!

• Please write your question in LaTeX notation. Here's a file to help you get started with that: meta.math.stackexchange.com/questions/5020/… – Chinmay Nirkhe Dec 31 '14 at 3:43
• Why is $\displaystyle\sum_{i=1}^n (x_i-\bar x)^2 = \left(\sum_{i=1}^n x_i^2\right) - \frac1n \left(\sum_{i=1}^n x_i \right)^2$, where $\displaystyle\bar x=\frac1n\sum_{i=1}^n x_i$? I think I've answered this one here a few times before. ${}\qquad{}$ – Michael Hardy Dec 31 '14 at 5:26