EconForge / Dolo.jl

Economic modeling in Julia
Other
57 stars 29 forks source link

Dolo.jl code for a simple example #181

Open azev77 opened 2 years ago

azev77 commented 2 years ago

Hi and thank you for this package! In this post I compare many different ways of solving economic models in the Julia ecosystem. Would it be at all possible for you to provide simple Dolo.jl code to solve this model?

albop commented 2 years ago

Thanks for the interest! From a very quick look at your post, it looks like the Euler equation version (augmented with the occasionally binding constraints) should be solvable with Dolo easily. @gabriellequeran : do you want to have a look at it ?

azev77 commented 2 years ago

Awesome, is it possible to also solve the dynamic programming version w VFI?

albop commented 2 years ago

It is possible to formulate the bellman problem and in principle it is possible to apply VFI methods to it, but the solution method hasn't received much love since its initial implementation.

azev77 commented 2 years ago

Hey @albop & @gabriellequeran has there been any luck solving these problems w/ Dolo.jl?

albop commented 2 years ago

Hey @azev77! We haven't had time yet, but we plan to look into it next week

On Tue, Jan 18, 2022 at 10:46 PM azev77 @.***> wrote:

Hey @albop https://github.com/albop & @gabriellequeran https://github.com/gabriellequeran has there been any luck solving these problems w/ Dolo.jl?

— Reply to this email directly, view it on GitHub https://github.com/EconForge/Dolo.jl/issues/181#issuecomment-1015863337, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACDSKLVS5THCMTIPZGFE4LUWXNT5ANCNFSM5LQA7UFQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

azev77 commented 2 years ago

Hey @albop & @gabriellequeran has there been any luck solving these problems w/ Dolo.jl?

gabriellequeran commented 2 years ago

Hello @azev77, I have just started working on your example and have added a new constraint on the investment for the case in which k is small. I will send you the implementation of that next week.

azev77 commented 2 years ago

Awesome. I look forward to it. btw, if you’d like feel free to post on the Discourse post (link above) to showcase & get exposure for Dolo

azev77 commented 2 years ago

Here is my attempt:

YAML:

name: Investment

symbols:
    states: [k]
    controls: [i]
    exogenous: [z]
    rewards: [u]
    parameters: [delta, r, zz]

equations:
    transition:
        - k[t] = (1-delta)*k[t-1] + i[t-1]
    arbitrage:
        - i[t] - ((r+delta-zz)/(1-delta)) - ((1+r)/(1-delta))*i[t-1]
    felicity:
        - u[t] = zz*k[t] -i[t] -.5*(i[t]^2)

############################
calibration:

    ## exogenous state
    z: 0

    # controls
    i: (zz-r-delta)/(r+delta)

    # states
    k: i/delta

    u: zz*k -i -.5*(i^2)

    # parameters:
    delta: 0.1
    r: 0.15
    zz: 0.27

domain:
    k: [k*0.5, k*1.5]

exogenous: 
    z: !VAR1
        ρ: r
        Σ: [[ 0.00001 ]]

Now in Julia: the perturb(model) solution works

using Dolo, Plots;
model = yaml_import("inv.yaml");
dr_pert    = perturb(model) # linear solution WORKS! 
tab_pert   = tabulate(model, dr_pert.dr,   :k)
p1 = plot()
plot!(tab_pert[V=:k],tab_pert[V=:i],label = "Perturbation", title = "Investment", xlabel = "k", ylabel = "i")

time_iteration(model) gives an error

julia> dr_global  = time_iteration(model)
---------------------------------------------------------------------
Time Iteration
---------------------------------------------------------------------
       n | ϵₙ=|F(xₙ,xₙ)| | ηₙ=|xₙ-xₙ₋₁| |   λₙ=ηₙ/ηₙ₋₁ |         Time
---------------------------------------------------------------------
ERROR: UndefVarError: i_m1_ not defined
Stacktrace:
 [1] macro expansion
   @ C:\Users\azevelev\.julia\packages\Dolang\FoOif\src\compiler.jl:391 [inlined]
 [2] macro expansion
   @ .\simdloop.jl:77 [inlined]
 [3] macro expansion
   @ C:\Users\azevelev\.julia\packages\Dolang\FoOif\src\compiler.jl:388 [inlined]
 [4] arbitrage(::Model{Symbol("##569"), Dolo.ContinuousProcess}, m::StaticArrays.SVector{1, Float64}, s::Vector{StaticArrays.SVector{1, Float64}}, x::SubArray{StaticArrays.SVector{1, Float64}, 1, Vector{StaticArrays.SVector{1, Float64}}, Tuple{UnitRange{Int64}}, true}, M::StaticArrays.SVector{1, Float64}, S::Vector{StaticArrays.SVector{1, Float64}}, X::Vector{StaticArrays.SVector{1, Float64}}, p::StaticArrays.SVector{3, Float64}, out::Nothing) (repeats 2 times)
   @ Dolo C:\Users\azevelev\.julia\packages\Dolang\FoOif\src\compiler.jl:443
 [5] euler_residuals(F::Dolo.Euler{Symbol("##569"), 1, 1, 1, Dolo.UCGrid{1}, Dolo.UCGrid{1}}, s::Vector{StaticArrays.SVector{1, Float64}}, x::Dolo.MSM{StaticArrays.SVector{1, Float64}}, dr::Dolo.CachedDecisionRule{Dolo.CubicDR{Dolo.UCGrid{1}, Dolo.UCGrid{1}, 1, 2}, Dolo.DiscretizedProcess{Dolo.UCGrid{1}}}, dprocess::Dolo.DiscretizedProcess{Dolo.UCGrid{1}}, parms::StaticArrays.SVector{3, Float64}; keep_J_S::Bool, out::Dolo.MSM{StaticArrays.SVector{1, Float64}})
   @ Dolo C:\Users\azevelev\.julia\packages\Dolo\RlF3H\src\algos\time_iteration_helpers.jl:174
 [6] (::Dolo.Euler{Symbol("##569"), 1, 1, 1, Dolo.UCGrid{1}, Dolo.UCGrid{1}})(x0::Dolo.MSM{StaticArrays.SVector{1, Float64}}, x1::Dolo.MSM{StaticArrays.SVector{1, Float64}}; set_future::Bool, ignore_constraints::Bool, out::Nothing, p::StaticArrays.SVector{3, Float64})
   @ Dolo C:\Users\azevelev\.julia\packages\Dolo\RlF3H\src\algos\time_iteration_helpers.jl:200
 [7] time_iteration(model::Model{Symbol("##569"), Dolo.ContinuousProcess}; dr0::Dolo.ConstantDecisionRule{1}, discretization::Dict{Any, Any}, interpolation::Symbol, verbose::Bool, details::Bool, ignore_constraints::Bool, trace::Bool, tol_η::Float64, tol_ε::Float64, maxit::Int64)
   @ Dolo C:\Users\azevelev\.julia\packages\Dolo\RlF3H\src\algos\time_iteration.jl:240
 [8] time_iteration(model::Model{Symbol("##569"), Dolo.ContinuousProcess})
   @ Dolo C:\Users\azevelev\.julia\packages\Dolo\RlF3H\src\algos\time_iteration.jl:203
 [9] top-level scope
   @ REPL[10]:1

julia> 

Is it bc Dolo.jl isn't meant for models w/o uncertainty?

Update: it seems to work if the exogenous variable z[t] is in the arbitrage equation

    arbitrage:
        - i[t+1] - ((r+delta-zz - .00001*z[t])/(1-delta)) - ((1+r)/(1-delta))*i[t]