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

Solve simple, dynamic firm investment problem #3

Open
azev77 opened this issue Dec 1, 2022 · 8 comments · Fixed by #6
Open

Solve simple, dynamic firm investment problem #3

azev77 opened this issue Dec 1, 2022 · 8 comments · Fixed by #6

Comments

@azev77
Copy link
Contributor

azev77 commented Dec 1, 2022

Hi and thanks for this package!
I'd like to use your package to solve a simple problem.
image

First the deterministic version:

using MacroModelling;

@model m begin
    I[0]  = ((ρ + δ - Z)/(1 - δ))  - ((1 + ρ)/(1 - δ)) * I[-1]
end
@parameters m begin
    ρ = 0.05
    δ = 0.10
    Z = .17
end
m_ss = get_steady_state(m)
# note the ss can be solved in closed form
#I_SS = (Z-ρ-δ)/(ρ+δ);  #(.17-0.05-0.10)/(0.05 + 0.10) # 0.13333333333333333

m_sol = get_solution(m) 
ERROR: TypeError: in Type, in parameter, expected Type, got a value of type Vector{Any}

Not sure what to do about this error.

@thorek1
Copy link
Owner

thorek1 commented Dec 1, 2022

Hi,

thanks for your feedback. Actually I was following your Julia discourse thread and intend to post there :)

Regarding your problem:
I think there is a sign flip in the equation. If you type the following he gets the steady state as you post in the comments. I also included the internal symbolic solution (identical to yours).

using MacroModelling;

@model m begin
    I[0]  = ((ρ + δ - Z)/(1 - δ))  + ((1 + ρ)/(1 - δ)) * I[-1]
end
# Model: m
# Variables: 1
# Shocks: 0
# Parameters: 3
# Auxiliary variables: 0

@parameters m begin
    ρ = 0.05
    δ = 0.10
    Z = .17
end

m_ss = get_steady_state(m)
# 2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
# ↓   Variables_and_calibrated_parameters ∈ 1-element Vector{Symbol}
# →   Steady_state_and_∂steady_state∂parameter ∈ 4-element Vector{Symbol}
# And data, 1×4 Matrix{Float64}:
#         (:Steady_state)  (:ρ)      (:δ)      (:Z)
#   (:I)   0.133333        -7.55556  -7.55556   6.66667

m.SS_solve_func
# RuntimeGeneratedFunction(#=in MacroModelling=#, #=using MacroModelling=#, :((parameters, initial_guess, 𝓂)->begin
#     
#       
#           ρ = parameters[1]
#           δ = parameters[2]
#           Z = parameters[3]
#      
#           I = ((Z - δ) - ρ) / (δ + ρ)
#           SS_init_guess = [I]
#           𝓂.SS_init_guess = if typeof(SS_init_guess) == Vector{Float64}
#                   SS_init_guess
#               else
#                   ℱ.value.(SS_init_guess)
#               end
#           return ComponentVector([I], Axis([sort(union(𝓂.exo_present, 𝓂.var))..., 𝓂.calibration_equations_parameters...]))
#       end))

For now I didn't think about the non-stochastic case. I'll have a look and will let you know.

@thorek1
Copy link
Owner

thorek1 commented Dec 1, 2022

So the above model doesn't work (as of now - patch coming the next days) because it has no shocks.

I took the example with shocks from the discourse thread and that would give you the following:

using MacroModelling;

@model m begin
    K[0] = (1 - δ) * K[-1] + I[0]
    Z[0] = (1 - ρ) * μ + ρ * Z[-1] + σ * ϵ[x]
    I[1] = (ρ + δ - Z[0])/(1 - δ) + (1 + ρ)/(1 - δ) * I[0]
end
# Model: m
# Variables: 1
# Shocks: 1
# Parameters: 3
# Auxiliary variables: 0
@parameters m begin
    ρ = 0.05
    δ = 0.10
    μ = .17
    σ = .2
end
m_ss = get_steady_state(m)
# 2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
# ↓   Steady_state__States__Shocks ∈ 4-element Vector{Symbol}
# →   Variable ∈ 3-element Vector{Symbol}
# And data, 4×3 adjoint(::Matrix{Float64}) with eltype Float64:
#                    (:I)        (:K)        (:Z)
#   (:Steady_state)   0.133333    1.33333     0.17
#   (:K₍₋₁₎)          0.0         0.9         0.0
#   (:Z₍₋₁₎)          0.0497512   0.0497512   0.05
#   (:ϵ₍ₓ₎)           0.199005    0.199005    0.2

