So, I’d like the perform the following steps in pystan:
- Generate C++ code from a specified stan model (presumably with stanc).
- modify said C++ code.
- Have pystan compile modified C++ code and run it.
What is the appropriate way of doing this in PyStan?
I think you might have better luck using cmdstan with this sort of work
flow. The code generation and compilation steps are cleanly separated there.
I agree, but my group has an obsession with Python. But, I seemed to have figured it out, I think. The pseudo code looks something like this:
#generate cpp code from stan model
sm_cpp = pystan.stanc(file='temp.stan')
#code in sm_cpp["cppcode"]
function_that_does_something_to_code( sm_cpp['cppcode'] )
#make stan model using new code
sm = pystan.StanModel(stanc_ret=sm_cpp)
#fit
fit = sm.sampling(data=efit_dat, iter=points, chains=chains)
Might work. I had forgotten that StanModel lets you pass in C++ code
like that.
(In retrospect, allowing people to compile arbitrary C++ code doesn’t
seem like the safest decision.)