What is random in Stan?

Hi guys,

Quick question.

If I run four chains from identical initial values, should I get the same result for the four chains?

If not, what else is random in Stan’s default algorithm?


1 Like

The actual random seed used for each chain is the supplied random seed with the first 2 ^ 50 * chain_id random numbers discarded. That happens here: stan/create_rng.hpp at develop · stan-dev/stan · GitHub

So the first chain (id=0) starts with the supplied seed, the second chain (id = 1) with the supplied seed and 2^50 random values discarded, etc.

So if you set all 4 chains to the same id and set the same initial values, you should get the exact same results.


Thanks! As you may presume, I’m getting different results from an identical starting point of just one dimension. The chains settle in two different locations (multimodality). What would be the cause for their divergence from the same starting point? I’m fitting an ODE model.

If the chains have different RNG seeds, there is no guarantee that they will evolve similarly, even from the same starting point (to be a bit more specific, the conditions for exact reproducibility are somewhat complex: 21 Reproducibility | Stan Reference Manual).

So if you init close to a boundary between the attraction basins of two well separated modes, you can easily have each chain basically toss a coin to determine the mode it ends in.

Hard to say anything more without seeing the specific model - ODE models can be very hard to fit and can exhibit all sorts of pathological behavior. (But please start a new thread if you want to discuss a specific model)

Best of luck with your model!


To answer this original question, another random thing is how the MCMC samples are generated. Inside that process, random things are at least

  • Sampling a random HMC trajectory (random initial momentum)
  • Sampling a random point from that trajectory

This might be a bit simplified description about the sampling algorithm, but anyway, if you don’t specify random seed then these things change, and consequently the end result, even if you start from the same initial point always.


Thank you all for the comments. The posterior was inherently bimodal & Stan didn’t let me sweep my degeneracies under the rug.