From 61db89552692cd1a3221b296b3d4780c49bff57f Mon Sep 17 00:00:00 2001 From: Aldo Date: Wed, 14 Aug 2024 16:25:08 +0200 Subject: [PATCH] make init functions return init arguments --- src/model_init/init.jl | 68 ++++++++++++-------- src/model_init/init_aggregates.jl | 24 ++++++- src/model_init/init_banks.jl | 45 +++++++++++-- src/model_init/init_firms.jl | 81 ++++++++++-------------- src/model_init/init_government.jl | 24 ++++++- src/model_init/init_rest_of_the_world.jl | 24 ++++++- src/model_init/init_workers.jl | 50 +++++++++------ 7 files changed, 215 insertions(+), 101 deletions(-) diff --git a/src/model_init/init.jl b/src/model_init/init.jl index 7bd0ef2..b355b3e 100644 --- a/src/model_init/init.jl +++ b/src/model_init/init.jl @@ -24,48 +24,66 @@ function initialise_model(parameters::Dict{String, Any}, initial_conditions::Dic properties = BeforeIT.init_properties(parameters, T; typeInt = typeInt, typeFloat = typeFloat) # firms - firms = BeforeIT.init_firms(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) + firms, _ = BeforeIT.init_firms(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) # workers, and update firms vacancies - workers_act, workers_inact, V_i_new = BeforeIT.init_workers(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat) + workers_act, workers_inact, V_i_new, _, _ = BeforeIT.init_workers(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat) firms.V_i = V_i_new # bank - bank = BeforeIT.init_bank(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat) + bank, _ = BeforeIT.init_bank(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat) # central bank - central_bank = BeforeIT.init_central_bank(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) + central_bank, _ = BeforeIT.init_central_bank(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) # government - government = BeforeIT.init_government(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) + government, _ = BeforeIT.init_government(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) # rest of the world - rotw = BeforeIT.init_rotw(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) + rotw, _ = BeforeIT.init_rotw(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat) # aggregates - agg = BeforeIT.init_aggregates(parameters, initial_conditions, T; typeInt = typeInt, typeFloat = typeFloat) - - # obtain total income by summing contributions from firm owners, workers and bank owner - - tot_Y_h = sum(firms.Y_h) + sum(workers_act.Y_h) + sum(workers_inact.Y_h) + bank.Y_h - - # uptade K_h and D_h in all agent types - firms.K_h .= firms.K_h / tot_Y_h - firms.D_h .= firms.D_h / tot_Y_h - workers_act.K_h .= workers_act.K_h / tot_Y_h - workers_act.D_h .= workers_act.D_h / tot_Y_h - workers_inact.K_h .= workers_inact.K_h / tot_Y_h - workers_inact.D_h .= workers_inact.D_h / tot_Y_h - bank.K_h = bank.K_h / tot_Y_h - bank.D_h = bank.D_h / tot_Y_h - - # get total deposits and update bank balance sheet - tot_D_h = sum(firms.D_h) + sum(workers_act.D_h) + sum(workers_inact.D_h) + bank.D_h - bank.D_k += tot_D_h + agg, _ = BeforeIT.init_aggregates(parameters, initial_conditions, T; typeInt = typeInt, typeFloat = typeFloat) # model model = Model(workers_act, workers_inact, firms, bank, central_bank, government, rotw, agg, properties) + # update the model with global quantities (total income, total deposits) obtained from all the agents + update_variables_with_totals!(model) + return model end + +""" + update_variables_with_totals!(model::Model) + +Update the variables in the given `model` with some global quantities obtained from all agents. +This is the last step in the initialization process and it must be performed after all agents have been initialized. + +# Arguments +- `model::Model`: The model object to update. + +# Returns +- Nothing + +""" +function update_variables_with_totals!(model::Model) + + # obtain total income by summing contributions from firm owners, workers and bank owner + tot_Y_h = sum(model.firms.Y_h) + sum(model.w_act.Y_h) + sum(model.w_inact.Y_h) + model.bank.Y_h + + # uptade K_h and D_h in all agent types using total income + model.firms.K_h .= model.firms.K_h / tot_Y_h + model.firms.D_h .= model.firms.D_h / tot_Y_h + model.w_act.K_h .= model.w_act.K_h / tot_Y_h + model.w_act.D_h .= model.w_act.D_h / tot_Y_h + model.w_inact.K_h .= model.w_inact.K_h / tot_Y_h + model.w_inact.D_h .= model.w_inact.D_h / tot_Y_h + model.bank.K_h = model.bank.K_h / tot_Y_h + model.bank.D_h = model.bank.D_h / tot_Y_h + + # get total deposits and update bank balance sheet + tot_D_h = sum(model.firms.D_h) + sum(model.w_act.D_h) + sum(model.w_inact.D_h) + model.bank.D_h + model.bank.D_k += tot_D_h +end diff --git a/src/model_init/init_aggregates.jl b/src/model_init/init_aggregates.jl index 354fcdc..0332d53 100644 --- a/src/model_init/init_aggregates.jl +++ b/src/model_init/init_aggregates.jl @@ -1,5 +1,22 @@ +""" + init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typeFloat = Float64) + +Initialize aggregates for the model. + +# Arguments +- `parameters`: The model parameters. +- `initial_conditions`: The initial conditions. +- `T`: The total simulation time. +- `typeInt`: The integer type to use (default: `Int64`). +- `typeFloat`: The floating-point type to use (default: `Float64`). + +# Returns +- `agg`: The initialized aggregates. +- `agg_args`: The arguments used to initialize the aggregates. + +""" function init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typeFloat = Float64) Y = initial_conditions["Y"] @@ -25,7 +42,7 @@ function init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typ epsilon_E = zero(typeFloat) epsilon_I = zero(typeFloat) - agg = Aggregates( + agg_args = ( Y, pi_, P_bar, @@ -42,5 +59,8 @@ function init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typ epsilon_I, t, ) - return agg + + agg = Aggregates(agg_args...) + + return agg, agg_args end \ No newline at end of file diff --git a/src/model_init/init_banks.jl b/src/model_init/init_banks.jl index 5c26136..9669fe4 100644 --- a/src/model_init/init_banks.jl +++ b/src/model_init/init_banks.jl @@ -1,5 +1,22 @@ +""" + init_bank(parameters, initial_conditions, firms; typeInt = Int64, typeFloat = Float64) + +Initialize a bank with the given parameters, initial conditions, and firms. + +# Arguments +- `parameters`: The parameters. +- `initial_conditions`: The initial conditions. +- `firms`: The already initialized firms. +- `typeInt`: (optional) The integer type to use. Default is `Int64`. +- `typeFloat`: (optional) The floating-point type to use. Default is `Float64`. + +# Returns +- bank::Bank: The initialized bank. +- bank_args::Tuple: The arguments used to initialize the bank. + +""" function init_bank(parameters, initial_conditions, firms; typeInt = Int64, typeFloat = Float64) theta_DIV = parameters["theta_DIV"] @@ -32,12 +49,30 @@ function init_bank(parameters, initial_conditions, firms; typeInt = Int64, typeF K_h = K_h D_h = D_h Pi_e_k = typeFloat(0.0) - bank = Bank(E_k, Pi_k, Pi_e_k, D_k, r, Y_h, C_d_h, I_d_h, C_h, I_h, K_h, D_h) - return bank + bank_args = (E_k, Pi_k, Pi_e_k, D_k, r, Y_h, C_d_h, I_d_h, C_h, I_h, K_h, D_h) + bank = Bank(bank_args...) + + return bank, bank_args end +""" + init_central_bank(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) + +Initialize the central bank with the given parameters and initial conditions. + +# Arguments +- `parameters`: The parameters. +- `initial_conditions`: The initial conditions. +- `typeInt`: (optional) The integer type to be used. Default is `Int64`. +- `typeFloat`: (optional) The floating-point type to be used. Default is `Float64`. + +# Returns +- central_bank::CentralBank: The initialized central bank. +- cb_args::Tuple: The arguments used to initialize the central bank. + +""" function init_central_bank(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) r_bar = initial_conditions["r_bar"] r_G = parameters["r_G"] @@ -48,6 +83,8 @@ function init_central_bank(parameters, initial_conditions; typeInt = Int64, type xi_gamma = parameters["xi_gamma"] E_CB = initial_conditions["E_CB"] - central_bank = CentralBank(r_bar, r_G, rho, r_star, pi_star, xi_pi, xi_gamma, E_CB) -return central_bank + cb_args = (r_bar, r_G, rho, r_star, pi_star, xi_pi, xi_gamma, E_CB) + central_bank = CentralBank(cb_args...) + + return central_bank, cb_args end \ No newline at end of file diff --git a/src/model_init/init_firms.jl b/src/model_init/init_firms.jl index fd52e9d..a2689fc 100644 --- a/src/model_init/init_firms.jl +++ b/src/model_init/init_firms.jl @@ -1,4 +1,20 @@ +""" + init_firms(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) + +Initialize firms with given parameters and initial conditions. + +# Arguments +- `parameters`: The parameters for initializing the firms. +- `initial_conditions`: The initial conditions for the firms. +- `typeInt`: (optional) The integer type to be used. Default is `Int64`. +- `typeFloat`: (optional) The floating-point type to be used. Default is `Float64`. + +# Returns +- firms::Firms: The initialized firms. +- firms_args::Tuple: The arguments used to initialize the firms. + +""" function init_firms(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) # unpacking useful parameters @@ -97,51 +113,22 @@ function init_firms(parameters, initial_conditions; typeInt = Int64, typeFloat = K_h = K_H * Y_h # TODO: K_h[(H_W + H_inact + 1):(H_W + H_inact + I)] D_h = D_H * Y_h # TODO: D_h[(H_W + H_inact + 1):(H_W + H_inact + I)] - firms = Firms( - G_i, - alpha_bar_i, - beta_i, - kappa_i, - w_i, - w_bar_i, - delta_i, - tau_Y_i, - tau_K_i, - N_i, - Y_i, - Q_i, - Q_d_i, - P_i, - S_i, - K_i, - M_i, - L_i, - pi_bar_i, - D_i, - Pi_i, - V_i, - I_i, - E_i, - P_bar_i, - P_CF_i, - DS_i, - DM_i, - zeros(typeFloat, I), # DL_i - zeros(typeFloat, I), # DL_d_i - zeros(typeFloat, I), # K_e_i - zeros(typeFloat, I), # L_e_i - zeros(typeFloat, I), # Q_s_i - zeros(typeFloat, I), # I_d_i - zeros(typeFloat, I), # DM_d_i - zeros(typeInt, I), # N_d_i - zeros(typeFloat, I), # Pi_e_i - Y_h, - C_d_h, - I_d_h, - C_h, - I_h, - K_h, - D_h, - ) - return firms + # additional tracking variables initialised to zero + DL_i = zeros(typeFloat, I) + DL_d_i = zeros(typeFloat, I) + K_e_i = zeros(typeFloat, I) + L_e_i = zeros(typeFloat, I) + Q_s_i = zeros(typeFloat, I) + I_d_i = zeros(typeFloat, I) + DM_d_i = zeros(typeFloat, I) + N_d_i = zeros(typeInt, I) + Pi_e_i = zeros(typeFloat, I) + + + firms_args = (G_i, alpha_bar_i, beta_i, kappa_i, w_i, w_bar_i, delta_i, tau_Y_i, tau_K_i, N_i, Y_i, Q_i, Q_d_i, + P_i, S_i, K_i, M_i, L_i, pi_bar_i, D_i, Pi_i, V_i, I_i, E_i, P_bar_i, P_CF_i, DS_i, DM_i, DL_i, + DL_d_i, K_e_i, L_e_i, Q_s_i, I_d_i, DM_d_i, N_d_i, Pi_e_i, Y_h, C_d_h, I_d_h, C_h, I_h, K_h, D_h) + + firms = Firms(firms_args...) + return firms, firms_args end \ No newline at end of file diff --git a/src/model_init/init_government.jl b/src/model_init/init_government.jl index 941dd89..a422299 100644 --- a/src/model_init/init_government.jl +++ b/src/model_init/init_government.jl @@ -1,5 +1,21 @@ +""" + init_government(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) + +Initialize the government agent. + +# Arguments +- `parameters`: The parameters. +- `initial_conditions`: The initial conditions. +- `typeInt`: The integer type to be used (default: `Int64`). +- `typeFloat`: The floating-point type to be used (default: `Float64`). + +# Returns +- The initialized government model. +- The arguments used to initialize the government model. + +""" function init_government(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) alpha_G = parameters["alpha_G"] beta_G = parameters["beta_G"] @@ -16,8 +32,10 @@ function init_government(parameters, initial_conditions; typeInt = Int64, typeFl C_j = zero(typeFloat) P_j = zero(typeFloat) Y_G = zero(typeFloat) - government = - Government(alpha_G, beta_G, sigma_G, Y_G, C_G[T_prime], L_G, sb_inact, sb_other, C_d_j, C_j, P_j) + + gov_args = (alpha_G, beta_G, sigma_G, Y_G, C_G[T_prime], L_G, sb_inact, sb_other, C_d_j, C_j, P_j) + + government = Government(gov_args...) - return government + return government, gov_args end \ No newline at end of file diff --git a/src/model_init/init_rest_of_the_world.jl b/src/model_init/init_rest_of_the_world.jl index c996721..ee2e125 100644 --- a/src/model_init/init_rest_of_the_world.jl +++ b/src/model_init/init_rest_of_the_world.jl @@ -1,5 +1,21 @@ +""" + init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) + +Initialize the rest of the world (rotw) agent. + +# Arguments +- `parameters`: The parameters. +- `initial_conditions`: The initial conditions. +- `typeInt`: The integer type to be used (default: `Int64`). +- `typeFloat`: The floating-point type to be used (default: `Float64`). + +# Returns +- rotw::RestOfTheWorld: The initialized rest of the world agent. +- rotw_args::Tuple: The arguments used to initialize the rest of the world agent. + +""" function init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64) L = typeInt(parameters["L"]) G = typeInt(parameters["G"]) @@ -34,7 +50,8 @@ function init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat = Q_m = Vector{typeFloat}(zeros(G)) Q_d_m = Vector{typeFloat}(zeros(G)) P_m = Vector{typeFloat}(zeros(G)) - rotw = RestOfTheWorld( + + rotw_args = ( alpha_E, beta_E, sigma_E, @@ -61,5 +78,8 @@ function init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat = P_m, P_l, ) - return rotw + + rotw = RestOfTheWorld(rotw_args...) + + return rotw, rotw_args end diff --git a/src/model_init/init_workers.jl b/src/model_init/init_workers.jl index 9cc3de3..7118800 100644 --- a/src/model_init/init_workers.jl +++ b/src/model_init/init_workers.jl @@ -1,5 +1,25 @@ +""" + init_workers(parameters, initial_conditions, firms; typeInt = Int64, typeFloat = Float64) + +Initialize the workers for the given parameters, initial conditions, and firms. + +# Arguments +- `parameters`: The parameters for the initialization. +- `initial_conditions`: The initial conditions for the initialization. +- `firms`: The already initialized firms. +- `typeInt`: (optional) The type for integer values. Default is `Int64`. +- `typeFloat`: (optional) The type for floating-point values. Default is `Float64`. + +# Returns +- The initialized active workers. +- The initialized inactive workers. +- The updated firm vacancies V_i_new, this is needed to update the firms. +- The arguments used to initialize the active workers. +- The arguments used to initialize the inactive workers. + +""" function init_workers(parameters, initial_conditions, firms; typeInt = Int64, typeFloat = Float64) H_act = typeInt(parameters["H_act"]) @@ -15,11 +35,10 @@ function init_workers(parameters, initial_conditions, firms; typeInt = Int64, ty D_H = initial_conditions["D_H"] K_H = initial_conditions["K_H"] - H_W = H_act - I - 1 P_bar_HH = one(typeFloat) - w_h = zeros(typeFloat, 1, H_W) - O_h = zeros(typeInt, 1, H_W) + w_h = zeros(typeFloat, H_W) + O_h = zeros(typeInt, H_W) h = one(typeInt) @@ -37,7 +56,7 @@ function init_workers(parameters, initial_conditions, firms; typeInt = Int64, ty w_h[O_h .== 0] .= w_UB / theta_UB - Y_h = zeros(typeFloat, 1, H_W) + Y_h = zeros(typeFloat, H_W) for h in 1:H_W if O_h[h] != 0 @@ -55,8 +74,11 @@ function init_workers(parameters, initial_conditions, firms; typeInt = Int64, ty I_d_h = zeros(typeFloat, length(ids)) C_h = zeros(typeFloat, length(ids)) I_h = zeros(typeFloat, length(ids)) + + # active workers (both employed and unemployed) - workers_act = Workers(Y_h[1:H_W], D_h[1:H_W], K_h[1:H_W], w_h[1:H_W], O_h[1:H_W], C_d_h, I_d_h, C_h, I_h) + w_act_args = (Y_h, D_h, K_h, w_h, O_h, C_d_h, I_d_h, C_h, I_h) + workers_act = Workers(w_act_args...) # inactive workers ids = Vector{typeInt}((I + H_W + 1):(I + H_W + H_inact)) @@ -75,17 +97,9 @@ function init_workers(parameters, initial_conditions, firms; typeInt = Int64, ty I_d_h = zeros(typeFloat, length(ids)) C_h = zeros(typeFloat, length(ids)) I_h = zeros(typeFloat, length(ids)) - workers_inact = Workers( - Y_h, - D_h, - K_h, - w_h_inact, - O_h_inact, - C_d_h, - I_d_h, - C_h, - I_h, - ) - - return workers_act, workers_inact, V_i_new + + w_inact_args = (Y_h, D_h, K_h, w_h_inact, O_h_inact, C_d_h, I_d_h, C_h, I_h) + workers_inact = Workers(w_inact_args...) + + return workers_act, workers_inact, V_i_new, w_act_args, w_inact_args end