# probability density function chi squared

Exercise

I've been tasked with deriving the probability density function for a chi-squared random variable

$$f(x;q) = \begin{cases} \hfill 0 \hfill & x\leq 0 \\ \hfill \tfrac{1}{\Gamma\big(\tfrac{q}{2}\big)}\big(\frac{1}{2}\big)^{q/2}x^{q/2-1}e^{-\tfrac{x}{2}} \hfill & x> 0 \\ \end{cases}$$

Useful Information

We've determined the following through previous exercises:

1. For $X\sim N(0,1)$, the PDF for $X^2$ is $$f_{X^2}(x) = \begin{cases} \hfill 0 \hfill & x\leq 0 \\ \hfill \tfrac{1}{\sqrt{2\pi x}}e^{-\tfrac{x}{2}} \hfill & x> 0. \\ \end{cases}$$

2. For two independent continuous random variables with respective PDFs $f_X$ and $f_Y$, the PDF for $Z=X+Y$ is $$f_Z(z)=\int_{-\infty}^{+\infty}f_X(z-x)f_Y(x)dx.$$

Attempt

We've been guided to show this by induction. (1) shows the base case. Set $Q=\sum_{k=1}^{q-1}X^2_k$. For $Z=Q+X^2_q$, (2) shows $$f_Z(z)=\int_{-\infty}^{+\infty}f_Q(z-x)f_{X^2_q}(x)dx.$$ Since $z-x>0$ iff $z>x$ we see that this can be rewritten as (by plugging in what the distributions are) \begin{align*} f_Z(z)&=\int_{0}^{z}\left(\tfrac{1}{\Gamma\big(\tfrac{q}{2}\big)}\big(\tfrac{1}{2}\big)^{q/2}(z-x)^{q/2-1}e^{-\tfrac{z-x}{2}}\right)\left(\tfrac{1}{\sqrt{2\pi x}}e^{-\tfrac{x}{2}}\right)dx.\\ &=\frac{\big(\frac{1}{2}\big)^{\frac{q+1}{2}}}{\Gamma\left(\frac{q}{2}\right)\Gamma\left(\frac{1}{2}\right)}\int_{0}^{z}(z-x)^{\frac{q}{2}-1}e^{x-\frac{z}{2}}dx\\ \end{align*} From here I am stuck.

Issues

I am stuck since this integral is disgusting to compute. Would it be easier using MATLAB? I can possibly use integration by parts, but I feel like this is pushing me in the wrong direction. Any help is greatly appreciated. Thanks in advance.

You have some algebra mistakes: the sum of $-\frac{z-x}2$ and $-\frac x2$ is $-\frac z2$. You also forgot to retain the $\frac1{\sqrt x}$. The final integral is then written $$\frac{\big(\frac{1}{2}\big)^{\frac{q+1}{2}}}{\Gamma\left(\frac{q}{2}\right)\Gamma\left(\frac{1}{2}\right)}\int_{0}^{z}(z-x)^{\frac{q}{2}-1}{e^{-\frac{z}{2}}\over\sqrt{\pi x}}dx\;.$$ Pulling the $e^{-\frac z2}/\sqrt{\pi}$ out of the integral, it remains to evaluate $$\int_{0}^{z}{(z-x)^{\frac{q}{2}-1}\over\sqrt{x}}dx\,,\tag1$$ which you can handle by the crazy trig substitution $\sqrt x=\sqrt z \sin \theta$. Then $${dx \over 2\sqrt x} = \sqrt z\cos\theta\, d\theta$$ and $$z-x = z-z\sin^2\theta=z\cos^2\theta$$ and the integral (1) is now $$\int_0^{\pi/2}(z\cos^2\theta)^{q/2-1}2\sqrt z\cos\theta\,d\theta =2\int_0^{\pi/2}z^{\frac{q+1}2-1}\cos^{q-1}\theta\,d\theta.$$ Consulting your favorite table of definite integrals, we find $$\int_0^{\pi/2}\cos^{q-1}\theta\,d\theta = {\sqrt\pi\Gamma\left(\frac q2\right)\over2\Gamma\left(\frac{q+1}2\right)},\tag2$$ and everything should magically cancel. Alternatively, you can take (2) as a constant to be determined and be satisfied you've got the proper form, up to a normalizing constant, for the chi-squared ($q+1$) distribution.
(I never remembered how disgusting the integral is--must by why convolution is not the usual way to connect the chi-squared density to the sum of squares of standard normals! The usual approach is to take the density as a given and use moment-generating functions to prove the chi-squared is the distribution of $\sum Z^2$.)
Such a method of solution is rather tedious. A simpler approach would be to use generating functions. Consider a sequence of IID standard normal variables $$Z_1, \ldots, Z_n \sim Z \sim \operatorname{Normal}(\mu = 0, \sigma^2 = 1), \quad f_Z(z) = \frac{1}{\sqrt{2\pi}} e^{-z^2/2}, \quad -\infty < z < \infty.$$ By transformation and symmetry, $W = Z^2$ has the density $$f_W(w) = 2f_Z(\sqrt{w}) \cdot \frac{1}{2\sqrt{w}} = \frac{1}{\sqrt{2\pi w}} e^{-w/2}, \quad w \ge 0,$$ and we take for granted that this PDF integrates to $1$ over its support. The MGF of $W$ is $$M_W(t) = \operatorname{E}[e^{tW}] = \int_{w=0}^\infty \frac{1}{\sqrt{2\pi w}} e^{-w(1/2-t)} \, dw = \left(1 - 2t\right)^{-1/2} \int_{u=0}^\infty \frac{1}{\sqrt{2\pi u}}e^{-u/2} \, du = \left(1 - 2t\right)^{-1/2},$$ where in the penultimate step we employed the substitution $u = (1-2t)w$, $du = (1-2t) \, dw$. Therefore, the random variable $$X = \sum_{i=1}^n Z_i^2$$ has MGF $$M_X(t) = (M_W(t))^n = (1-2t)^{-n/2}.$$ But if we recall that the MGF of a gamma-distributed random variable $G$ with shape $a$ and rate $b$ is $$M_G(t) = (1-t/b)^{-a}$$ (the proof of which is straightforward), then we note that $X$ is gamma-distributed with parameters $a = n/2$, $b = 1/2$, hence has PDF $$f_X(x) = \frac{x^{n/2 - 1} e^{-x/2}}{2^{n/2} \Gamma(n/2)}, \quad x \ge 0,$$ which is chi-squared with $n$ degrees of freedom. In fact, we could have recognized the MGF of $W$ directly as being gamma with shape $a = 1/2$, thus the sum of $n$ IID gamma variables would be gamma with shape $a = n/2$.