Speed up posterior_epred?

Hi there - I’m evaluating models fitted using brms in the context of a population projection model that requires different newdata values at each timestep. Ultimately this needs to be done iteratively (sampling from posteriors) in order to get a distribution of model outputs. For each full set of iterations, this might require more than 100,000 executions of posterior_epred. And it seems pretty clear from profiling that posterior_epred is really a bottleneck. For comparison, I used to do something similar using MCMCglmm, and posterior_epred seems to be ~37 times slower. I really need to use brms in this case because of some key features for specifying models.

I’ve thought about pre-evaluating before running the population model and sampling from an existing array, but the number of possible combinations of dependent variables is huge (though it would depend on how coarsely I discretize independent variables). This approach seems less ideal.

There’s a similar issue posted here, but I’ve got a more recent version of the posterior package, which appears to have been the solution. (It seems it would have been even slower before)

It seems that the issue might be due to the fact that the posterior is being extracted every single time. Is there a way to extract it once and pass it to posterior_epred? I was hoping that specifying a single draw using the draw_ids argument would speed things up, but it doesn’t at all. (For each iteration I only need a single draw)

I’m using brms 2.19.0 (R 4.2.1 on CentOS 7)


Sorry it’s taken a while to get to your question. For what it’s worth, to me this feels like one of those questions where a fuller understanding of your problem and what you’re trying to do might point the way to some clever tricks that achieve what you need. Are you able to elaborate a little bit on your problem?