Optim: return inverse Hessian

Hi all

I am using Optim.jl BFGS() to optimise a function. I would like the function to return the negative Hessian, for example

function f(x)
   return (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
end

function g!(storage, x)
   storage[1] = -2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1]
   storage[2] = 200.0 * (x[2] - x[1]^2)
end

function h!(storage, x)
   storage[1, 1] = 2.0 - 400.0 * x[2] + 1200.0 * x[1]^2
   storage[1, 2] = -400.0 * x[1]
   storage[2, 1] = -400.0 * x[1]
   storage[2, 2] = 200.0
end

res = optimize(f, g!, h!, zeros(2), BFGS(), Optim.Options(store_trace=true, extended_trace=true))

And the inverse Hessian can be obtained by

Optim.trace(res)[end].metadata["~inv(H)"]

However, this stores every inverse Hessian along the path, but I only need the last one. Is there an memory efficient way to do this, without saving all the history?

Thank you very much

2 Likes