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.
-
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?
-
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