m.SS_solve_func
# RuntimeGeneratedFunction(#=in MacroModelling=#, #=using MacroModelling=#, :((parameters, initial_guess, 𝓂)->begin
#
#
#           ρ = parameters[1]
#           δ = parameters[2]
#           μ = parameters[3]
#  
#           Z = μ
#           I = ((Z - δ) - ρ) / (δ + ρ)
#           K = I / δ
#           SS_init_guess = [I, K, Z]
#           𝓂.SS_init_guess = if typeof(SS_init_guess) == Vector{Float64}
#                   SS_init_guess
#               else
#                   ℱ.value.(SS_init_guess)
#               end
#           return ComponentVector([I, K, Z], Axis([sort(union(𝓂.exo_present, 𝓂.var))..., 𝓂.calibration_equations_parameters...]))
#       end))

m_sol = get_solution(m) 
# 2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
# ↓   Steady_state__States__Shocks ∈ 4-element Vector{Symbol}
# →   Variable ∈ 3-element Vector{Symbol}
# And data, 4×3 adjoint(::Matrix{Float64}) with eltype Float64:
#                    (:I)        (:K)        (:Z)
#   (:Steady_state)   0.133333    1.33333     0.17
#   (:K₍₋₁₎)          0.0         0.9         0.0
#   (:Z₍₋₁₎)          0.0497512   0.0497512   0.05
#   (:ϵ₍ₓ₎)           0.199005    0.199005    0.2
init = m_ss(:,:Steady_state) |> collect
init[2] *= 1.5

plot_irf(m, initial_state = init, shocks = :none)

irf__m__no_shock__1

@azev77
Copy link
Contributor Author

azev77 commented Dec 2, 2022

Sounds awesome!
I look forward to your comments on discourse!

@thorek1 thorek1 mentioned this issue Dec 2, 2022
@thorek1 thorek1 closed this as completed in #6 Dec 2, 2022
@azev77
Copy link
Contributor Author

azev77 commented Dec 2, 2022

For a generic problem, some things that are nice to have:
-get steady states (cases: no steady state, unique ss, multiple ss (skiba mode))
-get & plot the Policy functions
-get & plot the Transition functions
-get & plot the Value functions
-get & plot a simulation of each Choice & State variable for any initial condition, for a given history of the shock(s)
(deterministic case: only 1 possible simulation b/c no shock history...)
-get & plot IRF of each Choice & State variable to each shock

(stochastic case: return density over time & stationary density)

@thorek1
Copy link
Owner

thorek1 commented Dec 2, 2022

thanks for the suggestions. here is what the package can do as of now:

  • get steady states: unique SS - via get_SS
  • get steady states: no SS or multiple SS
    to be added later. I need to look into it. my current understanding is that this concerns global solution algos (coming later)
  • get policy functions - via get_solution
  • get transition functions - via get_solution
  • get value functions
    to be added later. need to think about implementing it while keeping the syntax simple
  • plot policy, transition, and value functions
    need to think of a standardisable output. do you have something specific in mind which scales well (Smets and Wouters 2007 size models)?
  • get & plot simulation of each Choice & State variable for any initial condition - via get_IRF(m, initial_state = x) and plot(m, initial_state = x)
  • get & plot simulation for a given history of the shock(s)
    coming soon
  • get & plot IRF of each Choice & State variable to each shock - via get_IRF(m) and plot(m) see docs for more details on selecting variables and shocks
  • (stochastic case: return density over time & stationary density)
    to be added later

suggestions on any of these points are very welcome

@azev77
Copy link
Contributor Author

azev77 commented Dec 2, 2022

Btw, you may consider adding these to your table:
https://www.gdsge.com/
https://sites.google.com/site/orenlevintal/taylor-projection?authuser=0

@thorek1
Copy link
Owner

thorek1 commented Jan 7, 2023

I added the two to the table

@thorek1 thorek1 reopened this Jan 7, 2023
@thorek1
Copy link
Owner

thorek1 commented Jan 11, 2023

v0.1.4 now includes plot_solution

it's a convenience function with which you can plot the mapping from past states to present variables (pair-wise) for all available solution algorithms. it is centred around the (non) stochastic steady state of the state variables and plots the range of +/- 2 standard deviations

any feedback on this implementation is welcome. check the documentation for examples

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

Successfully merging a pull request may close this issue.

2 participants