# function f(x::Vector{T}) where T <: Number
function f(x)
a = x[1]
b = x[2]
x1 = a+b
x2 = a*exp(b)
return [x1,x2]
endPerturbation of Neoclassical Model
Our goal here is to compute a linear approximation of solution to the neoclassical model, c# execute: # freeze: true
# execute: # enabled: falselose ot the steady-state.
- Warm-up: install the
ForwardDifflibrary. Use it to differentiate the function below. Check the jacobian function.
Note: the signature of function f needs to be fixed first to allow for dual numbers as arguments.
# your code here- Create a NamedTuple to hold the model parameters.
# your code here- Define two functions:
transition(z::Number, k::Number, i::Number, p)::Tuple{Number}which returns productivity and capital at datet+1as a function of productivity, capital and investment at datetarbitrage(z::Number, k::Number, i::Number, Z::Number, K::Number, I::Number, p)::Numberwhich returns the residual of the euler equation (lower case variable for date t, upper case for date t+1)
- Using multiple dispatch, define two variants of the same functions, that take vectors as input and output arguments:
arbitrage(s::Vector{T}, x::Vector{T}, S::Vector{T}, X::Vector{T}, p) where T<:Numbertransition(s::Vector{T}, x::Vector{T}, p) where T<:Number
# your code here# your code here# your code here- Write a function
steady_state(p)::Tuple{Vector,Vector}which computes the steady-state of the model computed by hand. It returns two vectors, one for the states, one for the controls. Check that the steady-state satisfies the model equations.
# your code here# your code hereThe first order system satisfies: \[\begin{align}A s_t + B x_t + C s_{t+1} + D x_{t+1} & = & 0 \\\\ s_{t+1} & = & E s_t + F x_t \end{align}\]
- Define a structure
PerturbedModelto hold matrices A,B,C,D,E,F.
# your code here- Write a function
first_order_model(s::Vector, x::Vector, p)::PerturbedModel, which returns the first order model, given the steady-state and the calibration. Suggestion: useForwardDiff.jllibrary.
# your code here- We look for a linear solution \(x_t = X s_t\) . Write the matrix equation which
Xmust satisfy. Write a functionresidual(X::Array, M::PerturbedModel)which computes the residual of this equation for a givenX.
# your code here- Write a function
T(X, M::PerturbedModel)which implements the time iteration step.
# your code here- Write function
linear_time_iteration(X_0::Matrix, m::PerturbedModel)::Matrixwhich implements the time iteration algorithm. Apply it toX0 = rand(1,2)and check that the result satisfies the first order model.
# your code here- Check blanchard Kahn Conditions
# your code here- Write a function
simulate(s0::Vector, X::Matrix, p, T::Int64)::Tuple{Matrix, Matrix}to simulate the model over \(T\) periods (by using the formula \(\Delta s_t = (E + F X) \Delta s_{t-1})\). Return a matrix for the states (one line per date) and another matrix for the controls. Bonus: add a keyword option to compute variables levels or log-deviations. If possible, return a DataFrame object.
# your code here- Make some nice plots.