Derivation of the Chi-Squared Distribution

Animated Animated version available


Introduction

To understand the derivation of the chi-squared distribution, it is first necessary to become familiar with the concept of applying functions to random variables. How does a mapping change the distribution of a random variable, and how can one determine, in general, the distribution of the newly defined random variable? We will not start from scratch in this story. It is therefore advisable to read, for example, the story Applying Functions to Random Variables and Propagation of Error beforehand.

Let us begin by taking a closer look at the following sum of squared random variables, which are independent and all follow a standard normal distribution

\begin{equation} Y = X_1^2 + \cdots + X_n^2,\quad X_1,\ldots,X_n \sim \mathcal{N}(0,1). \end{equation}

What does the distribution of the newly defined random variable \(Y\) look like? This might seem like a very far-fetched example, and without further background it is; however, exactly this sum appears very frequently in statistical applications. We will not go into this background much here, but restrict ourselves to the actual derivation. How such a sum comes about is told, for example, in the story Introduction to Statistical Tests.

In any case, the question of the distribution of \(Y\) is a problem from the field of applying functions to random variables, in this case to a set of random variables \(X_i\). In this scenario, the problem is indeed not solved that quickly, because the usual approach of rearranging the above equation for the \(X_i\) is not straightforward.

However, if we look at the equation from a geometric perspective, things quickly become simpler.

Geometric Perspective on the Density

When we want to determine the probability density function of a random variable, we are essentially asking how often it takes on a certain value (or, more precisely, how often it lies in a certain range). Notice: if we set \(Y\) equal to some constant value \(y\), this defines the surface of an \(n\)-dimensional sphere, right?

\begin{equation} Y = y \Leftrightarrow X_1^2 + \cdots + X_n^2 = y \end{equation}

So let us imagine the vector space in which each \(X_i\) corresponds to exactly one axis. The value \(y\) then corresponds to the squared \(L_2\)-norm of the vector \((x_1,\ldots,x_n)^T\). If we ask which concrete values \(x_i\) add up to \(y\) in the sum of squares, these will be all the vectors that lie on the surface of the sphere with radius \(\sqrt{y}\). This is illustrated in the following figure.

Three standard normal values x₁, x₂, x₃ as a vector; all vectors with the same sum of squares y lie on a sphere of radius √y

Coming back to the density, we see that every infinitesimal range of \(Y\) geometrically corresponds to a spherical surface with infinitesimal thickness. In particular, we can therefore determine the density of \(Y\) at a point \(y\), denoted \(Y_{PDF}(y)\), via the integral along this spherical surface with radius \(\sqrt{y}\).

Since all \(X_i\) are independent and follow a standard normal distribution, the joint density at a point in this space is given by the product of the densities of the individual \(X_i\). Therefore we get

\begin{equation} f(x_1,\ldots,x_n) = \prod_{i=1}^n\frac{1}{\sqrt{2\pi}}e^{-\frac{x_i^2}{2}} = (2\pi)^{-\frac{n}{2}}e^{-\frac{1}{2}\sum_{i=1}^n x_i^2}. \end{equation}

Thus the probability density function at a point \(y\) can be expressed as

\begin{equation} Y_{PDF}(y) = \int_{surface}(2\pi)^{-\frac{n}{2}}e^{-\frac{1}{2}\sum_{i=1}^n x_i^2}\,dS \end{equation}

The clear advantage of this geometric perspective is that the product of the normal densities turns into a sum in the exponent, which again gives us a sum of squared \(x_i\). This sum is constant on the whole spherical surface, namely equal to \(y\), so we can move the entire expression out of the integral. All that remains is the function 1 integrated over the entire surface of an \(n\)-dimensional sphere. Determining the density has thus turned into the question of a formula for the surface area \(S_{n-1}\) of an \(n\)-dimensional sphere (we use the notation \(S_{n-1}\) because the surface is, so to speak, always a 'volume' of one dimension less than the dimension \(n\) of the sphere).

