TITLE: Derivation of the density function of student t-distribution from this big integral.
QUESTION [24 upvotes]: My lecturer posed a question where we derive the density function of the student t-distribution from the Chi-square and Standard normal distribution.
I worked on this question for days, and I am pretty sure the below integral is correct (Verified by others)

$$f_T(t)=\int_{-\infty}^\infty|x|2nx\times \frac{\frac{1}{2}^{\frac{n}{2}}}{\Gamma\left(\frac{n}{2}\right)}(x^2n)^{\frac{n}{2}-1}e^{-\frac{1}{2}x^2n}\frac{1}{\sqrt{2\pi}}e^{-\frac{(xt)^2}{2}}dx$$
where n is the degree of freedom of the t-distribution and $\Gamma$ is the gamma function from the Gamma distribution.
My goal is $$f_T(t)=\frac{\Gamma[(n+1)/2]}{\sqrt{n\pi}\Gamma(n/2)}\left(1+\frac{t^2}{n}\right)^{-(n+1)/2}$$

I was given 2 hints. To proceed, I need to do integration by parts first, then I should use the fact that the Gamma d.f integrates to 1.
From this point on, I am unsure, but I shall show you my steps.

We know that the d.f of the Gamma density with parameters $\alpha=\frac{n+1}{2} \lambda=\frac{1}{2}$ integrates to $1$, that is $\int_{0}^{\infty}g(t)dt= \int_{0}^{\infty}\frac{\frac{1}{2}^{\frac{n+1}{2}}}{\Gamma\left(\frac{n+1}{2}\right)}t^{\frac{n+1}{2}-1}e^{-\frac{1}{2}t}dt=1$
Let $t=x^2n$.  Therefore,      $dt=2xn\,dx$
We have $\int_{0}^{\infty}g(t)dt=\int_{0}^{\infty}g(x^2n)2xn\,dx=\int_{0}^{\infty}\frac{\frac{1}{2}^{\frac{n+1}{2}}}{\Gamma\left(\frac{n+1}{2}\right)}(x^2n)^{\frac{n+1}{2}-1}e^{-\frac{1}{2}(x^2n)}2xn\,dx=1$
This should be useful because I noticed the $\Gamma[(n+1)/2]$ in the end result.

Working on the big integral now...
$f_T(t)=\int_{-\infty}^\infty|x|2nx\times \frac{\frac{1}{2}^{\frac{n}{2}}}{\Gamma\left(\frac{n}{2}\right)}(x^2n)^{\frac{n}{2}-1}e^{-\frac{1}{2}x^2n}\frac{1}{\sqrt{2\pi}}e^{-\frac{(xt)^2}{2}}$
$=\frac{\Gamma[(n+1]/2)]}{\sqrt{n\pi}\Gamma(n/2)}\int_{-\infty}^\infty \frac{\sqrt{n\pi}\Gamma(n/2)}{\Gamma[(n+1]/2)]}|x|2nx\times \frac{\frac{1}{2}^{\frac{n}{2}}}{\Gamma\left(\frac{n}{2}\right)}(x^2n)^{\frac{n}{2}-1}e^{-\frac{1}{2}x^2n}\frac{1}{\sqrt{2\pi}}e^{-\frac{(xt)^2}{2}}dx$
After a load of manipulation,
$=\frac{\Gamma[(n+1]/2)]}{\sqrt{n\pi}\Gamma(n/2)}\int_{-\infty}^\infty 2nx \frac{(\frac{1}{2})^{\frac{n+1}{2}}}{\Gamma [(n+1)/2)] }(x^2n)^{\frac{n-1}{2}}e^{-\frac{1}{2}(x^2n)}\times2n|x|e^{-\frac{(xt)^2}{2}}dx$
Note that the first half is integrate to 1. Hence I do by parts.

But I still cannot get to my goal.
I had tried this question for at least 6 times already now. Can I get some help? I tried to type these out as neatly as I knew how.

