using InstantiateFromURL github_project("QuantEcon/quantecon-notebooks-julia", version = "0.2.0") using LinearAlgebra, Statistics, Compat using QuantEcon, Plots, Random Random.seed!(42) # For reproducible results. n = 40 # Data size ϕ = 0.5 # AR parameter θ = [0, -0.8] # MA parameter σ = 1.0 lp = ARMA(ϕ, θ, σ) X = simulation(lp, ts_length = n) x, y = periodogram(X) x_sd, y_sd = spectral_density(lp, two_pi=false, res=120) plot(x, y,linecolor="blue", linewidth=2, linealpha=0.5, lab="periodogram") plot!(x_sd, y_sd, linecolor="red", linewidth=2, linealpha=0.8, lab="spectral density") function hanning_window(M) w = [0.5 - 0.5 * cos(2 * pi * n / (M - 1)) for n = 0:(M-1)] return w end window = hanning_window(25) / sum(hanning_window(25)) x = range(-12, 12, length = 25) plot(x, window, color="darkblue", title="Hanning window", ylabel="Weights", xlabel="Position in sequence of weights", legend=false, grid=false) n = 400 ϕ = 0.5 θ = [0, -0.8] σ = 1.0 lp = ARMA(ϕ, θ, 1.0) X = simulation(lp, ts_length = n) xs = [] x_sds = [] x_sms = [] ys = [] y_sds = [] y_sms = [] titles = [] for (i, wl) in enumerate([15, 55, 175]) # window lengths x, y = periodogram(X) push!(xs, x) push!(ys, y) x_sd, y_sd = spectral_density(lp, two_pi=false, res=120) push!(x_sds, x_sd) push!(y_sds, y_sd) x, y_smoothed = periodogram(X, "hamming", wl) push!(x_sms, x) push!(y_sms, y_smoothed) t = "window length = $wl" push!(titles, t) end plot(xs, ys, layout=(3,1), color=:blue, alpha=0.5, linewidth=2, label=["periodogram" "" ""]) plot!(x_sds, y_sds, layout=(3,1), color=:red, alpha=0.8, linewidth=2, label=["spectral density" "" ""]) plot!(x_sms, y_sms, layout=(3,1), color=:black, linewidth=2, label=["smoothed periodogram" "" ""]) plot!(title=reshape(titles,1,length(titles))) lp2 = ARMA(-0.9, 0.0, 1.0) wl = 65 p = plot(layout=(3,1)) for i in 1:3 X = simulation(lp2,ts_length=150) plot!(p[i],xlims = (0,pi)) x_sd, y_sd = spectral_density(lp2,two_pi=false, res=180) plot!(p[i],x_sd, y_sd, linecolor=:red, linestyle=:solid, yscale=:log10, linewidth=2, linealpha=0.75, label="spectral density",legend=:topleft) x, y_smoothed = periodogram(X, "hamming", wl) plot!(p[i],x, y_smoothed, linecolor=:black, linestyle=:solid, yscale=:log10, linewidth=2, linealpha=0.75, label="standard smoothed periodogram",legend=:topleft) x, y_ar = ar_periodogram(X, "hamming", wl) plot!(p[i],x, y_ar, linecolor=:blue, linestyle=:solid, yscale=:log10, linewidth=2, linealpha=0.75, label="AR smoothed periodogram",legend=:topleft) end p