using InstantiateFromURL github_project("QuantEcon/quantecon-notebooks-julia", version = "0.1.0") using LinearAlgebra, Statistics, Compat using Distributions, Parameters, Plots, QuantEcon import Distributions: loglikelihood gr(fmt = :png); AMF_LSS_VAR = @with_kw (A, B, D, F = 0.0, ν = 0.0, lss = construct_ss(A, B, D, F, ν)) function construct_ss(A, B, D, F, ν) H, g = additive_decomp(A, B, D, F) # Build A matrix for LSS # Order of states is: [1, t, xt, yt, mt] A1 = [1 0 0 0 0] # Transition for 1 A2 = [1 1 0 0 0] # Transition for t A3 = [0 0 A 0 0] # Transition for x_{t+1} A4 = [ν 0 D 1 0] # Transition for y_{t+1} A5 = [0 0 0 0 1] # Transition for m_{t+1} Abar = vcat(A1, A2, A3, A4, A5) # Build B matrix for LSS Bbar = [0, 0, B, F, H] # Build G matrix for LSS # Order of observation is: [xt, yt, mt, st, tt] G1 = [0 0 1 0 0] # Selector for x_{t} G2 = [0 0 0 1 0] # Selector for y_{t} G3 = [0 0 0 0 1] # Selector for martingale G4 = [0 0 -g 0 0] # Selector for stationary G5 = [0 ν 0 0 0] # Selector for trend Gbar = vcat(G1, G2, G3, G4, G5) # Build LSS struct x0 = [0, 0, 0, 0, 0] S0 = zeros(5, 5) return LSS(Abar, Bbar, Gbar, mu_0 = x0, Sigma_0 = S0) end function additive_decomp(A, B, D, F) A_res = 1 / (1 - A) g = D * A_res H = F + D * A_res * B return H, g end function multiplicative_decomp(A, B, D, F, ν) H, g = additive_decomp(A, B, D, F) ν_tilde = ν + 0.5 * H^2 return ν_tilde, H, g end function loglikelihood_path(amf, x, y) @unpack A, B, D, F = amf T = length(y) FF = F^2 FFinv = inv(FF) temp = y[2:end] - y[1:end-1] - D*x[1:end-1] obs = temp .* FFinv .* temp obssum = cumsum(obs) scalar = (log(FF) + log(2pi)) * (1:T-1) return -0.5 * (obssum + scalar) end function loglikelihood(amf, x, y) llh = loglikelihood_path(amf, x, y) return llh[end] end function simulate_xy(amf, T) foo, bar = simulate(amf.lss, T) x = bar[1, :] y = bar[2, :] return x, y end function simulate_paths(amf, T = 150, I = 5000) # Allocate space storeX = zeros(I, T) storeY = zeros(I, T) for i in 1:I # Do specific simulation x, y = simulate_xy(amf, T) # Fill in our storage matrices storeX[i, :] = x storeY[i, :] = y end return storeX, storeY end function population_means(amf, T = 150) # Allocate Space xmean = zeros(T) ymean = zeros(T) # Pull out moment generator moment_generator = moment_sequence(amf.lss) for (tt, x) = enumerate(moment_generator) ymeans = x[2] xmean[tt] = ymeans[1] ymean[tt] = ymeans[2] if tt == T break end end return xmean, ymean end F = 0.2 amf = AMF_LSS_VAR(A = 0.8, B = 1.0, D = 0.5, F = F) T = 150 I = 5000 # Simulate and compute sample means Xit, Yit = simulate_paths(amf, T, I) Xmean_t = mean(Xit, dims = 1) Ymean_t = mean(Yit, dims = 1) # Compute population means Xmean_pop, Ymean_pop = population_means(amf, T) # Plot sample means vs population means plt_1 = plot(Xmean_t', color = :blue, label = "1/I sum_i x_t^i") plot!(plt_1, Xmean_pop, color = :black, label = "E x_t") plot!(plt_1, title = "x_t", xlim = (0, T), legend = :bottomleft) plt_2 = plot(Ymean_t', color = :blue, label = "1/I sum_i x_t^i") plot!(plt_2, Ymean_pop, color = :black, label = "E y_t") plot!(plt_2, title = "y_t", xlim = (0, T), legend = :bottomleft) plot(plt_1, plt_2, layout = (2, 1), size = (800,500)) function simulate_likelihood(amf, Xit, Yit) # Get size I, T = size(Xit) # Allocate space LLit = zeros(I, T-1) for i in 1:I LLit[i, :] = loglikelihood_path(amf, Xit[i, :], Yit[i, :]) end return LLit end # Get likelihood from each path x^{i}, Y^{i} LLit = simulate_likelihood(amf, Xit, Yit) LLT = 1 / T * LLit[:, end] LLmean_t = mean(LLT) plot(seriestype = :histogram, LLT, label = "") plot!(title = "Distribution of (I/T)log(L_T)|theta_0") vline!([LLmean_t], linestyle = :dash, color = :black, lw = 2, alpha = 0.6, label = "") normdist = Normal(0, F) mult = 1.175 println("The pdf at +/- $mult sigma takes the value: $(pdf(normdist,mult*F))") println("Probability of dL being larger than 1 is approx: "* "$(cdf(normdist,mult*F)-cdf(normdist,-mult*F))") # Compare this to the sample analogue: L_increment = LLit[:,2:end] - LLit[:,1:end-1] r,c = size(L_increment) frac_nonegative = sum(L_increment.>=0)/(c*r) print("Fraction of dlogL being nonnegative in the sample is: $(frac_nonegative)") xgrid = range(-1, 1, length = 100) println("The pdf at +/- one sigma takes the value: $(pdf(normdist, F)) ") plot(xgrid, pdf.(normdist, xgrid), label = "") plot!(title = "Conditional pdf f(Delta y_(t+1) | x_t)") # Create the second (wrong) alternative model amf2 = AMF_LSS_VAR(A = 0.9, B = 1.0, D = 0.55, F = 0.25) # parameters for θ_1 closer to θ_0 # Get likelihood from each path x^{i}, y^{i} LLit2 = simulate_likelihood(amf2, Xit, Yit) LLT2 = 1/(T-1) * LLit2[:, end] LLmean_t2 = mean(LLT2) plot(seriestype = :histogram, LLT2, label = "") vline!([LLmean_t2], color = :black, lw = 2, linestyle = :dash, alpha = 0.6, label = "") plot!(title = "Distribution of (1/T)log(L_T) | theta_1)") plot(seriestype = :histogram, LLT, bin = 50, alpha = 0.5, label = "True", normed = true) plot!(seriestype = :histogram, LLT2, bin = 50, alpha = 0.5, label = "Alternative", normed = true) vline!([mean(LLT)], color = :black, lw = 2, linestyle = :dash, label = "") vline!([mean(LLT2)], color = :black, lw = 2, linestyle = :dash, label = "") LLT_diff = LLT - LLT2 plot(seriestype = :histogram, LLT_diff, bin = 50, label = "") plot!(title = "(1/T)[log(L_T^i | theta_0) - log(L_T^i |theta_1)]") function simulate_martingale_components(amf, T = 1_000, I = 5_000) # Get the multiplicative decomposition @unpack A, B, D, F, ν, lss = amf ν, H, g = multiplicative_decomp(A, B, D, F, ν) # Allocate space add_mart_comp = zeros(I, T) # Simulate and pull out additive martingale component for i in 1:I foo, bar = simulate(lss, T) # Martingale component is third component add_mart_comp[i, :] = bar[3, :] end mul_mart_comp = exp.(add_mart_comp' .- (0:T-1) * H^2 / 2)' return add_mart_comp, mul_mart_comp end # Build model amf_2 = AMF_LSS_VAR(A = 0.8, B = 0.001, D = 1.0, F = 0.01, ν = 0.005) amc, mmc = simulate_martingale_components(amf_2, 1_000, 5_000) amcT = amc[:, end] mmcT = mmc[:, end] println("The (min, mean, max) of additive Martingale component in period T is") println("\t ($(minimum(amcT)), $(mean(amcT)), $(maximum(amcT)))") println("The (min, mean, max) of multiplicative Martingale component in period T is") println("\t ($(minimum(mmcT)), $(mean(mmcT)), $(maximum(mmcT)))") plot(seriestype = :histogram, amcT, bin = 25, normed = true, label = "") plot!(title = "Histogram of Additive Martingale Component") plot(seriestype = :histogram, mmcT, bin = 25, normed = true, label = "") plot!(title = "Histogram of Multiplicative Martingale Component") function Mtilde_t_density(amf, t; xmin = 1e-8, xmax = 5.0, npts = 5000) # Pull out the multiplicative decomposition νtilde, H, g = multiplicative_decomp(amf.A, amf.B, amf.D, amf.F, amf.ν) H2 = H*H # The distribution mdist = LogNormal(-t * H2 / 2, sqrt(t * H2)) x = range(xmin, xmax, length = npts) p = pdf.(mdist, x) return x, p end function logMtilde_t_density(amf, t; xmin = -15.0, xmax = 15.0, npts = 5000) # Pull out the multiplicative decomposition @unpack A, B, D, F, ν = amf νtilde, H, g = multiplicative_decomp(A, B, D, F, ν) H2 = H * H # The distribution lmdist = Normal(-t * H2 / 2, sqrt(t * H2)) x = range(xmin, xmax, length = npts) p = pdf.(lmdist, x) return x, p end times_to_plot = [10, 100, 500, 1000, 2500, 5000] dens_to_plot = [Mtilde_t_density(amf_2, t, xmin=1e-8, xmax=6.0) for t in times_to_plot] ldens_to_plot = [logMtilde_t_density(amf_2, t, xmin=-10.0, xmax=10.0) for t in times_to_plot] # plot_title = "Densities of M_t^tilda" is required, however, plot_title is not yet # supported in Plots plots = plot(layout = (3,2), size = (600,800)) for (it, dens_t) in enumerate(dens_to_plot) x, pdf = dens_t plot!(plots[it], title = "Density for time (time_to_plot[it])") plot!(plots[it], pdf, fill_between = ([0], pdf), label = "") end plot(plots) function Uu(amf, δ, γ) @unpack A, B, D, F, ν = amf ν_tilde, H, g = multiplicative_decomp(A, B, D, F, ν) resolv = 1 / (1 - exp(-δ) * A) vect = F + D * resolv * B U_risky = exp(-δ) * resolv * D u_risky = exp(-δ) / (1 - exp(-δ)) * (ν + 0.5 * (1 - γ) * (vect^2)) U_det = 0 u_det = exp(-δ) / (1 - exp(-δ)) * ν_tilde return U_risky, u_risky, U_det, u_det end # Set remaining parameters δ = 0.02 γ = 2.0 # Get coeffs U_r, u_r, U_d, u_d = Uu(amf_2, δ, γ) x0 = 0.0 # initial conditions logVC_r = U_r * x0 + u_r logVC_d = U_d * x0 + u_d perc_reduct = 100 * (1 - exp(logVC_r - logVC_d)) perc_reduct