# Chi-Square Distribution: Definition, Formula and Examples

The chi-square distribution is a family of right-skewed probability distributions used to model sums of squared standard normal variables. Its single parameter, the degrees of freedom, controls the shape. It is the reference distribution for goodness-of-fit tests, tests of independence, and confidence intervals for variances.

## Quick Answer

- The chi-square distribution results when ν independent standard normal variables are squared and summed [1].
- It has one parameter, the degrees of freedom (df or k), which sets the shape, mean, and variance.
- The mean equals df and the variance equals 2 × df [2].
- The distribution is positively skewed and becomes more symmetric as df grows [3].
- It powers goodness-of-fit tests, tests of independence, and variance inference.

## What the Chi-Square Distribution Means

In plain terms, the chi-square distribution describes how much squared error you expect to accumulate when you compare data to a model. If you take a standard normal variable, square it, and add up several such squares, the total follows a chi-square distribution.

The precise definition: if $Z_1, Z_2, \dots, Z_\nu$ are independent standard normal variables, then the sum of their squares follows a chi-square distribution with ν degrees of freedom [1]:

$$\chi^2_\nu = \sum_{i=1}^{\nu} Z_i^2$$

The degrees of freedom count the independent pieces of information in that sum [2]. The chi-square distribution is a special case of the gamma distribution with shape $a = \nu/2$ and scale 2 [4][5]. With 2 degrees of freedom it is identical to an exponential distribution with rate 1/2 [5].

## How It Works

The probability density function for a chi-square variable $x$ with $k$ degrees of freedom is [1][4][5]:

$$f(x; k) = \frac{x^{k/2 - 1} e^{-x/2}}{2^{k/2}\,\Gamma(k/2)}, \quad x > 0,\ k > 0$$

Each symbol means:

- $x$ is the value of the chi-square statistic, always non-negative.
- $k$ (also written ν or df) is the degrees of freedom.
- $e$ is Euler's number, about 2.71828.
- $\Gamma$ is the gamma function, defined as $\Gamma(a) = \int_0^\infty t^{a-1} e^{-t}\,dt$ [1].

The cumulative distribution function has no simple closed form and is computed numerically [1]. In software you get it directly. In SciPy, `chi2.pdf`, `chi2.cdf`, and `chi2.sf` take the degrees of freedom as the shape argument [4]. In R, `dchisq`, `pchisq`, and `qchisq` do the same [5].

Degrees of freedom change the curve. At df = 1 the density spikes near zero and has a long right tail. At df = 3 and df = 5 the peak moves right and the curve flattens. This is why the chi-square distribution is a family, not a single curve [3].

## Worked Example

A small clinic survey recorded two observed counts per group across eight groups. The table below shows the raw data.

| group | observed_a | observed_b |
|-------|-----------|-----------|
| A | 12 | 8 |
| B | 15 | 10 |
| C | 9 | 11 |
| D | 14 | 7 |
| E | 11 | 13 |
| F | 13 | 9 |
| G | 10 | 12 |
| H | 16 | 6 |

**Goodness-of-fit test.** Treat the 100 total observations as spread across 8 groups. If all groups are equally likely, the expected count per group is $100 / 8 = 12.5000$. The chi-square statistic is:

$$\chi^2 = \sum_{i=1}^{k} \frac{(f_i - \hat{f}_i)^2}{\hat{f}_i}$$

Plugging in the observed and expected counts gives $\chi^2 = 3.3600$. The degrees of freedom are $n - 1 = 8 - 1 = 7$. The upper-tail p-value is `chi2.sf(3.3600, 7) = 0.8498`. A statistic this small is very common under the null hypothesis, so there is no evidence the groups differ.

**Independence test.** Now collapse the data into a 2 × 2 contingency table, with groups A to D and groups E to H as the rows and observed_a and observed_b as the columns (50 and 36 in the first row, 50 and 40 in the second). The row totals are 86.0 and 90.0, the column totals are 100.0 and 76.0, and the grand total is 176. Expected counts come from (row total × column total) / grand total. The chi-square statistic, without Yates' continuity correction, is $\chi^2 = 0.1197$. The degrees of freedom are $(r - 1)(c - 1) = (2 - 1)(2 - 1) = 1$. The p-value is `chi2.sf(0.1197, 1) = 0.7294`. Again, no evidence of association.

