QuantEcon.lectures.code
QuantEcon.lectures.code copied to clipboard
Types, methods and dispatch: Incomplete simulate function
While working in the Julia version of the lectures (they're awesome, by the way) in this chapter: https://lectures.quantecon.org/jl/types_methods.html I noticed that the solution of the exercise seems to be incomplete. While the definition of AR(1) includes the use of sigma, the implemented version doesn't:
using Distributions
struct AR1_ex1{T <: Real}
a::T
b::T
sigma::T
phi::Distribution
end
function simulate(m::AR1_ex1, n::Integer, x0::Real)
X = Array{Float64}(n)
X[1] = x0
for t in 1:(n-1)
X[t+1] = m.a * X[t] + m.b + rand(m.phi)
end
return X
end
Because the test is made using sigma=1, this isn't immediately obvious when looking at the output.
My solution:
struct AR1{T <: Real}
a::T
b::T
σ::T
ϕ::Distribution
end
function simulate(m::AR1, n::Integer, x0::Real)
X = Array{Float64}(n)
X[1] = x0
for t in 1:(n-1)
X[t+1] = m.a * X[t] + m.b + m.σ * rand(m.ϕ)
end
return X
end
Let me know if I misunderstood the exercise!
Thanks @AgustinCB !
You're 100% right. I'll put in a fix that will show up over the next week or so.
Your comments also remind me that I want to use unicode variable names --- we held back at first because of issues with typesetting but those issues have been fixed upstream.
Please let us know if you spot more errors!