Skip to content

Commit b398877

Browse files
committed
make init functions return init arguments
1 parent 2224afb commit b398877

File tree

7 files changed

+215
-101
lines changed

7 files changed

+215
-101
lines changed

src/model_init/init.jl

+43-25
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,66 @@ function initialise_model(parameters::Dict{String, Any}, initial_conditions::Dic
2424
properties = BeforeIT.init_properties(parameters, T; typeInt = typeInt, typeFloat = typeFloat)
2525

2626
# firms
27-
firms = BeforeIT.init_firms(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
27+
firms, _ = BeforeIT.init_firms(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
2828

2929
# workers, and update firms vacancies
30-
workers_act, workers_inact, V_i_new = BeforeIT.init_workers(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat)
30+
workers_act, workers_inact, V_i_new, _, _ = BeforeIT.init_workers(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat)
3131
firms.V_i = V_i_new
3232

3333
# bank
34-
bank = BeforeIT.init_bank(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat)
34+
bank, _ = BeforeIT.init_bank(parameters, initial_conditions, firms; typeInt = typeInt, typeFloat = typeFloat)
3535

3636
# central bank
37-
central_bank = BeforeIT.init_central_bank(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
37+
central_bank, _ = BeforeIT.init_central_bank(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
3838

3939
# government
40-
government = BeforeIT.init_government(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
40+
government, _ = BeforeIT.init_government(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
4141

4242
# rest of the world
43-
rotw = BeforeIT.init_rotw(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
43+
rotw, _ = BeforeIT.init_rotw(parameters, initial_conditions; typeInt = typeInt, typeFloat = typeFloat)
4444

4545
# aggregates
46-
agg = BeforeIT.init_aggregates(parameters, initial_conditions, T; typeInt = typeInt, typeFloat = typeFloat)
47-
48-
# obtain total income by summing contributions from firm owners, workers and bank owner
49-
50-
tot_Y_h = sum(firms.Y_h) + sum(workers_act.Y_h) + sum(workers_inact.Y_h) + bank.Y_h
51-
52-
# uptade K_h and D_h in all agent types
53-
firms.K_h .= firms.K_h / tot_Y_h
54-
firms.D_h .= firms.D_h / tot_Y_h
55-
workers_act.K_h .= workers_act.K_h / tot_Y_h
56-
workers_act.D_h .= workers_act.D_h / tot_Y_h
57-
workers_inact.K_h .= workers_inact.K_h / tot_Y_h
58-
workers_inact.D_h .= workers_inact.D_h / tot_Y_h
59-
bank.K_h = bank.K_h / tot_Y_h
60-
bank.D_h = bank.D_h / tot_Y_h
61-
62-
# get total deposits and update bank balance sheet
63-
tot_D_h = sum(firms.D_h) + sum(workers_act.D_h) + sum(workers_inact.D_h) + bank.D_h
64-
bank.D_k += tot_D_h
46+
agg, _ = BeforeIT.init_aggregates(parameters, initial_conditions, T; typeInt = typeInt, typeFloat = typeFloat)
6547

6648
# model
6749
model = Model(workers_act, workers_inact, firms, bank, central_bank, government, rotw, agg, properties)
6850

51+
# update the model with global quantities (total income, total deposits) obtained from all the agents
52+
update_variables_with_totals!(model)
53+
6954
return model
7055

7156
end
57+
58+
"""
59+
update_variables_with_totals!(model::Model)
60+
61+
Update the variables in the given `model` with some global quantities obtained from all agents.
62+
This is the last step in the initialization process and it must be performed after all agents have been initialized.
63+
64+
# Arguments
65+
- `model::Model`: The model object to update.
66+
67+
# Returns
68+
- Nothing
69+
70+
"""
71+
function update_variables_with_totals!(model::Model)
72+
73+
# obtain total income by summing contributions from firm owners, workers and bank owner
74+
tot_Y_h = sum(model.firms.Y_h) + sum(model.w_act.Y_h) + sum(model.w_inact.Y_h) + model.bank.Y_h
75+
76+
# uptade K_h and D_h in all agent types using total income
77+
model.firms.K_h .= model.firms.K_h / tot_Y_h
78+
model.firms.D_h .= model.firms.D_h / tot_Y_h
79+
model.w_act.K_h .= model.w_act.K_h / tot_Y_h
80+
model.w_act.D_h .= model.w_act.D_h / tot_Y_h
81+
model.w_inact.K_h .= model.w_inact.K_h / tot_Y_h
82+
model.w_inact.D_h .= model.w_inact.D_h / tot_Y_h
83+
model.bank.K_h = model.bank.K_h / tot_Y_h
84+
model.bank.D_h = model.bank.D_h / tot_Y_h
85+
86+
# get total deposits and update bank balance sheet
87+
tot_D_h = sum(model.firms.D_h) + sum(model.w_act.D_h) + sum(model.w_inact.D_h) + model.bank.D_h
88+
model.bank.D_k += tot_D_h
89+
end

src/model_init/init_aggregates.jl

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11

22

3+
"""
4+
init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typeFloat = Float64)
5+
6+
Initialize aggregates for the model.
7+
8+
# Arguments
9+
- `parameters`: The model parameters.
10+
- `initial_conditions`: The initial conditions.
11+
- `T`: The total simulation time.
12+
- `typeInt`: The integer type to use (default: `Int64`).
13+
- `typeFloat`: The floating-point type to use (default: `Float64`).
14+
15+
# Returns
16+
- `agg`: The initialized aggregates.
17+
- `agg_args`: The arguments used to initialize the aggregates.
18+
19+
"""
320
function init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typeFloat = Float64)
421

522
Y = initial_conditions["Y"]
@@ -25,7 +42,7 @@ function init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typ
2542
epsilon_E = zero(typeFloat)
2643
epsilon_I = zero(typeFloat)
2744

28-
agg = Aggregates(
45+
agg_args = (
2946
Y,
3047
pi_,
3148
P_bar,
@@ -42,5 +59,8 @@ function init_aggregates(parameters, initial_conditions, T; typeInt = Int64, typ
4259
epsilon_I,
4360
t,
4461
)
45-
return agg
62+
63+
agg = Aggregates(agg_args...)
64+
65+
return agg, agg_args
4666
end

src/model_init/init_banks.jl

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11

22

3+
"""
4+
init_bank(parameters, initial_conditions, firms; typeInt = Int64, typeFloat = Float64)
5+
6+
Initialize a bank with the given parameters, initial conditions, and firms.
7+
8+
# Arguments
9+
- `parameters`: The parameters.
10+
- `initial_conditions`: The initial conditions.
11+
- `firms`: The already initialized firms.
12+
- `typeInt`: (optional) The integer type to use. Default is `Int64`.
13+
- `typeFloat`: (optional) The floating-point type to use. Default is `Float64`.
14+
15+
# Returns
16+
- bank::Bank: The initialized bank.
17+
- bank_args::Tuple: The arguments used to initialize the bank.
18+
19+
"""
320
function init_bank(parameters, initial_conditions, firms; typeInt = Int64, typeFloat = Float64)
421

522
theta_DIV = parameters["theta_DIV"]
@@ -32,12 +49,30 @@ function init_bank(parameters, initial_conditions, firms; typeInt = Int64, typeF
3249
K_h = K_h
3350
D_h = D_h
3451
Pi_e_k = typeFloat(0.0)
35-
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)
3652

37-
return bank
53+
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)
54+
bank = Bank(bank_args...)
55+
56+
return bank, bank_args
3857
end
3958

4059

60+
"""
61+
init_central_bank(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
62+
63+
Initialize the central bank with the given parameters and initial conditions.
64+
65+
# Arguments
66+
- `parameters`: The parameters.
67+
- `initial_conditions`: The initial conditions.
68+
- `typeInt`: (optional) The integer type to be used. Default is `Int64`.
69+
- `typeFloat`: (optional) The floating-point type to be used. Default is `Float64`.
70+
71+
# Returns
72+
- central_bank::CentralBank: The initialized central bank.
73+
- cb_args::Tuple: The arguments used to initialize the central bank.
74+
75+
"""
4176
function init_central_bank(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
4277
r_bar = initial_conditions["r_bar"]
4378
r_G = parameters["r_G"]
@@ -48,6 +83,8 @@ function init_central_bank(parameters, initial_conditions; typeInt = Int64, type
4883
xi_gamma = parameters["xi_gamma"]
4984
E_CB = initial_conditions["E_CB"]
5085

51-
central_bank = CentralBank(r_bar, r_G, rho, r_star, pi_star, xi_pi, xi_gamma, E_CB)
52-
return central_bank
86+
cb_args = (r_bar, r_G, rho, r_star, pi_star, xi_pi, xi_gamma, E_CB)
87+
central_bank = CentralBank(cb_args...)
88+
89+
return central_bank, cb_args
5390
end

src/model_init/init_firms.jl

+34-47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11

2+
"""
3+
init_firms(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
4+
5+
Initialize firms with given parameters and initial conditions.
6+
7+
# Arguments
8+
- `parameters`: The parameters for initializing the firms.
9+
- `initial_conditions`: The initial conditions for the firms.
10+
- `typeInt`: (optional) The integer type to be used. Default is `Int64`.
11+
- `typeFloat`: (optional) The floating-point type to be used. Default is `Float64`.
12+
13+
# Returns
14+
- firms::Firms: The initialized firms.
15+
- firms_args::Tuple: The arguments used to initialize the firms.
16+
17+
"""
218
function init_firms(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
319

420
# unpacking useful parameters
@@ -97,51 +113,22 @@ function init_firms(parameters, initial_conditions; typeInt = Int64, typeFloat =
97113
K_h = K_H * Y_h # TODO: K_h[(H_W + H_inact + 1):(H_W + H_inact + I)]
98114
D_h = D_H * Y_h # TODO: D_h[(H_W + H_inact + 1):(H_W + H_inact + I)]
99115

100-
firms = Firms(
101-
G_i,
102-
alpha_bar_i,
103-
beta_i,
104-
kappa_i,
105-
w_i,
106-
w_bar_i,
107-
delta_i,
108-
tau_Y_i,
109-
tau_K_i,
110-
N_i,
111-
Y_i,
112-
Q_i,
113-
Q_d_i,
114-
P_i,
115-
S_i,
116-
K_i,
117-
M_i,
118-
L_i,
119-
pi_bar_i,
120-
D_i,
121-
Pi_i,
122-
V_i,
123-
I_i,
124-
E_i,
125-
P_bar_i,
126-
P_CF_i,
127-
DS_i,
128-
DM_i,
129-
zeros(typeFloat, I), # DL_i
130-
zeros(typeFloat, I), # DL_d_i
131-
zeros(typeFloat, I), # K_e_i
132-
zeros(typeFloat, I), # L_e_i
133-
zeros(typeFloat, I), # Q_s_i
134-
zeros(typeFloat, I), # I_d_i
135-
zeros(typeFloat, I), # DM_d_i
136-
zeros(typeInt, I), # N_d_i
137-
zeros(typeFloat, I), # Pi_e_i
138-
Y_h,
139-
C_d_h,
140-
I_d_h,
141-
C_h,
142-
I_h,
143-
K_h,
144-
D_h,
145-
)
146-
return firms
116+
# additional tracking variables initialised to zero
117+
DL_i = zeros(typeFloat, I)
118+
DL_d_i = zeros(typeFloat, I)
119+
K_e_i = zeros(typeFloat, I)
120+
L_e_i = zeros(typeFloat, I)
121+
Q_s_i = zeros(typeFloat, I)
122+
I_d_i = zeros(typeFloat, I)
123+
DM_d_i = zeros(typeFloat, I)
124+
N_d_i = zeros(typeInt, I)
125+
Pi_e_i = zeros(typeFloat, I)
126+
127+
128+
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,
129+
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,
130+
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)
131+
132+
firms = Firms(firms_args...)
133+
return firms, firms_args
147134
end

src/model_init/init_government.jl

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11

22

3+
"""
4+
init_government(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
5+
6+
Initialize the government agent.
7+
8+
# Arguments
9+
- `parameters`: The parameters.
10+
- `initial_conditions`: The initial conditions.
11+
- `typeInt`: The integer type to be used (default: `Int64`).
12+
- `typeFloat`: The floating-point type to be used (default: `Float64`).
13+
14+
# Returns
15+
- The initialized government model.
16+
- The arguments used to initialize the government model.
17+
18+
"""
319
function init_government(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
420
alpha_G = parameters["alpha_G"]
521
beta_G = parameters["beta_G"]
@@ -16,8 +32,10 @@ function init_government(parameters, initial_conditions; typeInt = Int64, typeFl
1632
C_j = zero(typeFloat)
1733
P_j = zero(typeFloat)
1834
Y_G = zero(typeFloat)
19-
government =
20-
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)
35+
36+
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)
37+
38+
government = Government(gov_args...)
2139

22-
return government
40+
return government, gov_args
2341
end

src/model_init/init_rest_of_the_world.jl

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11

22

3+
"""
4+
init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
5+
6+
Initialize the rest of the world (rotw) agent.
7+
8+
# Arguments
9+
- `parameters`: The parameters.
10+
- `initial_conditions`: The initial conditions.
11+
- `typeInt`: The integer type to be used (default: `Int64`).
12+
- `typeFloat`: The floating-point type to be used (default: `Float64`).
13+
14+
# Returns
15+
- rotw::RestOfTheWorld: The initialized rest of the world agent.
16+
- rotw_args::Tuple: The arguments used to initialize the rest of the world agent.
17+
18+
"""
319
function init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat = Float64)
420
L = typeInt(parameters["L"])
521
G = typeInt(parameters["G"])
@@ -34,7 +50,8 @@ function init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat =
3450
Q_m = Vector{typeFloat}(zeros(G))
3551
Q_d_m = Vector{typeFloat}(zeros(G))
3652
P_m = Vector{typeFloat}(zeros(G))
37-
rotw = RestOfTheWorld(
53+
54+
rotw_args = (
3855
alpha_E,
3956
beta_E,
4057
sigma_E,
@@ -61,5 +78,8 @@ function init_rotw(parameters, initial_conditions; typeInt = Int64, typeFloat =
6178
P_m,
6279
P_l,
6380
)
64-
return rotw
81+
82+
rotw = RestOfTheWorld(rotw_args...)
83+
84+
return rotw, rotw_args
6585
end

0 commit comments

Comments
 (0)