getting negative binomial from poisson and gamma This equation is from a statistical genetics research paper. I'm struggling to understand how they get negative binomial from the integral. x_cn is poisson and q is gamma. Is there such a rule? Or is this an approximation? I tried to do the integration but at one point it got complicated so I didn't pursue it further.

Thanks!
 A: This is not an approximation.  The negative binomial distribution can be thought of as a mixture of a gamma and a poisson. Essentially, I draw lambda from a gamma distribution, and then use that lambda in my poisson.
Mathematically, the computations are quite straight forward.
$$f(k ; r, p)=\int_{0}^{\infty} f_{\text {Poisson }(\lambda)}(k) \cdot f_{\text {Gamma }\left(r, \frac{1-p}{p}\right)}(\lambda) \mathrm{d} \lambda$$
$$=\int_{0}^{\infty} \frac{\lambda^{k}}{k !} e^{-\lambda} \cdot \lambda^{r-1} \frac{e^{-\lambda(1-p) / p}}{\left(\frac{p}{1-p}\right)^{r} \Gamma(r)} \mathrm{d} \lambda$$
This integral is with respect to lambda, so let's collect the relevant terms
$$=\frac{(1-p)^{r} p^{-r}}{k ! \Gamma(r)} \int_{0}^{\infty} \lambda^{r+k-1} e^{-\lambda / p} \mathrm{d} \lambda$$
Which evaluates to
$$=\frac{(1-p)^{r} p^{-r}}{k ! \Gamma(r)} p^{r+k} \Gamma(r+k)$$
And then simplifying
$$=\frac{\Gamma(r+k)}{k ! \Gamma(r)} p^{k}(1-p)^{r}$$
You might recall that the gamma is a sort of generalized factorial, so the normalizing coefficient is actually a binomial coefficient.
We can verify this by simulating some random numbers from the negative binomial and the mixture and them comparing frequencies.
library(tidyverse)

r = 2
p = 0.9

lams = rgamma(100000, shape = r, rate = p/(1-p))
y = rpois(length(lams), lambda = lams)

d1 = tibble(y = y, dist = 'mixture')

yy = rnbinom(100000, r, p)

d = tibble(y =yy, dist = 'nbinom') %>% bind_rows(d1)

d %>% 
  count(dist,y) %>% 
  group_by(dist) %>% 
  mutate(prop = prop.table(n)) %>% 
  ungroup %>% 
  select(-n) %>% 
  spread(dist,prop)


# A tibble: 6 x 3
      y mixture  nbinom
  <int>   <dbl>   <dbl>
1     0 0.811   0.808  
2     1 0.161   0.163  
3     2 0.0242  0.0244 
4     3 0.00303 0.00332
5     4 0.0004  0.00048
6     5 0.00006 0.00003

The frequencies are close enough.  If you wanted, you could do a chi-square test to determine if the frequencies are actually different.