Concluding our geometrical insight we can state the following

\begin{equation} Y_{PDF}(y) = (2\pi)^{-\frac{n}{2}}e^{-\frac{y}{2}} \int_{surface}dS \end{equation} \begin{equation} = (2\pi)^{-\frac{n}{2}}e^{-\frac{y}{2}} S_{n-1}. \end{equation}

To be more precise, when we express the integral in terms of differentials, it reduces to the surface area multiplied by the infinitesimal thickness \(dr\). Since our probability density function is a function of \(y\), we of course still need to take the substitution \(\sqrt{y}=r\) into account. In terms of the locally valid linear map (i.e., differential), this implies \(S_{n-1}dr\) becomes \(S_{n-1}\frac{dy}{2\sqrt{y}}\) which means

\begin{equation} Y_{PDF}(y)dy = (2\pi)^{-\frac{n}{2}}e^{-\frac{y}{2}} S_{n-1} \frac{dy}{2y^{\frac{1}{2}}}. \end{equation}

Another approach would be to consider cumulative probabilities, that is \(P(Y\leq y)\). If you examine the previous equations, you will quickly realize that this involves integrating over the entire space inside the sphere. However, when looking at the concrete equation

\begin{equation} \int_{x_1^2+\cdots+x_n^2\leq y}\prod_{i=1}^{n}\frac{1}{\sqrt{2\pi}}e^{-\frac{x_i^2}{2}}\,dx_1\cdots dx_n \end{equation} \begin{equation} = \frac{1}{(2\pi)^{\frac{n}{2}}}\int_{x_1^2+\cdots+x_n^2\leq y} e^{-\frac{1}{2}\sum x_i^2}\,dx_1\cdots dx_n, \end{equation}

we see that we can not move the term \(e^{-\frac{1}{2}\sum x_i^2}\) out of the integral, because unlike in the direct approach to the probability density function, this expression is not constant with respect to the integral. So we will leave this approach aside for now. However, there is also a possible way to solve it via cumulative probabilities. We will come back to it at the end of this Story.

Prerequisites for the Surface Area of an \(n\)-Dimensional Sphere

Before we start on deriving an expression for the surface area, we will try to simplify the problem as much as possible. This is always a useful approach for tackling complex riddles. By breaking it down to its most simple version we only have to solve its core question. From there, we can build upon our solution by exploring how it relates to more complex variations of the problem.

In our case, it is surely advantageous to focus on a unit sphere in the familiar 2- or 3-dimensional space first. Once we have solved for that, we can examine how the surface area scales with the radius and the dimension \(n\). It could be that it is easier to determine the volume rather than the surface area. If we understand the relationship between volume and surface area, solving just for the volume alone will be sufficient as well.

Let us start with the most easy prerequisite. Let us limit ourselves to a unit sphere and try to find how it scales if we move to a different value for the radius \(r\). Since changing the radius \(r\) can be interpreted as just scaling all coordinate axes by some factor, that is a simple change of units, any \(n\)-dimensional volume will of course change by that factor to the power of \(n\). For the volume of a sphere, this means

\begin{equation} V_n(r) = r^nV_n(1). \end{equation}

For the surface area, the approach is quite similar. However, since the surface area is a measure of a dimension lower than the actual volume (for a 2-dimensional sphere, the surface is a 1-dimensional line; for a 3-dimensional sphere, the surface is a 2-dimensional area), the scaling factor is \(r^{n-1}\). Consequently, we arrive at the following relationship

\begin{equation} S_{n-1}(r) = r^{n-1}S_{n-1}(1). \end{equation}

Having established how the volume and the surface scale with the radius \(r\), the next step will be to find the direct relationship between the surface and the volume. For that, we will take a geometric perspective again. Imagine a sphere approximated by thin pyramids, with their tops lying in the center of the sphere. In the limit, if the size of these pyramids gets towards zero and the number of pyramids increases towards infinity, the sum of the volumes of all those pyramids will obviously converge towards the volume of that sphere. Furthermore, since the alignment of the base of those pyramids matches the tangent plane of the sphere at that point, the sum of all the bottom surfaces will also converge toward the total surface area of that sphere. (The alignment with the tangent plane is essential here: the derivative of the approximation also has to approach the derivative of the sphere's surface, otherwise one could easily 'prove' that the circumference of a circle is 8 times its radius.) In the following illustration, this approximation is shown for the 3-dimensional case.

A sphere approximated by pyramids with base Bᵢ and height r, getting finer until it becomes the sphere in the limit

The volume of a single pyramid is given by the area of its base \(B_i\) times its height, which in our case is always the radius \(r\), divided by \(n\). The division by \(n\) might not be immediately obvious. While it is well known in 3 dimensions that one divides by 3, it is less clear that this continues in the same way for all dimensions. At least for the 2-dimensional case, we see that this generalization holds — the area of a triangle is the base times the height divided by 2. Let us find out how we can show that this principle indeed holds in all dimensions. We have already explored this connection a little in the story Cavalieri — Solving the Integral of xⁿ with Nothing but Geometry — the volume of an \(n\)-dimensional pyramid is closely tied to the area under the curve \(x^{n-1}\), and there the integral readily shows that we have to divide by \(n\).

If we take a closer look at the cross-sections of a pyramid at different heights, we find that each of them is always a smaller, similar copy of the base. How much smaller depends only on how far we have moved away from the base along the height. Since each cross-section is an \((n-1)\)-dimensional object, its area in an \(n\)-dimensional pyramid scales with the fraction of the height that is still left up to the top, raised to the power \(n-1\). In the 3-dimensional case, for example, if we stand at half the height, the area of the cross-section will be \((\frac{1}{2})^2 = \frac{1}{4}\) of the base area.

This scaling down with the power \(n-1\) thus follows directly from the definition of a pyramid. Take any flat \((n-1)\)-dimensional shape, draw a line perpendicular to it and scale the shape down linearly along this line. The rate of scaling determines how far you get, that is, it fixes the height. The cross-section, being a 'volume' of \(n-1\) dimensions, consequently scales with the scaling factor raised to the power \(n-1\).

To sum up, for our approximation we can now state that

\begin{equation} V_n^{\text{pyramid}} = \int_0^r B \left(1-\frac{x}{r}\right)^{n-1} dx \end{equation}

holds. Here we have simply integrated the cross-sections to arrive at the volume of the pyramid. Applying the substitution \(u = 1-\frac{x}{r}\), that is, thinking in proportions of the total height, we get

\begin{equation} V_n^{\text{pyramid}} = - r\int_1^0 B u^{n-1}\, du = rB\, \frac{u^n}{n}\bigg\rvert_0^1, \end{equation}

which solves to the following relationship

\begin{equation} V_n^{\text{pyramid}} = B\, \frac{r}{n}. \end{equation}

Since the volume of the sphere is approximated by the sum of the volumes of the pyramids, and the surface by the sum of the bases of exactly the same pyramids, we can state the following relationship between the volume and the surface area of an \(n\)-dimensional sphere

\begin{equation} V_n(r) = \sum_i B_i\, \frac{r}{n} = S_{n-1}(r)\, \frac{r}{n}. \end{equation}

Deriving the Surface Area of an \(n\)-Dimensional Sphere

The preliminary considerations we have just made will help us tremendously in finding an expression for the surface area of an \(n\)-dimensional sphere. So let us start with the actual derivation. As mentioned before, one approach would be to first find a solution for low, familiar dimensions and then determine how it scales with the dimension \(n\). For instance, a simple recursion between \(n\) and \(n+1\) would already be enough to solve the problem. So let us try to find something in this direction.

Why is this approach via a recursion so well suited? If we intersect a sphere of any dimension with a (hyper)plane (the generalization of a plane in an \(n\)-dimensional space), we get a sphere again, namely an \((n-1)\)-dimensional one.

Let us look at this geometrically in the familiar 3-dimensional space. In the following figure, we see a sphere that is intersected by a plane. The resulting intersection is a circle, that is, a '2-dimensional sphere'.

A sphere cut by a hyperplane in a lower-dimensional sphere, which gives the recursion Vₙ(1) = Vₙ₋₁(1) ∫ (1 − x²)^(n/2) dx

From this figure, we can easily deduce that our claim holds for \(n=3\), but how do we generalize it? Let us go through it rigorously.

Let \(X_1,\ldots,X_n \in \mathbb{R}\) be arbitrary numbers and \(c_1, c_2 \in \mathbb{R}\) with \(c_2^2 \leq c_1\) be fixed constants. If we call the set of all numbers satisfying \(\sum_{i=1}^n X_i^2 = c_1\) the surface of an \(n\)-dimensional sphere of radius \(\sqrt{c_1}\) and the set of all points with \(X_j = c_2\) a hyperplane perpendicular to the \(j\)-th axis, what is the set of numbers that satisfies both conditions at once?

First of all, note that restricting ourselves to a hyperplane perpendicular to one axis is no loss of generality, due to the rotational symmetry of the sphere. Next, we consider our condition \(\sum_{i=1}^n X_i^2 = c_1\) under the additional constraint that \(X_j = c_2\)

\begin{equation} X_1^2+\cdots+X_{j-1}^2+c_2^2+X_{j+1}^2+\cdots+X_n^2 = c_1 \end{equation}

which can be rearranged to

\begin{equation} X_1^2+\cdots+X_{j-1}^2+X_{j+1}^2+\cdots+X_n^2 = c_1-c_2^2 \end{equation}

If we now set \(c_3 = c_1-c_2^2\), it follows from \(c_2^2 \leq c_1\) that \(c_3 \geq 0\), and we find ourselves in the same situation as in \(\sum_{i=1}^n X_i^2 = c_1\), just that the sum now only runs over \(n-1\) terms and the constant is \(c_3\). Therefore, this set of numbers can be called the surface of an \((n-1)\)-dimensional sphere with radius \(\sqrt{c_1-c_2^2}\). Of course, the same argument would also work for the volume of a sphere, we would just have to replace the equal sign by a less-than-or-equal sign.

With this geometric insight, now also verified rigorously, we will set up a recursion by establishing a relationship between the intersection, that is, the \((n-1)\)-dimensional sphere, and the \(n\)-dimensional sphere. The most direct way is to note that the integral over all cross-sections equals the volume of the \(n\)-dimensional sphere, similar to what we did with the pyramids. We thus get

\begin{equation} V_n(1) = \int_{-1}^1V_{n-1}\left(\sqrt{1-x^2}\right)dx. \end{equation}

We can now simplify this by reusing the scaling with the radius we determined before, \(V_n(r) = r^nV_n(1)\), which turns the above equation into

\begin{equation} V_n(1) = V_{n-1}(1)\int_{-1}^1(1-x^2)^{\frac{n-1}{2}}dx \end{equation}

All that remains now is to solve this integral. If you have even a little experience in calculus, you will share the feeling that this integral is begging for a trigonometric substitution. So let us choose \(x = \cos(\varphi)\), which gives us

\begin{equation} \int_{0}^{\pi}\sin^n(\varphi)\,d\varphi. \end{equation}

So after the substitution, the integral looks much simpler, but finding a solution for it is still quite a challenge. Since trigonometric functions recur in their derivatives, it makes sense to see what integration by parts brings. Having done this, we get

\begin{equation} \int_{0}^{\pi}\sin^n(\varphi)\,d\varphi = -\sin^{n-1}(\varphi)\cos(\varphi) \Big\rvert_{0}^{\pi} \end{equation} \begin{equation} + \int_{0}^{\pi}(n-1)\sin^{n-2}(\varphi)\cos^2(\varphi)\,d\varphi, \end{equation}

whereby we see that the first term is 0 and the second one can again be expressed purely in terms of sine functions

\begin{equation} \int_{0}^{\pi}\sin^n(\varphi)\,d\varphi = \end{equation} \begin{equation} \int_{0}^{\pi}(n-1)\sin^{n-2}(\varphi)-(n-1)\sin^n(\varphi)\,d\varphi, \end{equation}

from which we can deduce the following recursive pattern

\begin{equation} I_n=\frac{n-1}{n}I_{n-2}, \quad \text{where} \quad I_n = \int_{0}^{\pi}\sin^n(\varphi)\,d\varphi. \end{equation}

This in turn is sufficient to solve the integral, because of course we know

\begin{equation} I_0 = \pi, \quad I_1 = 2. \end{equation}

This integral is also of greater importance outside the context of this derivation, and it can be used to determine an infinite product for \(\pi\), the so-called Wallis product. The story of its original derivation via Wallis' so-called method of interpolation can be explored in Seeking an Expression for π — Wallis' Audacious Adventure.

Coming back to our \(n\)-dimensional sphere, we can now summarize that

\begin{equation} V_n(1) = \frac{n-1}{n}\frac{n-3}{n-2}\cdots\pi\quad\cdot \frac{n-2}{n-1}\frac{n-4}{n-3}\cdots 2 \end{equation} \begin{equation} \cdot \frac{n-3}{n-2}\frac{n-5}{n-4}\cdots\pi \quad\cdot \cdots\cdot\frac{1}{2}\pi \cdot 2 \end{equation}

must hold, whereby the exact first term of the product (either the one with \(\pi\) or the one with the factor 2) depends on whether the dimension \(n\) is even or odd. Whichever it is, it is obvious that a lot of terms will cancel out. The numerator always cancels with the denominator of the following term. Hence, we are left with the denominator of the first term, which is \(n(n-2)(n-4)\cdots\) down to 2 (if \(n\) is even) or 1 (if \(n\) is odd), and the numerator of the last term, which is just 1, together with the \(\pi\)'s and the factors 2. In particular, the number of these (\(\pi\) and 2) increases with every second step in \(n\), and so we get

\begin{equation} V_n(1) = \frac{\pi^{[\frac{n}{2}]}\, 2^{[\frac{n+1}{2}]}}{n(n-2)(n-4)\cdots} \end{equation}

This result looks quite promising, but the Gaussian brackets and the associated distinction between even and odd \(n\) are somewhat annoying. Notice that if we combine the denominator with the product of 2's, we get exactly \(1/(\frac{n}{2})!\) when \(n\) is even. For odd \(n\), we get a product of the form \(1/\big(\frac{n}{2}\cdot(\frac{n}{2}-1)\cdots\frac{1}{2}\big)\). This sequence also resembles a factorial, just for half-integer values, but it does not really interpolate the factorials of the integers well. Even if the concept behind something like \(\frac{3}{2}!\) seems rather unconventional at first glance, we could at least agree that the central defining property \((n-1)!\cdot n = n!\) should hold, which would be the case for our product. In addition, we could define \(\frac{1}{2}!\) such that it interpolates the integer values better. In fact, \(\frac{1}{2}! = \frac{\sqrt{\pi}}{2}\) would work perfectly, and it would also simplify our result a lot, because it removes the inconvenience that the number of \(\pi\) factors only increases every second step. So if we interpolate the factorial function for half steps in this way, the Gaussian brackets vanish and we are left with

\begin{equation} V_n(1) = \frac{\pi^{\frac{n}{2}}}{(\frac{n}{2})!}. \end{equation}

Whether one regards this as the continuous version of the factorial or not is a matter of perspective. It is certainly a possible extension, but we could also simply say that we have defined a new function, say \(P\),

\begin{equation} P\Big(\frac{n}{2}\Big) = \Big(\frac{n}{2}\Big)! \quad \text{if} \quad n \quad \text{even}, \end{equation} \begin{equation} P\Big(\frac{n}{2}\Big) = \sqrt{\pi} \prod_{i=1}^{\frac{n+1}{2}} \frac{2i-1}{2} \quad \text{if} \quad n \quad \text{odd}, \end{equation}

in order to write down our result in a clearer and more elegant way. In fact, however, it turns out that this kind of interpolation of the factorials is encountered very often, and, assuming logarithmic convexity, it is also the only possible one (the Bohr–Mollerup theorem). It has therefore been given a name: the so-called gamma function, originally discovered by Euler, is nowadays usually denoted by \(\Gamma\), with \(\Gamma(x+1) = x!\), and is defined on the whole complex plane except for 0 and the negative integers. However, regarding this function merely as an interpolation of the factorials would be just as limited a view as regarding \(\pi\) merely as the ratio of a circle's circumference to its diameter.

Without going into further detail — we will do that in another story —, let us continue with our derivation of the volume of an \(n\)-dimensional sphere. Summarizing the recursion we obtained and extending it to an arbitrary radius with the scaling in \(r\) we derived before, we get

\begin{equation} V_n(r) = \frac{r^n\pi^{\frac{n}{2}}}{(\frac{n}{2})!}. \end{equation}

Using the relationship between volume and surface area, we can now also easily switch to a formula for the surface area, which gives us

\begin{equation} S_{n-1}(r) = \frac{r^{n-1}n\pi^{\frac{n}{2}}}{(\frac{n}{2})!} = \frac{2r^{n-1}\pi^{\frac{n}{2}}}{(\frac{n}{2}-1)!} \end{equation}

This finally allows us to state a complete expression for the probability density function of our random variable \(Y\)

\begin{equation} Y_{PDF}(y)\,dy = (2\pi)^{-\frac{n}{2}}e^{-\frac{y}{2}} \frac{2y^{\frac{n-1}{2}}\pi^{\frac{n}{2}}}{(\frac{n}{2}-1)!} \frac{dy}{2y^{\frac{1}{2}}}. \end{equation}

Obviously, the \(\pi\) terms and both terms of 2's cancel, and after combining the powers of \(y\), we are left with

\begin{equation} Y_{PDF}(y)\,dy = e^{-\frac{y}{2}} \frac{y^{\frac{n}{2}-1}}{2^{\frac{n}{2}}(\frac{n}{2}-1)!}\, dy. \end{equation}

And that is it. We have successfully derived the probability density function of \(Y\). Writing our function for the factorials as the gamma function and taking into account that the density is of course 0 for negative values (the derivation above only makes sense if \(y\) is positive, since a sphere with a negative radius does not really make sense), the final result of our little journey of discovery is given by

\begin{equation} Y_{PDF}(y) = \left\{\begin{array}{ll} e^{-\frac{y}{2}} \frac{y^{\frac{n}{2}-1}}{2^{\frac{n}{2}}\Gamma(\frac{n}{2})} & \text{if} \quad y > 0 \\ 0 & \text{otherwise}\end{array}\right. \end{equation}

To round things off, let us take a quick look at the graph of this density, as well as at the other way of deriving it via the cumulative probability. The following figure shows the density function (no longer written with \(y\), but in the more common notation \(f(x)\)).

Density of the chi-squared distribution for n = 2, falling from its maximum at 0, and for n = 4, with a hump

We see that the shape of the graph changes significantly as soon as \(n\) is greater than 2. For the first two dimensions, we get a steeply falling curve with its maximum at 0. For higher dimensions, it rather resembles a normal distribution with a hump. This is no coincidence: the random variable \(Y\) is defined as the sum of independent, identically distributed random variables, and due to the central limit theorem, its distribution approaches a normal distribution for large \(n\).

Geometric Perspective on the Cumulative Distribution

As a last point, as already mentioned, we will briefly turn to the derivation via the cumulative probability. We will keep it rather short so that the story does not get too long.

Let us go back to the very beginning, and instead of asking how often the random variable lands in an infinitesimal region, we consider

\begin{equation} P(Y\leq y) = P(X_1^2+\cdots+X_n^2\leq y). \end{equation}

If we put this equation into the same geometric context again, we see that once more a sphere emerges. However, because of the \(\leq y\), we are no longer interested in its surface but have to integrate over its entire interior. Writing this out as an integral, we get

\begin{equation} \frac{1}{(2\pi)^{\frac{n}{2}}}\int_{x_1^2+\cdots+x_n^2\leq y} e^{-\frac{1}{2}\sum x_i^2}\,dx_1\cdots dx_n. \end{equation}

As already mentioned, the problem now is that \(e^{-\frac{1}{2}\sum x_i^2}\) is not constant over the domain of integration. Therefore, it is no longer enough to simply determine the volume of an \(n\)-dimensional sphere. However, if we look at the whole thing in spherical coordinates

\begin{equation} \frac{1}{(2\pi)^{\frac{n}{2}}}\int_{0}^{2\pi}\int_0^{\pi}\cdots\int_0^{\pi}\int_0^{\sqrt{y}}e^{-\frac{r^2}{2}}r^{n-1}\,dr \end{equation} \begin{equation} \sin^{n-2}(\varphi_1)\,d\varphi_1\cdots\sin(\varphi_{n-2})\,d\varphi_{n-2}\,d\varphi_{n-1}, \end{equation}

a way will reveal itself. Obviously, we can now separate the integrals, which leads to

\begin{equation} \frac{1}{(2\pi)^{\frac{n}{2}}}\int_0^{\sqrt{y}}e^{-\frac{r^2}{2}}r^{n-1}\,dr\int_{0}^{2\pi}d\varphi_{n-1} \end{equation} \begin{equation} \int_0^{\pi}\sin^{n-2}(\varphi_1)\,d\varphi_1\cdots\int_0^{\pi}\sin(\varphi_{n-2})\,d\varphi_{n-2}. \end{equation}

Here, we can quickly see that the second integral is given by \(2\pi\). The other integrals contain powers of the sine function, which should look familiar. As mentioned before, this integral shows up quite often.

Plugging in the solution we already derived above, we end up with

\begin{equation} \frac{1}{(2\pi)^{\frac{n}{2}-1}}\int_0^{\sqrt{y}}e^{-\frac{r^2}{2}}r^{n-1}\,dr\frac{(n-3)(n-5)\cdots}{(n-2)(n-4)\cdots}\text{ }\cdot \end{equation} \begin{equation} \pi\frac{(n-4)(n-6)\cdots}{(n-3)(n-5)\cdots}2\text{ }\cdot\text{ }\cdots, \end{equation}

and analogous to before, starting with the first term, the numerator will cancel with the denominator of the following term, and the product simplifies in a similar way as before. The only difference is that this time we start at \(n-2\), and hence we get

\begin{equation} \frac{\pi^{[\frac{n-2}{2}]}\, 2^{[\frac{n-1}{2}]}}{(n-2)(n-4)\cdots}. \end{equation}

Expanding the factorials in the same way as before, the expression simplifies to

\begin{equation} \frac{\pi^{\frac{n}{2}-1}}{(\frac{n}{2}-1)!}. \end{equation}

Plugging this solution back into the collection of integrals from above, we can now state

\begin{equation} Y_{CDF}(y) = \frac{1}{2^{\frac{n}{2}-1}\Gamma(\frac{n}{2})}\int_0^{\sqrt{y}}e^{-\frac{r^2}{2}}r^{n-1}\,dr. \end{equation}

The remaining integral may appear complicated at first glance, and indeed, in general, it has no solution that can be expressed in terms of elementary functions (for even \(n\) it works, but for odd \(n\) it does not). However, if we simply want to obtain the density again, we do not need to solve it. By taking the derivative with respect to \(y\), the integral is effectively eliminated

\begin{equation} Y_{CDF}'(y) = \frac{1}{2^{\frac{n}{2}-1}\Gamma(\frac{n}{2})}e^{-\frac{y}{2}}y^{\frac{n-1}{2}}\frac{1}{2\sqrt{y}} \end{equation} \begin{equation} = e^{-\frac{y}{2}} \frac{y^{\frac{n}{2}-1}}{2^{\frac{n}{2}}\Gamma(\frac{n}{2})}. \end{equation}

As we can see, this result matches the one derived before. So both approaches work, although the direct way via the density seems a little clearer, because everything falls into place more nicely. All terms can be pulled out of the integral, and only the surface of the \(n\)-dimensional sphere remains.

But what about the cumulative probability of our random variable \(Y\), the cumulative distribution function? We stopped here because we encountered an integral without a real solution. However, it turns out that this integral shows up quite often, and its solution has in turn been given a name as a function: the lower incomplete gamma function, usually denoted as

\begin{equation} \gamma(n,x) = \int_0^{x}e^{-t}t^{n-1}\,dt. \end{equation}

We will not go into further detail here, but we can at least write down a result for the cumulative distribution function of \(Y\) using the so-defined function \(\gamma\)

\begin{equation} Y_{CDF}(y) = \left\{\begin{array}{ll} \frac{1}{\Gamma(\frac{n}{2})}\gamma\big(\frac{n}{2},\frac{y}{2}\big) & \text{if} \quad y > 0 \\ 0 & \text{otherwise}\end{array}\right. \end{equation}

To fully follow this, it should be noted that our extension of the factorials, the gamma function, is in fact also the solution of the integral \(\int_0^{\infty}e^{-t}t^{n-1}\,dt = \Gamma(n)\). This also explains why the integral up to \(x\) is called the lower incomplete gamma function: it represents the incomplete integral, integrating only up to \(x\). Naturally, there is also an upper incomplete gamma function, which starts integrating at \(x\) and goes all the way up to \(\infty\). However, going any deeper here would be far beyond the scope of this story.

Cumulative distribution function of the chi-squared distribution, γ(n/2, x/2)/Γ(n/2), for n = 2 and n = 4

Finally, let us take a look at the graph of the cumulative distribution function, shown here for \(n=2\) and \(n=4\), just like the density above. Again, we can see that for higher \(n\) the whole thing flattens more and more, towards the cumulative distribution function of the normal distribution.

Conclusion and Outlook

That's it for this story. We have successfully derived both the density function and the cumulative distribution function of our random variable \(Y\). We solved both from a geometric perspective. Of course, such an approach always requires some creativity, but as we could see here, this geometric point of view was very helpful. In our case, this meant in particular that we could reduce the problem to determining the \(n\)-dimensional volume of a sphere. That was also quite a challenge, but it was a concrete, isolated problem, for which we moreover already knew the solution for \(n\leq 3\). Generalizing it to arbitrary \(n\) finally led us to the answer to the question of the distribution of the random variable \(Y\).

The density and cumulative distribution function obtained in this way are usually referred to as the chi-squared distribution, or \(\chi^2\) distribution for short. It is the distribution of the sum of \(n\) squared, independent standard normally distributed random variables. Such a sum shows up quite often. Especially in the field of statistical tests, it is often possible to define a test statistic in such a way that it reduces to exactly such a normalized sum. We already examined this in more detail in the story Introduction to Statistical Tests.

The derivation itself is quite elaborate and lengthy. We have touched on many different topics, like the volume and surface of an \(n\)-dimensional sphere and the different variants of the gamma function. We saw that both are closely connected to the factorials. Here we have only seen the tip of the iceberg, and we will certainly dive deeper into this in future stories, but for now we will leave it at that.

Did you enjoy reading this story?