cmdstan icon indicating copy to clipboard operation
cmdstan copied to clipboard

Make generate quantities fill failed draws with Nan

Open funko-unko opened this issue 4 years ago • 1 comments

Currently, when some gq iteration fails, eg because the ODE solver fails, nothing appears to get written to the output csv. This makes it impossible to link the gq to the parameters.

Can this be changed?

Will provide details later.

funko-unko avatar May 07 '21 13:05 funko-unko

Here is an MWE:

Stan

parameters {
  real x;
}
model {
  x ~ normal(0,1);
}
generated quantities{
  if(x < 0){
    reject(1);
  }
  real y = x;
}

Python

import cmdstanpy

model = cmdstanpy.CmdStanModel(stan_file='gq_reject.stan')
fit = model.sample(iter_sampling=2, chains=1, seed=3)
gq = model.generate_quantities(dict(), fit)
print(gq.sample_plus_quantities[['x', 'y']])

Output

          x         y
0 -1.796810  0.572324
1  0.572324       NaN

Note:

Cmdstanpy just fills everything with NaNs from below, but this is not what I want/should be done. Cmdstanpy can of course not do much else, else no info is in the csv file:

GQ CSV:

# stan_version_major = 2
# stan_version_minor = 26
# stan_version_patch = 1
# model = gq_reject_model
# method = generate_quantities
#   generate_quantities
#     fitted_params = /tmp/tmp3trp0svo/gq_reject-202105071916-1-wsy9ydpy.csv
# id = 1
# data
#   file = /tmp/tmp3trp0svo/z4b1lt3j.json
# init = 2 (Default)
# random
#   seed = 59707
# output
#   file = /tmp/tmp3trp0svo/gq_reject-202105071916-1-8x6tu4ey.csv
#   diagnostic_file =  (Default)
#   refresh = 100 (Default)
#   sig_figs = -1 (Default)
#   profile_file = profile.csv (Default)
# stanc_version = stanc3 v2.26.1
# stancflags = 
y
0.572324

funko-unko avatar May 07 '21 17:05 funko-unko