Stan 2.10 changed the NUTS algorithm from using slice sampling along a Hamiltonian trajectory to a new algorithm that uses categorical sampling of points along the trajectory proportional to the density (plus biases to the second half of the chain, which is a subtle aspect of the original NUTS algorithm). The new approach is described here:

Let me temper the panic by saying that the bias is relatively small and affects only variances but not means, which is why it snuck through all our testing and application analyses. Ultimately posterior intervals are smaller than they should be, but not so much that the inferences are misleading and the shrinkage will be noticeable only if you have more than thousands of effective samples, which is much more that we typically recommend.

What we’re doing to fix it

Michael and I are poring over the proofs and the code, but it’s unfortunate timing with the holidays here as everyone’s traveling. We’ll announce a fix and make a new release as soon
as we can. Let’s just say this is our only priority at the moment.

If all else fails, we’ll roll back the sampler to the 2.09 version in a couple days and do a new release with all the other language updates and bug fixes since then.

What you can do until then

While some people seem to think the error is of small enough magnitude not to be worrisome (see comments), we’d rather see you all getting the right answers. Until we get this fixed, the only thing I can recommend is using straight up static HMC (which is not broken in the Stan releases) or rolling back to Stan 2.09 (easy with CmdStan, not sure how to do that with other interfaces).

Even diagnosing problems like these is hard

Matthew Becker, the original poster, diagnosed the problem with fake data simulations, but it required a lot of effort.

In particular, you can see that the posterior sd is too low for NUTS (not by much 0.95 vs. 1.0), and the posterior 90% intervals are (-1.89, 1.88) rather than (-1.95, 1.95) for z[1] (here, for some reason listed as "z[0]").

We're really sorry about this

Again, our sincere apologies here for messing up so badly. I hope everyone can forgive us. It is going to cause us to focus considerable energy on functional tests that'll diagnose these issues---it's a challening problem to balance sensitivity and specificity of such tests.

12 Comments

It’s good to announce this—but, as Robert says, the bias must be pretty small, at least in all the examples I’ve been working on for the past several months, as Stan’s inferences have matched my fake-data simulations.

Of course it’s good to fix the bug! But I don’t know that, for practical work, it’s necessary for people to go back to an old version of Stan.

I’d recommend that users stick with Stan 2.13 but just check their results using fake data simulations (which they should be doing anyway!).

I have had the same experience at least so far: my own fake data simulations show that I can recover the parameters, and for standard lmer type hierarchical models the parameter estimates are similar to the frequentist ones for a given data-set.

So glad to hear everyone’s doing fake data simulation. Diagnosing this bug requires quite a few iterations to get low enough MCMC standard error. You can follow the link to the issue to see the way Michael Betancourt coded a model for the null hypothesis that our draws were correct:

In particular, you can see that the posterior sd is too low for NUTS (not by much 0.95 vs. 1.0), and the posterior 90% intervals are (-1.89, 1.88) rather than (-1.95, 1.95) for z[1] (here, for some reason listed as "z[0]").

I have no idea which of our interfaces is using this bad indexing from 0 in the output. It should match the Stan indexing and be "z[1]" and "z[2]"!

Yes, this error seems pretty tiny. I wouldn’t ditch all my existing programs because of such a small error. I’d recommend that users keep doing what they’re doing in the current version of Stan, and then update Stan when the anticipated patch becomes available. Going back to an earlier version of Stan seems like a pretty drastic step.

Now, this may be a really simple question, but do the version numbers between rstan and Stan match up? The release dates seem to match up, so I am guess they do? If so, maybe I should be grateful to my company’s IT organization for not updating the version since rstan 2.7.0-1. Or does the rstan version not tell me, which Stan version I am using?

Claiming that a Markov chain Monte Carlo sampler is biased is a delicate issue and, unfortunately, requires more evidence than “numbers kind of look off”. The problem is that MCMC is an approximate algorithm and so there will be some deviation from the true value even when things are working properly. Fortunately, MCMC is one of the few algorithms where we can quantify the approximation error (formally this is a bit complicated, but if you heed all of the diagnostics in Stan then it’s pretty robust).

We can see that quantification already in the results of Matthew’s example that Bob showed above — the means for both z[0] and z[1] and within a few MCMC standard errors (listed as MCSE in the fit output) of their true values, just as we’d expect them to if everything were working correctly. And exactly for what our tests look!

But the concern wasn’t the means but rather the variances. To examine those we need a MCMC standard error for marginal variances and z[0] and z[1] and their correlation. To that end I augmented Matthew’s example with a generated quantities block that calculated some functions whose means yield the deviation of the estimated marginal variances and correlation from their true values.

generated quantities {
// The means of these quantities will give the difference between
// estimated marginal variances and correlation and their true values.
// If everything is going correctly then these values should be with
// the respective MCMC-SE of zero.
real delta_var1;
real delta_var2;
real delta_corr;

There are no sampler diagnostics indicating that something is wrong with the sampler, and the deviations are all pretty close to zero with respect to their MCMC standard errors! BUT if generate many more samples then we start to see a bias arise,

Now those deviations are all many MCMC standard errors away from zero indicating that there’s a real bias exhibited by the sampler.

To summarize, when validating MCMC samplers you need to be very careful to not just claim a bias but quantify that bias relative to the expected error. There does seem to be a small bias in the breadth of the posterior distribution recovered by Stan. The location of the posterior, however, seems to be fine, and the bias in the breadth isn’t really significant if you’re running with the Stan defaults, so there’s no need to panic about the general results of any analyses you’ve made in the past few years.

This is a pretty subtle bias which is why it got past all of our tests and analysis validations. As Bob noted we’re working to figure out what’s going on as quickly as we can — as subtle as the bias is, it’s cause is even more subtle and seems to be due to a complicated interaction between the sampler components, maybe? — and we’ll have a fix as soon as we find the issue.