Skip to content

Commit

Permalink
make init functions return init arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
AldoGl committed Aug 14, 2024
1 parent 2224afb commit 61db895
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 101 deletions.
68 changes: 43 additions & 25 deletions src/model_init/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 22 additions & 2 deletions src/model_init/init_aggregates.jl
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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,
Expand All @@ -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
45 changes: 41 additions & 4 deletions src/model_init/init_banks.jl
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -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"]
Expand All @@ -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
81 changes: 34 additions & 47 deletions src/model_init/init_firms.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
24 changes: 21 additions & 3 deletions src/model_init/init_government.jl
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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
24 changes: 22 additions & 2 deletions src/model_init/init_rest_of_the_world.jl
Original file line number Diff line number Diff line change
@@ -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"])
Expand Down Expand Up @@ -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,
Expand All @@ -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
Loading

0 comments on commit 61db895

Please sign in to comment.