using InstantiateFromURL github_project("QuantEcon/quantecon-notebooks-julia", version = "0.4.0") using LinearAlgebra, Statistics using Plots gr(fmt=:png); plt_1=plot() plt_2=plot() plots = [plt_1, plt_2] for (i, ϕ) in enumerate((0.8, -0.8)) times = 0:16 acov = [ϕ.^k ./ (1 - ϕ.^2) for k in times] label = "autocovariance, phi = $ϕ" plot!(plots[i], times, acov, color=:blue, lw=2, marker=:circle, markersize=3, alpha=0.6, label=label) plot!(plots[i], legend=:topright, xlabel="time", xlim=(0,15)) plot!(plots[i], seriestype=:hline, [0], linestyle=:dash, alpha=0.5, lw=2, label="") end plot(plots[1], plots[2], layout=(2,1), size=(700,500)) ar1_sd(ϕ, ω) = 1 ./ (1 .- 2 * ϕ * cos.(ω) .+ ϕ.^2) ω_s = range(0, π, length = 180) plt_1=plot() plt_2=plot() plots=[plt_1, plt_2] for (i, ϕ) in enumerate((0.8, -0.8)) sd = ar1_sd(ϕ, ω_s) label = "spectral density, phi = $ϕ" plot!(plots[i], ω_s, sd, color=:blue, alpha=0.6, lw=2, label=label) plot!(plots[i], legend=:top, xlabel="frequency", xlim=(0,π)) end plot(plots[1], plots[2], layout=(2,1), size=(700,500)) ϕ = -0.8 times = 0:16 y1 = [ϕ.^k ./ (1 - ϕ.^2) for k in times] y2 = [cos.(π * k) for k in times] y3 = [a * b for (a, b) in zip(y1, y2)] # Autocovariance when ϕ = -0.8 plt_1 = plot(times, y1, color=:blue, lw=2, marker=:circle, markersize=3, alpha=0.6, label="gamma(k)") plot!(plt_1, seriestype=:hline, [0], linestyle=:dash, alpha=0.5, lw=2, label="") plot!(plt_1, legend=:topright, xlim=(0,15), yticks=[-2, 0, 2]) # Cycles at frequence π plt_2 = plot(times, y2, color=:blue, lw=2, marker=:circle, markersize=3, alpha=0.6, label="cos(pi k)") plot!(plt_2, seriestype=:hline, [0], linestyle=:dash, alpha=0.5, lw=2, label="") plot!(plt_2, legend=:topright, xlim=(0,15), yticks=[-1, 0, 1]) # Product plt_3 = plot(times, y3, seriestype=:sticks, marker=:circle, markersize=3, lw=2, label="gamma(k) cos(pi k)") plot!(plt_3, seriestype=:hline, [0], linestyle=:dash, alpha=0.5, lw=2, label="") plot!(plt_3, legend=:topright, xlim=(0,15), ylim=(-3,3), yticks=[-1, 0, 1, 2, 3]) plot(plt_1, plt_2, plt_3, layout=(3,1), size=(800,600)) ϕ = -0.8 times = 0:16 y1 = [ϕ.^k ./ (1 - ϕ.^2) for k in times] y2 = [cos.(π * k/3) for k in times] y3 = [a * b for (a, b) in zip(y1, y2)] # Autocovariance when ϕ = -0.8 plt_1 = plot(times, y1, color=:blue, lw=2, marker=:circle, markersize=3, alpha=0.6, label="gamma(k)") plot!(plt_1, seriestype=:hline, [0], linestyle=:dash, alpha=0.5, lw=2, label="") plot!(plt_1, legend=:topright, xlim=(0,15), yticks=[-2, 0, 2]) # Cycles at frequence π plt_2 = plot(times, y2, color=:blue, lw=2, marker=:circle, markersize=3, alpha=0.6, label="cos(pi k/3)") plot!(plt_2, seriestype=:hline, [0], linestyle=:dash, alpha=0.5, lw=2, label="") plot!(plt_2, legend=:topright, xlim=(0,15), yticks=[-1, 0, 1]) # Product plt_3 = plot(times, y3, seriestype=:sticks, marker=:circle, markersize=3, lw=2, label="gamma(k) cos(pi k/3)") plot!(plt_3, seriestype=:hline, [0], linestyle=:dash, alpha=0.5, lw=2, label="") plot!(plt_3, legend=:topright, xlim=(0,15), ylim=(-3,3), yticks=[-1, 0, 1, 2, 3]) plot(plt_1, plt_2, plt_3, layout=(3,1), size=(600,600)) using QuantEcon, Random # plot functions function plot_spectral_density(arma, plt) (w, spect) = spectral_density(arma, two_pi=false) plot!(plt, w, spect, lw=2, alpha=0.7,label="") plot!(plt, title="Spectral density", xlim=(0,π), xlabel="frequency", ylabel="spectrum", yscale=:log) return plt end function plot_spectral_density(arma) plt = plot() plot_spectral_density(arma, plt=plt) return plt end function plot_autocovariance(arma, plt) acov = autocovariance(arma) n = length(acov) plot!(plt, 0:(n-1), acov, seriestype=:sticks, marker=:circle, markersize=2,label="") plot!(plt, seriestype=:hline, [0], color=:red, label="") plot!(plt, title="Autocovariance", xlim=(-0.5, n-0.5), xlabel="time", ylabel="autocovariance") return plt end function plot_autocovariance(arma) plt = plot() plot_spectral_density(arma, plt=plt) return plt end function plot_impulse_response(arma, plt) psi = impulse_response(arma) n = length(psi) plot!(plt, 0:(n-1), psi, seriestype=:sticks, marker=:circle, markersize=2, label="") plot!(plt, seriestype=:hline, [0], color=:red, label="") plot!(plt, title="Impluse response", xlim=(-0.5,n-0.5), xlabel="time", ylabel="response") return plt end function plot_impulse_response(arma) plt = plot() plot_spectral_density(arma, plt=plt) return plt end function plot_simulation(arma, plt) X = simulation(arma) n = length(X) plot!(plt, 0:(n-1), X, lw=2, alpha=0.7, label="") plot!(plt, title="Sample path", xlim=(0,0,n), xlabel="time", ylabel="state space") return plt end function plot_simulation(arma) plt = plot() plot_spectral_density(arma, plt=plt) return plt end function quad_plot(arma) plt_1 = plot() plt_2 = plot() plt_3 = plot() plt_4 = plot() plots = [plt_1, plt_2, plt_3, plt_4] plot_functions = [plot_spectral_density, plot_impulse_response, plot_autocovariance, plot_simulation] for (i, plt, plot_func) in zip(1:1:4, plots, plot_functions) plots[i] = plot_func(arma, plt) end return plot(plots[1], plots[2], plots[3], plots[4], layout=(2,2), size=(800,800)) end Random.seed!(42) # For reproducible results. ϕ = 0.5; θ = [0, -0.8]; arma = ARMA(ϕ, θ, 1.0) quad_plot(arma)