Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaner tests #4

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 40 additions & 50 deletions test/accounting_identities.jl
Original file line number Diff line number Diff line change
@@ -1,53 +1,43 @@
using BeforeIT, MAT, FileIO, Random
using Test

dir = @__DIR__

parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

T = 1
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
data = BeforeIT.initialise_data(model)

println(BeforeIT.get_accounting_identities(data))
println(BeforeIT.get_accounting_identity_banks(model))

for t in 1:T
println(t)
BeforeIT.one_epoch!(model; multi_threading = false)
BeforeIT.update_data!(data, model)
end

# println(BeforeIT.get_accounting_identities(data))
# println(BeforeIT.get_accounting_identity_banks(model))

# income accounting and production accounting should be equal
zero = sum(data.nominal_gva - data.compensation_employees - data.operating_surplus - data.taxes_production)
# println(zero)
@test isapprox(zero, 0.0, atol = 1e-9)

# compare nominal_gdp to total expenditure
zero = sum(
data.nominal_gdp - data.nominal_household_consumption - data.nominal_government_consumption -
data.nominal_capitalformation - data.nominal_exports + data.nominal_imports,
)
# println(zero)
@test isapprox(zero, 0.0, atol = 1e-9)

zero = sum(
data.real_gdp - data.real_household_consumption - data.real_government_consumption - data.real_capitalformation - data.real_exports + data.real_imports,
)
# println(zero)
@test isapprox(zero, 0.0, atol = 1e-8)

# accounting identity of balance sheet of central bank
zero = model.cb.E_CB + model.rotw.D_RoW - model.gov.L_G + model.bank.D_k
# println(zero)
@test isapprox(zero, 0.0, atol = 1e-9)

# accounting identity of balance sheet of commercial bank
tot_D_h = sum(model.w_act.D_h) + sum(model.w_inact.D_h) + sum(model.firms.D_h) + model.bank.D_h
zero = sum(model.firms.D_i) + tot_D_h + sum(model.bank.E_k) - sum(model.firms.L_i) - model.bank.D_k
# println(zero)
@test isapprox(zero, 0.0, atol = 1e-9)
@testset "accounting identities" begin
dir = @__DIR__

parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

T = 1
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
data = BeforeIT.initialise_data(model)

for t in 1:T
BeforeIT.one_epoch!(model; multi_threading = false)
BeforeIT.update_data!(data, model)
end

# income accounting and production accounting should be equal
zero = sum(data.nominal_gva - data.compensation_employees - data.operating_surplus - data.taxes_production)
@test isapprox(zero, 0.0, atol = 1e-8)

# compare nominal_gdp to total expenditure
zero = sum(
data.nominal_gdp - data.nominal_household_consumption - data.nominal_government_consumption -
data.nominal_capitalformation - data.nominal_exports + data.nominal_imports,
)
@test isapprox(zero, 0.0, atol = 1e-8)

zero = sum(
data.real_gdp - data.real_household_consumption - data.real_government_consumption - data.real_capitalformation - data.real_exports + data.real_imports,
)
@test isapprox(zero, 0.0, atol = 1e-8)

# accounting identity of balance sheet of central bank
zero = model.cb.E_CB + model.rotw.D_RoW - model.gov.L_G + model.bank.D_k
@test isapprox(zero, 0.0, atol = 1e-8)

# accounting identity of balance sheet of commercial bank
tot_D_h = sum(model.w_act.D_h) + sum(model.w_inact.D_h) + sum(model.firms.D_h) + model.bank.D_h
zero = sum(model.firms.D_i) + tot_D_h + sum(model.bank.E_k) - sum(model.firms.L_i) - model.bank.D_k
@test isapprox(zero, 0.0, atol = 1e-8)
end
42 changes: 20 additions & 22 deletions test/deterministic/deterministic_ouput_t1_t5.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@testset "time 1 and 5 deterministic" begin

dir = @__DIR__

T = 1
parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
Expand All @@ -28,30 +27,29 @@
end
end

T = 5
parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
data = BeforeIT.initialise_data(model)
for t in 1:T
BeforeIT.one_epoch!(model; multi_threading = false)
BeforeIT.update_data!(data, model)
end

# T = 4
# parameters = matread(joinpath(dir, "../../data/austria/parameters/2010Q1.mat"))
# initial_conditions = matread(joinpath(dir, "../../data/austria/initial_conditions/2010Q1.mat"))
# model = BeforeIT.initialise_model(parameters, initial_conditions, T)
# data = BeforeIT.initialise_data(model)
# for t in 1:T-1
# BeforeIT.one_epoch!(model; multi_threading = false)
# BeforeIT.update_data!(data, model)
# end

# output_t5 = matread(joinpath(dir, "../matlab_code/output_t5.mat"))list
output_t5 = matread(joinpath(dir, "../matlab_code/output_t5.mat"))

# # confront results between julia and matlab code
# confront results between julia and matlab code

