stan-dev / cmdstanr

CmdStanR: the R interface to CmdStan
https://mc-stan.org/cmdstanr/
Other
143 stars 63 forks source link

generate_quantities can fail with simplex #420

Open avehtari opened 3 years ago

avehtari commented 3 years ago

Running generated quantities block that works fine with sampling() method can fail with generate_quantities() method. This is caused as by default sig_figs=6 and thus the mapping to rounded stored values and back can produce wrong result. This is not a bug, but the error message is not helpful. Possible actions

Reproducible example (with high probability). Stan code

data { vector[2] mu; }
parameters { simplex[2] x; }
model { x ~ normal(mu, 0.1); }
generated quantities { real y = sum(x); }

Sampling goes fine

datat <- list(mu=array(c(0,1)));
fitt <- modelt$sample(chains=1, refresh=0, data=datat)

Generating quantities

gq <- modelt$generate_quantities(fitt, data=datat)

fails with an error

Chain 1 Exception: stan::math::simplex_free: Simplex variable is not a valid simplex. sum(Simplex variable) = 0.9999998, but should be 1 (in '/tmp/RtmpqJOJQA/model-330c1654e1f.stan', line 5, column 2 to column 15)
Warning: Chain 1 finished unexpectedly!

The same example works with datat <- list(mu=array(c(0.3,0.6))); If it would happen that the user would have a very time consuming sampling result, it's possible to work around the constraint by creating another model code with unrestricted parameter type.

data { vector[2] mu; }
parameters { vector[2] x; }
generated quantities { real y = sum(x); }
jgabry commented 3 years ago

Thanks Aki. I like the idea of hinting to the user in the error message that the problem could be due to a small value of sig_figs.

@rok-cesnovar I think it would be preferable to do this in CmdStan itself since this would be an issue for CmdStanPy as well, but would it be possible possible for the CmdStan generate quantities method to catch and modify exceptions from Stan math?

rok-cesnovar commented 3 years ago

but would it be possible possible for the CmdStan generate quantities method to catch and modify exceptions from Stan math

Right now no and I am not really sure how big of a refactor that would be

The cleanest solution for these cases would definitely be to finally move to a binary output format. That would fix a bunch of these precision issues.