QuantEcon.lectures.code icon indicating copy to clipboard operation
QuantEcon.lectures.code copied to clipboard

Types, methods and dispatch: Incomplete simulate function

Open AgustinCB opened this issue 8 years ago • 1 comments

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!

AgustinCB avatar Sep 19 '17 11:09 AgustinCB

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!

jstac avatar Sep 19 '17 17:09 jstac