# How to derivative quassi likelihood function using ForwardDiff

**URL:** <https://discourse.julialang.org/t/how-to-derivative-quassi-likelihood-function-using-forwarddiff/96928>\
**Category:** General Usage\
**Created:** [April 1, 2023, 6:31am UTC](https://discourse.julialang.org/t/how-to-derivative-quassi-likelihood-function-using-forwarddiff/96928 "2023-04-01T06:31:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KotaAihara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kotaaihara/32/43089_2.png) [@KotaAihara](https://discourse.julialang.org/u/KotaAihara)\
**Post date:** [April 1, 2023, 6:31am UTC](https://discourse.julialang.org/t/how-to-derivative-quassi-likelihood-function-using-forwarddiff/96928/1 "2023-04-01T06:31:30Z")

</div>

Continuing the discussion from [How to derivative using ForwardDiff](https://discourse.julialang.org/t/how-to-derivative-using-forwarddiff/96269):

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

```julia
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

> **[A multi-parameter-level model for simulating future mortality scenarios with...](https://www.cambridge.org/core/journals/annals-of-actuarial-science/article/multiparameterlevel-model-for-simulating-future-mortality-scenarios-with-covidalike-effects/FF51EC4AD87937E7934A3617C5386665)**
>
> A multi-parameter-level model for simulating future mortality scenarios with COVID-alike effects - Volume 16 Issue 3

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 1, 2023, 10:42am UTC](https://discourse.julialang.org/t/how-to-derivative-quassi-likelihood-function-using-forwarddiff/96928/2 "2023-04-01T10:42:27Z")

</div>

Unlike other autodiff frameworks like Zygote, ForwardDiff only supports [functions with a single array argument](https://juliadiff.org/ForwardDiff.jl/stable/user/limitations/). I think a good solution is to use a [ComponentArray](https://github.com/jonniedie/ComponentArrays.jl) instead of a tuple

---

<div class="post-metadata">

**Author:** ![KotaAihara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kotaaihara/32/43089_2.png) [@KotaAihara](https://discourse.julialang.org/u/KotaAihara)\
**Post date:** [April 2, 2023, 1:39am UTC](https://discourse.julialang.org/t/how-to-derivative-quassi-likelihood-function-using-forwarddiff/96928/3 "2023-04-02T01:39:06Z")

</div>

Thank you for your reply.

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

```julia
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](https://file+.vscode-resource.vscode-cdn.net/Users/kaihara/Library/CloudStorage/GoogleDrive-ai1902vo%40gmail.com/%E3%83%9E%E3%82%A4%E3%83%89%E3%83%A9%E3%82%A4%E3%83%96/Seminor/Julia/~/.julia/packages/ChainRulesCore/a4mIA/src/tangent_types/thunks.jl:35) -(::Any, ::ZeroTangent) at [~/.julia/packages/ChainRulesCore/a4mIA/src/tangent\_arithmetic.jl:102](https://file+.vscode-resource.vscode-cdn.net/Users/kaihara/Library/CloudStorage/GoogleDrive-ai1902vo%40gmail.com/%E3%83%9E%E3%82%A4%E3%83%89%E3%83%A9%E3%82%A4%E3%83%96/Seminor/Julia/~/.julia/packages/ChainRulesCore/a4mIA/src/tangent_arithmetic.jl:102) -(::Any, ::NoTangent) at [~/.julia/packages/ChainRulesCore/a4mIA/src/tangent\_arithmetic.jl:62](https://file+.vscode-resource.vscode-cdn.net/Users/kaihara/Library/CloudStorage/GoogleDrive-ai1902vo%40gmail.com/%E3%83%9E%E3%82%A4%E3%83%89%E3%83%A9%E3%82%A4%E3%83%96/Seminor/Julia/~/.julia/packages/ChainRulesCore/a4mIA/src/tangent_arithmetic.jl:62) … Stacktrace: [1] top-level scope @ In[122]:23

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 2, 2023, 7:58am UTC](https://discourse.julialang.org/t/how-to-derivative-quassi-likelihood-function-using-forwarddiff/96928/4 "2023-04-02T07:58:44Z")

</div>

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)
