<s>[INST] Finding a Probability for a Gamma Distributed Random Variable Below is a problem from the book "Probability and Statistics". It is one of the Schaum's books. I am getting the
wrong answer and I believe that I am doing the integration incorrectly.
Thanks
Bob
Problem:
A random variable $X$ is gamma distributed with $\alpha = 3$, $\beta = 2$. Find $P( X \leq 1 )$.
Answer:
The density function for a gamma distribution is:
$$f(x) = \begin{cases}
\frac{x^{\alpha-1} \, e^{-\frac{x}{\beta}}}{ \beta^\alpha \Gamma(\alpha)} & \text{for }x > 0 \\
0 &  \text{for }x <= 0  \\
\end{cases} \\
$$
In this case, the density function is:
$$f(x) = \begin{cases}
\frac{x^{2}e^{-\frac{x}{2}}}{2^3 \Gamma(3)} & \text{for }x > 0 \\
0 &  \text{for }x <= 0  \\
\end{cases} \\
$$
Now we can simplify the density function.
\begin{align*}
\Gamma(3) &= 2 \Gamma(2) \\
\Gamma(2) &= 1 \Gamma(1) = 1 \\
\Gamma(3) &= 2 \\
\end{align*}
Hence the density function is:
$$f(x) = \begin{cases}
\frac{x^{2}e^{-\frac{x}{2}}}{16} & \text{for }x > 0 \\
0 &  \text{for }x <= 0  \\
\end{cases} \\
$$
\begin{align*}
P( X \leq 1 ) &= \int_0^1 \frac{x^{2}e^{-\frac{x}{2}}}{16} \, dx \\
\text{Let } u &= -\frac{x}{2} \\
x &= -2u \\
du &= -\frac{1}{2} \, dx \\
P( X \leq 1 ) &= \int_{0}^{ -\frac{1}{2} } -\frac{4u^2e^{u}}{16} \, du \\
P( X \leq 1 ) &= \int_{0}^{ -\frac{1}{2} } -\frac{u^2e^{u}}{4} \, du \\
\end{align*}
Now let's consider the integral:
$$ \int u^2e^u \, du$$
To evaluate this integral, we will use integration by parts twice.
\begin{align*}
\int u^2e^u \, du &= -u^2e^{-u} - \int -2ue^{-u} \, du \\
\int u^2e^u \, du &= -u^2e^{-u} + \int 2ue^{-u} \, du \\
\int u^2e^u \, du &= -u^2e^{-u} -2ue^{-u}  - 2 \int -e^{-u} \, du \\
\int u^2e^u \, du &= -u^2e^{-u} -2ue^{-u}  + 2 \int e^{-u} \, du \\
\int u^2e^u \, du &= -u^2e^{-u} - 2ue^{-u} - 2e^{-u} + C \\
P( X \leq 1 ) &= \left( -\frac{1}{4} \right) \left( -u^2e^{-u} - 2ue^{-u} - 2e^{-u} \Big|_{0}^{-\frac{1}{2}} \right) \\
\end{align*}
\begin{align*}
P( X \leq 1 ) &= \left( \frac{1}{4} \right) \left( u^2e^{-u} + 2ue^{-u} + 2e^{-u} \Big|_{0}^{-\frac{1}{2}} \right) \\ 
P( X \leq 1 ) &=  \left( \frac{ u^2e^{-u} }{4} + \frac{ue^{-u}}{2} + \frac{e^{-u}}{2} \Big|_{0}^{-\frac{1}{2}} \right) \\ 
P( X \leq 1 ) &=
\frac{e^{\frac{1}{2}}}{16} - \frac{ \frac{1}{2} e^{\frac{1}{2}} }{2} + \frac{e^{\frac{1}{2}}}{2} - \frac{e^0}{2} \\
P( X \leq 1 ) &= \frac{e^{\frac{1}{2}}}{16} - \frac{ e^{\frac{1}{2}} }{4} + \frac{e^{\frac{1}{2}}}{2} - \frac{1}{2} \\ 
%
P( X \leq 1 ) &= \frac{e^{\frac{1}{2}}}{16} + \frac{e^{\frac{1}{2}}}{4} - \frac{1}{2} \\ 
P( X \leq 1 ) &= \frac{5 e^{ \frac{1}{2} } }{16} - \frac{1}{2} \\
\end{align*}
However, the book's answer is:
$$ 1 - \frac{13}{8\sqrt{e}} \doteq 0.0143877 $$
An online integral calculator found the book's answer.
 [/INST] Until here everything is perfect:
$$P( X \le 1 ) = \int_0^1 \frac{x^{2}e^{-\frac{x}{2}}}{16} \, dx $$
Now when you substitute $-\frac{x}{2}=u\Rightarrow x=-2u\Rightarrow dx=-2du\,$ don't forget to add the coefficient of $du$, like this:
$$P( X \le 1 ) = \int_0^1 \frac{x^{2}e^{-\frac{x}{2}}}{16} \, dx=\int_0^{-1/2}\frac{(-2u)^2e^u}{16}(-\color{red}{2})du=\int_0^{-1/2}-\frac{u^2e^u}{2}du$$

The next issue would be when you integrate by parts. Note that the rule is:
$$(fg)'=f'g+fg'\Rightarrow fg=\int f'g +\int fg'\Rightarrow \boxed{\int f'g=fg-\int fg'}$$

So when you chose $f'=e^u$ then $f=e^u$ not $e^{-u}$. I am talking about the first row from here:
$$\int u^2e^u \, du = -u^2e^{-u} - \int -2ue^{-u} \, du$$
The correct way would be:
$$\int u^2 (e^u)'du=u^2 e^u -\int 2u e^udu$$
But I think the previous factor of $e^{-x/2}$ made you see an $e^{-u}$. 
Anyway the rest is fine, I'll finish it for you so you can verify.
$$\int u^2 e^u du=u^2 e^u -2\int u e^{u} du$$
$$=u^2 e^u -2 \left(ue^u - \int e^u du\right)$$
$$=u^2e^u -2 ue^u +2 e^u+C$$
$$P(X\le 1)=-\frac12e^u\left(u^2 -2 u +2 \right)\bigg|_0^{-1/2}$$
$$=-\frac12e^{-1/2}\left(\frac14+1 +2\right)+\frac12(0-0+2)$$
$$=-\frac12 \frac{1}{\sqrt{e}} \cdot \frac{13}{4} +1=1-\frac{13}{8\sqrt{e}}$$

As a side note, I would recommend you to write
$$\int_0^{-1/2} \color{red}- u^2 e^u du=\int_{-1/2}^0 u^2 e^u du$$
But there is no mistake in letting it your way.
</s>