# Specify metric regularization constant?

**URL:** <https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451>\
**Category:** General\
**Tags:** techniques\
**Created:** [March 23, 2021, 7:30pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451 "2021-03-23T19:30:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [March 23, 2021, 7:30pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/1 "2021-03-23T19:30:02Z")

</div>

Currently, the regularization constant pulling the metric back towards the identity is hard coded as `1e-3 * (5.0 / (n + 5.0))` where `n` is the size of the current metric adaptation window ([stan/covar\_adaptation.hpp at develop · stan-dev/stan · GitHub](https://github.com/stan-dev/stan/blob/develop/src/stan/mcmc/covar_adaptation.hpp)). Would it be possible to change this, or make this modifiable?

Or, alternatively, is it possible to let stan print the unconstrained values instead of the constrained values to the output csv?

---

<div class="post-metadata">

**Author:** ![maxbiostat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/maxbiostat/32/1326_2.png) [@maxbiostat](https://discourse.mc-stan.org/u/maxbiostat)\
**Post date:** [March 23, 2021, 10:36pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/2 "2021-03-23T22:36:01Z")

</div>

@betanalpha

---

<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:** [March 24, 2021, 12:15pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/3 "2021-03-24T12:15:52Z")

</div>

> [@Funko\_Unko](#):
>
> Would it be possible to change this, or make this modifiable?

The easiest way is to modify the source at this point :D.

> [@Funko\_Unko](#):
>
> stan print the unconstrained values

These are available in the files here: [Save output and data files — fit-method-save\_output\_files • cmdstanr](https://mc-stan.org/cmdstanr/reference/fit-method-save_output_files.html) . The cmdstan option is named differently – like diagnostic\_files or something?

The unconstrained variables, gradients, and momentums get saved in that file. The unconstrained variables are the ones that don’t start with `p_` or `g_` or end in `__`.

---

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [March 24, 2021, 12:34pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/4 "2021-03-24T12:34:03Z")

</div>

> [@bbbales2](#):
>
> The easiest way is to modify the source at this point :D.

That’s what I feared D: It’s not a big deal though, I was just wondering aloud…

> [@bbbales2](#):
>
> like diagnostic\_files or something?

Aha! I found something in the manual, will have a look.

Thank you!

---

<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:** [April 2, 2021, 6:01pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/5 "2021-04-02T18:01:55Z")

</div>

> [@Funko\_Unko](#):
>
> Currently, the regularization constant pulling the metric back towards the identity is hard coded as `1e-3 * (5.0 / (n + 5.0))` where `n` is the size of the current metric adaptation window ([stan/covar\_adaptation.hpp at develop · stan-dev/stan · GitHub](https://github.com/stan-dev/stan/blob/develop/src/stan/mcmc/covar_adaptation.hpp)). Would it be possible to change this, or make this modifiable?

What’s the motivation for the change? It can be changed in the source easily but changes can have very large unintended effects.

> [@Funko\_Unko](#):
>
> Or, alternatively, is it possible to let stan print the unconstrained values instead of the constrained values to the output csv?

In CmdStan the unconstrained values are stored in a file specified by the `output diagnostic_file` option. This functionality should be available in all of the CmdStan wrappers and it also available in RStan with the rarely used `diagnostic_file` argument.

Keep in mind that the column names are not changed, so if one has `real<lower=0> x` then the column for the constrained and unconstrained values will be both `x` (because of how the model and samplers are abstracted there’s now way to determine which transformation is being applied, and hence a more principled naming convention). Similarly multivariate constrained types with lower-dimensional unconstrained spaces, such as simplices and covariance matrices, will have fewer elements in the `diagnostic_file` output.

---

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [April 6, 2021, 9:54am UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/6 "2021-04-06T09:54:48Z")

</div>

> [@betanalpha](#):
>
> What’s the motivation for the change? It can be changed in the source easily but changes can have very large unintended effects.

It looks like for chaotic ODE systems, the diagonal entries/eigenvalues of the covariance matrix can very quickly decay from unit scale on the prior to the order of `1e-6` or less. Note that I don’t want to change the default behavior, I only would like to know how different regularization constants would affect the behavior of the sampler/warmup.

> [@betanalpha](#):
>
> In CmdStan the unconstrained values are stored in a file specified by the `output diagnostic_file` option. This functionality should be available in all of the CmdStan wrappers and it also available in RStan with the rarely used `diagnostic_file` argument.

Thank you, I will have a look.

---

<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:** [April 12, 2021, 7:12pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/7 "2021-04-12T19:12:34Z")

</div>

> [@Funko\_Unko](#):
>
> It looks like for chaotic ODE systems, the diagonal entries/eigenvalues of the covariance matrix can very quickly decay from unit scale on the prior to the order of `1e-6` or les

I am fairly confident that this is not a problem with the metric adaptation but rather the warmup exploration itself, and hence changing the metric adaptation configuration won’t help.

Singular covariance function _estimates_ arise when the sampler freezes. For dynamical Hamiltonian Monte Carlo this can happen due to biased gradient evaluations (the state probabilities quickly decay from the initial point so that even if a long trajectory is constructed only points near the initial point are ever sampled) or poor initial adaptation (the step size quickly drops to zero and the numerical trajectories end up confined near the initial point because the max treedepth limit). These are often related, with the small step size early in warmup caused by numerical problems in evaluating the target density or its gradient.

No amount of fussing with the metric will fix this; one has to instead work out why the sampler is freezing.

---

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [April 13, 2021, 6:46am UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/8 "2021-04-13T06:46:35Z")

</div>

> [@betanalpha](#):
>
> Singular covariance function _estimates_ arise when the sampler freezes.

I’m fairly confident in the _correctness_ of the covariance estimate, just as I do not doubt the inadequacy of the regular warmup procedure for even moderately difficult chaotic ode problems. Still, I’m curious how the regularizatiom constant influences the behavior.

Note that due to the “sensitive dependence on initial conditions” (and on the ODE parameters), a large part of the parameter space very quickly becomes incompatible with the (in silico) “observations”.

(The simplest non-boring chaotic ODE problem should be the Lorenz system with not-super-tight priors.)

---

<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:** [April 26, 2021, 8:48pm UTC](https://discourse.mc-stan.org/t/specify-metric-regularization-constant/21451/9 "2021-04-26T20:48:44Z")

</div>

> [@Funko\_Unko](#):
>
> chaotic ode problems

Chaotic ODEs have bigger problems. If the base system is chaotic then the forward sensitivity system (and eventually the backward adjoint system) will also be chaotic, and numerical solves will quickly decouple from each other. In other words the numerically-solved sensitives will no longer correspond to sensitivities of the numerically-solved states and the overall target density will no longer be related to the gradients of the target density. This is where the entire numerical correction scheme in Stan’s dynamic Hamiltonian Monte Carlo breaks down and adaptation is irrelevant.
