Chi Square Distribution
The chi-square (χ²) distribution is the distribution of a sum of squared standard normal values. If you draw k independent values from a standard normal distribution, square them and add them up, the result follows a chi-square distribution with k degrees of freedom.
That sounds abstract, but it is the backbone of several everyday statistical tests: the chi-square goodness-of-fit test (is this die fair?), the test of independence in contingency tables, and confidence intervals for a variance.
| Parameter | Meaning |
|---|---|
df |
degrees of freedom k (must be > 0) |
size |
output shape |
Generating chi-square data
import numpy as np
rng = np.random.default_rng(42)
print(rng.chisquare(df=2, size=6).round(3))
print(rng.chisquare(df=10, size=6).round(3))[4.808 4.672 4.77 0.56 0.173 2.905]
[ 9.897 9.261 13.672 9.622 11.502 11.019]
Values are always ≥ 0 (they are sums of squares). The mean is df and the variance is 2·df.
Building it from normal numbers
We can verify the definition directly: square and sum k standard normal values per row.
import numpy as np
rng = np.random.default_rng(42)
k = 4
z = rng.standard_normal(size=(200_000, k))
built = (z ** 2).sum(axis=1) # one chi-square value per row
direct = rng.chisquare(df=k, size=200_000)
print(f"built from normals: mean {built.mean():.3f}, var {built.var():.3f}")
print(f"rng.chisquare: mean {direct.mean():.3f}, var {direct.var():.3f}")
print(f"theory: mean {k}, var {2 * k}")built from normals: mean 4.004, var 8.109
rng.chisquare: mean 4.002, var 8.022
theory: mean 4, var 8
Visualising
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
bins = np.linspace(0, 25, 76)
plt.figure(figsize=(6, 3.5))
for df, color in [(1, "#e07a5f"), (3, "#4dabcf"), (6, "#81b29a"), (10, "#9d79bc")]:
plt.hist(rng.chisquare(df, 50_000), bins=bins, density=True, histtype="step",
linewidth=1.5, color=color, label=f"df = {df}")
plt.ylim(0, 0.5)
plt.xlabel("value")
plt.legend()
plt.tight_layout()
plt.show()
print("Plotted chi-square distributions for df = 1, 3, 6 and 10")Plotted chi-square distributions for df = 1, 3, 6 and 10
With few degrees of freedom the distribution is strongly skewed, piled up near zero. As df grows it shifts right and becomes more symmetric, slowly approaching a normal shape.
A real use: is this die fair?
The goodness-of-fit statistic compares observed counts O with expected counts E: χ² = Σ (O − E)² / E. If the die is fair, this statistic roughly follows a chi-square distribution with (number of categories − 1) degrees of freedom. A large value means the observations don't fit the expectation.
import numpy as np
rng = np.random.default_rng(42)
observed = np.array([18, 22, 16, 25, 17, 32]) # 130 rolls of a suspicious die
expected = np.full(6, observed.sum() / 6)
stat = ((observed - expected) ** 2 / expected).sum()
print(f"chi-square statistic: {stat:.2f}")
# How often would a FAIR die give a statistic at least this large?
fair = rng.multinomial(observed.sum(), [1/6] * 6, size=100_000)
fair_stats = ((fair - expected) ** 2 / expected).sum(axis=1)
print(f"simulated p-value: {np.mean(fair_stats >= stat):.4f}")
print(f"same via chisquare(df=5): {np.mean(rng.chisquare(5, 100_000) >= stat):.4f}")chi-square statistic: 8.55
simulated p-value: 0.1295
same via chisquare(df=5): 0.1258
Both estimates agree: a perfectly fair die produces a statistic at least this large about 13 % of the time. That is well above the usual 0.05 threshold for "significant", so these 130 rolls are not evidence that the die is loaded, even though the six looks frequent. In practice you would call scipy.stats.chisquare(observed), which performs exactly this test.
Test yourself
A chi-square value with k degrees of freedom is the sum of...
- k uniform values
- k squared standard normal values
- k exponential values
- k binomial values
What is the mean of a chi-square distribution with df=7?
- 1
- 7
- 14
- 49
Can rng.chisquare(3) return a negative number?
- Yes
- No
- Only when df < 1
- Only with a seed