{{Short description|Statistical hypothesis test}} {{CS1 config|mode=cs1}} [[File:Chi-square distributionCDF-English.png|thumb|right|300px|Chi-squared distribution, showing {{math|χ<sup>2</sup>}} on the first axis and ''p''-value (right tail probability) on the second axis.]]
A '''chi-squared test''' (also '''chi-square''' or '''{{math|χ<sup>2</sup>}} test''') is a statistical hypothesis test used in the analysis of contingency tables when the sample sizes are large. In simpler terms, this test is primarily used to examine whether two categorical variables (''two dimensions of the contingency table'') are independent in influencing the test statistic (''values within the table'').<ref>{{Cite web |title=Chi-Square – Sociology 3112 – Department of Sociology – The University of utah |url=https://soc.utah.edu/sociology3112/chi-square.php |access-date=2022-11-12 |website=soc.utah.edu}}</ref>
The test is valid when the test statistic is chi-squared distributed under the null hypothesis, specifically Pearson's chi-squared test and variants thereof. Pearson's chi-squared test is used to determine whether there is a statistically significant difference between the expected frequencies and the observed frequencies in one or more categories of a contingency table. For contingency tables with smaller sample sizes, a Fisher's exact test is used instead.
In the standard applications of this test, the observations are classified into mutually exclusive classes. If the null hypothesis that there are no differences between the classes in the population is true, the test statistic computed from the observations follows a {{math|χ<sup>2</sup>}} frequency distribution. The purpose of the test is to evaluate how likely the observed frequencies would be assuming the null hypothesis is true.
Test statistics that follow a {{math|χ<sup>2</sup>}} distribution occur when the observations are independent. There are also {{math|χ<sup>2</sup>}} tests for testing the null hypothesis of independence of a pair of random variables based on observations of the pairs.
''Chi-squared tests'' often refers to tests for which the distribution of the test statistic approaches the {{math|χ<sup>2</sup>}} distribution asymptotically, meaning that the sampling distribution (if the null hypothesis is true) of the test statistic approximates a chi-squared distribution more and more closely as sample sizes increase.
== History == In the 19th century, statistical analytical methods were mainly applied in biological data analysis and it was customary for researchers to assume that observations followed a normal distribution, such as Sir George Airy and Mansfield Merriman, whose works were criticized by Karl Pearson in his 1900 paper.<ref name = Pearson1900> {{cite journal | last = Pearson | first = Karl | author-link = Karl Pearson | title = On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling | journal = Philosophical Magazine |series=Series 5 | volume = 50 | issue = 302 | year = 1900 | pages = 157–175 | url = https://www.tandfonline.com/doi/abs/10.1080/14786440009463897 | doi = 10.1080/14786440009463897 | url-access = subscription }}</ref>
At the end of the 19th century, Pearson noticed the existence of significant skewness within some biological observations. To model the observations regardless of being normal or skewed, Pearson, in a series of articles published from 1893 to 1916,<ref name = Pearson1893> {{cite journal | last = Pearson | first = Karl | author-link = Karl Pearson | title = Contributions to the mathematical theory of evolution [abstract] | journal = Proceedings of the Royal Society | volume = 54 | year = 1893 | pages = 329–333 | jstor = 115538 | doi = 10.1098/rspl.1893.0079 | doi-access = free }} </ref><ref name = Pearson1895> {{cite journal | last = Pearson | first = Karl | author-link = Karl Pearson | title = Contributions to the mathematical theory of evolution, II: Skew variation in homogeneous material | journal = Philosophical Transactions of the Royal Society | volume = 186 | year = 1895 | pages = 343–414 | bibcode = 1895RSPTA.186..343P | jstor = 90649 | doi = 10.1098/rsta.1895.0010 | url = https://zenodo.org/record/1432104 | doi-access = free }} </ref><ref name = Pearson1901> {{cite journal | last = Pearson | first = Karl | author-link = Karl Pearson | title = Mathematical contributions to the theory of evolution, X: Supplement to a memoir on skew variation | journal = Philosophical Transactions of the Royal Society A | volume = 197 | issue = 287–299 | year = 1901 | pages = 443–459 | bibcode = 1901RSPTA.197..443P | jstor = 90841 | doi = 10.1098/rsta.1901.0023 | doi-access = }} </ref><ref name = Pearson1916> {{cite journal | last = Pearson | first = Karl | author-link = Karl Pearson | title = Mathematical contributions to the theory of evolution, XIX: Second supplement to a memoir on skew variation | journal = Philosophical Transactions of the Royal Society A | volume = 216 | issue = 538–548 | year = 1916 | pages = 429–457 | bibcode = 1916RSPTA.216..429P | jstor = 91092 | doi = 10.1098/rsta.1916.0009 | doi-access = free }} </ref> devised the Pearson distribution, a family of continuous probability distributions, which includes the normal distribution and many skewed distributions, and proposed a method of statistical analysis consisting of using the Pearson distribution to model the observation and performing a test of goodness of fit to determine how well the model really fits to the observations.
=== Pearson's chi-squared test === {{See also|Pearson's chi-squared test}}
In 1900, Pearson published a paper<ref name = Pearson1900 /> on the {{math|χ<sup>2</sup>}} test which is considered to be one of the foundations of modern statistics.<ref name = Cochran1952> {{cite journal | last = Cochran | first = William G. | author-link = William G. Cochran | title = The Chi-square Test of Goodness of Fit | journal = The Annals of Mathematical Statistics | volume = 23 | issue = 3 | year = 1952 | pages = 315–345 | jstor = 2236678 | doi=10.1214/aoms/1177729380 | doi-access = free }} </ref> In this paper, Pearson investigated a test of goodness of fit.
Suppose that {{mvar|n}} observations in a random sample from a population are classified into {{mvar|k}} mutually exclusive classes with respective observed numbers of observations {{mvar|x<sub>i</sub>}} (for {{math|''i'' {{=}} 1,2,…,''k''}}), and a null hypothesis gives the probability {{mvar|p<sub>i</sub>}} that an observation falls into the {{mvar|i}}th class. So we have the expected numbers {{math|''m<sub>i</sub>'' {{=}} ''np<sub>i</sub>''}} for all {{mvar|i}}, where
:<math>\begin{align} & \sum^k_{i=1}{p_i} = 1 \\[8pt] & \sum^k_{i=1}{m_i} = n\sum^k_{i=1}{p_i} = n \end{align}</math>
Pearson proposed that, under the circumstance of the null hypothesis being correct, as {{math|''n'' → ∞}} the limiting distribution of the quantity given below is the {{math|χ<sup>2</sup>}} distribution.
:<math>X^2=\sum^k_{i=1}{\frac{(x_i-m_i)^2}{m_i}}=\sum^k_{i=1}{\frac{x_i^2}{m_i}-n}</math>
Pearson dealt first with the case in which the expected numbers {{mvar|m<sub>i</sub>}} are large enough known numbers in all cells assuming every observation {{mvar|x<sub>i</sub>}} may be taken as normally distributed, and reached the result that, in the limit as {{mvar|n}} becomes large, {{math|''X''{{isup|2}}}} follows the {{math|χ<sup>2</sup>}} distribution with {{math|''k'' − 1}} degrees of freedom.
However, Pearson next considered the case in which the expected numbers depended on the parameters that had to be estimated from the sample, and suggested that, with the notation of {{mvar|m<sub>i</sub>}} being the true expected numbers and {{math|''m''′<sub>''i''</sub>}} being the estimated expected numbers, the difference
:<math>X^2-{X'}^2=\sum^k_{i=1}{\frac{x_i^2}{m_i}}-\sum^k_{i=1}{\frac{x_i^2}{m'_i}}</math>
will usually be positive and small enough to be omitted. In a conclusion, Pearson argued that if we regarded {{math|''X''′{{isup|2}}}} as also distributed as {{math|χ<sup>2</sup>}} distribution with {{math|''k'' − 1}} degrees of freedom, the error in this approximation would not affect practical decisions. This conclusion caused some controversy in practical applications and was not settled for 20 years until Fisher's 1922 and 1924 papers.<ref name = Fisher1922>
{{cite journal | last = Fisher | first = Ronald A. | author-link = Ronald A. Fisher | title = On the Interpretation of {{math|χ<sup>2</sup>}} from Contingency Tables, and the Calculation of P | journal = Journal of the Royal Statistical Society | volume = 85 | issue = 1 | year = 1922 | pages = 87–94 | jstor = 2340521 | doi=10.2307/2340521 }}
</ref><ref name = Fisher1924> {{cite journal | last = Fisher | first = Ronald A. | author-link = Ronald A. Fisher | title = The Conditions Under Which {{math|χ<sup>2</sup>}} Measures the Discrepancey Between Observation and Hypothesis | journal = Journal of the Royal Statistical Society | volume = 87 | issue = 3 | year = 1924 | pages = 442–450 | jstor = 2341149 }}</ref>
== Other examples of chi-squared tests ==
One test statistic that follows a chi-squared distribution exactly is the test that the variance of a normally distributed population has a given value based on a sample variance. Such tests are uncommon in practice because the true variance of the population is usually unknown. However, there are several statistical tests where the chi-squared distribution is approximately valid:
=== Fisher's exact test === For an exact test used in place of the 2 × 2 chi-squared test for independence when all the row and column totals were fixed by design, see Fisher's exact test. When the row or column margins (or both) are random variables (as in most common research designs) this tends to be overly conservative and underpowered.<ref name="campbell2007">{{cite journal | last=Campbell | first=Ian | title=Chi-squared and Fisher–Irwin tests of two-by-two tables with small sample recommendations | journal=Statistics in Medicine | volume=26 | issue=19 | date=2007-08-30 | issn=0277-6715 | doi=10.1002/sim.2832 | pages=3661–3675| pmid=17315184 }}</ref>
=== Binomial test === For an exact test used in place of the 2 × 1 chi-squared test for goodness of fit, see binomial test.
=== Other chi-squared tests === * Cochran–Mantel–Haenszel chi-squared test. * McNemar's test, used in certain {{nowrap|2 × 2}} tables with pairing * Tukey's test of additivity * The portmanteau test in time-series analysis, testing for the presence of autocorrelation * Likelihood-ratio tests in general statistical modelling, for testing whether there is evidence of the need to move from a simple model to a more complicated one (where the simple model is nested within the complicated one).
== Yates's correction for continuity == {{Main|Yates's correction for continuity}} Using the chi-squared distribution to interpret Pearson's chi-squared statistic requires one to assume that the discrete probability of observed binomial frequencies in the table can be approximated by the continuous chi-squared distribution. This assumption is not quite correct and introduces some error.
To reduce the error in approximation, Frank Yates suggested a correction for continuity that adjusts the formula for Pearson's chi-squared test by subtracting 0.5 from the absolute difference between each observed value and its expected value in a {{nowrap|2 × 2}} contingency table.<ref name=Yates>{{cite journal|author-link=Frank Yates|last=Yates|first=Frank|date=1934|title=Contingency table involving small numbers and the {{math|χ<sup>2</sup>}} test|journal=Supplement to the Journal of the Royal Statistical Society|volume=1|issue=2|pages=217–235|jstor=2983604|doi=10.2307/2983604}}</ref> This reduces the chi-squared value obtained and thus increases its ''p''-value.
==Chi-squared test for variance in a normal population== If a sample of size {{math|''n''}} is taken from a population having a normal distribution, then there is a result (see distribution of the sample variance) which allows a test to be made of whether the variance of the population has a pre-determined value. For example, a manufacturing process might have been in stable condition for a long period, allowing a value for the variance to be determined essentially without error. Suppose that a variant of the process is being tested, giving rise to a small sample of {{math|''n''}} product items whose variation is to be tested. The test statistic {{math|''T''}} in this instance could be set to be the sum of squares about the sample mean, divided by the nominal value for the variance (i.e. the value to be tested as holding). Then {{math|''T''}} has a chi-squared distribution with {{math|''n'' − 1}} degrees of freedom. For example, if the sample size is 21, the acceptance region for {{math|''T''}} with a significance level of 5% is between 9.59 and 34.17. <!-- ==Chi-squared test for contingency table example== Dispute: This example is actually for a goodness-of-fit test, and NOT a test of independence in a contingency table Dispute claim is valid
A chi-squared test may be applied on a contingency table for testing a null hypothesis of independence of rows and columns.
As an example of the use of the chi-squared test, a fair coin is one where heads and tails are equally likely to turn up after it is flipped. Suppose one is given a coin and asked to test if it is fair. After 200 trials, heads turn up 153 times and tails result 147 times. The following is a chi-squared analysis, where the null hypothesis is that the coin is fair:
{|class="wikitable" align="center" |+ Chi-squared calculation of coin tosses | | | | Heads | Tails | Total |- | | Observed | | 53 | | 47 | | 100 |- | | Expected | | 50 | | 50 | | 100 |- | | {{math|(''O'' − ''E'')<sup>2</sup>}} | | 9 | | 9 | | |- | | {{math|1=χ<sup>2</sup> = (''O'' − ''E'')<sup>2</sup>/''E''}} | | 0.18 | | 0.18 | | 0.36 |} In this case, the test has one degree of freedom and the chi-squared value is 0.36. To see whether this result is statistically significant, the p-value (the probability that at least as extreme a result is observed when the null hypothesis is true) must be calculated or looked up in a chart. The p-value, {{math|Prob(χ<sup>2</sup> ≥ 0.36)}}, is found to be 0.5485. There is thus a probability of about 55% of seeing data that deviates at least this much from the expected results if indeed the coin is fair. This probability is not considered statistically significant evidence of an unfair coin.-->
==Example chi-squared test for categorical data==
Suppose there is a city of 1,000,000 residents with four neighborhoods: {{math|''A''}}, {{math|''B''}}, {{math|''C''}}, and {{math|''D''}}. A random sample of 650 residents of the city is taken and their occupation is recorded as "white collar", "blue collar", or "no collar". The null hypothesis is that each person's neighborhood of residence is independent of the person's occupational classification. The data are tabulated as:
:{| class="wikitable" style="text-align: right;" |- ! !! {{math|''A''}} !! {{math|''B''}} !! {{math|''C''}} !! {{math|''D''}} !! Total |- |style="text-align: left;"| White collar || 90 || 60 || 104 || 95 || 349 |- |style="text-align: left;"| Blue collar || 30 || 50 || 51 || 20 || 151 |- |style="text-align: left;"| No collar || 30 || 40 || 45 || 35 || 150 |- !style="text-align: left;"| Total || 150 || 150 || 200 || 150 || 650 |}
Let us take the sample living in neighborhood {{math|''A''}}, 150, to estimate what proportion of the whole 1,000,000 live in neighborhood {{math|''A''}}. Similarly we take {{sfrac|349|650}} to estimate what proportion of the 1,000,000 are white-collar workers. By the assumption of independence under the hypothesis we should "expect" the number of white-collar workers in neighborhood {{math|''A''}} to be
: <math> 150\times\frac{349}{650} \approx 80.54 </math>
Then in that "cell" of the table, we have
: <math>\frac{\left(\text{observed}-\text{expected}\right)^2}{\text{expected}} = \frac{\left(90-80.54\right)^2}{80.54} \approx 1.11</math>
The sum of these quantities over all of the cells is the test statistic; in this case, <math> \approx 24.57 </math>. Under the null hypothesis, this sum has approximately a chi-squared distribution whose number of degrees of freedom is
: <math> (\text{number of rows}-1)(\text{number of columns}-1) = (3-1)(4-1) = 6 </math>
If the test statistic is improbably large according to that chi-squared distribution, then one rejects the null hypothesis of independence.
A related issue is a test of homogeneity. Suppose that instead of giving every resident of each of the four neighborhoods an equal chance of inclusion in the sample, we decide in advance how many residents of each neighborhood to include. Then each resident has the same chance of being chosen as do all residents of the same neighborhood, but residents of different neighborhoods would have different probabilities of being chosen if the four sample sizes are not proportional to the populations of the four neighborhoods. In such a case, we would be testing "homogeneity" rather than "independence". The question is whether the proportions of blue-collar, white-collar, and no-collar workers in the four neighborhoods are the same. However, the test is done in the same way.
==Applications==
In cryptanalysis, the chi-squared test is used to compare the distribution of plaintext and (possibly) decrypted ciphertext. The lowest value of the test means that the decryption was successful with high probability.<ref name=practicalcrypto>{{cite web|title=Chi-squared Statistic|url=http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-statistic/|website=Practical Cryptography|access-date=18 February 2015|archive-url=https://web.archive.org/web/20150218203349/http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-statistic/|archive-date=18 February 2015|url-status=dead}}</ref><ref name=ibmath>{{cite web|title=Using Chi Squared to Crack Codes|url=http://ibmathsresources.com/2014/06/15/using-chi-squared-to-crack-codes/|website=IB Maths Resources|date=15 June 2014 |publisher=British International School Phuket}}</ref> This method can be generalized for solving modern cryptographic problems.<ref name=elsevier>{{cite journal|last1=Ryabko|first1=B. Ya.|last2=Stognienko|first2=V. S.|last3=Shokin|first3=Yu. I.|title=A new test for randomness and its application to some cryptographic problems|journal=Journal of Statistical Planning and Inference|date=2004|volume=123|issue=2|pages=365–376|url=http://boris.ryabko.net/jspi.pdf|access-date=18 February 2015|doi=10.1016/s0378-3758(03)00149-6}}</ref>
In bioinformatics, the chi-squared test is used to compare the distribution of certain properties of genes (e.g., genomic content, mutation rate, interaction network clustering, etc.) belonging to different categories (e.g., disease genes, essential genes, genes on a certain chromosome etc.).<ref name=pnas-bics>{{cite journal|last1=Feldman|first1=I.|last2=Rzhetsky|first2=A.|last3=Vitkup|first3=D.|title=Network properties of genes harboring inherited disease mutations|journal=PNAS|date=2008|volume=105|issue=11|pages=4323–432|doi=10.1073/pnas.0701722105|bibcode=2008PNAS..105.4323F|pmc=2393821|pmid=18326631|doi-access=free}}</ref><ref name=chi-bics>{{cite web|title=chi-square-tests|url=https://visa.pharmacy.wsu.edu/bioinformatics/documents/chi-square-tests.pdf|access-date=29 June 2018|archive-url=https://web.archive.org/web/20180629131548/https://visa.pharmacy.wsu.edu/bioinformatics/documents/chi-square-tests.pdf|archive-date=29 June 2018|url-status=dead}}</ref>
== Limitations == {{unsourced section|date=April 2026}} The chi-squared test, while widely used for categorical data analysis, has several important limitations that researchers should consider. First, it assumes that observations are independent; violating this assumption can lead to misleading results. Second, the test is sensitive to sample size. In very large samples, even trivial differences between observed and expected frequencies may produce statistically significant results, while in very small samples, the test may lack power to detect meaningful associations. Third, the chi-squared test does not measure the strength or practical importance of an association—it only indicates whether a significant relationship exists. Effect size measures, such as Cramér's V or the contingency coefficient, should be reported alongside the test statistic to provide context. Finally, the test may be inaccurate when expected frequencies in any cell are very small, often recommended to be at least 5; in such cases, exact tests or alternative methods are preferred. Researchers must also be cautious when dealing with unevenly distributed categories, as dominant groups can overshadow patterns in smaller groups.
==See also== {{Portal|Mathematics}} * Chi-squared test nomogram * Cramér's V * GEH statistic * ''G''-test * Minimum chi-square estimation * Nonparametric statistics * Wald test * Wilson score interval
== References == {{Reflist}}
* {{Cite journal |last=Homer |first=Matt |date=2017-05-30 |title=An introduction to secondary data analysis with IBM SPSS statistics (1st ed.) |url=https://doi.org/10.1080/00131911.2017.1330503 |journal=Educational Review |volume=70 |issue=2 |pages=251–252 |doi=10.1080/00131911.2017.1330503 |issn=0013-1911|url-access=subscription }}
==Further reading== * {{MathWorld | urlname=Chi-SquaredTest | title=Chi-Squared Test}} * {{cite book |last1=Corder |first1=G. W. |last2=Foreman |first2=D. I. |title=Nonparametric Statistics: A Step-by-Step Approach |year=2014 |publisher=Wiley |location=New York |isbn=978-1118840313 |ref=none|url=https://books.google.com/books?id=hYVYAwAAQBAJ&q=%22Chi-square%22}} * {{cite book |last1=Greenwood |first1=Cindy |author-link1=Cindy Greenwood |last2=Nikulin |first2=M. S. |title=A guide to chi-squared testing |year=1996 |publisher=Wiley |location=New York |isbn=0-471-55779-X |ref=none}} * {{cite conference |last=Nikulin |first=M. S. |title=Chi-squared test for normality |year=1973 |work=Proceedings of the International Vilnius Conference on Probability Theory and Mathematical Statistics |volume=2 |pages=119–122 |ref=none}} * {{cite journal | last1 = Bagdonavicius | first1 = Vilijandas B. | last2 = Nikulin | first2 = Mikhail S. | journal = International Journal of Applied Mathematics & Statistics | mr = 2800388 | pages = 30–50 | title = Chi-squared goodness-of-fit test for right censored data | url = https://www.researchgate.net/publication/325193712 | volume = 24 | year = 2011}}
{{Statistics|inference}} {{Authority control}}
{{DEFAULTSORT:Chi-Squared Test}} Category:Statistical tests for contingency tables Category:Nonparametric statistics