# Sampling on a fixed grid or on a specified list of points

Hi, I’m working on a model which has a small number of variables. To gain some insight (I hope) I am thinking that I’ll evaluate the posterior on a fixed grid of points, let’s say m^n points where each of n variables is divided into m equally spaced values. (The priors for all variables are uniform, so evaluating the posterior is equivalent to just evaluating the likelihood function.)

Is there a way to tell Stan that I want to sample on a fixed grid? Or perhaps a way to say that I want to evaluate the posterior at a specified list of points? Then I would just generate the grid points and put them in a list. (If I can get that working, I would be interested to try low-discrepancy sequences a.k.a. quasi Monte Carlo, although that’s a lower priority.)

I realize that grid sampling doesn’t make sense in more than a few dimensions, but luckily, I am indeed working in just a few dimensions. Thank you in advance for any light you can shed on this question.

best,

Robert Dodier

2 Likes

Not directly. You can use the “fixed param” sampler to recompute generated quantities, and I think also the log likelihood.

However I believe getting grid sampling to run in Stan is much more difficult than just use the built-in NUTS sampler and there is little to be gained by evaluating at a fixed grid. What exactly do you plan to achieve with that? Note that in low dimensions, the default sampler will most likely also cover the relevant part of the parameter space very well.

If you really want to do grid sampling, I think it would actually be much easier to just rewrite the model in whatever language you use and then simply calculate the grid there.

Best of luck with your model!

1 Like

I’d actually also like this functionality.

Just to mention–I assume you already know this–but these are not equivalent. They will differ by a factor of the (generally unknown) normalizing constant for the posterior (p(data) in Bayes’ formula).

Hi Martin, thanks for your reply. The situation is that I’m working with a complex-enough ODE model with a small number of uncertain variables. The number of variables is small enough to contemplate grid sampling, and the ODE model is complex enough that reimplementing it in a different environment would require some effort. So that’s the motivation for trying to get Stan to handle the grid. I’ll take a look at the fixed param sampler.

1 Like

I just realized that instead of running a single fixed param run for each point on the grid (which might be expensive as Stan’s startup and data copy will dominate the runtime), why not just implement the grid in `generated quantities` ? This let’s you almost directly reuse any Stan code you have and is likely to be quite efficient…

I actually want to do the exact same thing as @Robert_Dodier (for different reasons) but including computing the gradient. That is not possible via GQ, or is it?

This is slowly getting a bit out of my depth. I am quite sure this is not possible in GQ. I think that to access the gradient you need to use the `diagnostic_file` in `rstan` (`save_latent_dynamics` in `cmdstanr`) or hack around in C++.

1 Like

If using RStan or certain versions of PyStan, you could use log_prob and log_prob_grad methods to evaluate the log denisty and gradients in arbitrary grid of parameter values. Just create the grid in R or Python. There are examples both in R and Python of using these methods, and I can include links if you don’t find them with quick googling (sorry didn’t have time to check the best pages to link)

3 Likes