RStan 2.0: how to control the sampler's behavior?

1,316 views
Skip to first unread message

Sergio Polini

unread,
Oct 20, 2013, 6:56:11 PM10/20/13
to stan-...@googlegroups.com
Two issues: the help pages are a bit confusing (to me, at least), and I
can't understand how to set controls.

1. The stan() help page says that there is a "control" parameter that
can be used to set adaptation parameters. "In addition, Depending on
which algorithm is specified, different parameters can be set as in Stan
for sampling. For algorithm HMC [...] we can set 1. int_time [...] 2.
stepsize [...] 3. stepsize_jitter [...] For algorithm NUTS, we can set
1. metric [...] 2. max_treedepth etc."
It looks as if max_treedepth _and_ metric cannot be set for static HMC.
However, when using command-line Stan, if I try:

./bernoulli sample algorithm=hmc engine=static \
metric=unit_e max_depth=12 data file=bernoulli.data.R

I get an error message: "max_depth=12 is either mistyped or misplaced."
Just max_depth!
But if I try:

./bernoulli sample algorithm=hmc engine=static metric=unit_e \
data file=bernoulli.data.R

there's no problem. (No apparent problem at least.)
Looking at the Manual, I read that "command skeleton for invoking the
basic Hamiltonian Monte Carlo Sampler (HMC) [...] is the same as the
NUTS command skeleton shown in Figure 4.3 other than for the engine"
(Figure 4.4) and there is "[metric={unit_e,diag_e,dense_e}]" too, but
pag. 48 says that static HMC can have just one valid subargument: int_time.

2. The "control" parameter should be a named list. AFAIK, a named list
is something like "list(adapt_delta = 0.6, adapt_t0 = 12)" but I'm not
able to get it working. A few examples:

> sm <- stan_model("normal.stan")
> sf <- sampling(sm, chains=1, algorithm="HMC",
control=list(adapt_delta=0.5))
SAMPLING FOR MODEL 'normal' NOW (CHAIN 1).
Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 0 seconds (Warm-up)
0.02 seconds (Sampling)
0.02 seconds (Total)

Ok. In fact:

> sf@stan_args[[1]]$control$adapt_delta
[1] 0.5
> attr(sf@sim$samples[[1]], "args")$control$adapt_delta
[1] 0.5

But:

> sf <- sampling(sm, chains=1, algorithm="NUTS",
control=list(adapt_delta=0.5))
SAMPLING FOR MODEL 'normal' NOW (CHAIN 1).
Error : Invalid adaptation parameter (found int_time=-6.86169e-140;
require int_time>0).
error occurred during calling the sampler; sampling not done

int_time???

As to metric:

> sf <- sampling(sm, chains=1, algorithm="HMC",
control=list(metric="unit_e"))
SAMPLING FOR MODEL 'normal' NOW (CHAIN 1).
Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 0.01 seconds (Warm-up)
0.01 seconds (Sampling)
0.02 seconds (Total)

Looks good, but I guess that metric is ignored:

> sf@stan_args[[1]]$control$metric
[1] "unit_e"
> attr(sf@sim$samples[[1]], "args")$control$metric
NULL

If algorithm is NUTS:

> sf <- sampling(sm, chains=1, algorithm="NUTS",
control=list(metric="unit_e"))
SAMPLING FOR MODEL 'normal' NOW (CHAIN 1).
Error : Invalid adaptation parameter (found int_time=-6.86169e-140;
require int_time>0).
error occurred during calling the sampler; sampling not done

int_time again... Are there any bugs somewhere or should I try something
else?

Thanks
Sergio

Bob Carpenter

unread,
Oct 20, 2013, 7:49:00 PM10/20/13
to stan-...@googlegroups.com
I'll admit the new command structure is rather quirky and
will take some getting used to.

Overall, 2.0 still has an efficiency issue we haven't resolved
despite working on it all weekend. More on that later, but
we may just "unrelease" 2.0 in the coming week if we can't
sort it out.

More on how the command line's supposed to work below.

On 10/20/13 6:56 PM, Sergio Polini wrote:
> Two issues: the help pages are a bit confusing (to me, at least), and I can't understand how to set controls.
>
> 1. The stan() help page says that there is a "control" parameter that can be used to set adaptation parameters. "In
> addition, Depending on which algorithm is specified, different parameters can be set as in Stan for sampling. For
> algorithm HMC [...] we can set 1. int_time [...] 2. stepsize [...] 3. stepsize_jitter [...] For algorithm NUTS, we can
> set 1. metric [...] 2. max_treedepth etc."
> It looks as if max_treedepth _and_ metric cannot be set for static HMC. However, when using command-line Stan, if I try:
>
> ./bernoulli sample algorithm=hmc engine=static \
> metric=unit_e max_depth=12 data file=bernoulli.data.R

