I’m estimating a maximum likelihood problem, and I’ve specified both the objective function and the gradient correctly. (I know this because I am able to reproduce estimates using the same exact data as a Stata example.)

When I allow `Optim`

's `ForwardDiff`

to compute the gradient and hessian, I get the correct answer, and the gradient evaluated at the optimum is very close to zero (as expected). [see code below:]

```
td = TwiceDifferentiable(f, bstart, autodiff = :forwarddiff)
rs = optimize(td, bstart, LBFGS(; linesearch = LineSearches.BackTracking()), Optim.Options(iterations=100_000,g_tol=1e-8,f_tol=1e-8))
q = Array{Float64}(undef, length(β))
grad = g!(q,β)
gradzero = g!(q,zeros(size(β)))
```

However, when I instead pass my user-defined gradient to `Optim`

, the algorithm terminates at the initial point (and the gradient evaluates to what it would be at the initial point). [see code below; only difference is I added `g!`

to the `TwiceDifferentiable`

call]

```
td = TwiceDifferentiable(f, g!, bstart, autodiff = :forwarddiff)
rs = optimize(td, bstart, LBFGS(; linesearch = LineSearches.BackTracking()), Optim.Options(iterations=100_000,g_tol=1e-8,f_tol=1e-8))
q = Array{Float64}(undef, length(β))
grad = g!(q,β)
gradzero = g!(q,zeros(size(β)))
```

The full code is available here (autodiff example) and here (user-defined example). [The two files are identical except for adding the `g!`

to the `TwiceDifferentiable`

call]

I’m using Optim v0.18.1.

Does anyone know what might be going on? It seems like I’m missing something basic, but not immediately apparent.

Also, if anyone has performance tips, they would be most welcome! I plan on using the `fg!`

capability once I figure out what’s going on with the `g!`

call, since there is so much computational overlap between them.

Finally, if anyone knows how to precompile this function, I’d really appreciate a pointer.