Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dolo.jl code for a simple example #181

Open
azev77 opened this issue Jan 8, 2022 · 9 comments
Open

Dolo.jl code for a simple example #181

azev77 opened this issue Jan 8, 2022 · 9 comments

Comments

@azev77
Copy link

azev77 commented Jan 8, 2022

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
Copy link
Member

albop commented Jan 10, 2022

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
Copy link
Author

azev77 commented Jan 10, 2022

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

@albop
Copy link
Member

albop commented Jan 10, 2022

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
Copy link
Author

azev77 commented Jan 18, 2022

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

@albop
Copy link
Member

albop commented Jan 20, 2022 via email

@azev77
Copy link
Author

azev77 commented Jan 31, 2022

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

@gabriellequeran
Copy link
Contributor

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
Copy link
Author

azev77 commented Feb 10, 2022

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
Copy link
Author

azev77 commented Jul 3, 2022

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] 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants