Hi,
I’m trying to use Optim to do maximum-likelihood estimation of some parameters in a custom likelihood model. In particular, I would like to incorporate autodiff to increase the accuracy of my estimates. However, setting the parameter autodiff = :forward
causes errors in code that looks (approximately) like this:
function test(vec_len, p1)
p = ones(Float64, vec_len)
for i = (vec_len - 1):-1:1
p[i] = p[i + 1] * p1
end
return p
end
I suspect it is because of the matrix/vector assignment which similar autodiff packages in other languages forbid. Indeed, I am able to get around this if, instead of preallocating the vector, I simply build it up by doing something like p = hcat(p[1] * p1, p)
. However, this becomes pretty slow (I imagine due to the loss of the preallocation).
Thus, my question: is there a way to construct such vectors in Julia either with preallocation or in some other way that doesn’t take a huge performance hit?
For completeness, the error message I get when trying to run the following code snippet:
function test(vec_len, p1)
p = ones(Float64, vec_len)
for i = (vec_len - 1):-1:1
p[i] = p[i + 1] * p1
end
return p
end
lower = [0.0, 0.0, 0.0]
upper = [1.0, 1.0, 1.0]
x0 = [0.95]
result = optimize(x -> test(15, x), lower, upper, x0, Fminbox(LBFGS()), autodiff = :forward)
Optim.minimizer(result)
is:
MethodError: Cannot `convert` an object of type Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##25#26")),Float64},Float64,1},1} to an object of type Float64
Any help would appreciated, thanks.