From bd7d74f6a059bef40d7d42e9dcf7522385371edb Mon Sep 17 00:00:00 2001 From: annabellasd Date: Mon, 6 Nov 2023 15:26:19 -0800 Subject: [PATCH] Updated with Trang's Comments --- lectures/dynamic_programming_squared/amss.md | 10 ++-- .../dynamic_programming_squared/dyn_stack.md | 7 ++- .../dynamic_programming_squared/lqramsey.md | 55 +++++++++---------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lectures/dynamic_programming_squared/amss.md b/lectures/dynamic_programming_squared/amss.md index a3afd086..6c63b9f1 100644 --- a/lectures/dynamic_programming_squared/amss.md +++ b/lectures/dynamic_programming_squared/amss.md @@ -542,7 +542,7 @@ function time1_value(pas::SequentialAllocation, mu::Real) end -function Τ(model::Model, c::Union{Real,Vector}, n::Union{Real,Vector}) +function T(model::Model, c::Union{Real,Vector}, n::Union{Real,Vector}) Uc, Un = model.Uc.(c, n), model.Un.(c, n) return 1. .+ Un./(model.Theta .* Uc) end @@ -562,12 +562,12 @@ function simulate(pas::SequentialAllocation, cHist = zeros(T) nHist = zeros(T) Bhist = zeros(T) - ΤHist = zeros(T) + THist = zeros(T) muHist = zeros(T) RHist = zeros(T-1) # time 0 mu, cHist[1], nHist[1], _ = time0_allocation(pas, B_, s_0) - ΤHist[1] = Τ(pas.model, cHist[1], nHist[1])[s_0] + THist[1] = T(pas.model, cHist[1], nHist[1])[s_0] Bhist[1] = B_ muHist[1] = mu # time 1 onward @@ -575,13 +575,13 @@ function simulate(pas::SequentialAllocation, c, n, x, Xi = time1_allocation(pas,mu) u_c = Uc(c,n) s = sHist[t] - ΤHist[t] = Τ(pas.model, c, n)[s] + THist[t] = T(pas.model, c, n)[s] Eu_c = dot(Pi[sHist[t-1],:], u_c) cHist[t], nHist[t], Bhist[t] = c[s], n[s], x[s] / u_c[s] RHist[t-1] = Uc(cHist[t-1], nHist[t-1]) / (beta * Eu_c) muHist[t] = mu end - return cHist, nHist, Bhist, ΤHist, sHist, muHist, RHist + return cHist, nHist, Bhist, THist, sHist, muHist, RHist end diff --git a/lectures/dynamic_programming_squared/dyn_stack.md b/lectures/dynamic_programming_squared/dyn_stack.md index 9542d8f6..36012503 100644 --- a/lectures/dynamic_programming_squared/dyn_stack.md +++ b/lectures/dynamic_programming_squared/dyn_stack.md @@ -929,7 +929,7 @@ We define named tuples and default values for the model and solver settings, and instantiate one copy of each ```{code-cell} julia -model(;a0 = 10, a1 = 2, beta = 0.96, gamma = 120., n = 300) = (; a0, a1, beta, gamma, n) +model(;a0 = 10, a1 = 2, beta = 0.96, gamma = 120., n = 300) = (; a0, a1, beta, gamma, n) # things like tolerances, etc. settings(;tol0 = 1e-8,tol1 = 1e-16,tol2 = 1e-2) = (;tol0, tol1, tol2) @@ -1166,7 +1166,7 @@ P_tilde # value function in the follower's problem ```{code-cell} julia # manually check that P is an approximate fixed point -all((P - ((R + F' * Q * F) + beta * (A - B * F)' * P * (A - B * F)) .< tol0)) +all((P - ((R + F' * Q * F) + beta * (A - B * F)' * P * (A - B * F)) .< tol0)) ``` ```{code-cell} julia @@ -1410,7 +1410,8 @@ end plot([vt_MPE, vt_leader, vt_follower], labels = ["MPE" "Stackelberg leader" "Stackelberg follower"], title = "MPE vs Stackelberg Values", - xlabel = L"t") + xlabel = L"t", + legend = :outertopright) ``` ```{code-cell} julia diff --git a/lectures/dynamic_programming_squared/lqramsey.md b/lectures/dynamic_programming_squared/lqramsey.md index a9cb6519..3fefa1b7 100644 --- a/lectures/dynamic_programming_squared/lqramsey.md +++ b/lectures/dynamic_programming_squared/lqramsey.md @@ -613,8 +613,8 @@ function compute_exog_sequences(econ, x) g, d, b, s = [dropdims(S * x, dims = 1) for S in (Sg, Sd, Sb, Ss)] #= solve for Lagrange multiplier in the govt budget constraint - In fact we solve for ν = lambda / (1 + 2*lambda). Here ν is the - solution to a quadratic equation a(ν^2 - ν) + b = 0 where + In fact we solve for nu = lambda / (1 + 2*lambda). Here nu is the + solution to a quadratic equation a(nu^2 - nu) + b = 0 where a and b are expected discounted sums of quadratic forms of the state. =# Sm = Sb - Sd - Ss @@ -622,12 +622,11 @@ function compute_exog_sequences(econ, x) end -function compute_allocation(econ, Sm, ν, x, b) - Sg, Sd, Sb, Ss = econ.Sg, econ.Sd, econ.Sb, econ.Ss - - # solve for the allocation given ν and x - Sc = 0.5 .* (Sb + Sd - Sg - ν .* Sm) - Sl = 0.5 .* (Sb - Sd + Sg - ν .* Sm) +function compute_allocation(econ, Sm, nu, x, b) + (;Sg, Sd, Sb, Ss) = econ + # solve for the allocation given nu and x + Sc = 0.5 .* (Sb + Sd - Sg - nu .* Sm) + Sl = 0.5 .* (Sb - Sd + Sg - nu .* Sm) c = dropdims(Sc * x, dims = 1) l = dropdims(Sl * x, dims = 1) p = dropdims((Sb - Sc) * x, dims = 1) # Price without normalization @@ -638,29 +637,29 @@ function compute_allocation(econ, Sm, ν, x, b) end -function compute_ν(a0, b0) +function compute_nu(a0, b0) disc = a0^2 - 4a0 * b0 if disc >= 0 - ν = 0.5 *(a0 - sqrt(disc)) / a0 + nu = 0.5 *(a0 - sqrt(disc)) / a0 else println("There is no Ramsey equilibrium for these parameters.") error("Government spending (economy.g) too low") end # Test that the Lagrange multiplier has the right sign - if ν * (0.5 - ν) < 0 + if nu * (0.5 - nu) < 0 print("Negative multiplier on the government budget constraint.") error("Government spending (economy.g) too low") end - return ν + return nu end -function compute_Pi(B, R, rvn, g,Xi) +function compute_Pi(B, R, rvn, g,xi) pi = B[2:end] - R[1:end-1] .* B[1:end-1] - rvn[1:end-1] + g[1:end-1] - Pi = cumsum(pi .*Xi) + Pi = cumsum(pi .*xi) return pi, Pi end @@ -685,10 +684,10 @@ function compute_paths(econ::Economy{<:AbstractFloat, <:DiscreteStochProcess}, T b0 = (F \ H')[1] ./ 2 # compute lagrange multiplier - ν = compute_ν(a0, b0) + nu = compute_nu(a0, b0) - # Solve for the allocation given ν and x - Sc, Sl, c, l, p, tau, rvn = compute_allocation(econ, Sm, ν, x, b) + # Solve for the allocation given nu and x + Sc, Sl, c, l, p, tau, rvn = compute_allocation(econ, Sm, nu, x, b) # compute remaining variables H = ((Sb - Sc) * x_vals) .* ((Sl - Sg) * x_vals) - (Sl * x_vals).^2 @@ -697,12 +696,12 @@ function compute_paths(econ::Economy{<:AbstractFloat, <:DiscreteStochProcess}, T H = dropdims(P[state, :] * ((Sb - Sc) * x_vals)', dims = 2) R = p ./ (beta .* H) temp = dropdims(P[state, :] *((Sb - Sc) * x_vals)', dims = 2) - Xi = p[2:end] ./ temp[1:end-1] + xi = p[2:end] ./ temp[1:end-1] # compute pi - pi, Pi = compute_Pi(B, R, rvn, g,Xi) + pi, Pi = compute_Pi(B, R, rvn, g, xi) - return (;g, d, b, s, c, l, p, tau, rvn, B, R, pi, Pi, Xi) + return (;g, d, b, s, c, l, p, tau, rvn, B, R, pi, Pi, xi) end function compute_paths(econ::Economy{<:AbstractFloat, <:ContStochProcess}, T) @@ -736,10 +735,10 @@ function compute_paths(econ::Economy{<:AbstractFloat, <:ContStochProcess}, T) b0 = 0.5 * var_quadratic_sum(A, C, H, beta, x0) # compute lagrange multiplier - ν = compute_ν(a0, b0) + nu = compute_nu(a0, b0) - # solve for the allocation given ν and x - Sc, Sl, c, l, p, tau, rvn = compute_allocation(econ, Sm, ν, x, b) + # solve for the allocation given nu and x + Sc, Sl, c, l, p, tau, rvn = compute_allocation(econ, Sm, nu, x, b) # compute remaining variables H = Sl'Sl - (Sb - Sc)' *(Sl - Sg) @@ -752,13 +751,13 @@ function compute_paths(econ::Economy{<:AbstractFloat, <:ContStochProcess}, T) R = 1 ./ Rinv AF1 = (Sb - Sc) * x[:, 2:end] AF2 = (Sb - Sc) * A * x[:, 1:end-1] - Xi = AF1 ./ AF2 - Xi = dropdims(Xi, dims = 1) + xi = AF1 ./ AF2 + xi = dropdims(xi, dims = 1) # compute pi - pi, Pi = compute_Pi(B, R, rvn, g,Xi) + pi, Pi = compute_Pi(B, R, rvn, g, xi) - return(;g, d, b, s, c, l, p, tau, rvn, B, R, pi, Pi, Xi) + return(;g, d, b, s, c, l, p, tau, rvn, B, R, pi, Pi, xi) end function gen_fig_1(path) @@ -789,7 +788,7 @@ function gen_fig_2(path) T = length(path.c) - paths = [path.Xi, path.Pi] + paths = [path.xi, path.Pi] labels = [L"\xi_t", L"\Pi_t"] plt_1 = plot() plt_2 = plot()