For reference, the 0.05 critical value at df = 3 is `chi2.ppf(1 - 0.05, 3) = 7.8147`. A statistic above that value would fall in the upper 5% tail.

```python
from scipy import stats
crit = stats.chi2.ppf(1 - 0.05, 3)  # 7.8147
p = stats.chi2.sf(3.3600, 7)  # 0.8498
p_ind = stats.chi2.sf(0.1197, 1)  # 0.7294
print(f"critical value = {crit:.4f}")
print(f"goodness-of-fit chi2 = 3.3600, df = 7, p = {p:.4f}")
print(f"independence chi2 = 0.1197, df = 1, p = {p_ind:.4f}")
```

Output:

```
critical value = 7.8147
goodness-of-fit chi2 = 3.3600, df = 7, p = 0.8498
independence chi2 = 0.1197, df = 1, p = 0.7294
```

You can reproduce these numbers with a [chi-square test calculator](/tools/chi-square-calculator).

## How to Interpret It

The chi-square statistic measures total squared deviation between observed and expected values, scaled by the expected values. Small values mean the data fit the model well. Large values mean the data depart from the model.

The p-value is the probability of getting a chi-square statistic at least as large as yours, assuming the null hypothesis is true. You compare it to your significance level, usually 0.05. If p is below that threshold, you reject the null hypothesis.

The mean of the distribution equals the degrees of freedom [2]. So a statistic near df is typical. A statistic far above df sits in the right tail and signals a poor fit or a real association.

## When to Use It (and when not to)

Use the chi-square distribution when:

- You test whether observed frequencies match expected frequencies (goodness-of-fit) [6].
- You test whether two categorical variables are independent in a contingency table [3].
- You build confidence intervals or tests for a population variance from normal data.
- You compare nested models through likelihood ratio statistics.

Do not use it when:

- Expected counts are very small. A common rule of thumb requires every group to have at least five data points [6].
- Your data are paired or repeated measures on the same units.
- You need exact inference with tiny samples. Fisher's exact test is the usual alternative, and the choice between them is a [decision framework worth reviewing](/knowledge/bioinformatics/chi-square-test-of-independence-vs-fisher-s-exact-test-a-decision-framework-for-small-sample-sizes-i).
- You want to know which specific cells drive a significant result. That calls for [post-hoc tests using standardized residuals](/knowledge/bioinformatics/post-hoc-tests-after-a-significant-chi-square-standardized-residuals-partitioning-and-pairwise-compa).

## Chi-Square Distribution vs Student's t-Distribution

Both are sampling distributions tied to degrees of freedom, but they answer different questions. The t-distribution is symmetric and used for means. The chi-square distribution is skewed and used for variances and counts.

| Feature | Chi-Square Distribution | Student's t-Distribution |
|---------|------------------------|--------------------------|
| Shape | Positively skewed, right tail [3] | Symmetric, bell-shaped |
| Range | $x \ge 0$ | All real numbers |
| Parameter | Degrees of freedom | Degrees of freedom |
| Mean | df [2] | 0 |
| Typical use | Variance, counts, fit | Means, regression coefficients |
| Behavior as df grows | Approaches normal | Approaches standard normal |

