{"id":174026,"date":"2023-11-27T09:24:08","date_gmt":"2023-11-27T08:24:08","guid":{"rendered":"https:\/\/liora.io\/en\/?p=174026"},"modified":"2026-08-09T18:48:58","modified_gmt":"2026-08-09T17:48:58","slug":"chi-squared-test-find-out-more-about-this-essential-statistical-test","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/chi-squared-test-find-out-more-about-this-essential-statistical-test","title":{"rendered":"Chi squared test: Find out more about this essential statistical test"},"content":{"rendered":"\n<p><strong>The chi squared test (or chi 2) is a statistical test for variables that take a finite number of possible values, making them categorical variables. As a reminder, a statistical test is a method used to determine whether a hypothesis, known as the null hypothesis, is consistent with the data or not.<\/strong><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-purpose-of-the-chi-squared-test\">What is the purpose of the Chi squared test?<\/h2>\n\n\n<p>The advantage of the <strong>Chi squared test<\/strong> is its wide range of applications:<\/p>\n\n\n<ul class=\"wp-block-list\"><li><strong>Test of goodness<\/strong> of fit to a predefined law or family of laws, for example: Does the size of a population follow a normal distribution?<\/li><li><strong>Test of independence,<\/strong> for example: Is hair color independent of gender?<\/li><li><strong>Homogeneity test:<\/strong> <a href=\"https:\/\/liora.io\/en\/datasets-top-5-places-to-find-quality-datasets\">Are two sets of data identically distributed?<\/a><\/li><\/ul>\n\n\n<h2 class=\"wp-block-heading\" id=\"how-does-the-chi-squared-test-work\">How does the Chi squared test work?<\/h2>\n\n\n<p>Its principle is to compare the proximity or divergence between the distribution of the sample and a theoretical distribution using the Pearson statistic <strong><span class=\"liora-inline-math\" role=\"math\">chi<sub>Pearson<\/sub><\/span>,<\/strong> which is based on the <strong>chi-squared distance.<\/strong><\/p>\n\n\n<p><strong>First problem:<\/strong> Since we have only a limited amount of data, we cannot perfectly know the distribution of the sample, but only an approximation of it, the empirical measure.<\/p>\n\n\n<p>The empirical measure <span class=\"liora-inline-math\" role=\"math\">{mathbb{P}}<sub>n,X<\/sub><\/span> represents the frequency of different observed values:<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">\u2200 x in \ud835\udd4f {mathbb{P}}<sub>n,X<\/sub> (x) = (1)\/(n) \u03a3<sub>k=1<\/sub><sup>n<\/sup> 1_{X<sub>k<\/sub> =x}<\/span><\/p>\n\n\n<p>Empirical measurement formula<\/p>\n\n\n<p>with<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">X<sub>1<\/sub>,&#8230; ,{X<sub>n<\/sub>}<\/span> = the sample<span class=\"liora-inline-math\" role=\"math\">{\ud835\udd4f}<\/span> = all possible values<\/p>\n\n\n<p>The Pearson statistic is defined as :<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">chi<sub>Pearson<\/sub> = n \u00d7 chi<sub>2<\/sub>({mathbb{P}}<sub>n,X<\/sub>, P<sub>theorique<\/sub>) = n \u00d7 \u03a3<sub>x in \ud835\udd4f<\/sub> frac{({mathbb{P}}<sub>n,X<\/sub> (x)- P<sub>theorique<\/sub>(x))<sup>2<\/sup>}{P<sub>theorique<\/sub>(x)}<\/span><\/p>\n\n\n<p>Pearson&#8217;s statistical formula<\/p>\n\n\n<p>Under the null hypothesis, which means that there is equality between the distribution of the sample and the theoretical distribution, this Pearson statistic will converge to the <strong>chi-squared<\/strong> distribution with d degrees of freedom.<\/p>\n\n\n<p>The number of degrees of freedom, d, depends on the dimensions of the problem and is generally equal to the number of possible values minus 1.<\/p>\n\n\n<p>As a reminder, the chi-2 law with d degrees of freedom<\/p>\n\n\n<p>centred reduced independent.<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">chi<sup>2<\/sup><sub>loi<\/sub>(d)<\/span><\/p>\n\n\n<p>is that of a sum of squares of d Gaussians<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">chi<sup>2<\/sup><sub>loi<\/sub>(d) := \u03a3<sub>k=1<\/sub><sup>d<\/sup> X<sub>k<\/sub> avec X<sub>k<\/sub> \u223c \u2115(0,1)<\/span><\/p>\n\n\n<p>Otherwise, this statistic will diverge to infinity, reflecting the distance between empirical and theoretical distributions.<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">Sous H<sub>0<\/sub> lim<sub>n\u2192 \u221e <\/sub> chi<sub>Pearson<\/sub> = chi<sup>2<\/sup><sub>loi<\/sub>(d). \\ Sous H<sub>1<\/sub> lim<sub>n\u2192 \u221e <\/sub> chi<sub>Pearson<\/sub> = \u221e<\/span><\/p>\n\n\n<p>Limit formula<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-benefits-of-the-chi-squared-test\">What are the benefits of the Chi squared test?<\/h2>\n\n\n<p><strong>So, we have a simple decision rule:<\/strong> if the Pearson statistic exceeds a certain threshold, we reject the initial hypothesis (the theoretical distribution does not fit the data), otherwise, we accept it.<\/p>\n\n\n<p>The advantage of the <strong>chi-squared test<\/strong> is that this threshold depends only on the chi-squared distribution and the confidence level alpha, so it is independent of the distribution of the sample.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"the-test-of-independence\">The test of independence:<\/h2>\n\n\n<p>Let&#8217;s take an example to illustrate this test: we want to determine if the genders of the first two children, X and Y, in a couple are independent?<\/p>\n\n\n<p>We have gathered the <a href=\"https:\/\/liora.io\/en\/what-is-a-dataset-how-do-i-work-with-it\">data in a contingency table:<\/a><\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th scope=\"col\">X \/ Y<\/th><th scope=\"col\">Child 2: son<\/th><th scope=\"col\">Child 2: daughter<\/th><th scope=\"col\">Total<\/th><\/tr><\/thead><tbody><tr><th scope=\"row\">Child 1: son<\/th><td>857<\/td><td>801<\/td><td>1,658<\/td><\/tr><tr><th scope=\"row\">Child 1: daughter<\/th><td>813<\/td><td>828<\/td><td>1,641<\/td><\/tr><tr><th scope=\"row\">Total<\/th><td>1,670<\/td><td>1,629<\/td><td>3,299<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<p>The<strong> Pearson statistic<\/strong> will determine if the empirical measure of the joint distribution (X, Y) is equal to the product of the empirical marginal measures, which<strong> characterizes independence:<\/strong><\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">chi<sub>Pearson<\/sub> = n \u00d7 chi<sup>2<\/sup> ({mathbb{P}}<sub>X \u00d7 Y<\/sub>, {mathbb{P}}<sub>X<\/sub> \u00d7 {mathbb{P}}<sub>Y<\/sub>) = \u03a3_{x in {daughter, son}, yin {daughter, son}} frac{(Observation<sub>x,y<\/sub> ,  Theory<sub>x,y<\/sub>)<sup>2<\/sup>}{Theory<sub>x,y<\/sub>}<\/span><\/p>\n\n\n<p>Here, Observation(x,y) represents the frequency of the value (x, y):<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">\u2200 x, y in {daughter, son} Observation<sub>x,y<\/sub> = (1)\/(n) \u03a3<sub>k=1<\/sub><sup>n<\/sup> 1_{(X<sub>k<\/sub>,Y<sub>k<\/sub>) = (x, y)}<\/span><\/p>\n\n\n<p>for exemple:<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">Observation(daughter, daughter) = (828)\/(3299) = 0.251<\/span><\/p>\n\n\n<p>For <strong>Theory(x, y), X and Y are assumed<\/strong> to be independent, so the theoretical distribution should be the product of the <strong>marginal distributions:<\/strong><\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">\u2200 x, y in {daughter, son} Theory<sub>x,y<\/sub> = Observation<sup>X<\/sup> \u00d7 Observation<sup>Y<\/sup> = \u03a3_{yin{daughter, son}} Observation<sub>x,y<\/sub> \u00d7 \u03a3_{xin{daughter, son}} Observation<sub>x,y<\/sub><\/span><\/p>\n\n\n<p>Thus, the theoretical probability for (son, son) is:<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">Theory(son, son) = (857+801)\/(3299) \u00d7 (857+813)\/(3299) = frac{1658 \u00d7 1670}{3299<sup>2<\/sup>} = 0.254<\/span><\/p>\n\n\n<p>Let&#8217;s calculate the test statistic using the following Python code:<\/p>\n\n\n<p>In our case, the variables X and Y have only 2 possible values: daughters or sons, so the dimension of the problem is (2-1)(2-1), which is 1.<\/p>\n\n\n<p>Therefore, we compare the test statistic to the<strong> chi-squared quantile<\/strong> with 1 degree of freedom using the chi2.ppf function from <a href=\"https:\/\/liora.io\/en\/scipy-all-about-the-python-machine-learning-library\">scipy.stats.<\/a><\/p>\n\n\n<p>If the test statistic is lower than the quantile and the p-value is greater than the significance level of 0.05, we cannot reject the null hypothesis with 95% confidence.<\/p>\n\n\n<p>Thus, we conclude that the gender of the first two children is independent.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-its-limits\">What are its limits?<\/h2>\n\n\n<p>While the <strong>chi squared test<\/strong> is very practical, it does have limitations. It can only detect the existence of correlations but does not measure their strength or causality.<\/p>\n\n\n<p>It relies on the approximation of the chi-squared distribution with the Pearson statistic, which is only valid if you have a sufficient amount of data. In practice, the validity condition is as follows:<\/p>\n\n\n<p><span class=\"liora-inline-math\" role=\"math\">\u2200 x in \ud835\udd4f n \u00d7 P<sub>theoretical<\/sub>(x) (1- P<sub>theoretical<\/sub>(x)) \u2265 5<\/span><\/p>\n\n\n<p>The Fisher exact test can address this limitation but requires significant computational power. In practice, it is often limited to 2&#215;2 contingency tables.<\/p>\n\n\n<p>Statistical tests are crucial in <a href=\"https:\/\/liora.io\/en\/data-science-in-education-how-data-is-transforming-schools\">Data Science to assess the relevance of explanatory variables<\/a> and validate modeling assumptions. You can find more information about the chi-squared test and other statistical tests in our module 104 ,  Exploratory Statistics.<\/p>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-scientist\">Discover our Data Scientist training<\/a><\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"references\">References:<\/h2>\n\n\n<p><a href=\"https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.stats.chi2.html\">https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.stats.chi2.html<\/a><\/p>\n\n\n<p><a href=\"https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.stats.chi2_contingency.html\">https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.stats.chi2_contingency.html<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>The chi squared test (or chi 2) is a statistical test for variables that take a finite number of possible values, making them categorical variables. As a reminder, a statistical test is a method used to determine whether a hypothesis, known as the null hypothesis, is consistent with the data or not. What is the [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":174027,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-174026","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=174026"}],"version-history":[{"count":4,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174026\/revisions"}],"predecessor-version":[{"id":210962,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174026\/revisions\/210962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/174027"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=174026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=174026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}