I’m trying to solve a dynamic programming problem and I am running into an InexactError()
that I have not been able to track down. Can you please help me out?
const nᵢ = 2
const δ = 0.8
const σ = 1.5
const ρ = 5.0
const S = 1:3
const γc = 1.2
const πmat = [ 0.9 0.05 0.05;
0.05 0.9 0.05;
0.05 0.05 0.9 ]
const hi = (0.52 * γc) ^ (1 - σ) / (1 - σ)
const lo = (0.35 * γc) ^ (1 - σ) / (1 - σ)
const w = [ hi lo lo; # is
lo hi lo ]
function J(m, y, s)
σ / (1 - σ) * sum( ((1 - δ) * (y[i] + m[i])) ^ (1 / σ) for i in 1:nᵢ ) * m[nᵢ + 1] ^ ((σ - 1) / σ) -
sum( m[i] * w[i, s] for i in 1:nᵢ ) + m[nᵢ + 1] * γc
end
const grid_m = 0:0.1:1.5
const grid_y′ = 0:0.1:1
using Interpolations
function interp1(S, grid_y′1, grid_y′2, Darr)
itp = interpolate(Darr, BSpline(Cubic(Line())), OnGrid())
sitp = scale(itp, S, grid_y′1, grid_y′2)
return sitp
end
interp(S, grid_y′1, grid_y′2, Darr, s, y1, y2) = interp1(S, grid_y′1, grid_y′2, Darr)[s, y1, y2]
using Optim
function main()
s = 2
y1 = 0.0
y2 = sqrt(abs(1.0 - y1^2.0))
Darr = [J([1.0, 1.0, 1.0], [y1, y2], s) for s in S, y1 in grid_y′, y2 in grid_y′]
z = x -> begin
m1 = x[1]
m2 = x[2]
m3 = x[3]
y′1 = x[4]
y′2 = sqrt(abs(1.0 - y′1^2.0))
return J([m1, m2, m3], [y1, y2], s) + δ * interp(S, grid_y′, grid_y′, Darr, s, y′1, y′2)
end
twicedif = TwiceDifferentiable(z, [0.0, 0.0, 0.0, 0.0]; autodiff = :forward)
opt = optimize(twicedif, [1.0, 1.0, 1.0, 1.0], Newton(linesearch = LineSearches.Static()))
end
Running main()
gives the error.