Custom, MatLab implemented, likelihood function

I’m dealing with a problem which involves a non-trivial simulation for the calculation of the likelihood.

The calculation of gradients of this kind of simulation is also non-trivial, therefore I lack an implementation to calculate them.

The current state of the art implementation of this kind of simulation, which would allow me to evaluate the likelihood of a point, is available in MatLab.

If I could call a MatLab function, an “external function” to provide the likelihood, I’d at least like to see how using Stan sampling with automatic differentiation turns out, since reimplementing the solving procedure as a Stan function would be very time demanding.

So,

What is the status of non-native external functions used as likelihood functions for a Stan model ? (In a way similar to the one in the “Custom Probability Functions” part of the manual for native functions)

Is it possible to use them if a C++ implementation is provided ? if so, Is this the only possible specification ?

What about (of course, inefficient) custom command line calls ?

Thanks in advance !!!

Yes to both. As far as I’m aware, currently a C++ implementation would be the only alternative to coding it in the Stan language.

The C++ implementation has to be in a specific form, either templated enough that we can autodiff it, or with custom analytic gradients for efficiency.

See: https://github.com/stan-dev/stan/wiki/Contributing-New-Functions-to-Stan

and https://github.com/stan-dev/math/wiki/Adding-a-new-function-with-known-gradients