# Saved mass matrix

**URL:** <https://discourse.mc-stan.org/t/saved-mass-matrix/2921>\
**Category:** General\
**Created:** [January 8, 2018, 11:01am UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921 "2018-01-08T11:01:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [January 8, 2018, 11:01am UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/1 "2018-01-08T11:01:18Z")

</div>

Hi!

I have just started to discover the `metric_file` option from cmdstan and I must say it is really awesome. A few questions:

- Should the `engaged` option be set to 0 if I give Stan the inverse mass matrix and the stepsize? I thought this would be the case, but I got very odd behavior such that I am confused. So when doing this Stan runs super-super fast, but gives me crap. Maybe this is a bug.

- Is there some documentation on the mapping from the parameters to the unconstrained parameter space. In particular I would be interested in ordering.

- Do we have recommendations or some guidance (likely with big warnings) what can be the minimal warmup when giving the metric file?

- Can cmdstan export the inverse masses? Currently I use rstan to parse things out.

- Can rstan already use the inverse mass matrix (@bgoodri? )?

This is getting really interesting in the context of MPI. Now you can use all available CPUs you have to get a single chain through warmup ASAP. Then you just start of as many chains as you like using the already calculated inverse weights. Certainly this is dangerous, but likely very handy for model-development.

Great new feature! Thanks to @mitzimorris, @bbbales2 and however else made this happen!

Best,  
Sebastian

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [January 8, 2018, 5:03pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/2 "2018-01-08T17:03:34Z")

</div>

> Should the engaged option be set to 0 if I give Stan the inverse mass matrix and the stepsize?

Can go either way. If you set it to zero, then there will be no adaptation, so you’ll just start sampling. It’s possible something is going crazy here, though I’d like to think not :P. I intended to do some work with this but never went beyond simple tests. I’ll look into this.

If it’s not zero, then adaptation will still happen, but you’ve just given the warmup a hopefully better guess about where to start. I’ve not experimented with the adaptation itself, but the algorithms are in: [https://github.com/stan-dev/stan/blob/develop/src/stan/mcmc/var\_adaptation.hpp](https://github.com/stan-dev/stan/blob/develop/src/stan/mcmc/var_adaptation.hpp) and [https://github.com/stan-dev/math/blob/develop/stan/math/prim/mat/fun/welford\_var\_estimator.hpp](https://github.com/stan-dev/math/blob/develop/stan/math/prim/mat/fun/welford_var_estimator.hpp) (and there are separate files for estimating a full covariance matrix)

> Do we have recommendations or some guidance (likely with big warnings) what can be the minimal warmup when giving the metric file?

I doubt the answer changes from anything else. Long enough :D

Re your other questions, I’d like to know the answers as well.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [January 8, 2018, 5:14pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/3 "2018-01-08T17:14:58Z")

</div>

> [@wds15](#):
>
> Can rstan already use the inverse mass matrix

Not yet.

---

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [January 8, 2018, 5:54pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/4 "2018-01-08T17:54:13Z")

</div>

> [@wds15](#):
>
> Should the engaged option be set to 0 if I give Stan the inverse mass matrix and the stepsize? I thought this would be the case, but I got very odd behavior such that I am confused. So when doing this Stan runs super-super fast, but gives me crap. Maybe this is a bug.

That sounds like it might be a bug. Can you capture the mass matrix at the end and compare?

---

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [January 8, 2018, 5:55pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/5 "2018-01-08T17:55:12Z")

</div>

If you don’t adapt, you’re not letting it adapt the stepsize which would be a problem since that’s not (necessarily?) saved in the mass matrix? Realizing I don’t know if the mass matrix diagonal includes the stepsize when CmdStan spits it out.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 8, 2018, 9:47pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/6 "2018-01-08T21:47:45Z")

</div>

here are a few differrent use cases in which this feature can be used:

- need to run the sampler to generate more samples to increase precision of your quantities of interest - no further adaptation

- want to continue adaptation, starting from specified mass matrix

the CmdStan manual, section “Euclidian Metric”, (starts on page 49) spells this out:

> The metric\_file option can be used with and without adaptation enabled.  
> If adaptation is enabled, the provided metric will be used as the initial guess in the adaptation process. If the initial guess is good, then adaptation should not change it much. If the metric is no good, then the adaptation will override the initial guess.

> If adaptation is disabled, both a metric\_file and stepsize should be provided to the sampler. Disabling adaptation disables both metric and stepsize adaptation, so a stepsize should be provided along with a metric to enable efficient sampling.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [January 9, 2018, 3:27pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/7 "2018-01-09T15:27:33Z")

</div>

> [@mitzimorris](#):
>
> If adaptation is disabled, both a metric\_file and stepsize should be provided to the sampler. Disabling adaptation disables both metric and stepsize adaptation, so a stepsize should be provided along with a metric to enable efficient sampling.

I think there is a bug… I did what you suggest in the manual. So I put num\_warmup=0, I give the program an inverse mass matrix and I do pass in the stepsize. However, Stan **ignores** the stepsize which I have given to it. The fit runs extremely fast, but gives me nothing meaningful. Looking at the generated csv it is reported that a step size of 1 was used instead of what I provided at the command line. The mass matrix on the other hand is equal to what I had specified as it looks like.

Other than that, I think that the text in the manual should also tell users to give the correct initial which is needed to make the chain explore in exactly the same way the posterior.

Do we have a test which runs warmup + some iterations and then starts another chain setup such that it must generate the same iterations (specifying seed, as initial the last draw from warmup and the mass matrix)? This is what I would expect to happen.

@sakrejda : I am currently using rstan to parse the csv files generated from cmdstan. You do get a step size and the inverse mass matrix elements of the diagonal. However, the stepsize seems to be ignored right now.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [January 9, 2018, 3:57pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/8 "2018-01-09T15:57:17Z")

</div>

Setting num\_warmup=1 and engaged=0 essentially gives me the behavior I want. So with these settings then Stan does not change the stepsize and the mass matrix is maintained. Looks like it works like this for the application I have in mind (branch off different chains which share the same adaptation).

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [January 9, 2018, 4:08pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/9 "2018-01-09T16:08:27Z")

</div>

that does sound like a bug. we’ll look into it.  
glad you have a workaround.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [January 9, 2018, 6:19pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/10 "2018-01-09T18:19:06Z")

</div>

Had a quick look at this. Definitely sounds like a bug, and the original tests in cmdstan I coded up did not check this case.

I made an issue for this just so there’s an issue number to reference ([https://github.com/stan-dev/cmdstan/issues/602](https://github.com/stan-dev/cmdstan/issues/602)).

I’m having trouble reproducing it though. If I just use the bernoulli example with cmdstan, make a metric file (metric.dat, containing “inv\_metric \<- c(50.0)”), and then execute the binary with the command line:

`./bernoulli method=sample num_samples=1 num_warmup=0 adapt engaged=0 algorithm=hmc engine=nuts metric=diag_e metric_file=metric.dat stepsize=0.5 data file=bernoulli.data.R output file=output.csv`

Then the output file says the stepsize was actually set to 0.5:

```
...
# refresh = 100 (Default)
lp __,accept_stat__ ,stepsize __,treedepth__ ,n_leapfrog __,divergent__ ,energy__,theta
# Adaptation terminated
# Step size = 0.5
# Diagonal elements of inverse mass matrix:
# 50
...

```

I also ran a regular Bernoulli calculation (with like 1000 post-warmup samples and 1000 warmup samples) and then initialized the inv\_metric and stepsize from those numbers and things seemed to work.

I added more tests to the cmdstan metric\_test.cpp file (which should be testing these interfaces) and am getting the expected behavior as well (can be found here: [https://github.com/bbbales2/cmdstan-rus/commit/8c13b906621cc3011de124cd6b234425f917ba93](https://github.com/bbbales2/cmdstan-rus/commit/8c13b906621cc3011de124cd6b234425f917ba93)).

Any ideas on what I’m doing different?

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [January 9, 2018, 6:30pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/11 "2018-01-09T18:30:20Z")

</div>

Hmm… I think I had num\_warmup=0 and engaged=1 (and stepsize + metric file) and then the stepsize was reset to 1. I can check tom again in case this does not lead to a reproducible disaster…

---

<div class="post-metadata">

**Author:** ![csetraynor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/csetraynor/32/5283_2.png) [@csetraynor](https://discourse.mc-stan.org/u/csetraynor)\
**Post date:** [October 17, 2019, 5:01pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/12 "2019-10-17T17:01:11Z")

</div>

I have a similar bug using cmdStan v2.20.  
So the bernoulli example works fine.  
(metric\_file is json with `inv_metric = 0.550527`)  
This is the plot with `adapt engage=0 metric_file = metric_file stepsize = 0.820551`

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/2/2faf666e83dc55ea73c3181ad259b3c9f2d9fc4d.png)

Now if I set `adapt engage=0 metric_file = "" stepsize = 1`

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/b/b10d9c9edadac4c624c6f1ca04088e05de78bba9.png)

So passing the inverse matrix helps but I see that this example already samples closely even wihout engaging.

I have issues trying to implement this to a model I am working on. The model works fine I tested it various times with an small example using the sample algorithm I get the following approximation.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/1/1a1618b461676cbc1657be3833da82e7c72df6cc.png)

Now when trying to use `adapt engage=0 metric_file = metric_file stepsize = 0.639891`  
(inv\_metric = array( c(0.00541849, 0.0232568, 0.0113541, 0.0143112, 0.0151313),  
dim = 5 ) )  
I get this disaster

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/3/38f60d1f1edc7bac4934250da2bda247c4a7dfa6.png)

The model that I use if you want to test it is a bit complex and just something I am working on that I attach here for testing purposes if can help.[simprogress4.data.R](https://discourse.mc-stan.org/uploads/short-url/5BKyVZRceJ7q7iwTvIJAQM8jBlf.R) (4.9 KB) [simprogress4.stan](https://discourse.mc-stan.org/uploads/short-url/dNyzkCHHOfe2merjJXwyC5cYhDE.stan) (2.6 KB) [test-inv-matrix-1-metric-file.R](https://discourse.mc-stan.org/uploads/short-url/gorgKtKS0QoLglF8xWWDpXfR6oU.R) (97 Bytes)[init.R](https://discourse.mc-stan.org/uploads/short-url/pV1JwCxk13qFZwQeR3rGyVk5r58.R) (102 Bytes)

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [October 17, 2019, 5:23pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/13 "2019-10-17T17:23:53Z")

</div>

Do you do any warmup?

It could be that your initial position is very far from the typical set. So does warmup help?

---

<div class="post-metadata">

**Author:** ![csetraynor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/csetraynor/32/5283_2.png) [@csetraynor](https://discourse.mc-stan.org/u/csetraynor)\
**Post date:** [October 17, 2019, 5:28pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/14 "2019-10-17T17:28:35Z")

</div>

No I am not doing warm-up. I try to see with warm-up but I had in mind to build a serialised code to submit to a cluster of workstations. Reading this I thought the inverse mass matrix saves the state of the Markov Chain so one can continue sampling is that correct? Still if a short wam-up works maybe I can still use it

---

<div class="post-metadata">

**Author:** ![csetraynor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/csetraynor/32/5283_2.png) [@csetraynor](https://discourse.mc-stan.org/u/csetraynor)\
**Post date:** [October 17, 2019, 5:35pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/15 "2019-10-17T17:35:39Z")

</div>

Checked again using this:  
# Adaptation terminated  
# Step size = 0.639891  
# Diagonal elements of inverse mass matrix:  
# 0.00541849, 0.0232568, 0.0113541, 0.0143112, 0.0151313

and running even with 1000 wam-up iterations if engaged=0 it seems not to work, disaster again.

---

<div class="post-metadata">

**Author:** ![csetraynor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/csetraynor/32/5283_2.png) [@csetraynor](https://discourse.mc-stan.org/u/csetraynor)\
**Post date:** [October 17, 2019, 5:37pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/16 "2019-10-17T17:37:19Z")

</div>

The error is like the beggining of the thread it runs super-fast but it does not seem to be working is the bug still persisting?  
I use a wrapper that call cmdStan and using the following arguments:

```
cmdStanTools::runModel2(model = "Model/model/simprogress4", 
                        data = "Model/data/simprogress4.data.R",
                        iter = 1000, warmup = 1000, thin = 1,
                        init = "Model/model/simprogress4/temp/1/init.R",
                        seed = 123456,
                        engaged = 0,
                        tag = "sample",
                        chain = 1, refresh = 100,
                        metric_file = "Model/MetricFile/test-inv-matrix-1-metric-file.R",
                        adapt_delta = 0.9, stepsize = 0.639891)

```

where engaged is adapt engaged. Just pasted for the purpose of reviewing the arguments.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 17, 2019, 7:14pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/17 "2019-10-17T19:14:58Z")

</div>

You’ll need to supply a stepsize or let that be adapted still. adapt engaged=0 turns timestep and metric adaptation off. Try:

```
./simprogress4 sample adapt init_buffer=0 window=0 term_buffer=50 num_warmup=50 algorithm=hmc metric_file=test-inv-matrix-1-metric-file.R data file=simprogress4.data.R init=init.R

```

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 17, 2019, 7:15pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/18 "2019-10-17T19:15:30Z")

</div>

(And yeah we should really document this, apologies for the frustration)

---

<div class="post-metadata">

**Author:** ![csetraynor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/csetraynor/32/5283_2.png) [@csetraynor](https://discourse.mc-stan.org/u/csetraynor)\
**Post date:** [October 17, 2019, 9:20pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/19 "2019-10-17T21:20:04Z")

</div>

Hi That worked great! Yes so the solution is then to keep `adapt engaged=1` and use the parameters `adapt init_buffer=0 window=0 term_buffer=50 num_warmup=50`  
Thank you!

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [October 18, 2019, 1:45pm UTC](https://discourse.mc-stan.org/t/saved-mass-matrix/2921/20 "2019-10-18T13:45:11Z")

</div>

> [@bbbales2](#):
>
> we should really document this

noted: [better documentation/discussion of sampler config for user-specified mass matrix · Issue #95 · stan-dev/docs · GitHub](https://github.com/stan-dev/docs/issues/95)

[Next page](https://discourse.mc-stan.org/t/saved-mass-matrix/2921.md?page=2)
