Skip to content

Commit

Permalink
Updated with Trang's Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
annabellasd authored and jlperla committed Jan 4, 2024
1 parent 50e6b26 commit bd7d74f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
10 changes: 5 additions & 5 deletions lectures/dynamic_programming_squared/amss.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -562,26 +562,26 @@ 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
for t in 2:T
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
Expand Down
7 changes: 4 additions & 3 deletions lectures/dynamic_programming_squared/dyn_stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
55 changes: 27 additions & 28 deletions lectures/dynamic_programming_squared/lqramsey.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,21 +613,20 @@ 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
return g, d, b, s, Sm
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bd7d74f

Please sign in to comment.