If you need the symmetric counterpart for means, see [Student's t-distribution](/blog/data-analysis/students-t-distribution-definition-formula).

## Common Mistakes

- **Using the wrong degrees of freedom.** For goodness-of-fit, df is (number of categories - 1). For independence, df is (rows - 1)(columns - 1). Mixing these up gives the wrong p-value. Fix: write the formula down before computing.
- **Ignoring small expected counts.** Chi-square p-values are unreliable when expected counts fall below about 5 [6]. Fix: combine sparse categories or switch to an exact test.
- **Confusing the statistic with the distribution.** The chi-square statistic is a number you compute. The chi-square distribution is the curve you compare it against. Fix: keep the two separate in your write-up.
- **Reading a small p-value as proof of a strong effect.** With large samples, trivial departures become significant. Fix: report an effect size alongside the p-value.
- **Applying it to percentages instead of counts.** The formula needs frequencies. Fix: convert percentages back to counts first.
- **Testing independence on paired data.** The test assumes independent observations. Fix: use McNemar's test for paired categorical data.

## Limitations

The chi-square test tells you whether a difference exists, not where it lives or how large it is. A significant result on a large table can come from one unusual cell while the rest fit fine. You need follow-up analysis to locate the source.

The approximation also degrades when expected counts are small or when the table is sparse. In those cases the test statistic does not follow the chi-square distribution closely, and p-values can be badly wrong. Exact methods exist but they are computationally heavier and can be conservative.

## Frequently Asked Questions

### What is the chi-square distribution in simple terms?

It is the distribution of a sum of squared standard normal values. If you square several independent standard normal variables and add them, the total follows a chi-square distribution with degrees of freedom equal to the number of variables you squared [1]. It is right-skewed and always non-negative.

### What are degrees of freedom in a chi-square test?

Degrees of freedom count the independent pieces of information in your calculation [2]. For a goodness-of-fit test with k categories, df = k - 1. For an r × c contingency table, df = (r - 1)(c - 1). The value sets the shape of the reference curve you compare your statistic against.

### What is the difference between the chi-square statistic and the chi-square distribution?

The statistic is a single number computed from your data, using the sum of squared observed-minus-expected differences divided by expected values. The distribution is the theoretical curve that statistic follows under the null hypothesis. You use the curve to convert the statistic into a p-value.

### Can the chi-square statistic be negative?

No. Every term in the sum is a squared quantity divided by a positive expected count, so the total is zero or positive. A statistic of zero means the observed counts match the expected counts exactly.

### When should I use Fisher's exact test instead?

Use Fisher's exact test when sample sizes are small or expected cell counts are low, especially in 2 × 2 tables. The chi-square approximation needs reasonably large expected counts to hold. For a fuller comparison, see [chi-square vs Fisher's exact test](/knowledge/bioinformatics/chi-square-test-of-independence-vs-fisher-s-exact-test-a-decision-framework-for-small-sample-sizes-i).

## References

1. [1.3.6.6.6. Chi-Square Distribution](https://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm)
2. [Statistics and the Treatment of Experimental Data](https://ned.ipac.caltech.edu/level5/Leo/Stats2_4.html)
3. [Chi-Square - Sociology 3112 - Department of Sociology - The University of utah](https://soc.utah.edu/sociology3112/chi-square.php)
4. [scipy.stats.chi2, SciPy v1.18.0 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2.html)
5. [R: The (non-central) Chi-Squared Distribution](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Chisquare.html)
6. [7.2.1.1. Chi-square goodness-of-fit test](https://www.itl.nist.gov/div898/handbook/prc/section2/prc211.htm)

## Further Reading

- [6.9: Chi-square distribution - Statistics LibreTexts](https://stats.libretexts.org/Bookshelves/Applied_Statistics/Mikes_Biostatistics_Book_(Dohm)/06%3A_Probability_and_Distributions/6.09%3A_Chi-square_distribution)

## Related Articles

- [What Is a Chi-Square Test? Formula and Examples](/blog/data-analysis/chi-square-test-formula-examples)
- [Standard Deviation of a Binomial Distribution: Formula and Example](/blog/data-analysis/standard-deviation-binomial-distribution)
- [Negative Binomial Distribution: Formula and Examples](/blog/data-analysis/negative-binomial-distribution-formula-examples)
- [Uniform Distribution: Definition, Formula and Examples](/blog/data-analysis/uniform-distribution)
- [Binomial Distribution: Formula, Mean and Examples](/blog/data-analysis/binomial-distribution-formula-examples)
- [Poisson Distribution: Formula and Examples](/blog/research-skills/poisson-distribution-formula-and-examples)
- [Right Skewed Distribution: Meaning and Examples](/blog/research-skills/right-skewed-distribution-meaning-and-examples)
- [Chi-Square Test of Independence vs. Fisher](/knowledge/bioinformatics/chi-square-test-of-independence-vs-fisher-s-exact-test-a-decision-framework-for-small-sample-sizes-i)