# Divergences with no pattern, master thesis

**URL:** <https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211>\
**Category:** Modeling\
**Tags:** divergences, rstan, fitting-issues, techniques\
**Created:** [April 26, 2023, 4:14pm UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211 "2023-04-26T16:14:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![javims](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/javims/32/19088_2.png) [@javims](https://discourse.mc-stan.org/u/javims)\
**Post date:** [April 26, 2023, 4:14pm UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/1 "2023-04-26T16:14:55Z")

</div>

Hello, I am quite new to Stan, for my master thesis I have been using BUGS, but I had to move due to the complexity of the models. I’ve spent quite a bit of time understanding how it works behind the scenes, and I’ve seen that it’s important to take divergences into account.

I am using a “new” probability distribution. From 1500 iterations, 944 divergences come out. Raising adapt\_delta to 0.99 brought them down to 780. I’ve looked at bayesplot tutorials and others but none help. I have tried to plot the divergence vs non-divergence random effects, but the only pattern is that the ones that diverge are more focused on the scatter. I haven’t found any pattern, and I don’t know if I could give the model as valid, or if I should put some justification in my thesis. The only pattern found is in teh acceptance rate, but it looks like a fine line. Thank you all in advance  
[divergence.pdf](https://discourse.mc-stan.org/uploads/short-url/9syb1jjhLkdfJW4FZlEnfMko9tS.pdf) (24.1 KB)

---

<div class="post-metadata">

**Author:** ![js592](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@js592](https://discourse.mc-stan.org/u/js592)\
**Post date:** [April 26, 2023, 4:18pm UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/2 "2023-04-26T16:18:41Z")

</div>

What is your “new” probability distribution?

---

<div class="post-metadata">

**Author:** ![javims](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/javims/32/19088_2.png) [@javims](https://discourse.mc-stan.org/u/javims)\
**Post date:** [April 26, 2023, 4:27pm UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/3 "2023-04-26T16:27:19Z")

</div>

It’s the Skellam’s distribution. So it is not “new”, but I wanted to refer to it as user-defined one. When using random effects to take into account the longitudinal data is when the divergences arise.

---

<div class="post-metadata">

**Author:** ![js592](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@js592](https://discourse.mc-stan.org/u/js592)\
**Post date:** [April 26, 2023, 4:35pm UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/4 "2023-04-26T16:35:19Z")

</div>

Have you tried fitting your model to simulated data (from fixed parameters)? Do the divergences still occur then or only when applying it to the real data you collected?

---

<div class="post-metadata">

**Author:** ![javims](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/javims/32/19088_2.png) [@javims](https://discourse.mc-stan.org/u/javims)\
**Post date:** [April 26, 2023, 4:37pm UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/5 "2023-04-26T16:37:36Z")

</div>

Yes, I tried the same model for the dataset, removing the repeated observations and hence, also the random intercept. Works fine. Simulating manually a data set like the one I am using, with random effects, and using it in the bayesian model, also raises a warning of divergences, but this time only about 40. All parameters of the regression came close except for 2.

Is there any way to look for the “root” of the cause? Can I ignore divergences?

---

<div class="post-metadata">

**Author:** ![js592](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@js592](https://discourse.mc-stan.org/u/js592)\
**Post date:** [April 27, 2023, 8:10pm UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/6 "2023-04-27T20:10:35Z")

</div>

I wonder if there are some issues with your implementation of random effects. Have you looked into possible re-parameterizations?

---

<div class="post-metadata">

**Author:** ![javims](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/javims/32/19088_2.png) [@javims](https://discourse.mc-stan.org/u/javims)\
**Post date:** [April 29, 2023, 10:55am UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/7 "2023-04-29T10:55:50Z")

</div>

It is the non-cetentered typical way, adding an intercept std\_normal() to the linear regression, and multpliying it by sigma and adding beta[1].

I attach the pairs plot

 ![pairs-1](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/f/0/f07e73a483ef19a4d01cf895c634788ac1bb6884.png)

---

<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:** [May 1, 2023, 6:22am UTC](https://discourse.mc-stan.org/t/divergences-with-no-pattern-master-thesis/31211/8 "2023-05-01T06:22:44Z")

</div>

Does the new distribution impose constraints on its parameters? For example, the following model gets many divergencies:

```stan
transformed data {
  int n = 1;
} parameters {
  real lambda;
} model {
  n ~ poisson(lambda);
}

```

The Poisson distribution requires `lambda > 0` but the parameter is declared as real. The sampler tries to explore negative values and the resulting error is reported as a divergent transition.

The solution is to declare the parameter with a constraint so that the sampler knows to only explore positive numbers:

```stan
transformed data {
  int n = 1;
} parameters {
  real<lower=0> lambda;
} model {
  n ~ poisson(lambda);
}

```

or you may be able to avoid the problem with alternative parametrization.  
Here `poisson_log` is a valid distribution for any value of `lambda`:

```stan
transformed data {
  int n = 1;
} parameters {
  real lambda;
} model {
  n ~ poisson_log(lambda);
}

```
