Skip to main content

How to Calculate Standard Deviation: Statistics Guide

Dr. Michael Chen · 2024-04-06 · 12 min read · Mathematics

Standard deviation measures how far data spreads from the mean. This guide covers the population and sample formulas, why the sample version divides by n−1, a full worked example, how to interpret the result, and when standard deviation is the wrong tool.

How to Calculate Standard Deviation

Standard deviation answers a question the average cannot: how spread out is the data?

Two datasets can share an identical mean and describe completely different realities. Consider two bus routes, each averaging a 10-minute wait. On the first, every wait is between 9 and 11 minutes. On the second, waits swing between 2 and 25 minutes. The average says they are the same service. Standard deviation says one is reliable and the other is not.

That is the entire purpose of the measure: it summarises how far, on average, individual values sit from the mean.

What standard deviation actually measures

Standard deviation is the typical distance between a data point and the mean of its dataset.

A small standard deviation means values cluster tightly around the mean. A large one means they scatter widely. A standard deviation of zero means every value is identical — there is no spread at all.

The units matter and are frequently forgotten. If you measure heights in centimetres, the standard deviation is in centimetres. This is precisely why standard deviation is preferred over variance in most reporting: variance is in squared units, and "the variance of height is 36 square centimetres" means nothing intuitive to a reader.

The two formulas, and why the difference matters

This is the single most common source of confusion, and it produces genuinely different numbers.

Population standard deviation

Use this when your data covers every member of the group you care about.

\sigma = √(\fracΣ(x_i - \mu)^2)N

Where \sigma (sigma) is the population standard deviation, \mu (mu) is the population mean, x_i is each individual value, and N is the total count.

Sample standard deviation

Use this when your data is a subset drawn from a larger population — which, in practice, is almost always the case.

s = √((Σ(x_i - \barx))^2)/(n - 1)

Where s is the sample standard deviation, \barx (x-bar) is the sample mean, and n is the sample size.

The only difference is the denominator: N versus n - 1.

Why divide by n − 1?

This is called Bessel's correction, and the reasoning is worth understanding rather than memorising.

When you calculate deviations from the sample mean rather than the true population mean, you are measuring distances from a value that was itself computed from that same data. The sample mean sits, by construction, at the exact centre of your sample. It is the value that minimises the sum of squared deviations.

That means the deviations you measure are systematically slightly too small. Any other value — including the true population mean — would produce a larger sum of squared deviations.

The result is bias: dividing by n would consistently underestimate the population's true spread. Dividing by the smaller number n - 1 inflates the result just enough to correct for it.

The correction matters most for small samples. With n = 5, dividing by 4 instead of 5 increases the variance by 25%. With n = 1000, dividing by 999 changes it by 0.1% — negligible. This is why the distinction is critical in a lab with ten measurements and nearly irrelevant in a dataset of a million rows.

A complete worked example

Take the dataset: 2, 4, 4, 4, 5, 5, 7, 9

Step 1 — Find the mean

\barx = (2 + 4 + 4 + 4 + 5 + 5 + 7 + 9)/(8) = (40)/(8) = 5

Step 2 — Find each deviation from the mean

Note that the deviations sum to zero: −3 −1 −1 −1 + 0 + 0 + 2 + 4 = 0. This is always true, and it is exactly why we square them. Without squaring, positive and negative deviations would cancel and every dataset would appear to have zero spread.

Step 3 — Sum the squared deviations

Σ(x_i - \barx)^2 = 9 + 1 + 1 + 1 + 0 + 0 + 4 + 16 = 32

Step 4 — Divide, then take the square root

As a population (these eight values are the entire group):

\sigma = √(\frac32)8 = √(4) = 2

As a sample (these eight are drawn from something larger):

s = √(\frac32)7 = √(4.571) ≈ 2.138

Same data, two legitimate answers. Reporting one when you meant the other is a real error, not a rounding difference — here it is a 7% gap.

A second example: when the choice changes the conclusion

The distinction between the two formulas is easiest to dismiss until it changes a decision.

A quality inspector measures the fill volume of six bottles from a production line, in millilitres: 498, 502, 499, 501, 500, 500.

The mean is (3000)/(6) = 500 ml. The squared deviations are 4, 4, 1, 1, 0, 0, summing to 10.

