First of all, I want to congratulate you all for this awesome work. Since I couldn’t find a solution in this forum I wanted to ask you in a new thread. I’ve searched in the manual and tried for hours but could find a satisfying solution.

So far I have implemented in matlab my own likelihood function for a different framework. This gives the logposterior and its gradient for a given theta in the following way:
[LogPost,GradLogPost]=likelihood(theta);

The framework I used before doesn’t have a NUTS implementation, why I try to run my model on Stan in Matlab. My aim is to only use the sampler of Stan, rather than the modeling with it.

Is there a way to import this into STAN in the following way:

This solution wouldn’t use the gradient calculation of the matlab-skript, but would be a great start. If there is a solution to even use the gradient calculation, I would even be more happy.
My major problem is the lack of C++ skills and I hope that you can provide a solution.

Edit: The Data would be already a part of likelihood.m, why I wouldn’t need to implement either. I’m only interested in the properties of theta.

No, that is not possible. However, the MATLAB language is not that different from the Stan language, so you should be able to define a user-defined function in the functions block of the Stan program that does the same thing as your likelihood function in MATLAB.

Thank you for the quick response. I tried that, but get at the end a likelihood of 0 all the time. Is there a way I can check if the loglikelihood is calculated correctly for a specific parameter? Something like:

If you can install R (e.g. RStudio is easy to install), you could call your newlprob_lpdf from R and check that it returns correct values given different data and parameter values. If you don’t want to install R, then you could add print statements to your model block to print out what your function is returning.

btw, it is not recommend to use <lower=0.00001,upper=100000> type constraints unless you have logical constraints for these. Now these look like arbitrary chosen values. The underlying transformation to unconstrained space makes the initialization and HMC to work badly for these. If these need to be positive, remove the upper constraint and add priors.

Thank you, after all the struggle I finally installed RStudio with RStan(the Getting Started walkthrough didn’t work for me, but I managed to run it in a virtual environment), and could find the problem in my function with the help of rstan::expose_stan_functions("filename.stan") .