# Persistent divergent transitions in simple model

**URL:** <https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365>\
**Category:** Modeling\
**Tags:** fitting-issues\
**Created:** [April 16, 2020, 4:43pm UTC](https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365 "2020-04-16T16:43:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Van\_Horn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kevin_van_horn/32/4513_2.png) [@Kevin\_Van\_Horn](https://discourse.mc-stan.org/u/Kevin_Van_Horn)\
**Post date:** [April 16, 2020, 4:43pm UTC](https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365/1 "2020-04-16T16:43:15Z")

</div>

(This refers to the same model mentioned in “Error codes from rstan::optimzing”.) I have a fairly simple model that has been giving me a lot of divergent transitions, and I can’t seem to make them go away, despite

- cranking up `adapt_delta` to 0.9999,
- doing a lot of reparameterization,
- modifying priors in various ways.

The model is basically this:

\mu\_t = a + b \cdot t  
a\_t \sim \mbox{AR(1) process}  
y\_t = \mu\_t + a\_t \cdot \mu\_t ^ x

All of x, a, b, and the AR(1) parameters are being estimated, with x restricted to the interval (0,1) and the autoregressive coefficient \phi restricted to (0,1). The parameterization used is a, b, \mathrm{logit}(\phi), \mathrm{logit}(x), and \log(\sigma\_{\epsilon} \cdot \mu\_u^x), where u is the midpoint of the time series.

Besides my original data set, I’ve been testing it on synthetic data generated from the intended model.

Here is a pairs plot from a run that used `iter=4000`, `adapt_delta=0.9999`, and `max_treedepth=15`. I’ve managed to reduce posterior correlations to moderate levels. There is a bit of funnel behavior in the interaction between `b` and `logit_phi`, but it’s not all that strong, and there are plenty of divergent transitions out in the broad part. Any suggestions?

 ![pairsplot](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/4/46107f44f231e3f55d9b8a6596e53fd4129e385c.jpeg)

---

<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:** [April 16, 2020, 5:34pm UTC](https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365/2 "2020-04-16T17:34:31Z")

</div>

Dear Kevin,

could you post the Stan model, data and calling instructions? That would be so helpful in analyzing the problem.

---

<div class="post-metadata">

**Author:** ![Kevin\_Van\_Horn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kevin_van_horn/32/4513_2.png) [@Kevin\_Van\_Horn](https://discourse.mc-stan.org/u/Kevin_Van_Horn)\
**Post date:** [April 16, 2020, 6:03pm UTC](https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365/3 "2020-04-16T18:03:20Z")

</div>

Here they are.  
`linear-ar1.stan` is the stan model file.  
`example-data.csv` is the data.  
`example.R` is a (simplified) script to run the model.  
[linear-ar1.stan](https://discourse.mc-stan.org/uploads/short-url/v6YS5wAJYmwkjsAB0S8lD3stsJF.stan) (2.3 KB)  
[example.R](https://discourse.mc-stan.org/uploads/short-url/3qeriCqAfgYfFuNsz6b7KX8AZ8K.R) (1.1 KB)  
[example-data.csv](https://discourse.mc-stan.org/uploads/short-url/uoF0kUTLg49vfeaxWaLrKUwnSaF.csv) (4.9 KB)

---

<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:** [April 17, 2020, 3:39pm UTC](https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365/4 "2020-04-17T15:39:10Z")

</div>

Your model is quite unusual. You use Jacobian adjustments, but changing of your model slightly  
wouldn’t need that. I think it’s the preferred way, see AR(1) in Stan’s Usermanual for details.

One statement that really needs improvement is

> ```
> real a;
> real b;
> 
> ```

following:

> real log\_mu\_mid = log(a + tmid \* b);

You have to specify:

> real\<lower=0\> a;  
> real\<lower=0\> b;

Due to time restrictions, I’m unable to fully check your Jacobian Adjustments. I believe that the model needs to be slightly modified in terms of the Stan Usermanual.

---

<div class="post-metadata">

**Author:** ![Kevin\_Van\_Horn](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kevin_van_horn/32/4513_2.png) [@Kevin\_Van\_Horn](https://discourse.mc-stan.org/u/Kevin_Van_Horn)\
**Post date:** [April 17, 2020, 9:05pm UTC](https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365/5 "2020-04-17T21:05:02Z")

</div>

Thanks for catching the missing restrictions on a and b. Once I put those in, the divergent transitions went away.

BTW, I think what you’re seeing as unusual in the model is what I had to do to allow for missing data. The zero-centered AR(1) equation to use when there are no data points between t and t+n is

a\_{t+n} \sim \mathrm{normal}(\phi^n a\_t, \sigma\_n),  
\sigma\_n = \sigma\_{\epsilon} \cdot \left(\sum\_{i=0}^{n-1} \phi^{2i}\right)^{1/2}.

---

<div class="post-metadata">

**Author:** ![asael\_am](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asael_am/32/5377_2.png) [@asael\_am](https://discourse.mc-stan.org/u/asael_am)\
**Post date:** [April 17, 2020, 10:58pm UTC](https://discourse.mc-stan.org/t/persistent-divergent-transitions-in-simple-model/14365/6 "2020-04-17T22:58:17Z")

</div>

Are you trying to perfom a stochastic trend ar(1) process?

Cause the x parameter might affect your model fit.
