# Optim: return inverse Hessian

**URL:** <https://discourse.julialang.org/t/optim-return-inverse-hessian/62709>\
**Category:** General Usage\
**Tags:** optim, optimization\
**Created:** [June 11, 2021, 12:03am UTC](https://discourse.julialang.org/t/optim-return-inverse-hessian/62709 "2021-06-11T00:03:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![yingqiuz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingqiuz/32/25165_2.png) [@yingqiuz](https://discourse.julialang.org/u/yingqiuz)\
**Post date:** [June 11, 2021, 12:03am UTC](https://discourse.julialang.org/t/optim-return-inverse-hessian/62709/1 "2021-06-11T00:03:52Z")

</div>

Hi all

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

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

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