From 55ace18b0a7748472dba89b4d24095e81b6e65c6 Mon Sep 17 00:00:00 2001 From: Florian Oswald Date: Tue, 16 Jul 2024 17:53:41 +0200 Subject: [PATCH 1/3] added euler eq test --- test/test_aiyagari.jl | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 test/test_aiyagari.jl diff --git a/test/test_aiyagari.jl b/test/test_aiyagari.jl new file mode 100644 index 0000000..ebc94ba --- /dev/null +++ b/test/test_aiyagari.jl @@ -0,0 +1,53 @@ + +@testitem "Euler Error" begin + + # defining the parameters of the model + rho = 0.966 + s = 0.5 + sig = s * sqrt(1 - rho^2) + params = SSJ.Params(0.96, 1.0, sig, rho, 0.025, 0.11, 0.0001, [0.0, 200.0], 200, 7, 300) + + # set an r + r = 0.05 + + # Setting up the model + BaseModel = SSJ.setup_Aiyagari(params) + + agg_labor = SSJ.aggregate_labor(BaseModel.Π, BaseModel.shockgrid) + aggregates, prices = SSJ.get_AggsPrices(r, agg_labor, BaseModel) + policies = SSJ.EGM(BaseModel, prices) + + # now test that `policies` are consistent + + μ = zeros(params.n_a) # vector of marginal utilities + iuprime = zeros(params.n_a,params.n_e) # matrix of inverse marginal utilities + + # basically does `consumptiongrid` + for ie in 1:params.n_e + fill!(μ , 0.0) + for je in 1:params.n_e + pr = BaseModel.Π[ie,je] # transprob + + # get next period consumption if state is je + # cprime = (1+r) policygrid + w * shock[je] - policy[je] + # (params.n_a by 1) + cprimevec = ((1 + prices.r) * BaseModel.policygrid) .+ (prices.w .* BaseModel.shockgrid[je]) .- policies.saving[:,je] + + # Expected marginal utility at each state of tomorrow's income shock + global μ += pr * (cprimevec .^ ((-1) * params.γ)) + end + # RHS of euler equation + rhs = params.β * (1 + prices.r) * μ + # today's consumption: inverse marginal utility over RHS + iuprime[:,ie] = rhs .^ ((-1)/params.γ) + end + # println([iuprime (policies.consumption)]) + + implied = ((1 + prices.r) * BaseModel.policymat) .+ (prices.w .* BaseModel.shockmat) .- policies.saving + + # checks last penultimate line in `EGM`: + @test all(implied .== policies.consumption) + + # checks reverse engineering of `consumptiongrid` + @test maximum(abs,(iuprime ./ policies.consumption) .- 1) < 0.0001 +end \ No newline at end of file From 6e6d792c0cc62d94bebf3f4b217bb2b1875b062f Mon Sep 17 00:00:00 2001 From: Florian Oswald Date: Tue, 16 Jul 2024 17:59:03 +0200 Subject: [PATCH 2/3] added euler eq test --- Project.toml | 8 +++++++- test/runtests.jl | 14 +++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Project.toml b/Project.toml index a4e2048..15c464d 100644 --- a/Project.toml +++ b/Project.toml @@ -12,5 +12,11 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" + +[targets] +test = ["Test", "TestItemRunner"] \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index ceef14c..3d845a5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,13 +1,5 @@ -using Test -using SSJ -@testset "SSJ.jl tests" begin - @test 1 == 1 - @testset "all run" begin - # not a real test, but breaks if breaks. - main(); - mainKS(); - mainSSJ(); - end -end \ No newline at end of file +using TestItemRunner + +@run_package_tests \ No newline at end of file From 942f138780d82207035815a21e173223aeb61834 Mon Sep 17 00:00:00 2001 From: Florian Oswald Date: Wed, 17 Jul 2024 11:13:33 +0200 Subject: [PATCH 3/3] fixed test --- test/test_aiyagari.jl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/test_aiyagari.jl b/test/test_aiyagari.jl index ebc94ba..3611b70 100644 --- a/test/test_aiyagari.jl +++ b/test/test_aiyagari.jl @@ -8,7 +8,7 @@ params = SSJ.Params(0.96, 1.0, sig, rho, 0.025, 0.11, 0.0001, [0.0, 200.0], 200, 7, 300) # set an r - r = 0.05 + r = 0.04 # Setting up the model BaseModel = SSJ.setup_Aiyagari(params) @@ -41,13 +41,10 @@ # today's consumption: inverse marginal utility over RHS iuprime[:,ie] = rhs .^ ((-1)/params.γ) end - # println([iuprime (policies.consumption)]) - implied = ((1 + prices.r) * BaseModel.policymat) .+ (prices.w .* BaseModel.shockmat) .- policies.saving - - # checks last penultimate line in `EGM`: - @test all(implied .== policies.consumption) + savings = SSJ.policyupdate(prices,BaseModel.policymat,BaseModel.shockmat,iuprime) + cons = ((1 + prices.r) * BaseModel.policymat) + (prices.w * BaseModel.shockmat) - savings # checks reverse engineering of `consumptiongrid` - @test maximum(abs,(iuprime ./ policies.consumption) .- 1) < 0.0001 + @test maximum(abs,(cons ./ policies.consumption) .- 1) < 0.00001 end \ No newline at end of file