Open avehtari opened 5 months ago
I agree it would be nice if this code could be easier to use outside cmdstanr. That said, I'm not sure BridgeStan is the right place for it, either.
Recently for cmdstanpy I spun off stanio to hold this code, which cmdstanpy now depends on, and so other things like bridgestan could use it more easily. It has both dump_stan_json
(returns a string) and write_stan_json
(writes to a file). The StanJulia universe did something similar recently
I would suggest seeing if the cmdstanr folks would be willing to do this. Ideally it would be a very small package that could be put on CRAN and both bridgestan and cmdstanr could take a dependency on it
Last time I used BridgeStan, I tried to use a dictionary and then realized I had to go look up JSON literal syntax instead. I think it would be worth having a dictionary interface for data in BridgeStan. For R, the equivalent structure is a list.
In R interface,
StanModel$new()
accepts data as "a JSON string literal, a path to a data file in JSON format ending in “.json”". CmdStanR has handy functionwrite_stan_json()
https://mc-stan.org/cmdstanr/reference/write_stan_json.html to write R data list to a JSON file compatible with CmdStan (the function does more than direct write of list in json).cmdstanr
The use case for 2. is when there is a need to instantiate the model with new data with minimal overhead. In the following code snippet
sd
is a data list, and we update part of the list, callnew
, and use log density, gradient and Hessian. Based on timing experiments, this is slightly faster than using`write_stan_json()
to write to a file and passing the file path tonew()
Another use case is when using BridgeStan with brms generated models and data
to_stan_json()
can be modified fromwrite_stan_json
by removing references to file (the code is using BSD-3 license).