beginner's question about using an external model

90 views
Skip to first unread message

kristophe...@gmail.com

unread,
Nov 30, 2013, 4:57:53 PM11/30/13
to stan-...@googlegroups.com
I am interested in using stan to fit a physis-based model  (a fortran program I wrote) to data, rather than a regression model specified using a line of R code.  How do I do this? 

I have been using DREAM_ZS in Matlab, which allows me to write a wrapper function that writes model parameters to a text input file, calls my code, and reads back in the model output from a text output file. The function returns the fortran model results in the expected format as the results of the function.  DREAM just calls my wrapper function.

I apologize if this is question is terribly simple or nonsense.  I have looked around in the stan documentation and using google, but maybe I just don't know what I am looking for. 

Thanks,

Kris

Michael Betancourt

unread,
Nov 30, 2013, 5:08:25 PM11/30/13
to stan-...@googlegroups.com
Unfortunately Stan will not easily work with this approach. The biggest issue is that
our sampler require gradients, which we compute using automatic differentiation techniques
requiring a C++ implementation of your log posterior (using some of our data types).

If you can convert your Fortran function to C++ then we can work with you to incorporate it
into the C++ API (up to some constraints).
> --
> You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

kristophe...@gmail.com

unread,
Nov 30, 2013, 5:19:07 PM11/30/13
to stan-...@googlegroups.com
Thanks for the quick response.  The use of derivatives is specific to the Hamiltonian Monte Carlo approach?  What are the derivatives computed using AD taken with respect to? The solution I have coded is an analytical solution in Laplace-transform space, it is back-transformed using numerical Laplace transform.

Kris

Michael Betancourt

unread,
Nov 30, 2013, 5:26:54 PM11/30/13
to stan-...@googlegroups.com
HMC requires derivatives, but we have also taken advantage of autodiff to implement
other algorithms that require derivatives (at the moment mostly optimization algorithms).

The derivatives are of the log posterior with respect to the parameters being sampled
(technical note, actually with parameters mapped into R^{n}, we also automatically
compute the Jacobian and its derivatives as well).

Depending on your problem you should be able to map the derivatives to their
Laplace equivalents and invert each element of the gradient to get reasonably
efficient analytic gradients, but it would be difficult to embed that into Stan given that
our code does not allow custom gradient evaluations.

kristophe...@gmail.com

unread,
Nov 30, 2013, 5:36:00 PM11/30/13
to stan-...@googlegroups.com
Thanks.  That answers my question.  stan seems like a great package, and I will probably try to use the python version for some other things in the future, but I guess I will have to work with non-gradient methods for now.

Cheers,

Kris
Reply all
Reply to author
Forward
0 new messages