Hello Stan Community!
Context: I’d like to fit a Bayesian hierarchical model with more than one set of spatio-temporal effects and unobserved/latent variables by using cmdstanr. In order to do so, I need to set initial values for the chains given the complexity of the model, which in turn also rule outs using the optimize method without initial values. In low dimensions (of the parameter space), I’ve managed to produce decent initial values as to successfully run the optimize method, whose output I use it as initial values for the sample method. This doesn’t work anymore as the dimension increases.
Problem: I’d like to extract the log posterior probability or the log likelihood from my Stan model without the need to call any method (given the model complexity) as a function in order to use different optimization algorithms and find the mode of such a function in order to use it as initial value for the sample method. Unfortunately, log_prob is a call from a method so I can’t use it without calling an inference algorithm. Is there a way to achieve this?
Thanks a lot in advance, I’d appreciate any suggestion for addressing this problem.
Best,
Roman.
Thanks a lot for you reply @avehtari!
- The Pathfinder method doesn’t work even with lower values for the argument init.
- I couldn’t managed to use `$log_prob` without calling any inference algorithm. In the example shown in the documentation it seems that the first line is calling the sample(HMC) method.
- I’ve tried to create a dummy object by calling the sample method with one chain and one iteration but it fails - I guess, because the (random) initial value isn’t a valid value.
I should mention that the first level of the hierarchical model is a state-space model on itself - may be I should say that it’s a state space model with parameters and hyperparameters in which the parameters are sets of spatio-temporal effects.
Sorry, I forgot that the model needs to be instantiated with data.
Does this work for you
fit <- mod$sample(data = data_bin, init=0, fixed_param=TRUE, iter_sampling=0)
fit_bin$log_prob(unconstrained_variables = theta)
If 0 is not valid init, you can still get init complaints, but the model is still instantiated with data.
1 Like
I can run the first line, that is, create the dummy object but when running the second line I get the message:
Error: Model methods cannot be used with a pre-compiled Stan executable, the model must be compiled again
By the way, the parameters of my model have different classes (real, vector, etc) but I guess that isn’t what is causing the aforementioned error.
When calling cmdstan_model() use argument compile_model_methods=TRUE, see complete example in Laplace method and Jacobian of parameter transformation
2 Likes
It didn’t work at the beginning but I also added force_recompile = TRUE and it works!
Thanks a lot! Now I can use the log probability as a function.
2 Likes