# Fixing "BFMI was low" problems

**URL:** <https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658>\
**Category:** Modeling\
**Tags:** performance, fitting-issues\
**Created:** [May 24, 2017, 8:00pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658 "2017-05-24T20:00:15Z")\
**Posts on this page:** 12\
**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:** [May 24, 2017, 8:00pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/1 "2017-05-24T20:00:15Z")

</div>

There is a short writeup at [http://mc-stan.org/misc/warnings.html#bfmi-low](http://mc-stan.org/misc/warnings.html#bfmi-low) about what it means when BFMI is low, but I am at a loss as to what to do about it. One of the suggestions given is “reparameterize your model,” but what sort of reparameterization helps here?

Also… I tried to read the referenced paper on BFMI at [https://arxiv.org/pdf/1604.00695.pdf](https://arxiv.org/pdf/1604.00695.pdf), but I couldn’t make much sense of it. I understand MCMC well, I understand the basics of HMC, and I speak measure theory, but I don’t know what the notation T\*Q means in the context of this paper, and terms like “momentum fiber” and “cotangent bundle,” and “cotangent disintegration” don’t mean anything to me. Can anyone suggest what I should read first to fill in the assumed background for this paper?

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [May 24, 2017, 9:17pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/2 "2017-05-24T21:17:32Z")

</div>

You may prefer the conceptual introduction, [https://arxiv.org/abs/1701.02434](https://arxiv.org/abs/1701.02434), which discusses the energy diagnostic outside of the geometric foundations.

The necessary mathematics for the formal discussion are discussed in [https://arxiv.org/abs/1410.5110](https://arxiv.org/abs/1410.5110) and the references therein.

We set the E-BFMI warning threshold at 0.3 based on some empirical heuristics but that may have been a bit conservative given how robustly dynamic HMC handles even extremely heavy-tailed distributions. Still, the performance can be greatly improved by the same reparameterizations that would increase E-BFMI above that threshold so it’s probably a good idea anyways.

A good example of a helpful reparameterization is using a gamma mixture of normals instead of Cauchy. Similarly, see the parameterizations used in the Finnish horseshoe of @avehtari and colleagues.

---

<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:** [May 25, 2017, 5:50pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/3 "2017-05-25T17:50:50Z")

</div>

Thanks for the references. Ironically, the problem I’m wrestling with already reparameterizes a student-t distribution as a gamma mixture of normals. It’s a state-space model with local level, local trend, weekly seasonality (daily data), and a student-t distribution for observation errors implemented by having a separate observation error sigma[t] for each time step p, with independent gamma priors on the variables sigma[t].

I’m still unclear as to what kind of reparameterization helps with low BFMI. It sounds like maybe I should be trying a parameterization that avoids long tails in the posterior, but when I do density plots for parameters and energy\_\_ I don’t see any long tails, despite getting BFMI warnings for all four chains.

Here are some more specifics. The time series is of length 1000. Using 2000 warmup and 2000 sampling iterations, with four chains running in parallel, it takes nearly three hours to run the estimation on my year-old laptop computer. (This is a big problem, as I need to scale up to running lots of analyses.) Rhat looks good for everything, but n\_eff is relatively low at 586 for the degrees-of-freedom parameter nu of the student-t. The parameter nu has a posterior correlation of -0.86 with with energy\_\_, and a posterior correlation of +0.70 with the student-t scale parameter. The student-t scale has a posterior correlation of -0.65 with energy\_\_.

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [May 25, 2017, 8:41pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/4 "2017-05-25T20:41:49Z")

</div>

Firstly, before complaining about Stan being slow might I recommend considering the alternatives? Being able to fit MCMC accurately on state space models with thousands of dimensions within a few hours on commodity hardware is pretty novel compared to what was possible even just a few years ago.

Heavy tails are one feature that manifest in low E-BFMI but they are not the only feature, and the other possible causes have to this point been sufficiently obscure that we haven’t had a great chance to analyze them. If you can reduce your problem into one that can be shared then we can try fitting it ourselves and see if we recognize anything, but otherwise these problems are too hard to diagnose in general because there are so many possible pathologies.

Oh, and keep in mind that the default output of RStan is on the constrained scale where as the algorithms are fitting on the unconstrained scale. If you have any constrained parameters it is often easier to diagnose using the unconstrained values – look up “diagnose\_file” for information on how to access those values.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [May 26, 2017, 4:32am UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/5 "2017-05-26T04:32:22Z")

</div>

It’s very hard to help with efficiency issues by guesswork. How did you code the state-space model? Using something like the forward dynamic programming algorithm for HMMs? Are redundant computatiosn removed?

An `n_eff` of 500 is more than you need for most inferential purposes. After 100 draws, your MCMC standard error is 1/10 of the posterior standard deviation and then you need 400 draws to get to 1/20, 900 draws to get to 1/30 and so on.

We’re also always happy to take contributions aimed at making Stan faster!

---

<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:** [May 26, 2017, 7:51pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/6 "2017-05-26T19:51:02Z")

</div>

I’m not complaining, Michael, and I really do appreciate all the great work you and the rest of the Stan team are doing. (I’ve described you to my coworkers as “a defector from the physics community who has been leaking their secrets to the Bayesian stats community” :-) ) I’m just trying to understand how I can improve performance, because I’m going to have to run a large number of these models on a regular basis.

---

<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:** [May 26, 2017, 8:15pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/7 "2017-05-26T20:15:08Z")

</div>

Hi, Bob. I’m building my model on top of Jeffrey B. Arnold’s “State Space Models in Stan” ([https://github.com/jrnold/ssmodels-in-stan](https://github.com/jrnold/ssmodels-in-stan) and [https://jrnold.github.io/ssmodels-in-stan/](https://jrnold.github.io/ssmodels-in-stan/)), in particular using his ssm\_lpdf() function, which uses the standard dynamic programming algorithm to compute the likelihood.

Yes, I understand that n\_eff=400 is already sufficient; I mentioned the n\_eff of 586 (for 4 chains and 2000 sampling iterations) only because it’s lower than what I’m used to seeing. I usually can get n\_eff \> 400 with only 1000 sampling iterations.

I am learning some things that might be of use to anyone else trying to implement time series models in Stan. For example, I originally implemented a single-source-of-error model (as described in Hyndman et al.'s book “Forecasting with Exponential Smoothing”) with the initial system state as an explicit variable (not integrated out), and found that I got a funnel interaction between the initial trend and the evolution variance for the trend.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [May 28, 2017, 12:14am UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/8 "2017-05-28T00:14:42Z")

</div>

Thanks for the feedback on these models.

Given that Stan doesn’t have discrete parameters, I’m not quite sure what you mean by treating the “initial system state as an explicit variable”. Or how you might go about integrating out something that’s continuous.

---

<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:** [May 30, 2017, 10:25pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/9 "2017-05-30T22:25:16Z")

</div>

By “system state” I mean the latent state vector alpha[t] that evolves from one time step to the next. In a single-source-of-error state-space model the observation and evolution errors are linked:

```
y[t] = w' * alpha[t] + epsilon[t]
epsilon[t] ~ normal(0, sigma)
alpha[t+1] = F * alpha[t] + epsilon[t] * g
alpha[1] ~ multinormal(mu0, Sigma0)

```

where F is a matrix and g and each alpha[t] are vectors. In a SSOE model it turns out that once you know alpha[1] and y[1],…,y[T], the values epsilon[1],…,epsilon[T] and alpha[2], …, alpha[T] are uniquely determined. The vector alpha[1] is the “initial system state” I mentioned. A direct (and unoptimized) implementation in Stan looks something like this:

```
parameters {
    vector[N] alpha1;
    ...
}
transformed parameters {
  vector[T] epsilon;
  // code to compute each epsilon[t] from alpha1 and y[1],...,y[t]
  ...
}
model {
  epsilon ~ normal(0, sigma);
  ...
}

```

Alternatively, you can compute the marginal likelihood, integrating out alpha1. The integral has a recurrent closed-form solution because everything is normal distributions. You get rid of N latent variables and a funnel-shaped linkage between alpha1[2] and g[2] at the cost of a much more complex likelihood computation.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [May 31, 2017, 6:15pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/10 "2017-05-31T18:15:42Z")

</div>

Nice! Thanks for the clarification.

---

<div class="post-metadata">

**Author:** ![Nicklaus\_Millican](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nicklaus_millican/32/9703_2.png) [@Nicklaus\_Millican](https://discourse.mc-stan.org/u/Nicklaus_Millican)\
**Post date:** [December 9, 2022, 11:54pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/11 "2022-12-09T23:54:03Z")

</div>

Does a low BFMI but with good rhat and neff yield reliable posterior estimates?

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [December 21, 2022, 9:49pm UTC](https://discourse.mc-stan.org/t/fixing-bfmi-was-low-problems/658/12 "2022-12-21T21:49:28Z")

</div>

Probably not. See for example some of the discussion in [Low EBFMI examples - #5 by betanalpha](https://discourse.mc-stan.org/t/low-ebfmi-examples/22671/5).
