cmdstanr icon indicating copy to clipboard operation
cmdstanr copied to clipboard

unconstrain_draws example does not work with extra variables

Open avehtari opened this issue 1 year ago • 3 comments

unconstrain_draws() doc has the following example

fit_mcmc$unconstrain_draws(draws = fit_mcmc$draws())

but this does not work if there are other variables (transformed parameters or generated quantities) stored in the draws, and fails with an error like

Error: Model has 469 unconstrained parameter(s), but 188800 were provided!

It is not trivial to figure out how to proceed after this error

avehtari avatar May 23 '24 06:05 avehtari

Hmm I can't replicate this, the following works for me without issue:

modcode <- "
data {
  real y_mean;
}

parameters {
  real y;
}

model {
  y ~ normal(y_mean, 1);
}
generated quantities {
  vector[10] y_gq = rep_vector(y * 2, 10);
}
"
mod <- cmdstan_model(write_stan_file(modcode))
fit <- mod$sample(data = list(y_mean = 0), parallel_chains=4)
dr <- fit$draws()

fit$init_model_methods()
tes <- fit$unconstrain_draws()
tes <- fit$unconstrain_draws(draws = dr)

andrjohns avatar May 23 '24 07:05 andrjohns

Sorry, it was grad_log_prob()

> fit$grad_log_prob(fit$unconstrain_draws(draws = dr))
Error: Model has 1 unconstrained parameter(s), but 4000 were provided!

avehtari avatar May 23 '24 11:05 avehtari

After discussion with @andrjohns , this is just a case of bit unclear error message for invalid input

avehtari avatar May 23 '24 12:05 avehtari