fix: fix init with draws or fit object and without .stan file#1171
fix: fix init with draws or fit object and without .stan file#1171
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1171 +/- ##
==========================================
+ Coverage 91.14% 91.23% +0.08%
==========================================
Files 15 15
Lines 6065 6070 +5
==========================================
+ Hits 5528 5538 +10
+ Misses 537 532 -5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
jgabry
left a comment
There was a problem hiding this comment.
There's an edge case that causes an error. I think it's inferring the wrong number of dimensions for higher dimensional objects that only contain 1 element:
stan_code <- write_stan_file(
"
data { real y; }
parameters { matrix[1,1] mu; }
model { y ~ normal(mu[1,1], 1); }
"
)
mod <- cmdstan_model(stan_code, force_recompile = TRUE)
fit <- mod$sample(data = list(y = 0))
mod_exe <- cmdstan_model(exe_file = mod$exe_file())
# both of these error
mod_exe$sample(data = list(y = 0), chains = 1, init = fit$draws())
mod_exe$sample(data = list(y = 0), chains = 1, init = fit)I think this would also error for array[1,1] real, array[1,1,1] real, etc., although I haven't tested those. These probably aren't very common in Stan programs, but could exist.
|
Fixed and the description in the first comment has been updated |
also fix some uses of `=` for assignment (unrelated to this PR)
jgabry
left a comment
There was a problem hiding this comment.
Looks good now, thanks. We can merge if everything passes. (I also pushed one commit cleaning up a couple of trivial unrelated things in args.R that I noticed while I was looking at it. I probably should do that separately from this PR, but it was just changing some = to <- for assignment)
|
I also notices some bad indentation making it harder to see nesting in if-else-statements. I think it's better to make a separate PR for formatting |
|
Actually @VisruthSK maybe we should do this before #1153 so that the diffs are separate? |
|
Thanks Aki! merging now. |
Closes #1088
Fix init from fit objects when model has no Stan file
When a
CmdStanModelis created with onlyexe_file(no.stanfile), using a fit object (e.g. from$pathfinder()) asinitfails with the uninformative error"'init' contains empty lists.".Root cause
Without a Stan file,
model$variables()is unavailable, somodel_variablesisNULLthroughout the init processing pipeline. Four functions (process_init.CmdStanMCMC,process_init_approx,process_init.CmdStanPathfinder,process_init.CmdStanMLE) had fallback code that constructedmodel_variables$parametersas a character vector of column names, butprocess_init.draws()expects it to be a named list with$dimensionsmetadata. This causednames(model_variables$parameters)to returnNULL, producing empty init lists.Fix
model_variables_from_draws()helper that infers parameter names and number of dimensions fromposterior::variables(as_draws_df(...)): names without[are scalars, names with[are containers (base name extracted viasub("\\[.*", "", ...)), and the number of,determine the number of dimensions.process_init.draws()calls this helper whenmodel_variablesisNULL, so it works without a Stan file.process_init.*methods — they now passmodel_variablesthrough as-is and letprocess_init.draws()handle theNULLcase.lw,weight) thatprocess_init_approxand the Pathfinder no-lp branch add to draws before passing downstream.Notes
model$variables()when Stan file is available still has benefit of creating the init list only with the actual model variables.model_variables_from_draws()has downside of including transformed paramaters and generated quantities, although that is unlikely to have big performance effect.fit$variable_skeleton(transformed_parameters = FALSE, generated_quantities = FALSE)but that causes compilation of model methods which adds overhead, and thus was not added here.Copyright and Licensing
Please list the copyright holder for the work you are submitting
(this will be you or your assignee, such as a university or company):
Aki Vehtari
By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the following licenses: