Interface to Chris Sims' csminwel
optimization code. The code borrows from DSGE.jl, but it is adapted to be compatible with Optim.jl's API. When the derivative of the minimand is not supplied, either Finite Difference of Forward Automatic Differentiation derivatives are used.
Differently from the solvers in Optim.jl
, Csminwel
returns an estimate of the inverse of the Hessian at the solution.
#=
Maximizing loglikelihood logistic models
=#
using CsminWel, StatsFuns, Random
Random.seed!(1)
x = [ones(200) randn(200,4)]
y = [rand() < 0.5 ? 1. : 0. for j in 1:200]
function loglik(beta)
xb = x*beta
sum(-y.*xb + log1pexp.(xb))
end
function dloglik(beta)
xb = x*beta
px = logistic.(xb)
-x'*(y.-px)
end
function g!(stor, beta)
copyto!(stor, dloglik(beta))
end
res1 = optimize(loglik, g!, zeros(5), BFGS())
res2 = optimize(loglik, g!, zeros(5), Csminwel())
## With finite-difference derivative
res3 = optimize(loglik, zeros(5), Csminwel())
## With forward AD derivative
res4 = optimize(Optim.OnceDifferentiable(loglik, zeros(5), autodiff = :forward), zeros(5), Csminwel())
## inverse Hessian
res2.invH