Written and maintained by CASRAI Editorial Board
Last updated
To find sample variance, subtract the sample mean from every value, square each difference, add the squares, and divide the total by n − 1, where n is the number of values. The result, written s², is the average squared distance from the mean, with the divisor reduced by one because the mean was estimated from the same data. The steps below work through a full example by hand, then show what Excel, R and Python each compute by default, since the three tools do not all give the same answer on the same range.
How to find sample variance: definition and formula
Sample variance measures how spread out the values in a sample are around their mean. A variance of zero means every value is identical; larger values mean wider spread. The NIST/SEMATECH e-Handbook of Statistical Methods defines it as:
s² = Σ(Yi − Y)² / (N − 1), summed from i = 1 to N.
- s²: the sample variance.
- Yi: the i-th observation.
- Y (Y-bar): the sample mean, the sum of all observations divided by N.
- N: the number of observations (written n elsewhere on this page).
- Σ: add up the terms for every observation.
The numerator is the sum of squared deviations from the mean; the sum of squares formula guide covers it on its own. The square root of s² is the sample standard deviation, s.
How to calculate sample variance step by step: worked example
Take a sample of five measurements: 5, 7, 3, 7, 10.
- Mean. 5 + 7 + 3 + 7 + 10 = 32, and 32 / 5 = 6.4.
- Deviations from the mean. 5 − 6.4 = −1.4; 7 − 6.4 = 0.6; 3 − 6.4 = −3.4; 7 − 6.4 = 0.6; 10 − 6.4 = 3.6.
- Square each deviation. 1.96, 0.36, 11.56, 0.36, 12.96.
- Add the squares. 1.96 + 0.36 + 11.56 + 0.36 + 12.96 = 27.2.
- Divide by n − 1. n − 1 = 4, so s² = 27.2 / 4 = 6.8.
- Optional: standard deviation. s = √6.8 = 2.608 (rounded to three decimals).
This is the answer to “how to compute the sample variance” and “how to calculate the sample variance” for any data set: only the numbers change, never the order of the steps. A quick sanity check is that the deviations from the mean always add to zero (−1.4 + 0.6 − 3.4 + 0.6 + 3.6 = 0), which catches most slips in step 1 or 2.
How to compute the sample variance in Excel, R or Python
Each tool’s documentation states which divisor it uses, so check the function name before trusting the number. All three snippets below return 6.8 for the worked example above.
Excel: VAR.S
Microsoft documents VAR.S as the function that “estimates variance based on a sample” and assumes its arguments are a sample of the population. Syntax: VAR.S(number1, [number2], …), with 1 to 254 arguments. It ignores logical values and text in a sample reference.
=VAR.S(A1:A5) ' 6.8 when A1:A5 holds 5, 7, 3, 7, 10
=VAR.P(A1:A5) ' 5.44, the population version (divisor n)
Microsoft’s own example range (1345, 1301, 1368, 1322, 1310, 1370, 1318, 1350, 1303, 1299) gives VAR.S = 754.27 and VAR.P = 678.84, which shows how much the choice of function matters even with ten values.
R: var()
The R manual states that var uses the denominator n − 1, “which gives an unbiased estimator of the (co)variance for i.i.d. observations.” R’s var() has no population option, so it always returns the sample variance.
x <- c(5, 7, 3, 7, 10)
var(x) # 6.8
var(x) * (length(x) - 1) / length(x) # 5.44, population variance
Python: statistics.variance
The Python standard library describes statistics.variance as the sample variance with Bessel’s correction, also known as variance with N − 1 degrees of freedom. It needs at least two values and raises a StatisticsError otherwise. statistics.pvariance divides by N instead.
import statistics
data = [5, 7, 3, 7, 10]
statistics.variance(data) # 6.8
statistics.pvariance(data) # 5.44
Why divide by n − 1?
The sample mean is computed from the same values it is then used to measure, so the deviations from it are slightly smaller, on average, than deviations from the true population mean. Dividing by n − 1 instead of n compensates for that. The Python documentation gives the practical statement: provided the data points are representative (for example independent and identically distributed), the result is an unbiased estimate of the true population variance. The R manual makes the same statement for i.i.d. observations. For a related use of the same divisor, see the “why divide by n − 1” discussion in the covariance formula guide.
Sample variance vs population variance
If the data set is the whole population, divide by N and you get the population variance. If the data are a sample used to estimate a wider population, divide by n − 1. On the worked example the two answers are 6.8 and 5.44. The gap shrinks as n grows, but with small samples it is large enough to change conclusions.
Interpretation: what the result means
| Result | How to read it |
|---|---|
| s² = 0 | Every value in the sample is the same. |
| Larger s² | Values lie farther from the mean on average; the data are more spread out. |
| Units of s² | The square of the data’s unit (centimetres give square centimetres), so s² cannot be read directly on the original scale. |
| Square root, s | Returns the spread to the original unit; use s to describe how far typical values sit from the mean (here 2.608 against a mean of 6.4). |
| Comparing two samples | Compare variances or standard deviations of the same quantity measured in the same unit; a variance by itself is not large or small without that context. |
Variance is the input to many later calculations, including the test statistic formula used in t tests, and the standard deviation feeds the normal and standard normal distribution. Spread is also the basis of rules for flagging unusual values, covered in how to find an outlier in statistics.
Assumptions and common mistakes
| Mistake or condition | What the documentation says |
|---|---|
| Using the population function on a sample (VAR.P, pvariance) | Python describes pvariance applied to a sample as “the biased sample variance,” with N degrees of freedom. Use VAR.S or variance for samples. |
| Fewer than two values | statistics.variance requires at least two values and raises an error otherwise; with one value there is no spread to estimate. |
| Text or logical values in an Excel range | VAR.S ignores them in a sample reference, so a column with text-formatted numbers silently uses fewer values than expected. Check the count with COUNT. |
| Expecting a population option in R | var() always divides by n − 1; rescale by (n − 1) / n if you need the population value. |
| Non-representative or dependent data | The unbiasedness statements in the Python and R documentation are conditional on representative or i.i.d. observations; the formula still computes a number without them, but the number no longer has that interpretation. |
| Rounding the mean early | Round only at the end; a rounded mean shifts every deviation. |
How to report sample variance
Report the sample size alongside the result and state whether you are giving a variance or a standard deviation. For the worked example:
“The sample (n = 5) had a mean of 6.4 and a sample variance of s² = 6.8 (s = 2.608).”
A standard deviation is often reported instead of (or beside) the variance because it is in the original units. Use the same number of decimals throughout, and name the software and function used (for example, “variance computed with R’s var(), divisor n − 1”) when the divisor could be in doubt. For reporting an interval around an estimate, see the confidence interval definition.
Frequently asked questions
How to find sample variance?
Compute the mean, subtract it from each value, square the differences, add them up, and divide by n − 1. For 5, 7, 3, 7, 10 the result is 27.2 / 4 = 6.8.
How to compute sample variance?
By hand, use the five steps above; in software use VAR.S in Excel, var() in R or statistics.variance() in Python, all of which divide by n − 1.
How to calculate sample variance?
Use s² = Σ(x − mean)² / (n − 1). The only inputs are the data values; n is how many there are.
How to find a sample variance when I only have a summary?
If you have the sample standard deviation s, square it: s² is the sample variance. If you have only the sum of squared deviations and n, divide the sum by n − 1.
How to find the sample variance in a spreadsheet?
Enter the values in one column and use =VAR.S over that range. Use VAR.P only if the column is the entire population.
How to calculate the sample variance by calculator?
Most scientific and statistical calculators report the sample standard deviation (often labelled s or sx) after you enter the data; square it to get the sample variance, and confirm the key is the n − 1 version, not the population one. The result for 5, 7, 3, 7, 10 should be 2.608, which squares to 6.8.
Is sample variance the same as variance?
No. “Variance” can mean the population variance (divisor N) or the sample variance (divisor n − 1). Software and textbooks differ in which they call the default, so check the function documentation. For the broader distinction between describing a data set and inferring beyond it, see descriptive statistics vs inferential statistics.
Sources
- NIST/SEMATECH e-Handbook of Statistical Methods, “Measures of Scale” (variance definition): itl.nist.gov/div898/handbook/eda/section3/eda356.htm, checked 2026-10-29.
- Python Software Foundation, statistics module documentation (variance, pvariance): docs.python.org/3/library/statistics.html, checked 2026-10-29.
- Microsoft Support, VAR.S function: support.microsoft.com, VAR.S function, checked 2026-10-29.
- R Core Team, R manual, stats package, cor/var: stat.ethz.ch/R-manual/R-devel/library/stats/html/cor.html, checked 2026-10-29.








