Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugomez committed Jun 13, 2024
1 parent 7c1ad13 commit 98e0018
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ yend = OrderedDict(:v => [(m.ρ / m.γ + (1 - 1 / m.γ) * m.r)^(-m.γ) * (a + y
y, residual_norm = pdesolve(m, stategrid, yend)
#
# # Important: check marginal value of wealth converges to 1.0
# # This happens ONLY if a >= 1000.0. Otherwise with 300 it does not work. This is interesting. Maybe it means there should be a better way to have bordering condition at top
# # This happens ONLY if a >= 1000.0. Otherwise with 300 it does not work.
# b = ((m.r + (m.ρ - m.r)/m.γ - (1-m.γ) / (2 * m.γ) * (m.μR - m.r)^2 / (m.γ * m.σR^2)))^(1/(1 - 1/m.γ))
# pw = (result[:v] * (1-m.γ)).^(1/(1-m.γ)-1) .* result[:va] ./ b

68 changes: 34 additions & 34 deletions examples/ConsumptionProblem/AchdouHanLasryLionsMoll_TwoStates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,61 @@ mutable struct AchdouHanLasryLionsMoll_TwoStatesModel
amax::Float64
end

function AchdouHanLasryLionsMoll_TwoStatesModel(;yl = 0.5, yh = 1.5, λlh = 0.2, λhl = 0.2, r = 0.03, ρ = 0.04, γ = 2.0, amin = - yl / r, amax = 500.0)
function AchdouHanLasryLionsMoll_TwoStatesModel(;yl = 0.5, yh = 1.5, λlh = 0.2, λhl = 0.2, r = 0.03, ρ = 0.04, γ = 2.0, amin = - yl / r, amax = 50.0)
AchdouHanLasryLionsMoll_TwoStatesModel(yl, yh, λlh, λhl, r, ρ, γ, amin, amax)
end

function (m::AchdouHanLasryLionsMoll_TwoStatesModel)(state::NamedTuple, value::NamedTuple)
(; yl, yh, λlh, λhl, r, ρ, γ, amin, amax) = m
(; a) = state
(; vl, vla_up, vla_down, vh, vha_up, vha_down) = value

# low income state
vla = vla_up
iter = 0
va = vla_up
@label start1
va = max(va, 1e-8)
c = va^(-1 / γ)
μa = yl + r * a - c
if (iter == 0) & (μa <= 0)
vla = vla_up
@label startl
vla = max(vla, eps())
cl = vla^(-1 / γ)
μla = yl + r * a - cl
if (iter == 0) & (μla <= 0)
iter += 1
va = vla_down
@goto start1
vla = vla_down
@goto startl
end
if (a amin) && (μa <= 0.0)
c = yl + r * amin
μa = 0.0
va = c^(-γ)
if (a amin) && (μla <= 0.0)
cl = yl + r * amin
μla = 0.0
vla = cl^(-γ)
end
vah = va
μal = μa
vlt = - (c^(1 - γ) / (1 - γ) + μa * va + λlh * (vh - vl) - ρ * vl)
vlt = - (cl^(1 - γ) / (1 - γ) + μla * vla + λlh * (vh - vl) - ρ * vl)


# high income state
vha = vha_up
iter = 0
va = vha_up
@label start2
va = max(va, 1e-8)
c = va^(-1 / γ)
μa = yh + r * a - c
if (iter == 0) & (μa <= 0)
vha = vha_up
@label starth
vha = max(vha, eps())
ch = vha^(-1 / γ)
μha = yh + r * a - ch
if (iter == 0) & (μha <= 0)
iter += 1
va = vha_down
@goto start2
vha = vha_down
@goto starth
end
if (a amin) && (μa <= 0.0)
c = yh + r * amin
μa = 0.0
va = c^(-γ)
if (a amin) && (μha <= 0.0)
ch = yh + r * amin
μha = 0.0
vha = ch^(-γ)
end
val = va
μah = μa
vht = - (c^(1 - γ) / (1 - γ) + μa * va + λhl * (vl - vh) - ρ * vh)
return (; vlt, vht), (; vlt, vht, vah, val, μah, μal)
vht = - (ch^(1 - γ) / (1 - γ) + μha * vha + λhl * (vl - vh) - ρ * vh)

return (; vlt, vht), (; vha, vla, μha, μla)
end

m = AchdouHanLasryLionsMoll_TwoStatesModel()
m.amin += 0.001
stategrid = OrderedDict(:a => m.amin .+ range(0, (m.amax - m.amin)^0.8, length = 5000).^(1/0.8))
stategrid = OrderedDict(:a => m.amin .+ range(0, (m.amax - m.amin)^(1/2), length = 200).^2)
yend = OrderedDict(:vl => (m.ρ ./ m.γ .+ (1 .- 1 / m.γ) .* m.r)^(-m.γ) .* (stategrid[:a] .+ m.yl ./ m.r).^(1-m.γ) ./ (1 - m.γ), :vh => (m.ρ ./ m.γ .+ (1 .- m.γ) .* m.r)^(-m.γ) .* (stategrid[:a] .+ m.yh ./ m.r).^(1-m.γ) ./ (1 - m.γ))
result = pdesolve(m, stategrid, yend)
@assert result.residual_norm <= 1e-5
Expand Down
3 changes: 0 additions & 3 deletions examples/ConsumptionProblem/WangWangYang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ result = pdesolve(m, stategrid, yend, bc = OrderedDict(:pw => (1.0, 1.0)))
@assert result.residual_norm <= 1e-5





# Alternative solution bypassing pdesolve
# just encode the PDE has a vector equation
using InfinitesimalGenerators
Expand Down
59 changes: 0 additions & 59 deletions examples/ConsumptionProblem/try.jl

This file was deleted.

2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ for x in (:CampbellCochrane, :Wachter, :BansalYaron, :GarleanuPanageas, :DiTella
@testset "$x" begin include("../examples/AssetPricing/$(x).jl") end
end

for x in (:AchdouHanLasryLionsMoll_Diffusion, :AchdouHanLasryLionsMoll_DiffusionTwoAssets)
for x in (:AchdouHanLasryLionsMoll_Diffusion, :AchdouHanLasryLionsMoll_DiffusionTwoAssets, :AchdouHanLasryLionsMoll_TwoStates, :WangWangYang)
@testset "$x" begin include("../examples/ConsumptionProblem/$(x).jl") end
end

Expand Down

0 comments on commit 98e0018

Please sign in to comment.