Search

Search papers and lessons

Degrees of Freedom (Statistics)

Level 2 · 11 min read

Degrees of freedom count the values that are still free to vary after the data have been used to estimate something, which is why a sample variance divides by n minus 1.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Inference
  • Sampling
  • ReadReady to readNot yet

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 nn values and you estimate their mean from those same values, only n−1n - 1 of the deviations from the mean can be chosen freely, because the last one is forced. That is why the sample variance divides by n−1n - 1 and why the tt 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 n−1n - 1. NumPy's variance function divides by nn 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 12−1+10−1=2012 - 1 + 10 - 1 = 20 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 nn observations and pp predictors, plus an intercept, leaves n−p−1n - p - 1 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.

  1. Count the values. There are n=3n = 3 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.
  2. Estimate the mean from the same data. The sample mean is (2+4+6)/3=4(2 + 4 + 6)/3 = 4. Check: 4 is in the middle of the three values, as it should be.
  3. Compute the deviations from the mean. They are 2−4=−22 - 4 = -2, 4−4=04 - 4 = 0 and 6−4=26 - 4 = 2. Check: these add to 0, as they must for any sample mean.
  4. Notice the constraint. Once you know the first two deviations, the third is forced, because the deviations must add to zero. If you choose −2-2 and 00, the third must be +2+2. Check: only two deviations are free, which is 3−13 - 1.
  5. 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 8/2=48/2 = 4. Check: dividing by 3 would give 2.67, which is smaller.
  6. Say why the smaller divisor is wrong. If the true mean were 5 instead, the squared deviations would be 9+1+1=119 + 1 + 1 = 11, 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.

510152011.52sample size ncorrection factor
  • n / (n - 1)
The factor n over n minus 1 that turns a divide-by-n variance into the usual sample variance. It is large for small samples and close to 1 for big ones.

The math

Definition: Degrees of freedom

When a statistic is computed from nn independent values and pp quantities are estimated from those same values, the degrees of freedom are ν=n−p\nu = n - p. This is the number of values that remain free to vary after the estimated quantities are fixed.

Step 4 in symbols: the constraint

For nn values x1,…,xnx_1, \dots, x_n with sample mean xˉ\bar{x}, the deviations satisfy

∑i=1n(xi−xˉ)=0.\sum_{i=1}^{n} (x_i - \bar{x}) = 0.

This is one linear constraint. Any n−1n - 1 deviations determine the last one, so only n−1n - 1 are free. Check with the example: −2+0+2=0-2 + 0 + 2 = 0.

Step 5 in symbols: why the divisor is n−1n - 1

Let μ\mu be the true mean and σ2\sigma^2 the true variance. Split each squared deviation around μ\mu into a part around xˉ\bar{x} and a correction. Using the constraint, the cross term vanishes, and

∑i=1n(xi−μ)2=∑i=1n(xi−xˉ)2+n(xˉ−μ)2.\sum_{i=1}^{n} (x_i - \mu)^2 = \sum_{i=1}^{n} (x_i - \bar{x})^2 + n(\bar{x} - \mu)^2.

Take expectations. The left side has expectation nσ2n\sigma^2. The last term has expectation n⋅σ2/n=σ2n \cdot \sigma^2/n = \sigma^2, because the sampling distribution of xˉ\bar{x} has variance σ2/n\sigma^2/n. So the expected sum of squares about the sample mean is (n−1)σ2(n - 1)\sigma^2. Dividing by n−1n - 1 therefore gives an average that equals σ2\sigma^2. Dividing by nn would give (n−1)σ2/n(n-1)\sigma^2/n, which is too small.

Beyond the walkthrough: the distributions that use the count

With S2=∑(xi−xˉ)2/(n−1)S^2 = \sum (x_i - \bar{x})^2 / (n - 1) and normal data,

(n−1)S2σ2∼χn−12,t=xˉ−μS/n∼tn−1.\frac{(n-1)S^2}{\sigma^2} \sim \chi^2_{n-1}, \qquad t = \frac{\bar{x} - \mu}{S/\sqrt{n}} \sim t_{n-1}.

The chi-squared distribution and the tt distribution are both indexed by these degrees of freedom. A smaller count means heavier tails in the tt case, because the estimated spread is noisier.

The general count

