# Gamma distribution samples nan at initialization with apparently reasonable parameters

**URL:** https://discourse.mc-stan.org/t/gamma-distribution-samples-nan-at-initialization-with-apparently-reasonable-parameters/25832
**Category:** Modeling
**Tags:** fitting-issues
**Created:** [December 31, 2021, 3:59pm UTC](https://discourse.mc-stan.org/t/gamma-distribution-samples-nan-at-initialization-with-apparently-reasonable-parameters/25832 "2021-12-31T15:59:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Oberauer](https://avatars.discourse-cdn.com/v4/letter/o/ed655f/32.png) [@Oberauer](https://discourse.mc-stan.org/u/Oberauer)
#### Post date: [December 31, 2021, 3:59pm UTC](https://discourse.mc-stan.org/t/gamma-distribution-samples-nan-at-initialization-with-apparently-reasonable-parameters/25832/1 "2021-12-31T15:59:03Z")

</div>

I’m trying to run a hierarchical model in which duration of a hypothetical process is drawn from a gamma distribution; the parameters of the gamma vary between individuals, and are drawn from a population distribution. The model fails to initialize; the error message points at the line with the gamma sampling, stating that it produces nan. When I print the parameters of the gamma, they look reasonable (mostly between 1 and 10). My code and data are attached.  
[IMSalience6\_demo1.stan](https://discourse.mc-stan.org/uploads/short-url/eYazaWVPMoBHOmLbwT57KmZrlST.stan) (2.8 KB)  
[IMSalience6Demo.R](https://discourse.mc-stan.org/uploads/short-url/jzAMfMVlVp4iC2iQRtqqYxNJc9a.R) (572 Bytes)  
[IMSalience6\_DemoData.RData](https://discourse.mc-stan.org/uploads/short-url/zS98FlDJ0N1DCaMoeskVoUXaDDi.RData) (853.5 KB)

---

<div class="post-metadata">

### Author: ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)
#### Post date: [January 1, 2022, 4:46pm UTC](https://discourse.mc-stan.org/t/gamma-distribution-samples-nan-at-initialization-with-apparently-reasonable-parameters/25832/2 "2022-01-01T16:46:25Z")

</div>

Is any of your data nan/negative/zero?

---

<div class="post-metadata">

### Author: ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)
#### Post date: [January 1, 2022, 9:57pm UTC](https://discourse.mc-stan.org/t/gamma-distribution-samples-nan-at-initialization-with-apparently-reasonable-parameters/25832/3 "2022-01-01T21:57:28Z")

</div>

Looks like the problem is line 103

```stan
    t1 ~ gamma(Tshape, Trate);

```

Here `t1` is neither a parameter nor data but a local variable declared in the model block. Despite being called a [“sampling statement”](https://mc-stan.org/docs/2_28/reference-manual/sampling-statements.html) this does not draw and assign a new value to the left-hand side. Instead, it computes the likelihood of the lefthand-side. In fact it is basically

```stan
   target += gamma_lpdf(t1 | Tshape, Trate);

```

This works only if `t1` is already initialized. Since it’s unknown it must be declared in the `parameters` block.

```stan
parameters {
  ...
  vector<lower=0>[N] t1;
}

```

Another thing that caught my attention is line 115-116

```stan
    logLik += sum(Lik);
    target += logLik;

```

`logLik` has not been initialized so incrementing just gives NaN, and if logLik is a cumulative quantity then maybe `target +=` is supposed to be ouside the loop?  
Also, it’s strange to go from `Lik` to `logLik` without taking a logarithm. If it’s mean to be `log(sum(Lik))` then note that `log_sum_exp(log(x) + y)` is usually more stable than `log(sum(x*exp(y)))`.

---

<div class="post-metadata">

### Author: ![Oberauer](https://avatars.discourse-cdn.com/v4/letter/o/ed655f/32.png) [@Oberauer](https://discourse.mc-stan.org/u/Oberauer)
#### Post date: [January 3, 2022, 7:39pm UTC](https://discourse.mc-stan.org/t/gamma-distribution-samples-nan-at-initialization-with-apparently-reasonable-parameters/25832/4 "2022-01-03T19:39:58Z")

</div>

Thanks a lot, this solved my problem!
