How to derivative quassi likelihood function using ForwardDiff

Continuing the discussion from How to derivative using ForwardDiff:

I want some help. I want to make more complicated function including more parameters

Please teach me how to apply ForwardDiff.gradient() and ForwardDiff.hessian() to this function.
g is a penalized quassi liklifood funcion

- g(m_{x,t}) = ∑_{x,t} (D_{x,t} \ln m_{x,t} − E_{x,t} m_{x,t} )− \dfrac{1}{2} (k− μ)′ {V}^{−1} (k− μ)

where

m_{x,t} = \exp \{ a_x + b_x k_t + c_{x,t} \pi_t \}

D_{x,t} and E_{x,t} is a fixed matrix

function g(params; X,T)
    a,b,k,c,π,μ_vec,V = params
    R = eltype(params)
    @show R 
    # a = zeros(R,X)
    # b = zeros(R,X)
    # k = zeros(R,T)
    # c = zeros(R,X,T)
    # π = zeros(R,T)
    # μ_vec = zeros(R,T)
    # V = zeros(R,T,T)
    term1 = Dxt .* (a .+ b * k' + c .* π' ) - Ext .* exp.( a .+ b * k' + c .* π') 
    term2 = (k - μ_vec)' * (V \ (k - μ_vec) )
    return sum(term1) - term2 / 2
end  

Here is a reference

Unlike other autodiff frameworks like Zygote, ForwardDiff only supports functions with a single array argument. I think a good solution is to use a ComponentArray instead of a tuple

Thank you for your reply.

Could you teach me how to maximize this quassi likelihood function using Zygote.jl or Optim.jl?

using Optim
using ComponentArrays
using Plots
using LaTeXStrings


function g(params; X,T)
    a,b,k,c,π,μ_vec,V = params
    term1 = Dxt .* (a .+ b * k' + c .* π' ) - Ext .* exp.( a .+ b * k' + c .* π') 
    term2 = (k - μ_vec)' * (V \ (k - μ_vec) )
    return sum(term1) - term2 / 2
end  


params = ComponentArray(a = a0, b = b0, k = k0, c = c0, π = π0, μ_vec = μ0_vec, V = V0)
g(params;X,T)
#jacoian of g
dg(params) = ForwardDiff.gradient(param -> g(param; X = X, T = T),params)
#Hessian of g
ddg(params) = ForwardDiff.hessian(param -> g(param; X = X, T = T),params)

#get solution of Optimization by Newton method
result = Optim.optimize(-g, -dg, -ddg, params, Optim.Newton(), Optim.Options(g_tol = 1e-10, iterations = 1000))

I got this error

MethodError: no method matching -(::typeof(g)) Closest candidates are: -(::Any, ::AbstractThunk) at ~/.julia/packages/ChainRulesCore/a4mIA/src/tangent_types/thunks.jl:35 -(::Any, ::ZeroTangent) at ~/.julia/packages/ChainRulesCore/a4mIA/src/tangent_arithmetic.jl:102 -(::Any, ::NoTangent) at ~/.julia/packages/ChainRulesCore/a4mIA/src/tangent_arithmetic.jl:62 … Stacktrace: [1] top-level scope @ In[122]:23

Trying to interpret error stacktraces in Julia is difficult at first, but it’s a worthy skill to acquire! What do you think this error means? Here’s a clue: -(typeof(g)) means that you’re trying to apply the function - (“minus”) to an object that has the same type as g (in this case the function g itself)