## Speed tests for Stan algorithms. HMC should scale linearly with int_time## and NUTS should be similar for average n_leapfrog__ for the warmup and## sampling period.seeds <- 1:20int_time <- seq(1,20, by=2)Nsim <- 5000schools_dat <- list(J=8, y=c(28,8,-3,7,-1,1,18,12), sigma=c(15,10,16,11,9,11,10,18))fit <- stan(file='schools/schools.stan', data=schools_dat, iter=10, chains=1, seed=seed, algorithm='HMC', control=list(int_time=1))speed.hmc <- ldply(seeds, function(seed){ fit.hmc <- ldply(int_time, function(L){ fit <- stan(fit=fit, data=schools_dat, iter=Nsim, chains=1, seed=seed, algorithm='HMC', control=list(int_time=L)) data.frame(L=L, seed=seed, rstan::get_elapsed_time(fit)) })})speed.nuts <- ldply(seeds, function(seed){ fit <- stan(fit=fit, data=schools_dat, iter=Nsim, chains=1, seed=seed, algorithm='NUTS') temp <- data.frame(get_sampler_params(fit)) data.frame(warmup.nleap=mean(temp[(1:Nsim/2), 'n_leapfrog__']), sample.nleap=mean(temp[-(1:Nsim/2),'n_leapfrog__']), get_elapsed_time(fit), seed=seed)})par(mfrow=c(1,2))plot(warmup~L, data=speed.hmc, ylab="Time", xlab="Steps", main='Warmup', ylim=c(0, max(speed.hmc$sample)))points(warmup~warmup.nleap, data=speed.nuts, col='red', pch=2)plot(sample~L, data=speed.hmc, ylab="Time", xlab="Steps", main='Sample', ylim=c(0, max(speed.hmc$sample)))points(sample~sample.nleap, data=speed.nuts, col='red', pch=2)legend('topleft', legend=c("HMC", "NUTS"), col=c('black', 'red'), pch=c(1,2))
- Why would the sampling period for HMC be variable when the warmup was not (both were the same number of iterations)?
- Shouldn't HMC be very consistent and scale directly with int_time?
- Shouldn't the NUTS algorithm take the same time as HMC given the same number of total leapfrog steps?
- Shouldn't the timing be independent of the tuning parameters, given total leapfrog steps?
Firstly, when including figures please make sure that they are large enough to at leastread the axis labels easily enough.
Secondly, let me be a bit pedantic regarding names. Hamiltonian Monte Carlo (HMC)is a very generic way of generating high-performance Markov chains. NUTS is one_implementation_ of HMC. As is _static HMC_, which uses a constant integrationtime and takes the final point as a proposal. In your comparisons you are comparingstatic HMC with NUTS.
Thirdly, integration time is not equal to the number of leapfrog steps. Rather integrationtime is the product of the integrator step size and total number of leapfrog steps, son_leapfrog = int_time / epsilon. Also, the mean n_leapfrog in NUTS isn’t particularlyhelpful due to the typical skewness of the distribution.
Finally, your expectations are almost correct _conditioned on all other tuning parametersbeing the same_. In particular, the integrator step size and mass matrix can make hugedifferences in the computational cost of a transition. This is important because theadaptation we do in warmup learns these other tuning parameters, and varying adaptationleads to varying performance.
Long story short, to do a proper comparison you’ll want to turn adaptation off to ensurethe same tuning parameters and make sure to compare the number of leapfrog stepsand not the integration time.
- Why would the sampling period for HMC be variable when the warmup was not (both were the same number of iterations)?
Because of adaptation in warmup. See the manual for a discussion of our windowedadaptation procedure. In particular, with iter=10 you have only 5 warmup iterationswhich will update the mass matrix only once, after the fifth iteration. If you use amuch more reasonable warmup then you’ll start to see variation in warmup timings,too.
- Shouldn't HMC be very consistent and scale directly with int_time?
Practical implementations of HMC use numerical integrators, which imply that thealgorithms should scales with the number of leapfrog steps, not integration timedirectly.
- Shouldn't the NUTS algorithm take the same time as HMC given the same number of total leapfrog steps?
Transition by transition, yes, if you ignore algorithmic overhead. But that doesn’tmean that they will when you average over chains set to different tunings.
- Shouldn't the timing be independent of the tuning parameters, given total leapfrog steps?
If you could set the total leapfrog steps then yes. But you can’t do that in any ofour samplers right now.
Secondly, let me be a bit pedantic regarding names. Hamiltonian Monte Carlo (HMC)is a very generic way of generating high-performance Markov chains. NUTS is one_implementation_ of HMC. As is _static HMC_, which uses a constant integrationtime and takes the final point as a proposal. In your comparisons you are comparingstatic HMC with NUTS.Fair enough, but suspect most users won't make that distinction b/c that's how you specify them in Stan.
It seems most other software programs with HMC (e.g., ADMB), as well as the papers describing it, you specify step size and number of steps. I think it might be worth explaining this in the adaptation section of the manual. The term int_time isn't used at all there, not really described in the help files in Rstan.
Finally, your expectations are almost correct _conditioned on all other tuning parametersbeing the same_. In particular, the integrator step size and mass matrix can make hugedifferences in the computational cost of a transition. This is important because theadaptation we do in warmup learns these other tuning parameters, and varying adaptationleads to varying performance.Neal (2011) describes two ways of using the mass matrix: a Chloesky decomposition of parameter space or using it in the kinetic energy function. The first seems to be more population. Does Stan use the mass matrix to do a Cholesky decomposition? Is that why there are such computational differences depending on tuning parameters? Are the differences only in unit vs. diagonal vs. dense matrices, or can there also be differences between different dense matrices (e.g. taking advantage of sparseness)?
I think it would be valuable for the user to be able to specify the number of steps opposed to the integration time.
For instance Langevin sampling can't be done directly in Stan (if I understand correctly). I could see that being potentially useful for some people in some models.
What was the reasoning behind specifying integration time instead?
Also, the calculation from n_step=int_time/stepsize isn't integer.. is this value rounded up?
I'm new to Stan but really like it so far and appreciate all the development that has gone into it. I think it may be helpful to the user base to have a more thorough explanation of the adaptation procedure that is going on internally. This will be particularly true for users who come from other software platforms or have coded it themselves on specific problems. For example I couldn't find the default behavior of argument metric, or how to pass an arbitrary mass matrix.
Is it correct that this is what happens when using static HMC: (1) User gives int_time, (2) a 'reasonable' stepsize is found and from this an approximate number of leapfrog steps (L), (3) the warmup procedure begins and stepsize is updated (and hence L) using dual averaging. (4) Continue with sampling.
Michael --- isn't Langevin just 1 step of HMC?
And if so, for a fixed step size (discretization interval),
couldn't we just fix integration time to be one step and get Langevin?
--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/FERVo5-h1L4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/FERVo5-h1L4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/FERVo5-h1L4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
> On Dec 28, 2015, at 1:25 PM, Cole Monnahan <mon...@uw.edu> wrote:
>
> Hi Bob, yes I've got my questions answered for the most part. It's been a useful discussion in that I've learned a lot. I think it's worth noting that ADMB coupled AD and static HMC (using inverse Hessian as mass matrix) probably a decade ago
I didn't see any mention of HMC in that paper.
Was that the wrong link?
That paper talks about random-walk Metropolis, which is
a terrible algorithm. Also, I don't think their proposed variant
will work, because (a) you need to regularize for positive definiteness (e.g.,
see Michael's paper on Softabs), and (b) with varying curvature, the
Hessian changes, and it defeats detailed balance. There's no further description
of the algorithm or adjustments made for soundness, only a pointer to Gelman
et al.'s book in section 2.3.2.
> and there's a community of scientists that are used to its way of doing things, who likely will migrate over to Stan. I think it's worthwhile for those users (me included, obviously) to be able to notice/understand the differences and improvements to ease that transition.
That'd be great from our perspective. We can explain Stan, but
I don't know ADMB. If you want to write a Stan tutorial for ADMB
users, we're more than happy to help out and advertise on Andrew's
blog.
Andrew's pushing us to use Laplace approximations, both for
penalized/marginal maximum likelihood and for black-box
variational inference, but there's no reason to do that (and
lots of reasons not to) in cases where MCMC will work.
--
The problem you run into with that approach is that the
mode often doesn't exist, or is not in the typical set, or the
posterior has varying curvature so that a static estimate
won't work well. We've found a diagonal variance to be more
robust and we have to compensate by taking small step sizes so
that an iterated first-order gradient can deal with the curvature.
But that introduces problems like stiff ODE solvers.
- Bob
>
> --
> 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.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/FERVo5-h1L4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
> On Dec 31, 2015, at 12:22 PM, Cole Monnahan <mon...@uw.edu> wrote:
>
> Andrew: Yes, I would assume so. Caveat the following with the fact I'm a student just learning all this myself... What makes ADMB so powerful is AD, a template language, and compiled C++ -- all of which Stan also has. ADMB has some additional nice features since it was designed for optimization. I could see the fishery and ecology communities moving to Stan over time but there's a ton of legacy ADMB code so that might be a slow transition.
We're already seeing people in that community use Stan.
And in the closely related population ecology fields. Always
amazing to me how intellectual communities organize.
> For instance, many fishery stock assessments done by the government are using a 20,000 line ADMB model (stock synthesis; Methot and Wetzel, 2011) which realistically no one is going to reproduce in Stan anytime soon.
How do you debug a 20K line ADMB model? We still haven't figured
that out for Stan. For something that big, we'd write the functions
in C++ anyway, and we know how to test that.
> Our field has relied on ADMB since the 1990s because it was the best (only?) option for robust non-linear optimization at the time, so it is really exciting to see the development of Stan and what it has to offer moving forward.
It's the combination of autodiff (or symbolic) gradients and
a simple language that's the key. I also don't know of anything
other than ADMB that did that. There were plenty of C++ and
autodiff packages, but nothing as far as I know putting them
together. Having a big community of users, like the fisheries and
wildlife people for ADMB is also a huge benefit for everybody.
- Bob