Fitting finished unexpectedly variational rstan

Hi guys, i would like to use CmdStan for some models, in particular the variational bayes feature. I am trying with a very simple model, just to see how it works, but i am getting the error : Fitting finished unexpectedly! This is my code:

model = "
data{
real x;
}
parameters {
  real mu;
}
model {
  mu ~ normal(0, 1);
  x ~ normal(mu,0.5);
}
"

p = "~/pathfinder_testing/cmdstan/" 

cmdstanr::set_cmdstan_path(pp)

model_file = '~/R/trial_model.stan'

write_stan_file(
  model,
  dir = ".",
  basename = "./R/trial_model.stan",
  force_overwrite = FALSE,
  hash_salt = ""
)

mod = cmdstanr::cmdstan_model(model_file)
data = list(x = 2)
fit_vb <- mod$variational(data = data, seed = 123, output_samples = 4000)

Instead, when i run the base stan fit function

fit <- stan(
    model_code = model,
    data = data,
    chains = 4,
    warmup = 500,
    iter = 1000,
    cores = 4,
    verbose = T)

the model works correctly. Do you have some advice?

Thank you a lot in advance!!!

Just to clarify, you are running something through cmdstanr, not through rstan as the title suggests.

Also, the following suggests our new pathfinder algorithm,

but what this call is doing is calling our autodiff variational inference algorithm:

Here’s the doc for the variational method:

I think Pathfinder will be in the next release of CmdStanR and of CmdStanPy. One of the reasons we developed it is because the ADVI algorithm can have problems when the posterior is even a bit challenging. That can be fixed with a combination of better step size tuning, more iterations in the nested ELBO evaluation, a slightly different init, and using the stick-the-landing gradient estimator. We will probably make those upgrades to ADVI over the next year. Now it can be finicky. You might try a smaller step size rather than letting it adapt.

1 Like

Ok, i think i had an installation problem. I reinstalled and could run CmdStanR with ADVI without problems. Thank you very much for the link you provided, i missed it in the beginning.

1 Like