# Deriving $\chi^2$ density from the standard normal $Z$ density

For the sake of learning, I am trying to derive a $\chi^2$ density from the standard normal $Z$ density.

The $\chi^2_k$ is a distribution of the sum of squares of $k$ standard normal variables, where $k$ is the the number of degrees of freedom.

Taking $k=1$ for simplicity (i.e. just one standard normal variable), I have tried the following in R:

library(data.table)

DT <- data.table(x = seq(0, 3.5, by=0.01)) #In a range from 0 to 3.5

Standard normal density multiplied by 2 to calculate the density of absolute $Z$ values (i.e. negative + positive); this is equivalent to a $\chi$ density.

DT[, Z_abs := 2*dnorm(x)]

Calculate the squares:

DT[, X_squared := x^2]

And plot the density against the squares:

library(ggplot2)

ggplot(DT, aes(X_squared, Z_abs)) + geom_line() +
scale_x_continuous(limits=c(0,3.5), breaks=seq(0,3.5,0.5))

However, this is apparently wrong as it differs from the correct $\chi^2$ density given by the built-in dchisq function (shown in red).

DT[, Chisq := dchisq(X_squared, df=1)]

ggplot(DT, aes(X_squared, Z_abs)) + geom_line() +
geom_line(aes(y=Chisq), color="red") +
scale_x_continuous(limits=c(0,3.5), breaks=seq(0,3.5,0.5)) +
scale_y_continuous(limits=c(0,0.8)) +
ylab("Density")

Where did I get it wrong ?

• Have you heard of a change of variable for densities, also called the Jacobian formula? – Xi'an Jul 6 '18 at 12:51
• This is not how densities transform. Please search our site for information: stats.stackexchange.com/search?q=transform+pdf. – whuber Jul 6 '18 at 12:51
• @whuber OK, I now see there are special formulae for this, but can you explain intuitively where is the flaw in my logic above? – Mihael Jul 6 '18 at 13:17
• you do not take into account how squaring $x$ "stretches" the interval over which the pdf takes values. The correct density would be DT[, Chi2 := Z_abs* 1/(2*sqrt(X_squared))]. See this answer for an intuitive explanation – matteo Jul 6 '18 at 14:05
• Yes: In addition to the explanation referenced by @matteo, some intuition is also given at stats.stackexchange.com/questions/4220/…. – whuber Jul 6 '18 at 14:05

Let us first find the density of the absolute value of $X\sim N(0,1)$, $Y=|X|$, $g(y)$. For $y>0$, \begin{eqnarray*} G(y)\equiv\Pr(Y\leqslant y)&=&\Pr(|X|\leqslant y)\\ &=&\Pr(-y\leqslant X\leqslant y)\\ &=&F(y)-F(-y) \end{eqnarray*} and hence $$g(y)=G'(y)=f(y)+f(-y)$$ (note the second inner derivative equals $-1$). Since $y=|x|$ cannot be negative, we have $g(y)=0$ for $y<0$. For densities symmetric about zero (such as the normal density) we have $$f(y)=f(-y)$$ and therefore $$g(y)=\begin{cases}0&y<0\\ 2f(y)&y\geqslant0\end{cases}$$ For the second step (the change of variable technique that you were referred to in the comments), we have $y=w(z)=\sqrt{z}$ and hence $w'(z)=\frac{1}{2}z^{-\frac{1}{2}}$. Thus, \begin{eqnarray*} h(z)&=&\frac{2}{\sqrt{2\pi}}\exp\biggl\{-\frac{(\sqrt{z})^2}{2}\biggr\}\Bigl|\frac{1}{2}z^{-\frac{1}{2}}\Bigr|\\ &=&\frac{1}{\sqrt{2\pi}}z^{-\frac{1}{2}}e^{-\frac{z}{2}} \end{eqnarray*} for $z>0$ and 0 elsewhere. This is the density of the chi-square distribution with $\nu=1$.