For a chi-squared goodness-of-fit test on mm categories, the expected counts come from a model with qq parameters estimated from the same counts. The degrees of freedom are m−1−qm - 1 - q. The "minus 1" is the fixed total, and the "minus qq" is the cost of estimating the model.

Worked examples

Example
1. Two values, one free piece (tiny)

Data: 1 and 3.

  1. Mean: (1+3)/2=2(1 + 3)/2 = 2.
  2. Deviations: −1-1 and +1+1. Check: they add to 0.
  3. Degrees of freedom: n−1=1n - 1 = 1. Once the first deviation is −1-1, the second must be +1+1.
  4. Sum of squared deviations: 1+1=21 + 1 = 2.
  5. Sample variance: 2/1=22 / 1 = 2.

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.

Example
2. Five measurements (standard)

A chemist measures the purity of a compound five times and gets 98, 100, 102, 99 and 101.

  1. Mean: (98+100+102+99+101)/5=500/5=100(98 + 100 + 102 + 99 + 101)/5 = 500/5 = 100.
  2. Deviations: −2,0,2,−1,1-2, 0, 2, -1, 1. Check: they add to 0.
  3. Sum of squared deviations: 4+0+4+1+1=104 + 0 + 4 + 1 + 1 = 10.
  4. Degrees of freedom: 5−1=45 - 1 = 4.
  5. Sample variance: 10/4=2.510 / 4 = 2.5. The standard deviation is 2.5≈1.58\sqrt{2.5} \approx 1.58.
  6. Comparison: dividing by 5 would give 10/5=2.010/5 = 2.0, 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.

Example
3. A two-group trial and a regression (realistic)

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.

  1. Each group's mean is estimated from its own patients, so the pooled degrees of freedom are (12−1)+(10−1)=20(12 - 1) + (10 - 1) = 20.
  2. Pooled variance: (66+54)/20=120/20=6.0(66 + 54)/20 = 120/20 = 6.0.
  3. Standard error of the difference in means: 6.0×(1/12+1/10)=6.0×0.18333≈1.049\sqrt{6.0 \times (1/12 + 1/10)} = \sqrt{6.0 \times 0.18333} \approx 1.049.
  4. If the difference in means is 3.0 units, the tt statistic is 3.0/1.049≈2.863.0 / 1.049 \approx 2.86.
  5. The two-sided 5 percent critical value for tt with 20 degrees of freedom is about 2.086, and 2.86>2.0862.86 \gt 2.086. The difference is significant at that level.
  6. 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: 30−3−1=2630 - 3 - 1 = 26, 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 nn to n−1n - 1 matters for every sample. In fact it matters most for small samples. The correction factor is 2 at n=2n = 2, about 1.25 at n=5n = 5, and about 1.05 at n=20n = 20. 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 n−1n - 1. In fact if the true mean μ\mu is known and not estimated, the sum of squares about μ\mu has expectation nσ2n\sigma^2, so dividing by nn 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|tt 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 n−1n - 1 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 n−1n - 1 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 n−1n - 1.
  • The divisor is needed because the sum of squared deviations about the sample mean is smaller than about any other centre.
  • The tt 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.

Check yourself

QuestionRecall: how many degrees of freedom does the sample variance of n values have, and why?Show answer
It has n−1n - 1 degrees of freedom. The sample mean is estimated from the same values, and the deviations from that mean must add to zero, so one deviation is determined by the others.
QuestionComputation: a sample of 8 values has sum of squared deviations about the sample mean equal to 35. What are the degrees of freedom and the sample variance?Show answer
The degrees of freedom are 8−1=78 - 1 = 7. The sample variance is 35/7=535/7 = 5.
QuestionWhy: why does dividing the sum of squared deviations by n underestimate the true variance?Show answer
Because the sample mean is the centre that makes the sum of squared deviations as small as possible. Measuring deviations from the data's own mean absorbs part of the true spread, so the expected sum is (n−1)σ2(n-1)\sigma^2, not nσ2n\sigma^2. Dividing by nn therefore gives (n−1)σ2/n(n-1)\sigma^2/n, which is too small.
QuestionApplied: a chi-squared goodness-of-fit test has 6 categories, and the expected counts come from a model with 2 parameters estimated from the same counts. What are the degrees of freedom?Show answer
The count is 6−1−2=36 - 1 - 2 = 3. The minus 1 is the fixed total, and the minus 2 is for the estimated parameters. Using 5 would be too lenient, so the test would reject a poor model less often than it should.

Sources