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
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))
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)