TL;DR
The degrees of freedom of a statistic are the number of pieces of information in the data that are still free to vary once the data have been used to estimate something. If you have values and you estimate their mean from those same values, only of the deviations from the mean can be chosen freely, because the last one is forced. That is why the sample variance divides by and why the and chi-squared distributions are indexed by a count that is usually the sample size minus something.
Builds on: Mean and Variance, Sampling Distribution · Leads to: Student's t-Distribution, Chi-Squared Distribution
Where it's used
Software defaults. When you compute a sample variance in statistical software, the function often asks which divisor to use. R's variance function divides by . NumPy's variance function divides by unless you pass a setting called ddof equal to 1, so the two can give different answers for the same numbers. Knowing which divisor you have is knowing which degrees of freedom you are using.
Small clinical trials. A study with 12 patients in one group and 10 in another compares the two means with a pooled variance in a two-sample t-test. The pooled estimate uses degrees of freedom, because each group's mean has been estimated from its own patients. The test then uses a t distribution with 20 degrees of freedom, as in the worked example below.
Regression. A linear regression with observations and predictors, plus an intercept, leaves degrees of freedom for the residuals. Each fitted coefficient uses up one degree of freedom, and the residual errors are what remains. Software reports this number next to every regression table, and it sets the width of the confidence intervals.
How it works
Trace the smallest case where the rule matters. You measure three values: 2, 4 and 6.
- Count the values. There are numbers. Before any estimate, each one could in principle take any value, so there are three free pieces of information. Check: 3 values, 3 pieces.
- Estimate the mean from the same data. The sample mean is . Check: 4 is in the middle of the three values, as it should be.
- Compute the deviations from the mean. They are , and . Check: these add to 0, as they must for any sample mean.
- Notice the constraint. Once you know the first two deviations, the third is forced, because the deviations must add to zero. If you choose and , the third must be . Check: only two deviations are free, which is .
- Measure the spread. The squared deviations are 4, 0 and 4, which total 8. Divide by the free count, 2, and the sample variance is . Check: dividing by 3 would give 2.67, which is smaller.
- Say why the smaller divisor is wrong. If the true mean were 5 instead, the squared deviations would be , which is larger than 8. The sample mean is the centre that makes the squared deviations as small as possible, so the spread around it is systematically too small. Dividing by the free count 2 corrects for that.
The rule that comes out of this is general: degrees of freedom equal the number of values minus the number of quantities that were estimated from them.
- n / (n - 1)
The math
When a statistic is computed from independent values and quantities are estimated from those same values, the degrees of freedom are . This is the number of values that remain free to vary after the estimated quantities are fixed.
Step 4 in symbols: the constraint
For values with sample mean , the deviations satisfy
This is one linear constraint. Any deviations determine the last one, so only are free. Check with the example: .
Step 5 in symbols: why the divisor is
Let be the true mean and the true variance. Split each squared deviation around into a part around and a correction. Using the constraint, the cross term vanishes, and
Take expectations. The left side has expectation . The last term has expectation , because the sampling distribution of has variance . So the expected sum of squares about the sample mean is . Dividing by therefore gives an average that equals . Dividing by would give , which is too small.
Beyond the walkthrough: the distributions that use the count
With and normal data,
The chi-squared distribution and the distribution are both indexed by these degrees of freedom. A smaller count means heavier tails in the case, because the estimated spread is noisier.
The general count
For a chi-squared goodness-of-fit test on categories, the expected counts come from a model with parameters estimated from the same counts. The degrees of freedom are . The "minus 1" is the fixed total, and the "minus " is the cost of estimating the model.
Worked examples
Data: 1 and 3.
- Mean: .
- Deviations: and . Check: they add to 0.
- Degrees of freedom: . Once the first deviation is , the second must be .
- Sum of squared deviations: .
- Sample variance: .
Interpretation: with two values, there is only one free piece of spread information. The estimated spread rests on a single number, which is why small samples give unstable variance estimates.
A chemist measures the purity of a compound five times and gets 98, 100, 102, 99 and 101.
- Mean: .
- Deviations: . Check: they add to 0.
- Sum of squared deviations: .
- Degrees of freedom: .
- Sample variance: . The standard deviation is .
- Comparison: dividing by 5 would give , which understates the spread by a fifth.
Interpretation: the four free deviations carry all the information about spread, and the fifth is determined by the others. The sample variance is 2.5, and the honest standard deviation is about 1.58.
A trial has 12 patients in a treatment group and 10 in a control group. The sums of squared deviations are 66 in the treatment group and 54 in the control group.
- Each group's mean is estimated from its own patients, so the pooled degrees of freedom are .
- Pooled variance: .
- Standard error of the difference in means: .
- If the difference in means is 3.0 units, the statistic is .
- The two-sided 5 percent critical value for with 20 degrees of freedom is about 2.086, and . The difference is significant at that level.
- Interpretation: the 20 degrees of freedom are the 22 patients minus the 2 group means. Using 22 would have made the test too confident.
Try it: a regression uses 30 observations and 3 predictors, plus an intercept. What are the residual degrees of freedom? Answer: , because the intercept counts as one of the estimated quantities.
Common mistakes and tradeoffs
It is tempting to think that degrees of freedom are just the sample size. In fact they are the sample size minus the number of quantities estimated from the same data. Ten values with an estimated mean have 9 degrees of freedom, not 10.
It is tempting to think the correction from to matters for every sample. In fact it matters most for small samples. The correction factor is 2 at , about 1.25 at , and about 1.05 at . For large samples the difference is negligible, and the choice of divisor hardly changes any conclusion.
It is tempting to think that the divisor is always . In fact if the true mean is known and not estimated, the sum of squares about has expectation , so dividing by is unbiased. The lost degree of freedom comes only from estimating the mean from the same data.
It is tempting to treat degrees of freedom as a free number you can fill in from the sample. In fact the correct count depends on the model. A test that ignores an estimated parameter will report too many degrees of freedom and too small a p-value, so the count should be reported with the test.
When to use the correction: whenever a spread or a test statistic is computed from the same data that estimated the mean or the model. When not to use it: when the reference value is known in advance, or when you rely on resampling. A bootstrap estimates the spread directly from the data and avoids most degrees-of-freedom bookkeeping, at the cost of more computation.
History
Gosset's 1908 paper on the [[student-t-distribution| distribution]] showed that the distribution of a standardised mean with an estimated spread depends on the sample size, and it was Ronald Fisher who put the idea on a rigorous footing in the 1920s. Fisher showed that the sample variance has degrees of freedom for a single sample, and that the goodness-of-fit statistic loses one degree of freedom for each parameter estimated from the counts. The name "degrees of freedom" is usually credited to Fisher for this use.
The correction itself is often called Bessel's correction, after the astronomer Friedrich Bessel, who used the divisor in his work on errors in the early nineteenth century. Statisticians came to see the count as a general principle, and it caught on because it made tests and intervals behave correctly for small samples, where the old large-sample formulas were too confident.
Recap
- Degrees of freedom are the number of values that remain free after the data have been used to estimate something.
- Estimating the mean from the data removes one degree of freedom, so the sample variance divides by .
- The divisor is needed because the sum of squared deviations about the sample mean is smaller than about any other centre.
- The and chi-squared distributions use these counts, and each estimated parameter reduces the count further.
- Read next: Student's t-Distribution for small-sample means, and Chi-Squared Distribution for variances and goodness-of-fit tests.