I. There's nothing to be gained speedwise from doing this:
for (n in 1:N)
... set log_p_vec[n] <- ...val comp for n ...
}
increment_log_prob( sum(log_p_vec) );
instead of:
for (n in 1:N)
... increment_log_prob(...val comp for n...); ...
The increment_log_prob builds up its own internal
vector and applies a big sum at the end that includes all
of the contributions of Jacobians and sampling statements and
other increment_log_prob calls. In fact, it'll be faster
than writing out into an array because (a) no array or
allocation or copies, and (b) flatter auto-diff expression
tree because the val will be flattened into every other
component of the log_prob (such as the priors here).
II. What would speed things up is only computing
pi[k] / sum(pi) for the k that you need. You only use
one per loop and then you take the log, so that'd be:
for(k in 1:N_type)
pi[k] <- exp(gamma_type[k] + delta[k] * x[n]);
as is, and then instead of using
pi <- pi/sum(pi);
and then pulling out one of the pi and logging it, as in
log(pi[k]), use:
log(pi[k] / sum(pi))
which is the same as:
log(pi[k]) - log(sum(pi)).
You could keep going and instead of taking the
log of an exp for log(pi[k]), just use
for(k in 1:N_type)
pi[k] <- gamma_type[k] + delta[k] * x[n];
(FWIW, I don't like setting variables and the renormalizing; I
know we do it in our examples, but here, two variables pi and exp_pi
would be preferable --- I should update our doc.)
Then, for whichever component j you use, just do:
pi[j] - log(sum(exp(pi))
which is more efficiently and arithmetically stably coded as:
pi[i] - log_sum_exp(pi)
Normalizing a log-scale multinomial is the bread-and-butter of
the log_sum_exp operation.
- Bob
- Bob
On 12/30/13, 10:31 PM, Avi Feller wrote:
> Dear wonderful Stan list,
>
> I've spent the past several months using Stan in the context of causal inference models. Overall, Stan has been a
> life-saver, allowing me to fit vastly more complicated models than I would be able to quickly code by hand. So thanks
> again for making such a great product.
>
> I'm writing because the upgrade from Stan 1.3 to Stan 2.x (both 2.0 and 2.1) proved to be a mixed blessing for the kinds
> of models I've been focusing on. On the one hand, effective sample sizes for the same models are marginally higher
> overall under Stan 2.1 than Stan 1.3. On the other, total computation time for the same number of iterations increased
> dramatically between v.1.3 and v.2.x, often by a factor of 3 or 4 in some of the more complicated models. While I
> haven't done a particularly scientific side-by-side (it's a pain to uninstall and re-install different versions), I'm
> confident that there's been a sizable decrease in ESS/sec when using Stan 2.x for these kinds of models.
>
> I have two related questions.
>
> First, is there a way to re-create the old Stan 1.3 default sampler behavior using the current version? I realize that
> this is not a sustainable question for future versions, but if someone's already thought about this, it would make it
> much easier for me to isolate the source of the slow-down and better understand the trade-offs in this case. (I've
> indeed tinkered with the sampler options but have yet to find a consistent speed-up)
>
> Second, I've attached some much-simplified example Stan code, which is based on Hirano, Imbens, Rubin, and Zhou (2000;
> /Biostatistics/). Unfortunately, I can't post the original data set, but am happy to email it directly to folks if
> that's useful. My actual data set of interest involves many more bells-and-whistles, but the overall speed change seems
> to hold for this simpler model. I very much welcome suggestions on how to speed up this base model (note that I've been
> hard-coding the 'softmax' transform given the persistent memory leak issues there).
>
> Thanks, as always, for the help. Session info below.
>
> Best,
> Avi
>
>
>
> > sessionInfo()
> R version 3.0.2 (2013-09-25)
> Platform: x86_64-apple-darwin10.8.0 (64-bit)
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] rstan_2.1.0 inline_0.3.13 Rcpp_0.10.5
>
> loaded via a namespace (and not attached):
> [1] stats4_3.0.2 tools_3.0.2
>
>