Skip to content

Commit

Permalink
Merge pull request #5 from floswald/main
Browse files Browse the repository at this point in the history
testing euler equation
  • Loading branch information
vasudeva-ram authored Jul 22, 2024
2 parents 8faae45 + 942f138 commit ad695fa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
8 changes: 7 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
14 changes: 3 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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
using TestItemRunner

@run_package_tests
50 changes: 50 additions & 0 deletions test/test_aiyagari.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

@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.04

# 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

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,(cons ./ policies.consumption) .- 1) < 0.00001
end

0 comments on commit ad695fa

Please sign in to comment.