# Probability of a Gamma r.v. greater than another.

I saw an interesting formula at:

https://stats.stackexchange.com/questions/264861/probability-of-gamma-greater-than-exponential

but I didn't know how to derive it. The question is:

Let $X,Y$ be independent r.v.s and $X\sim\mathrm{Gamma}(k_1,\theta_1)$, $Y\sim\mathrm{Gamma}(k_2,\theta_2)$, with $\mathbb{E}[X]=k_1\theta_1$.

How to derive

$$P[X>Y]=IB(\frac{\theta_1}{\theta_1+\theta_2};k_2,k_1),$$

where $IB(x;a,b)$ is the regularized incomplete beta function defined as

$$IB(x;a,b)=\frac{1}{B(a,b)}\int_0^xt^{a-1}(1-t)^{b-1}dt,$$

and $B(a,b)$ is the beta function.

First of all, a well known, preliminary result used here is that if $X \sim \text{Gamma}(k_1, \theta), Y \sim \text{Gamma}(k_2, \theta)$ and they are independent, then $$\frac {X} {X+Y} \sim \text{Beta}(k_1, k_2)$$
Note that this result require both $X, Y$ have the same scale parameters, which you do not have yet. And as $\theta_1, \theta_2$ are the scale parameters, we have
$$\frac {X} {\theta_1} \sim \text{Gamma}(k_1, 1), \frac {Y} {\theta_2} \sim \text{Gamma}(k_2, 1)$$
\begin{align} \Pr\{X > Y\} &= \Pr\left\{\frac {X} {\theta_1} > \left(\frac {1} {\theta_1} + \frac {1} {\theta_2}\right) Y - \frac {Y} {\theta_2}\right\} \\ &= \Pr\left\{ \frac {Y} \frac {Y} {\theta_2} + \frac {X} {\theta_1}} < \frac {\theta_1\theta_2} {\theta_1 + \theta_2} \right\} \\ &= \Pr\left\{ \frac \frac {Y} {\theta_2}} \frac {X} {\theta_1} + \frac {Y} {\theta_2} } < \frac {\theta_1} {\theta_1 + \theta_2} \right\} \\ &= F_Z\left(\frac {\theta_1} {\theta_1 + \theta_2} \right) \end{align where $$Z = \frac \frac {Y} {\theta_2}} \frac {X} {\theta_1} + \frac {Y} {\theta_2} } \sim \text{Beta}(k_2, k_1$$ and $F_Z$ is the CDF of $Z$. Finally you just need to use the result from Beta CDF: