Pystan `get_mass_matrix` method

Is it possible to get or set the mass matrix from a fit object in Pystan?
More generally, is it possible to save the critical info from warmup and reuse it later for another chain? The idea being to run a long warmup chain, save the output and use it in the future to skip warmup by initializing a future chain with the saved output.

I came across this commit by @ahartikainen which seems to be the answer. I installed the development version of pystan but it seems like get_mass_matrix is not an available method for a fit object.

Thank you in advance for any help!

Hi, see

and small example

That’s great - Thank you, @ahartikainen !
For future readers, the method that returns the mass matrix is

Thank you so much @ahartikainen and Pystan team for implementing this, it looks incredibly useful. A couple questions about using this feature:

  1. Is there any straightforward intuition for why continuing sampling “does not equal sampling done in one step”? Does this change how we should interpret inference results from this pseudo-sampling method? Or will it just not give you the same results because of the seed?

  2. In the docs example for running the warmup first and then sampling in a different run, only one chain is used. Is it reasonable to do this? Would it make sense to compute some sort of r-hat on the inverse metric to see if you’ve run enough warmup steps?

  1. The random number generator used by Stan is not at the same state, when you restart your sampling

2a. For basic usage, no. It is something that can be done, if user has complicated model and wants to minimize the sampling time (this could happen for example in “production”). I think I should add some warning in there.

2b. (edit.) Maybe, sounds interesting.

Thanks for the quick response!

  1. The random number generator used by Stan is not at the same state, when you restart your sampling

So just to clarify, this means that the results won’t be exactly the same but inferences based on the samples won’t be any less valid than from samples collected in a single session?
In principle, could Stan return a seed at the end of sampling that would allow you to perfectly recover the state when you restart? I understand this might not be straightforward to implement though.


I don’t know if it is possible to return / save the RNG state used by Stan.

cc @Bob_Carpenter