Search

Search papers and lessons

Chi-Squared Distribution

Level 2 · 8 min read

The chi-squared distribution describes a sum of squared independent standard Gaussians, and it tells you how much variance estimates and goodness-of-fit scores wander by chance.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Sign in to see your progress

TL;DR

Take kk independent standard Gaussians, square each one, and add the squares. The total follows a chi-squared distribution with kk degrees of freedom. It is never negative, its average is kk, and it has a long right tail.

It is the distribution behind many classic tests: whether a sample's spread matches a theory, and whether observed counts match expected counts.

Where you'll see it: in testing whether a sample variance is unusually large or small, in goodness-of-fit tests on survey or experiment counts, and in confidence intervals for a variance.

Intuition

Squares of noise

Each Gaussian error can be positive or negative. Squaring removes the sign, so what is left measures how far off you are, not which way. Add the squares of kk independent errors and you get the total squared distance from the target. That total is chi-squared with kk degrees of freedom. The name "degrees of freedom" counts the independent directions the randomness can move in.

Statistics estimates spread by summing squared deviations. Squared deviations of Gaussian noise add up to a known distribution, so we can say how large such a sum is normally and when it is suspicious. That is the problem this distribution solves.

0510152000.10.20.30.40.5Q (sum of squares)density
  • k = 2
  • k = 4
  • k = 8
Chi-squared densities for three degrees of freedom. The peak sits at k − 2 for k ≥ 2, and the curves become more symmetric as k grows.

For k=2k = 2 the density is largest at zero and falls away. For larger kk the bump moves right and the curve looks more like a bell, which is the central limit theorem at work: a sum of many independent pieces becomes Gaussian-shaped.

The math

Definition

Definition: Chi-squared distribution

Let Z1,…,ZkZ_1, \dots, Z_k be independent, each with distribution N(0,1)\mathcal{N}(0, 1). The random variable

Q=Z12+Z22+⋯+Zk2Q = Z_1^2 + Z_2^2 + \cdots + Z_k^2

has a chi-squared distribution with kk degrees of freedom, written Q∼χk2Q \sim \chi^2_k. For x>0x \gt 0 its density is

fk(x)=xk/2−1 e−x/22k/2 Γ(k/2),f_k(x) = \frac{x^{k/2 - 1}\, e^{-x/2}}{2^{k/2}\, \Gamma(k/2)},

where Γ\Gamma is the gamma function, which extends factorials: Γ(n)=(n−1)!\Gamma(n) = (n-1)! for a positive whole number nn.

Here kk is the number of squared terms, xx is a possible value of QQ, and e−x/2e^{-x/2} is the exponential decay that controls the right tail. The denominator is the constant that makes the area under the curve equal 1. The density is the gamma distribution with shape k/2k/2 and scale 2, which is why the two appear together in software.

Mean and variance

For one standard Gaussian ZZ, E[Z2]=Var(Z)=1\mathbb{E}[Z^2] = \mathrm{Var}(Z) = 1. The fourth moment of a standard Gaussian is E[Z4]=3\mathbb{E}[Z^4] = 3, a standard fact you can check by integration by parts. So

Var(Z2)=E[Z4]−(E[Z2])2=3−1=2.\mathrm{Var}(Z^2) = \mathbb{E}[Z^4] - \left(\mathbb{E}[Z^2]\right)^2 = 3 - 1 = 2.

Each squared term has mean 1 and variance 2. The terms are independent, and means and variances of independent terms add:

E[Q]=k,Var(Q)=2k.\mathbb{E}[Q] = k, \qquad \mathrm{Var}(Q) = 2k.

The mean grows with kk, and the spread grows with the square root of kk, so the relative wobble shrinks as kk grows.

Adding chi-squared variables

If Q1∼χa2Q_1 \sim \chi^2_a and Q2∼χb2Q_2 \sim \chi^2_b are independent, then Q1+Q2∼χa+b2Q_1 + Q_2 \sim \chi^2_{a+b}. This follows directly from the definition: the sum is a total of a+ba + b squared standard Gaussians.

The sample variance

This is the result that makes the distribution useful in practice. Suppose X1,…,XnX_1, \dots, X_n are independent N(μ,σ2)\mathcal{N}(\mu, \sigma^2), with sample mean Xˉ\bar X and sample variance

S2=1n−1∑i=1n(Xi−Xˉ)2.S^2 = \frac{1}{n-1} \sum_{i=1}^{n} (X_i - \bar X)^2.

Then

(n−1) S2σ2∼χn−12.\frac{(n-1)\, S^2}{\sigma^2} \sim \chi^2_{n-1}.

One degree of freedom is lost because Xˉ\bar X is estimated from the same data. The variance sum is a sum of squares of Gaussian pieces, so it is chi-squared. It cannot be Gaussian itself, because a Gaussian takes negative values and a sum of squares cannot.

Goodness of fit

For counts O1,…,OmO_1, \dots, O_m in mm categories, with expected counts EiE_i under a model, Pearson's statistic is

χ2=∑i=1m(Oi−Ei)2Ei.\chi^2 = \sum_{i=1}^{m} \frac{(O_i - E_i)^2}{E_i}.

When the expected counts are large, this statistic is approximately χm−12\chi^2_{m-1} if the model is right. The degrees of freedom are m−1m - 1 because the total count is fixed, so one category is determined by the others.