Static HMC has an integration time, whereas NUTS has max_depth
parameter. Both allow metric, stepsize and stepsize_jitter.

> I get an error message: "max_depth=12 is either mistyped or misplaced."

Right. No max_depth parameter for engine=static. Look at
figure 4.4.

> Just max_depth!
> But if I try:
>
> ./bernoulli sample algorithm=hmc engine=static metric=unit_e \
> data file=bernoulli.data.R
>
> there's no problem. (No apparent problem at least.)

Right. metric=unit_e is a valid subargument for algorithm=hmc.

> Looking at the Manual, I read that "command skeleton for invoking the basic Hamiltonian Monte Carlo Sampler (HMC) [...]
> is the same as the NUTS command skeleton shown in Figure 4.3 other than for the engine" (Figure 4.4) and there is
> "[metric={unit_e,diag_e,dense_e}]" too, but pag. 48 says that static HMC can have just one valid subargument: int_time.

The indentation in the diagrams indicates scope. So
metric is a subargument of algorithm=hmc, whereas int_time is a subargument
of engine=static.

> 2. The "control" parameter should be a named list. AFAIK, a named list is something like "list(adapt_delta = 0.6,
> adapt_t0 = 12)" but I'm not able to get it working.

I'm not sure how this is all configured in RStan, so
I'll leave this for someone else to answer.

...snip...

- Bob

Jiqiang Guo

unread,
Oct 20, 2013, 11:18:41 PM10/20/13
to stan-...@googlegroups.com
According to current rstan's doc, there is at least one bug, which is that you got the error about int_time when specifying metric.  I will fix it. 

I will allow specifying metric for HMC sampler as well. 

--
Jiqiang 




--
You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Sergio Polini

unread,
Oct 21, 2013, 6:37:39 AM10/21/13
to stan-...@googlegroups.com
Il 21/10/2013 05:18, Jiqiang Guo ha scritto:
> I will allow specifying metric for HMC sampler as well.

If I look to str(<stanfit>) I find:

.. .. .. .. ..$ sampler_t : chr "HMC"
.. .. .. .. ..$ control :List of 6
.. .. .. .. .. ..$ adapt_engaged: logi TRUE
.. .. .. .. .. ..$ adapt_gamma : num 0.05
.. .. .. .. .. ..$ adapt_delta : num 0.65
.. .. .. .. .. ..$ adapt_kappa : num 0.75
.. .. .. .. .. ..$ adapt_t0 : num 10
.. .. .. .. .. ..$ int_time : num 6.28

so I think that specifying stepsize and stepsize_jitter should be
allowed too.

Sergio

Jiqiang Guo

unread,
Oct 21, 2013, 6:37:52 PM10/21/13
to stan-...@googlegroups.com
This should be fixed in develop branch now. 

--
Jiqiang 


Ryan

unread,
Nov 30, 2013, 5:55:59 PM11/30/13
to stan-...@googlegroups.com
I believe I am having a similar control issue with rstan. Suppose I want to adapt the stepsize outside of Stan (or say I want to fix it at some value based on previous runs). So this means I want warmup=0 and have stepsize set to a value of my choice. Does this even make sense? Are some other parameters needed for this to be fixed properly?

As of now when I set something like control=list(stepsize=0.25) with warmup=0 I get no good samples:

> summary( get_sampler_params(fitb.prev$fit)[[1]] )
 accept_stat__   stepsize__  treedepth__
 Min.   :0     Min.   :1    Min.   :-1  
 1st Qu.:0     1st Qu.:1    1st Qu.:-1  
 Median :0     Median :1    Median :-1  
 Mean   :0     Mean   :1    Mean   :-1  
 3rd Qu.:0     3rd Qu.:1    3rd Qu.:-1  
 Max.   :0     Max.   :1    Max.   :-1  

Seeing stepsize=1 makes me think something is wrong with the control list, but if I set stepsize_jitter then I get stepsize jittered around 1.

Assuming stepsize is getting set properly, is this because the diag_e is not adapted properly? Big picture speaking, is this not even a good idea to try and do? Any other ideas why this isn't working as expected?

Thanks!

Ryan

Kevin

unread,
Dec 5, 2013, 7:42:39 PM12/5/13
to stan-...@googlegroups.com
I have been having the same problem. Anybody have any idea what's wrong?

Michael Betancourt

unread,
Dec 5, 2013, 10:02:33 PM12/5/13
to stan-...@googlegroups.com
Setting the step size only sets the initialization of the step size.  Even if there are no warmup iterations a step size initialization routine will still be run to get at a good initial guess.   If you want to maintain the specified step size then turn off adaptation entirely be setting "adapt engaged=false".
--
You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages