I would like to try to compute analytically the gradient of a linear Gaussian state space model in parallel with filtering. I’d prefer to avoid coding the whole thing in c++, so I came up with this Idea.

I do all the computation, log likelihood and the gradient, in Stan and then have a simple c++ function similar to this. Only my function would be something like (I don’t know c++):

My function would take as inputs the likelihood and gradient and the parameters declared in the parameters block. The double valued function returns the likelihood. The var valued function returns as gradient the precomputed gradient values and zeros for the “gradient” of the input likelihood and input gradient. The output of the function is then simply added to the “target” in the model block.

Would this work? Or would the gradient evaluation algorithm still go through all the previous transformations?

Could you be clearer about what you’re trying to do? Is this supposed to be called from a Stan program? What would a minimal Stan program look like? That would give us some context for answering without just trying to explain Stan’s auto-diff from scratch :/

I would like to utilize Stan’s NUT sampler to infer the posteriors of the parameters in a linear Gaussian state space model. If I use the Kalman filter to evaluate the likelihood, the gradient gets complicated and will be slow to evaluate using automatic differentiation.

I want to test if computing the gradients analytically would make things faster. I know that this requires an external c++ function. Since the only reasonable way of computing the gradient is in parallel with the likelihood, it seems that I would need to do everything in one big c++ function that takes as input the parameters, forms the model matrices and their gradients wrt the parameters, loops through the Kalman filter, updates the gradients in parallel, and returns the likelihood and the gradients. As I have no experience in c++, I would like to avoid this.

My question is, is there a way to do all the computations in Stan, in the transformed parameters block, and then call a small c++ function that takes as inputs the untransformed parameters, the computed log likelihood and its computed gradients wrt the parameters and then puts out the likelihood unchanged. If this output is then added to the target in the model block, will Stan simply take as gradients the gradients given by this small c++ function, ignoring the transformations of the parameters done before this function is called?

I hope this makes my problem clearer. And please don’t feel obliged to explain Stan’s auto-diff from scratch, a short answer will do:)

Thanks for the explanation: if you do the calculations for the gradient in Stan, you’ll be adding a lot of unrelated gradient calculations so no, I don’t think you can use the strategy you are suggesting.

My suggestion is to implement the model in pure Stan first (there are a few Kalman filter implementations kicking around if you search the forums) and when you nail down the changes you need for your specific case it may either a) just work fast enough; or b) require small changes to the C++ code for an existing function. Don’t forget you can also call gaussian_dlm_obs in a loop in Stan with a new transition matrix for each time step.