amckay / EC702

Numerical analysis code and notes for EC 702
24 stars 14 forks source link

Euler Equation iteration #1

Open azev77 opened 2 years ago

azev77 commented 2 years ago

Hi and thank you for posting! I tried to implement your policy iteration algorithm in Julia.

using Plots, LinearAlgebra
#
PolyBasis(K)     = [ones(size(K)) K K.^2] 
PolyGetCoef(K,Y) = PolyBasis(K) \ Y
#
β = 0.99; α = 0.36; z=1.0; γ = 2; δ = 1.0;
s_ss = ( (1/β -1 +δ) / (α*z) )^(1/(α-1)); 
f(s1; z=z,α=α,δ=δ)      =   z*(s1^α)     +(1-δ)*s1
fprime(s1; z=z,α=α,δ=δ) = α*z*(s1^(α-1)) +(1-δ)
#
S1 = [range(0.75*s_ss, 1.25*s_ss,length=100);]
T1(s1,c1) = f(s1) - c1
Π10 = [ f(s1) - δ*s1  for s1 in S1] 
EE1(s1,sp1,cp1; β=β,γ=γ) = (cp1) * ((β*fprime(sp1))^(-1/γ))
#
@inline function FPI_EE(S1, Π1; k_max=100, tol=0.001)
    dist = 10.0; k=1;
    @inbounds while(dist > tol && k<=k_max) 
        println(k)
        #
        b  = PolyGetCoef(S1, Π1)
        c1 = PolyBasis(S1)*b # ≈Π1
        sp1 = T1.(S1, c1)
        cp1 = PolyBasis(sp1)*b
        Πp1 = EE1.(S1, sp1, cp1)
        dist = norm(Π1 - Πp1) 
        k += 1
        Π1 = Πp1
    end 
    return Π1
end
@time Pol_EE = FPI_EE(S1, Π10; k_max=500)
plot(S1, Pol_EE, legend=:false)

The policy function converges nicely in 33 iterations. Then I change one parameter γ = 1 to compare to the closed form solution. The policy function no longer converges.

Do you know why this is? Are there theoretical results that Euler Equation iteration converges? (Like the contraction mapping theorem for VFI).

amckay commented 2 years ago

Hi,

The Euler equation iteration is not guaranteed to converge. What that means in practice is you may need to experiment with initial guesses of the policy rule to find one that converges. One way to do this is to start with a parameter configuration that converges and slowly adjust the parameters towards the configuration you want.

I hope you get it working,

Alisdair

On Tue, Jul 12, 2022 at 6:34 PM azev77 @.***> wrote:

Hi and thank you for posting! I tried to implement your policy iteration algorithm in Julia.

using Plots, LinearAlgebra # PolyBasis(K) = [ones(size(K)) K K.^2] PolyGetCoef(K,Y) = PolyBasis(K) \ Y #

β = 0.99; α = 0.36; z=1.0; γ = 2; δ = 1.0;

s_ss = ( (1/β -1 +δ) / (αz) )^(1/(α-1)); f(s1; z=z,α=α,δ=δ) = z(s1^α) +(1-δ)s1 fprime(s1; z=z,α=α,δ=δ) = αz*(s1^(α-1)) +(1-δ) #

S1 = [range(0.75s_ss, 1.25s_ss,length=100);] T1(s1,c1) = f(s1) - c1

Π10 = [ f(s1) - δs1 for s1 in S1] EE1(s1,sp1,cp1; β=β,γ=γ) = (cp1) ((β*fprime(sp1))^(-1/γ)) # @inline function FPI_EE(S1, Π1; k_max=100, tol=0.001)

dist = 10.0; k=1;

@inbounds while(dist > tol && k<=k_max)

    println(k)

    #

    b  = PolyGetCoef(S1, Π1)

    c1 = PolyBasis(S1)*b # ≈Π1

    sp1 = T1.(S1, c1)

    cp1 = PolyBasis(sp1)*b

    Πp1 = EE1.(S1, sp1, cp1)

    dist = norm(Π1 - Πp1)

    k += 1

    Π1 = Πp1

end

return Π1

end @time Pol_EE = FPI_EE(S1, Π10; k_max=500) plot(S1, Pol_EE, legend=:false)

The policy function converges nicely in 33 iterations. Then I change one parameter γ = 1 to compare to the closed form solution. The policy function no longer converges.

Do you know why this is? Are there theoretical results that Euler Equation iteration converges? (Like the contraction mapping theorem for VFI).

— Reply to this email directly, view it on GitHub https://github.com/amckay/EC702/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAV2IVLIYQPZ6NJ3ER2K6DVTX6ITANCNFSM53MVWXLA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

azev77 commented 2 years ago

Thanks!

I get robust convergence for a wide variety of initial guesses when gamma>1.78.

I don’t get any convergence for a wide variety of initial guesses when gamma<1.78.

Changing the degree of the polynomial doesn’t seem to help…

https://discourse.julialang.org/t/euler-equation-iteration-to-solve-an-optimization-problem/84135

amckay commented 2 years ago

That's surprising. I'd have to dig into it to give more advice but unfortunately it's a busy moment so I doubt I'll get to it anytime soon.

-Alisdair

On Tue, Jul 12, 2022 at 11:52 PM azev77 @.***> wrote:

Thanks!

I get robust convergence for a wide variety of initial guesses when gamma>1.78.

I don’t get any convergence for a wide variety of initial guesses when gamma<1.78.

Changing the degree of the polynomial doesn’t seem to help…

— Reply to this email directly, view it on GitHub https://github.com/amckay/EC702/issues/1#issuecomment-1182764580, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAV2IVPPYJS2UPHCEA7M5TVTZDQ7ANCNFSM53MVWXLA . You are receiving this because you commented.Message ID: @.***>

azev77 commented 2 years ago

Got it to work (mostly): https://discourse.julialang.org/t/euler-equation-iteration-to-solve-an-optimization-problem/84135?u=albert_zevelev