# How to Calculate Variance: Formula, Steps and Examples

To calculate variance, subtract the mean from each value, square each deviation, add the squared deviations, then divide by N for a population or n - 1 for a sample. The result tells you how spread out the data is around the mean. This guide shows how to calculate variance by hand, with a calculator, and in code, using one dataset throughout.

## Quick Answer

- Variance measures the average squared distance of each value from the mean.
- Population variance: $\sigma^2 = \frac{\sum (x_i - \mu)^2}{N}$, divide by the number of values.
- Sample variance: $s^2 = \frac{\sum (x_i - \bar{x})^2}{n - 1}$, divide by one less than the number of values.
- The numerator, the sum of squared deviations, is the same for both. Only the denominator changes.
- Variance is in squared units, so take the square root to get the standard deviation.

## Before You Start

You need one column of numeric values and a decision about whether it is a sample or a population. A population is every member of the group you care about. A sample is a subset drawn from a larger group. In practice, most datasets are samples, so you divide by n - 1.

You also need the mean first, because every deviation is measured from it. If you want a refresher, see how to calculate the mean before continuing.

Two formulas describe the same idea. The definitional formula uses deviations from the mean, which is what this article walks through. The computational formula uses raw sums of squares and is faster on a calculator, but it is more prone to rounding error. The NIST handbook shows both styles in its ANOVA calculations, where sums of squares are built from raw values and then corrected for the mean [1].

## Step by Step

1. **Count your values.** Call the count N for a population or n for a sample.
2. **Find the mean.** Add every value and divide by the count.
3. **Subtract the mean from each value.** These are the deviations. They always sum to zero, which is a useful check.
4. **Square each deviation.** Squaring removes the sign and makes large gaps count more.
5. **Add the squared deviations.** This total is the sum of squares.
6. **Divide.** Use N for population variance or n - 1 for sample variance.

In symbols, the population formula is:

$$\sigma^2 = \frac{\sum_{i=1}^{N}(x_i - \mu)^2}{N}$$

The sample formula is:

$$s^2 = \frac{\sum_{i=1}^{n}(x_i - \bar{x})^2}{n - 1}$$

The n - 1 denominator is called Bessel's correction. Dividing by n - 1 makes the sample variance an unbiased estimate of the population variance, because a sample tends to be less spread out than the population it came from.

## Worked Example

Five lab measurements of a sample concentration, in mg/L, are 12.1, 11.8, 12.4, 11.9, and 12.3.

| Value | Deviation from mean (12.1000) | Squared deviation |
|---|---|---|
| 12.1 | 0.0000 | 0.0000 |
| 11.8 | -0.3000 | 0.0900 |
| 12.4 | 0.3000 | 0.0900 |
| 11.9 | -0.2000 | 0.0400 |
| 12.3 | 0.2000 | 0.0400 |

The mean is (12.1 + 11.8 + 12.4 + 11.9 + 12.3) / 5 = 12.1000.

The squared deviations add to 0.0000 + 0.0900 + 0.0900 + 0.0400 + 0.0400 = 0.2600.

Population variance: 0.2600 / 5 = 0.0520.

Sample variance: 0.2600 / 4 = 0.0650.

The population standard deviation is sqrt(0.0520) = 0.2280, and the sample standard deviation is sqrt(0.0650) = 0.2550. Notice how the deviations cancel to zero before squaring, which confirms the mean was correct.

Here is the same calculation in Python:

```python
import numpy as np
data = [12.1, 11.8, 12.4, 11.9, 12.3]
print(f"numpy ddof=0 = {np.var(data, ddof=0):.4f}")  # population variance
print(f"numpy ddof=1 = {np.var(data, ddof=1):.4f}")  # sample variance
```

Output:

```text
numpy ddof=0 = 0.0520
numpy ddof=1 = 0.0650
```

The `ddof` argument sets the delta degrees of freedom. `ddof=0` divides by N, and `ddof=1` divides by n - 1.

## Other Ways to Do It

**Spreadsheet.** In Excel, `VAR.P` returns population variance and `VAR.S` returns sample variance. The full walkthrough is in how to calculate variance in Excel.

**Calculator.** Many scientific and graphing calculators have a statistics mode that reports both values. You enter the data list, run the one-variable statistics command, and read the population and sample results. The labels differ by model, so check which symbol the display uses before you record the answer. If you want to skip the manual entry, the variance calculator returns both figures from a pasted list.

**By hand with the computational formula.** Square every value, add them, subtract the correction term $(\sum x)^2 / n$, then divide. This avoids computing each deviation separately but loses precision when values are large.

## Troubleshooting

If your deviations do not sum to zero, the mean is wrong. Recheck the addition.

If the variance is negative, you made an arithmetic error. Squared numbers cannot be negative, so the sum of squares is always zero or positive.

If the sample and population answers look far apart, the sample is small. With n = 5, dividing by 4 instead of 5 raises the result by 25 percent. The gap shrinks as n grows.

