Skip to content

Types, methods and dispatch: Incomplete simulate function #5

@AgustinCB

Description

@AgustinCB

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions