Data Science
Standard Deviation & Variance
You take one pass to find the mean, then one more pass for the squared differences — O(n) time, O(1) extra space.
The idea, in plain English
Think of a classroom full of kids' heights. The mean is just the average height. Standard deviation tells you how spread out everyone is around that average. A small standard deviation means most kids are close to average, with similar heights. A large one means heights vary a lot, from short to tall. Variance measures the same thing, one step earlier. It is the 'squared spread'. Take its square root and you get the standard deviation, back in normal units, like centimeters instead of centimeters-squared.
How it works
- 1Find the mean of all the numbers.
- 2For each number, find how far it is from the mean, then square that difference. Squaring makes every difference positive, so distances above and below the mean do not cancel out.
- 3Average all those squared differences. This is the variance. Take the square root of the variance to get the standard deviation, back in the original units.
When you'd use it
Use standard deviation to describe how consistent or volatile something is, such as exam scores, stock returns, or manufacturing tolerances. A weather forecaster comparing two cities' temperatures uses standard deviation to see which city has more unpredictable weather.
Common beginner mistakes
- Do not forget to square the differences. Without squaring, the positive and negative differences would just cancel out to zero.
- Do not confuse variance (squared units, hard to interpret directly) with standard deviation (the square root, back in original units). They are easy to mix up.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
Data: 2 4 4 4 5 5 7 9
Mean: 5.00
Variance: 4.00
Standard deviation: 2.00Not sure this is the right topic? See the learning paths → or where this leads →