Hoping for some guidance / help with implementing custom log likelihood and gradient for research project (details below)

Since, I haven’t managed to work out what I was doing wrong in the other approaches. I have decided to move forward with the partial solution I posted above as, while its a bit convoluted, this allows me to add a new distribution that I can then use in my models.

This gets me to the next point. Which is that I have some very efficient code already written to compute the log likelihood and gradient of the log likelihood. This code has the header

void ll_and_dll(varint n, varint m, varint p, varint na, varint nb, varint N, bool smoothing, double *y, double *u, double *a, double *b, double *theta, double *X1, double *P1, double *LL, double *dLL);

The log likelihood and its gradients is returned via the pointers double *LL and double *dLL

all the double arrays will be mapped to eigen matrices or vectors of known sizes

This code relies on blas and lapack (#include “blas.h” and #include “lapack.h”). I don’t want to convert this code to use eigen and boost, since a substantial amount of time was required to optimise the performance of this code and make it numerically stable under all cases. What I would like to do is to call this function from within a new distribution that I define in stan math.

Two questions remain for me on this.

  1. When I call make my_model.stan and the generated hpp file gets compiled, I need to make sure that it links to the blas and lapack libraries. How can i go about doing this?

  2. I need to convert the eigen types to double arrays that can then be passed to this function. While I can currently do this if I know what eigen type I have, I am confused how to do this in the templated code when I don’t know specifically which type I need to convert from? I’m pretty sure I can do this using the .data() method if I have an eigen matrix.

Thanks in advance for any pointers

Appreciate the work to try and get this stuff to work. I however ask the you please enclose long streams of code or errors using back ticks like so ```. See EDIT above.

I’ll definitely do that from now. Thanks for the pointer.

I have decided to mark my partial solution posted previously as the solution so this thread can be closed. The one addition to that partial solution is that building the hpp file with stanc3 is done using

./_build/default/src/stanc/stanc.exe ssm.stan

Whilst not a perfect solution, I am happy enough with it and it does allow me to move forward with the project.