# for fieldname in fieldnames(typeof(data))
# julia_output = getfield(data, fieldname)
# matlab_output = output_t5[string(fieldname)]
for fieldname in fieldnames(typeof(data))
julia_output = getfield(data, fieldname)
matlab_output = output_t5[string(fieldname)]

# if length(size(julia_output)) == 1
# @test isapprox(julia_output, matlab_output', rtol = 1e-4)
# else
# @test isapprox(julia_output, matlab_output, rtol = 1e-5)
# end
# end
if length(julia_output) == 6
@test isapprox(julia_output[2:end], matlab_output', rtol = 1e-4)
else
@test isapprox(julia_output[2:end, :], matlab_output, rtol = 1e-5)
end
end

end
1 change: 1 addition & 0 deletions test/utils/epsilon.jl → test/deterministic/epsilon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using BeforeIT
using Test
import Random: randn


function randn(n1::Int, n2::Int)
return ones(n1, n2)
end
Expand Down
1 change: 1 addition & 0 deletions test/deterministic/runtests_deterministic.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@warn "making the model deterministic all subsequent calls to the model will be deterministic."
include("epsilon.jl")
include("make_model_deterministic.jl")
include("initialize_deterministic.jl")
include("one_epoch_deterministic.jl")
Expand Down
200 changes: 101 additions & 99 deletions test/markets/search_and_matching.jl
Original file line number Diff line number Diff line change
@@ -1,102 +1,104 @@
using BeforeIT, Test, MAT, StatsBase
using Random

Random.seed!(1)

dir = @__DIR__

parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

T = 1
model = BeforeIT.initialise_model(parameters, initial_conditions, T;)


gov = model.gov # government
cb = model.cb # central bank
rotw = model.rotw # rest of the world
firms = model.firms # firms
bank = model.bank # bank
w_act = model.w_act # active workers
w_inact = model.w_inact # inactive workers
agg = model.agg # aggregate variables

prop = model.prop # model properties


gamma_e = 0.01 # set expected growth in euro area
pi_e = 0.001 # set expected inflation in euro area

agg.gamma_e = gamma_e
agg.pi_e = pi_e

Q_s_i, I_d_i, DM_d_i, N_d_i, Pi_e_i, DL_d_i, K_e_i, L_e_i, P_i =
BeforeIT.firms_expectations_and_decisions(model.firms, model)

firms.Q_s_i .= Q_s_i
firms.I_d_i .= I_d_i
firms.DM_d_i .= DM_d_i
firms.N_d_i .= N_d_i
firms.Pi_e_i .= Pi_e_i
firms.P_i .= P_i
firms.DL_d_i .= DL_d_i
firms.K_e_i .= K_e_i
firms.L_e_i .= L_e_i

Pi_e_k = BeforeIT.bank_expected_profits(bank, model)
bank.Pi_e_k = Pi_e_k

C_d_h, I_d_h = BeforeIT.households_budget_act(w_act, model)
w_act.C_d_h .= C_d_h
w_act.I_d_h .= I_d_h
C_d_h, I_d_h = BeforeIT.households_budget_inact(w_inact, model)
w_inact.C_d_h .= C_d_h
w_inact.I_d_h .= I_d_h
C_d_h, I_d_h = BeforeIT.households_budget(firms, model)
firms.C_d_h .= C_d_h
firms.I_d_h .= I_d_h
bank.C_d_h, bank.I_d_h = BeforeIT.households_budget(bank, model)

C_G, C_d_j = BeforeIT.gov_expenditure(gov, model)
gov.C_G = C_G
gov.C_d_j .= C_d_j


epsilon_E = 0.28
epsilon_I = 0.36

agg.epsilon_E = epsilon_E
agg.epsilon_I = epsilon_I

C_E, Y_I, C_d_l, Y_m, P_m = BeforeIT.rotw_import_export(rotw, model)
rotw.C_E = C_E
rotw.Y_I = Y_I
rotw.C_d_l .= C_d_l
rotw.Y_m .= Y_m
rotw.P_m .= P_m

BeforeIT.search_and_matching!(model, false)

rtol = 0.0001

# NOTE: the expected numbers come out of the original implementation,
# and only hold for the serial code (without multithreading)
@test isapprox(mean(w_act.C_h), 4.148850396106796, rtol = rtol)
@test isapprox(mean(w_inact.C_h), 2.205381003981018, rtol = rtol)
@test isapprox(mean(firms.C_h), 9.060799641122962, rtol = rtol)
@test isapprox(bank.C_h, 2931.5395701704915, rtol = rtol)

@test isapprox(mean(w_act.I_h), 0.34186063655926524, rtol = rtol)
@test isapprox(mean(w_inact.I_h), 0.18217582636296747, rtol = rtol)
@test isapprox(mean(firms.I_h), 0.7442975169996757, rtol = rtol)
@test isapprox(bank.I_h, 233.5381841004737, rtol = rtol)

@test isapprox(gov.C_j, 14686.094833493271, rtol = rtol)
@test isapprox(rotw.C_l, 44241.742486622454, rtol = rtol)

@test isapprox(mean(firms.I_i), 20.671016463479898, rtol = rtol)
@test isapprox(mean(firms.DM_i), 110.18635469222951, rtol = rtol)
@test isapprox(mean(firms.P_bar_i), 1.0010000000000023, rtol = rtol)
@test isapprox(mean(firms.P_CF_i), 1.0010000000000023, rtol = rtol)
@test isapprox(mean(firms.Q_d_i), 216.70740037345882, rtol = rtol)
@test isapprox(mean(rotw.Q_d_m), 719.2385742449192, rtol = rtol)
@testset "search and matching" begin
Random.seed!(1)

dir = @__DIR__

parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

T = 1
model = BeforeIT.initialise_model(parameters, initial_conditions, T;)


gov = model.gov # government
cb = model.cb # central bank
rotw = model.rotw # rest of the world
firms = model.firms # firms
bank = model.bank # bank
w_act = model.w_act # active workers
w_inact = model.w_inact # inactive workers
agg = model.agg # aggregate variables

prop = model.prop # model properties


gamma_e = 0.01 # set expected growth in euro area
pi_e = 0.001 # set expected inflation in euro area

agg.gamma_e = gamma_e
agg.pi_e = pi_e

Q_s_i, I_d_i, DM_d_i, N_d_i, Pi_e_i, DL_d_i, K_e_i, L_e_i, P_i =
BeforeIT.firms_expectations_and_decisions(model.firms, model)

firms.Q_s_i .= Q_s_i
firms.I_d_i .= I_d_i
firms.DM_d_i .= DM_d_i
firms.N_d_i .= N_d_i
firms.Pi_e_i .= Pi_e_i
firms.P_i .= P_i
firms.DL_d_i .= DL_d_i
firms.K_e_i .= K_e_i
firms.L_e_i .= L_e_i

Pi_e_k = BeforeIT.bank_expected_profits(bank, model)
bank.Pi_e_k = Pi_e_k

C_d_h, I_d_h = BeforeIT.households_budget_act(w_act, model)
w_act.C_d_h .= C_d_h
w_act.I_d_h .= I_d_h
C_d_h, I_d_h = BeforeIT.households_budget_inact(w_inact, model)
w_inact.C_d_h .= C_d_h
w_inact.I_d_h .= I_d_h
C_d_h, I_d_h = BeforeIT.households_budget(firms, model)
firms.C_d_h .= C_d_h
firms.I_d_h .= I_d_h
bank.C_d_h, bank.I_d_h = BeforeIT.households_budget(bank, model)

C_G, C_d_j = BeforeIT.gov_expenditure(gov, model)
gov.C_G = C_G
gov.C_d_j .= C_d_j


epsilon_E = 0.28
epsilon_I = 0.36

agg.epsilon_E = epsilon_E
agg.epsilon_I = epsilon_I

C_E, Y_I, C_d_l, Y_m, P_m = BeforeIT.rotw_import_export(rotw, model)
rotw.C_E = C_E
rotw.Y_I = Y_I
rotw.C_d_l .= C_d_l
rotw.Y_m .= Y_m
rotw.P_m .= P_m

BeforeIT.search_and_matching!(model, false)

rtol = 0.0001

# NOTE: the expected numbers come out of the original implementation,
# and only hold for the serial code (without multithreading)
@test isapprox(mean(w_act.C_h), 4.148850396106796, rtol = rtol)
@test isapprox(mean(w_inact.C_h), 2.205381003981018, rtol = rtol)
@test isapprox(mean(firms.C_h), 9.060799641122962, rtol = rtol)
@test isapprox(bank.C_h, 2931.5395701704915, rtol = rtol)

@test isapprox(mean(w_act.I_h), 0.34186063655926524, rtol = rtol)
@test isapprox(mean(w_inact.I_h), 0.18217582636296747, rtol = rtol)
@test isapprox(mean(firms.I_h), 0.7442975169996757, rtol = rtol)
@test isapprox(bank.I_h, 233.5381841004737, rtol = rtol)

@test isapprox(gov.C_j, 14686.094833493271, rtol = rtol)
@test isapprox(rotw.C_l, 44241.742486622454, rtol = rtol)

@test isapprox(mean(firms.I_i), 20.671016463479898, rtol = rtol)
@test isapprox(mean(firms.DM_i), 110.18635469222951, rtol = rtol)
@test isapprox(mean(firms.P_bar_i), 1.0010000000000023, rtol = rtol)
@test isapprox(mean(firms.P_CF_i), 1.0010000000000023, rtol = rtol)
@test isapprox(mean(firms.Q_d_i), 216.70740037345882, rtol = rtol)
@test isapprox(mean(rotw.Q_d_m), 719.2385742449192, rtol = rtol)
end
Loading
Loading