I'm currently rewriting MATLAB code in Julia, and while I'm generally happy
with the ease of transition (and even more so with the speed, even without
having figured out how to parallelize in Julia most things seem to run
faster than comparable parfor loops on four MATLAB workers), there's one
thing that has thrown me off, as I can't seem to figure out how to properly
use nested functions.
I'm trying to maximize a function that contains an integral which depends
on the argument of the maximization as follows:
using Distributions
using Optim
for y = 1:Y
f2(x, y) = f3(x, y)*pdf(LogNormal(mu_y, sig_y), y)
f1(x, y) = quadgk(f2(x, y), y_l, y_h)
result = optimize(f1, x_l, x_h)
end
I think my problem is that I can't get the optimize function to optimize
over the choices of x, while keeping y constant at the level set in the
loop.
To clarify, here's what I'd do in MATLAB:
for y = 1:Y
function f2 = f2(x, y)
f = @(y)( f3(x,y)*lognpdf(y, mu_y, sig_y)
f2 = integral(f, y_l, y_h)
end
f1 = @(x)( f2(x, y) )
result = fminbnd(f1, x_l, x_h)
end
I hope this question makes sense and someone can help me with this!
Thanks in advance,
Nils