Use gradient of log likelihood in model block

I want to use the gradient of log likelihood in my model to update X as the codes show. Is there any method that I can use the gradient in the model during fitting instead of getting the gradient after fitting it? In other words, it there anyway I can define grad in model block and use it?

model {
  Xlw,] = to_array_1d((diag_matrix(Al) * to_vector(Xl[w-1,]) + Bl * to_vector(U[s, w-1,]) + grad))

It seems I should use C++ to achieve this. But I have no idea how to reach the backend code to expose the gradint stuff in the model itself. Thanks ahead!

What gradient you want?

Wouldn’t that line also affect gradient value?

1 Like

I want the gradient for the whole log likelihood function with respect to my parameters.

Yeah, I also think it will make the gradient to become higher-order derivative. And I wonder whether there is a way that I can get such gradient from Stan.