REPLY [6 votes]: Following Casella and Berger Statistical Inference derivation:
If the standard deviation of the population, $\sigma$, is unknown we can replace it by the estimation based on a sample, $S$, but then the expression of the one-sample t-test statistic will follow a $t$-distribution:
$$ T_{\text{test}}=\frac{\bar{X}\,-\,\mu}{S/\sqrt{n}}\sim t_{n-1}$$ 
with $$S=\sqrt{\frac{\sum(X_i-\bar X)^2}{n-1}}.$$ 
Minimal manipulations of this equation for $T_{\text{test}}$
$$\begin{align} \frac{\bar{X}\,-\,\mu}{S/\sqrt{n}} &= \frac{\bar{X}\,-\,\mu}{\frac{\sigma}{\sqrt{n}}} \frac{1}{\frac{S}{\sigma}}\\[2ex]
&= Z\,\frac{1}{\frac{S}{\sigma}}\\[2ex] 
&= \frac{Z}{\sqrt{\frac{\sum(X_i-\bar X)^2}{(n-1)\,\sigma^2}}} \sim\frac{Z}{\sqrt{\frac{\chi_{n-1}^2}{n-1}}} \sim t_{n-1}
\end{align}$$
In the above expression, 
$Z=\frac{(\bar{X}-\mu)}{\sigma/\sqrt{n}}\,\,\sim \,\,\small N(0,1)$ 
and as shown here:
$\sqrt{\frac{S^2}{\sigma^2}} =\large\sqrt{\frac{\frac{\sum_{x=1}^n(X - \bar{X})^2}{n-1}}{\sigma^2}} \,\,\sim \,\,\sqrt{\frac{\chi_{(n-1)}^2}{n-1}}$.