If a calculator gives a different answer, check whether it is reporting the standard deviation instead of the variance. The standard deviation is the square root of the variance, so it is always smaller for values above 1.

If your data has units, the variance carries squared units. A variance of 0.0520 in mg/L data is in mg²/L², which is why analysts usually report the standard deviation instead.

## Common Mistakes

- **Dividing by n when you have a sample.** Use n - 1 for sample variance. Dividing by n underestimates the spread of the wider population.
- **Forgetting to square before adding.** Adding raw deviations gives zero every time. Square first, then sum.
- **Squaring after summing.** The order matters. Each deviation is squared individually, then the squares are added.
- **Rounding the mean too early.** Keep at least four decimal places in the mean, or the squared deviations drift. In the example, the mean is exactly 12.1000, but that is not typical.
- **Confusing variance with standard deviation.** Variance is in squared units. If you need the original units, take the square root. See how to calculate standard deviation.
- **Mixing up the two symbols.** $\sigma^2$ is the population variance and $s^2$ is the sample variance. Using the wrong one changes the reported result.

## Limitations

Variance is sensitive to outliers because deviations are squared. One extreme value can dominate the sum of squares and make the spread look much larger than it is. If your data has heavy tails or extreme values, report the median and interquartile range alongside the variance.

Variance also assumes the values are measured on an interval or ratio scale. It is meaningless for categorical data, and it says nothing about the shape of the distribution. Two datasets can share a variance while looking completely different. The equal-variance assumption matters in tests like ANOVA, where the pooled variance is used and unequal group variances can distort the result [2]. When group sizes are roughly equal, the F-test stays valid if the largest variance is no more than four times the smallest [2].

## Frequently Asked Questions

### What is the difference between sample and population variance?

Population variance divides the sum of squared deviations by N, the full count. Sample variance divides by n - 1, one less than the count. Use population variance when your data covers the entire group. Use sample variance when your data is a subset and you want to estimate the wider group.

### Why do you divide by n - 1 for sample variance?

Dividing by n - 1 corrects for the fact that a sample is usually less spread out than its population. The sample mean sits closer to the sample values than the population mean does, so dividing by n would understate the true variance. The correction makes the sample variance an unbiased estimator.

### Can variance be negative?

No. Every squared deviation is zero or positive, so the sum of squares is never negative. If you get a negative variance, you made an arithmetic or data entry error. Check for a value that was subtracted instead of squared.

### How do I find variance on a calculator?

Enter your values into the statistics or list mode, then run the one-variable statistics function. The display shows the sample and population results, often labeled with $s$ and $\sigma$ symbols. The variance is the square of the standard deviation shown, so square the reported value if your model only lists standard deviation.

### What does a high variance mean?

A high variance means the values are spread far from the mean. A low variance means they cluster tightly around it. The number is only meaningful next to the scale of the data, so compare it with the mean or with the variance of a similar dataset. For related measures, see the sample variance equation and covariance formula.

## References

1. [7.4.3.4. 1-Way ANOVA calculations](https://www.itl.nist.gov/div898/handbook/prc/section4/prc434.htm)
2. [Analysis of Variance](https://www2.stat.duke.edu/courses/Fall19/sta210.001/slides/lec-slides/06-anova.html)

## Further Reading

- [11.2: Introduction to ANOVA's Sum of Squares - Statistics LibreTexts](https://stats.libretexts.org/Courses/Taft_College/PSYC_2200%3A_Elementary_Statistics_for_Behavioral_and_Social_Sciences_(Oja)/02%3A_Mean_Differences/11%3A_BG_ANOVA/11.02%3A_Introduction_to_ANOVA's_Sum_of_Squares)
- [12: One-way Analysis of Variance - Statistics LibreTexts](https://stats.libretexts.org/Bookshelves/Applied_Statistics/Mikes_Biostatistics_Book_(Dohm)/12%3A_One-way_Analysis_of_Variance)
- [NIST/SEMATECH e-Handbook of Statistical Methods](https://www.itl.nist.gov/div898/handbook/index.htm)
- [OpenStax. Introductory Statistics 2e](https://openstax.org/details/books/introductory-statistics-2e)

## Related Articles

- [How to Calculate Variance in Excel (Step by Step)](/blog/data-analysis/how-to-calculate-variance-in-excel)
- [Sample Variance Equation: Formula, Steps and Examples](/blog/data-analysis/sample-variance-equation-formula-examples)
- [How to Calculate the Mean: Formula and Step by Step Examples](/blog/data-analysis/how-to-calculate-the-mean)
- [How to Calculate Standard Deviation: Formula and Steps](/blog/data-analysis/how-to-calculate-standard-deviation)
- [How to Calculate Midrange: Formula and Examples](/blog/data-analysis/how-to-calculate-midrange)
- [Test Statistic Formula: How to Calculate and Use It](/blog/guides/test-statistic-formula-how-to-calculate-and-use-it)