# Given likelihood of gamma (external approximation convoluted gamma) calculate likelihood of NB

**URL:** <https://discourse.mc-stan.org/t/given-likelihood-of-gamma-external-approximation-convoluted-gamma-calculate-likelihood-of-nb/17464>\
**Category:** Modeling\
**Created:** [August 15, 2020, 2:06am UTC](https://discourse.mc-stan.org/t/given-likelihood-of-gamma-external-approximation-convoluted-gamma-calculate-likelihood-of-nb/17464 "2020-08-15T02:06:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stemangiola](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stemangiola/32/260_2.png) [@stemangiola](https://discourse.mc-stan.org/u/stemangiola)\
**Post date:** [August 15, 2020, 2:06am UTC](https://discourse.mc-stan.org/t/given-likelihood-of-gamma-external-approximation-convoluted-gamma-calculate-likelihood-of-nb/17464/1 "2020-08-15T02:06:11Z")

</div>

I found a function that approximates efficiently the density of a convoluted gamma

```
gamma(a1, b1) + gamma(a2, b2) + ...

```

```
.Call(
  "_coga_dcoga_approx", 
  PACKAGE = "coga", 
  1.5, 
  c(1, 3, 5, 2, 2), 
  c(3, 5, 3, 5, 3)
)
[1] 0.003897655

```

Now is it possible to algebrically calulate the likelihood of a negative binomial from that?

I guess the explicit formulation would be

```stan
y ~ poisson(mu)
mu ~ R_convoluted_gamma(alpha1, alpha2, alpha3, beta1, beta2, beta3);

```

But I would like to avoid the creation of an intermediate parameter mu.

---

<div class="post-metadata">

**Author:** ![andre.pfeuffer](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@andre.pfeuffer](https://discourse.mc-stan.org/u/andre.pfeuffer)\
**Post date:** [August 19, 2020, 11:19am UTC](https://discourse.mc-stan.org/t/given-likelihood-of-gamma-external-approximation-convoluted-gamma-calculate-likelihood-of-nb/17464/2 "2020-08-19T11:19:44Z")

</div>

The sum of independend gamma distributions leads to a nested integral expression, but maybe the approximiation [https://projecteuclid.org/download/pdfview\_1/euclid.ejs/1403812157](https://projecteuclid.org/download/pdfview_1/euclid.ejs/1403812157) can bring you closer.

---

<div class="post-metadata">

**Author:** ![bnicenboim](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bnicenboim/32/21197_2.png) [@bnicenboim](https://discourse.mc-stan.org/u/bnicenboim)\
**Post date:** [August 19, 2020, 11:43am UTC](https://discourse.mc-stan.org/t/given-likelihood-of-gamma-external-approximation-convoluted-gamma-calculate-likelihood-of-nb/17464/3 "2020-08-19T11:43:22Z")

</div>

I played a little bit with the difference of gamma distributions.

> [@Optimization of a custom likelihood --when is it worthwile to code it in C++ with its derivative?](https://discourse.mc-stan.org/t/optimization-of-a-custom-likelihood-when-is-it-worthwile-to-code-it-in-c-with-its-derivative/16665):
>
> Hi, I wrote the likelihood for the difference between two gamma distributions with the same rate using the saddle point approximation (thanks to @martinmodrak’s blog post!). I could find an analytical solution to the approximation, and here it is below. The thing is that it’s quite slow (~10 mins for 1000 observations), and I don’t think it can be optimized further (I’ll be happy to be proved wrong). Will I gain a significant speedup if I code it in c++ with its derivative? (I’m not fluent in …

The saddle approximation has an analytical solution for the convolution of two gammas.