Treating the six bottles as the whole population:

\sigma = √(\frac10)6 = √(1.667) ≈ 1.29 \text ml

Treating them as a sample of the production run:

s = √(\frac10)5 = √(2) ≈ 1.41 \text ml

Now suppose the specification requires that virtually all bottles fall within ±4 ml of 500 ml. Using the three-sigma rule, the population figure predicts a spread of ±3.87 ml — inside specification. The sample figure predicts ±4.24 ml — outside it.

Same six measurements. One says the line is compliant, the other says it is not. Since the inspector clearly cares about the entire production run rather than these six bottles specifically, the sample formula is the correct choice, and the honest conclusion is that the process needs attention.

Computing it in software

Nobody calculates standard deviation by hand at scale. What matters is knowing which function you are calling.

Excel and Google Sheets

The older =STDEV() is an alias for STDEV.S and is retained for backwards compatibility. STDEVA and STDEVPA additionally treat text and logical values as zeros, which is rarely what you want.

Python

With NumPy the default is the opposite of most people's expectation:

This catches people out regularly. NumPy's ddof ("delta degrees of freedom") defaults to 0, giving the population formula, whereas Python's built-in statistics.stdev gives the sample formula. Pandas, meanwhile, defaults to ddof=1, so df.std() returns the sample standard deviation. Three libraries in the same language, two different defaults.

R

R's sd() provides no population option at all; it always divides by n - 1. For the population figure you multiply by √((n-1)/n).

Standard deviation versus standard error

These two are routinely confused, and they answer different questions.

Standard deviation describes the spread of individual observations. It answers: how much do the data points vary?

Standard error of the mean describes the precision of your estimate of the mean. It answers: how confident am I about where the average lies?

SE = (s)/(√(n))

If you measure 100 people's heights and find a mean of 175 cm with a standard deviation of 7 cm, the standard error is 7 / √(100) = 0.7 cm. Individual heights vary by about 7 cm; your estimate of the average height is precise to within roughly 0.7 cm.

Collect more data and the standard error shrinks, because you pin the mean down more tightly. The standard deviation does not shrink — people do not become more similar in height because you measured more of them.

This distinction drives confidence intervals. A 95% confidence interval for the mean is approximately \barx ± 1.96 × SE, not \barx ± 1.96 × s. Using the standard deviation where the standard error belongs produces an interval roughly ten times too wide for a sample of 100.

Interpreting the number you get

A standard deviation on its own is not meaningful until you compare it to something.

The empirical rule

For data that is roughly normally distributed (the familiar bell curve):

  • About 68% of values fall within 1 standard deviation of the mean
  • About 95% fall within 2 standard deviations
  • About 99.7% fall within 3 standard deviations

If adult male height has a mean of 175 cm and a standard deviation of 7 cm, then roughly 68% of men are between 168 and 182 cm, and about 95% are between 161 and 189 cm.

This rule only holds for approximately normal data. Applied to skewed data — income, house prices, waiting times — it will mislead you badly.

Coefficient of variation

To compare spread between datasets with different units or wildly different scales, use the coefficient of variation:

CV = (s)/(\barx) × 100\%

A standard deviation of 5 is enormous for a dataset averaging 10, and trivial for one averaging 10,000. The CV expresses spread as a percentage of the mean, making the comparison fair.

Z-scores

A z-score expresses how many standard deviations a single value sits from the mean:

z = \fracx - \barxs

A z-score of 2.5 means the value is 2.5 standard deviations above average — unusual, but not extraordinary. A z-score beyond ±3 is genuinely rare in normal data.

Common mistakes

Using the population formula for sample data. The default assumption should be that you have a sample. You almost never measure every member of a population. Spreadsheet software makes this trap easy to fall into: in Excel and Google Sheets, STDEV.P is the population formula and STDEV.S is the sample formula. Choosing the wrong one silently produces a wrong answer.

Forgetting to square-root. Stopping at the variance is a frequent slip. Variance is the intermediate step; standard deviation is the square root of it.

Averaging the raw deviations instead of the squared ones. They sum to zero by definition. This is the error that motivates the entire squaring step.

Comparing standard deviations across different units. A standard deviation of 3 kg and one of 3 cm are not comparable quantities. Use the coefficient of variation.

Applying the empirical rule to skewed data. In a strongly right-skewed distribution such as household income, "mean minus two standard deviations" can produce a negative number — which is obviously meaningless for income.

Letting outliers drive the result. Standard deviation squares every deviation, so a single extreme value has an outsized effect. One data-entry error of 5000 instead of 50 can dominate the entire calculation.

Where standard deviation is used in practice

Finance. Volatility is standard deviation applied to returns. An asset whose annual returns have a standard deviation of 20% is substantially riskier than one at 5%, even if both average the same return. Portfolio theory is built on this measure.

Manufacturing and quality control. Process capability indices compare the allowed tolerance against the process standard deviation. The phrase "six sigma" refers literally to fitting six standard deviations between the process mean and the nearest specification limit.

Research and medicine. Standard deviation feeds directly into standard error, confidence intervals and hypothesis tests. It determines whether an observed difference between a treatment group and a control group is plausibly real or plausibly chance.

Weather and climate. Describing a month as unusually warm requires knowing the typical variation for that month, not just the long-run average.

Education. Standardised test scores are frequently reported in standard-deviation units precisely so that results from different tests can be compared.

Related measures, and when to prefer them

Standard deviation is not always the right summary of spread.

Variance (s^2) is the standard deviation before the square root. It is mathematically convenient — variances of independent variables add together, standard deviations do not — but its squared units make it awkward to report.

Range is simply the maximum minus the minimum. Trivial to compute, but it uses only two data points and is entirely determined by the most extreme values.

Interquartile range (IQR) is the spread of the middle 50% of the data. Because it ignores the tails, it is far more robust to outliers, which is why box plots use it. For skewed data such as salaries or response times, median and IQR usually describe reality better than mean and standard deviation.

Mean absolute deviation (MAD) averages the absolute deviations instead of squaring them. It is more intuitive and less outlier-sensitive, but it lacks the mathematical properties that make standard deviation central to statistical theory.

The practical rule: use mean and standard deviation for roughly symmetric data, and median and IQR for skewed data or data with meaningful outliers.

Frequently asked questions

Can standard deviation be negative? No. It is a square root of a sum of squares, so it is always zero or positive. A negative result means an arithmetic error. Zero means every value in the dataset is identical.

What is a "good" standard deviation? There is no universal threshold — it depends entirely on context and units. In precision manufacturing, a standard deviation of 0.01 mm might be unacceptably large. In a survey of household incomes, a standard deviation of £10,000 is unremarkable. Compare it against the mean using the coefficient of variation, or against a historical benchmark for the same process.

Should I use n or n−1? Use n - 1 unless your data genuinely covers the entire population. If you measured every student in one specific classroom and only care about that classroom, use n. If those students represent students generally, use n - 1. When uncertain, use n - 1: it is the conservative choice, giving a slightly larger and therefore safer estimate of spread.

Why square the deviations rather than take absolute values? Two reasons. Squaring eliminates the sign problem so deviations do not cancel to zero, and squared terms are differentiable, which makes the measure tractable in the calculus underlying regression, ANOVA and most of inferential statistics. Mean absolute deviation does exist and is perfectly valid — it is simply less mathematically convenient.

How does sample size affect standard deviation? The standard deviation itself estimates a fixed property of the population and does not systematically shrink as the sample grows. What shrinks is the standard error of the mean, s / √(n), which measures how precisely you have located the mean. Larger samples make you more confident about the average, not about individual variability.

What if my data has outliers? Investigate them before deciding. If an outlier is a data-entry error, correct or remove it. If it is a real observation, removing it is a substantive decision you should document. Consider reporting the median and IQR alongside, or both figures with and without the outlier so readers can judge for themselves.

Summary

Standard deviation converts a dataset's scatter into one interpretable number in the original units. Calculate the mean, square each deviation from it, average those squares — dividing by n - 1 for a sample and N for a population — and take the square root.

The distinction between the sample and population formulas is not academic pedantry. It changes the answer, and on small datasets it changes it substantially.

Check your working with our Standard Deviation Calculator, which reports both the sample and population values so you can see the difference directly.

Topics: standard deviation, statistics, variance, data analysis, bessel correction