Worked examples

Example
Tiny case: two degrees of freedom

Let k=2k = 2. The density is f2(x)=12e−x/2f_2(x) = \tfrac12 e^{-x/2} for x>0x \gt 0.

  1. We want P(Q>2)=∫2∞12e−x/2 dxP(Q \gt 2) = \int_2^\infty \tfrac12 e^{-x/2}\, dx.
  2. An antiderivative of 12e−x/2\tfrac12 e^{-x/2} is −e−x/2-e^{-x/2}.
  3. Evaluate: [−e−x/2]2∞=0−(−e−1)=e−1\left[-e^{-x/2}\right]_2^\infty = 0 - \left(-e^{-1}\right) = e^{-1}.
  4. e−1≈0.368e^{-1} \approx 0.368.

Interpretation: when QQ is the sum of squares of two independent standard Gaussians, it exceeds 2 about 37% of the time. For k=2k = 2 the distribution is simply exponential, which makes it a good first case to compute by hand.

Example
A realistic check on a sensor's noise

A lab assumes its sensor has noise standard deviation σ=1.5\sigma = 1.5, so σ2=2.25\sigma^2 = 2.25. It takes n=10n = 10 readings and finds a sample variance S2=6.0S^2 = 6.0. Is that surprising?

  1. Compute the test statistic: (n−1)S2σ2=9×6.02.25=542.25=24\dfrac{(n-1)S^2}{\sigma^2} = \dfrac{9 \times 6.0}{2.25} = \dfrac{54}{2.25} = 24.
  2. If the assumption is true, this statistic follows χ92\chi^2_9, which has mean 9.
  3. The 95th percentile of χ92\chi^2_9 is about 16.92, and the 99th is about 21.67. The value 24 lies beyond both, so its tail probability is below 0.01.
  4. Interpretation: either the sensor is noisier than the lab assumed, or the Gaussian noise assumption is wrong. The test is only as good as that assumption.

Tradeoffs and failure modes

SituationProblemBetter choice
Data have heavy tails or outliersThe squared-Gaussian premise fails, so the chi-squared quantiles are too optimisticRobust estimators, or a bootstrap that makes no distributional claim
Expected counts are small (roughly below 5 in a cell)The chi-squared approximation is poorAn exact test, or merge sparse categories
You estimated parameters from the same dataDegrees of freedom are overstated and the test is too lenientSubtract the number of estimated parameters from the degrees of freedom
Comparing two variancesA single chi-squared value does not answer the questionA ratio of two scaled chi-squared variables (the F distribution)
Positive data that are not sums of squaresThe chi-squared shape is too rigidThe gamma distribution, which has a free scale parameter

Large samples make tiny differences significant. A goodness-of-fit test can reject a model that is useful but not perfect, so report the size of the deviation, not only the p-value.

History

Friedrich Robert Helmert, a German geodesist, worked out the distribution of a sum of squared Gaussian errors in the 1870s while analysing measurement errors. In 1900 Karl Pearson introduced the goodness-of-fit test that uses the statistic above, and he gave the distribution its name from the Greek letter chi. The test caught on because it turned "does this model fit these counts?" into one number with a known reference distribution. In the 1920s Ronald Fisher showed that the degrees of freedom must be reduced by the number of parameters estimated from the data, a correction that is still applied today.

Check yourself

QuestionRecall: What are the mean and variance of a chi-squared variable with k degrees of freedom?Show answer

The mean is kk and the variance is 2k2k. Each squared standard Gaussian has mean 1 and variance 2, and the variances of independent terms add, so kk terms give variance 2k2k. The means add too, giving kk.

QuestionComputation: If Q∼χ22Q \sim \chi^2_2, what is P(Q>4)P(Q \gt 4)?Show answer

P(Q>4)=e−2≈0.135P(Q \gt 4) = e^{-2} \approx 0.135. Integrate 12e−x/2\tfrac12 e^{-x/2} from 4 to infinity. The antiderivative −e−x/2-e^{-x/2} evaluated at the limits gives e−4/2=e−2e^{-4/2} = e^{-2}.

QuestionWhy: why is a sample variance chi-squared distributed rather than Gaussian?Show answer

The scaled sample variance (n−1)S2/σ2(n-1)S^2/\sigma^2 is a sum of squared deviations, and each deviation is a Gaussian quantity. A sum of squares is never negative, while a Gaussian is symmetric and takes negative values as well as positive ones. The chi-squared distribution is the one built from squares of Gaussians, so it is the right shape. The lost degree of freedom comes from estimating the mean from the same data.

QuestionApplied: A survey of 100 people is split across 4 answers, with 25 expected in each. The observed counts are 30, 20, 28 and 22. Which chi-squared distribution applies, and is the fit suspicious at the 5% level?Show answer

The statistic is 2525+2525+925+925=2.72\frac{25}{25} + \frac{25}{25} + \frac{9}{25} + \frac{9}{25} = 2.72. The degrees of freedom are 4−1=34 - 1 = 3, because the total of 100 is fixed. The 95th percentile of χ32\chi^2_3 is about 7.81. Since 2.72 is well below that, the observed counts are consistent with the expected split, and there is no evidence against the model at the 5% level.

Sources