The derivation of the pdf of the Student's t distribution with $n$ degrees of freedom (not $n-1$ as above) proceeds by solving a simplified problem: finding the distribution of $U/{\sqrt{V/n}}$ with $U\sim N(0,1),$ and $V\sim\chi^2_n,$ mutually independent. The expression (1) is then replaced with the following expression ($k$ is used from this point on instead of $n$ for degrees of freedom) with the claim:
$$T=\frac{U}{\sqrt{\frac{V}{k}}}\sim t_k$$
With the premise of independence, the joint density of $U$ and $V$ is:
$f_{U,V}(u,v) = \underbrace{\frac{1}{(2\pi)^{1/2}} e^{-u^2/2}}_{\text{pdf } N(0,1)}\quad \underbrace{\frac{1}{\Gamma(\frac{k}{2})\,2^{k/2}}\,v^{(k/2)-1}\, e^{-v/2}}_{\text{pdf }\chi^2_k}$ with $-\infty<u<\infty$ and $0<v<\infty$.
Making the transformation $t=\frac{u}{\sqrt{v/k}}$ and $w=v$, hence, $u=t\,\left(\frac{w}{k}\right)^{1/2},$ and with $(w/k)^{1/2}$ as the Jacobian, the marginal pdf will be:
$$\begin{align}
f_T(t) &= \displaystyle\int_0^\infty \,f_{U,V}\bigg(t\,(\frac{w}{k})^{1/2},w\bigg)(w/k)^{1/2}\,\mathrm{d} w\\[2ex]
&= \frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma(\frac{k}{2})2^{k/2}}\,
\int_0^\infty\,
e^{-\frac{\left(t(\frac{w}{k})^{1/2}\right)^2}{2}}
w^{(k/2)-1}
e^{-(\frac{w}{2})}
\frac{w^{1/2}}{k^{1/2}}\,\mathrm{d}w\\[2ex]
&= \frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma(\frac{k}{2})2^{k/2}k^{1/2}}\,
\displaystyle\int_0^\infty\,
w^{((k+1)/2)-1}\,e^{-(1/2)(1 + t^2/k)w}\,\mathrm{d}w
\end{align}$$
The next step entails identifying in the previous equation the kernel of a gamma distribution pdf:
$x^{\alpha-1}\,e^{x\,\lambda}$
with parameters $\left(\alpha=(k+1)/2,\,\lambda=(1/2)(1+t^2/k)\right).$
The generic pdf for the gamma distribution is,
$\large \frac{\lambda^\alpha}{\Gamma(\alpha)}\,x^{\alpha-1}\,e^{x\,\lambda}$. 
The strategy is then to synthesize the entire gamma pdf within the improper integral in our $f_T(t)$ pdf in progress, so that we can simplify it as just $1$, as we know to be true of all pdf's. To get away with it we need to multiply numerator and denominator by the same coefficient:
$\frac{\Gamma(\alpha)\,\lambda^\alpha}{\Gamma(\alpha)\,\lambda^\alpha}$. And since neither $\alpha$ nor $\lambda$ include the integrating factor $w$ we can include them inside the integral, or leave them out. Naturally, we want to leave within the integral $\frac{\lambda^\alpha}{\Gamma(\alpha)}$, and keep $\frac{\Gamma(\alpha)}{\lambda^\alpha}$ outside the integral. Now $f_T(t)$ will look hideous for just one second:
$f_T(t)=\frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma(\frac{k}{2})2^{k/2}k^{1/2}}\,
\int_0^\infty\frac{\left((1/2)(1+t^2/k)\right)^{(k+1)/2}}{\Gamma((k+1)/2)}
w^{((k+1)/2)-1}
e^{-(1/2)(1 + t^2/k)w}
\mathrm{d}w\;
\frac{\Gamma((k+1)/2)}{((1/2)(1+t^2/k))^{(k+1)/2}}$
... because everything between $\int$ and $\mathrm{d}w$ is just the gamma $\text{pdf}$ integrated over its entire support, so it becomes $1$, and we are left with:
$$\begin{align}
f_T(t)&= \frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma\left(\frac{k}{2}\right)2^{k/2}k^{1/2}}\,
\frac{\Gamma\left((k+1)/2\right)}{\left((1/2)(1+t^2/k)\right)^{(k+1)/2}}\\[2ex]
&=\frac{1}{(2\pi)^{1/2}}\frac{1}{\Gamma\left(\frac{k}{2}\right)\,2^{k/2}k^{1/2}}\,\Gamma\left((k+1)/2\right)\,
\Big[\frac{2}{(1+t^2/k)}\Big]^{(k+1)/2}\\[2ex]
&= \frac{\Gamma\left(\frac{k+1}{2}\right)}{\Gamma\left(\frac{k}{2}\right)}\,
\frac{1}{(2\pi)^{1/2}2^{k/2}k^{1/2}}\,
\Big[\frac{2}{(1+t^2/k)}\Big]^{(k+1)/2}\\[2ex]
&=\frac{\Gamma\left(\frac{k+1}{2}\right)}{\Gamma\left(\frac{k}{2}\right)}\,
\frac{1}{(2\pi)^{1/2}2^{k/2}k^{1/2}}\,
\frac{2^{(k+1)/2}}{(1+t^2/k)^{(k+1)/2}}\\[2ex]
&= \frac{\Gamma\left(\frac{k+1}{2}\right)}{\Gamma\left(\frac{k}{2}\right)}\,
\frac{1}{(\pi)^{1/2}k^{1/2}}\,
\frac{1}{(1+t^2/k)^{(k+1)/2}}\\[2ex]
&=\frac{\Gamma(\frac{k+1}{2})}{\Gamma(\frac{k}{2})}\,
\frac{1}{\sqrt{k\,\pi}}\,
\left(1+\frac{t^2}{k}\right)^{-\frac{k+1}{2}}
\end{align}$$
which is the $\text{pdf}$ of the $t$-Student or Gosset distribution with $k$ degrees of freedom (or $n$ degrees of freedom). Here is the Wikpedia expression:

where $\nu$ are the degrees of freedom.