Skip to content

Commit b92982a

Browse files
committed
refactor: initialise_model -> init_model and initialise_data -> init_data
1 parent b398877 commit b92982a

22 files changed

+57
-44
lines changed

examples/basic_example.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ initial_conditions = Bit.AUSTRIA2010Q1.initial_conditions
1515
# We can now initialise the model, by specifying in advance the maximum number of epochs.
1616

1717
T = 16
18-
model = Bit.initialise_model(parameters, initial_conditions, T)
18+
model = Bit.init_model(parameters, initial_conditions, T)
1919

2020

2121
# Note that the it is very simple to inspect the model by typing
@@ -28,7 +28,7 @@ fieldnames(typeof(model.bank))
2828

2929
# We can now define a data tracker, which will store the time series of the model.
3030

31-
data = Bit.initialise_data(model);
31+
data = Bit.init_data(model);
3232

3333
# We can run now the model for a number of epochs and progressively update the data tracker.
3434

@@ -53,11 +53,12 @@ p7 = plot(data.wages, title = "wages", titlefont = 10)
5353
p8 = plot(data.euribor, title = "euribor", titlefont = 10)
5454
p9 = plot(data.nominal_gdp ./ data.real_gdp, title = "gdp deflator", titlefont = 10)
5555

56+
5657
plot(p1, p2, p3, p4, p5, p6, p7, p8, p9, layout = (3, 3), legend = false)
5758

5859
# To run multiple monte-carlo repetitions in parallel we can use
5960

60-
model = Bit.initialise_model(parameters, initial_conditions, T)
61+
model = Bit.init_model(parameters, initial_conditions, T)
6162
data_vector = Bit.run_n_sims(model, 4)
6263

6364
# Note that this will use the number of threads specified when activating the Julia environment.
@@ -96,3 +97,10 @@ p9 = errorline(
9697
titlefont = 10,
9798
)
9899
plot(p1, p2, p3, p4, p5, p6, p7, p8, p9, layout = (3, 3), legend = false)
100+
101+
plot(p1, p4, p5, p3, p8, p9, layout = (3, 2), legend = false, size = (400, 600), dpi = 300, left_margin = 3Plots.mm)
102+
103+
plot(p1, p4, p5, p3, p8, p9, layout = (2, 3), legend = false, size = (600, 400), dpi = 300)#, left_margin = 3Plots.mm)
104+
105+
106+
savefig("output.png")

examples/benchmark_w_matlab.jl

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ T = 12*2
1111

1212
# We will run the model without any output to avoid the overhead of printing the results.
1313
function run_no_output(;multi_threading = false)
14-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
15-
data = BeforeIT.initialise_data(model);
14+
model = BeforeIT.init_model(parameters, initial_conditions, T)
15+
data = BeforeIT.init_data(model);
1616

1717
for _ in 1:T
1818
BeforeIT.one_epoch!(model; multi_threading = multi_threading)
@@ -25,7 +25,7 @@ end
2525
@time run_no_output(;multi_threading = true)
2626

2727
# time taken by the MATLAB code, computed independently on an Apple M1 chip
28-
matlab_times = [3.1465, 3.0795, 3.0274, 3.0345, 3.0873]
28+
matlab_times = [3.1919, 3.2454, 3.1501, 3.1074, 3.1551]
2929
matlab_time = mean(matlab_times)
3030
matlab_time_std = std(matlab_times)
3131

@@ -50,11 +50,16 @@ julia_time_multi_thread_std = std(julia_times_multi_thread)
5050
# get the number of threads used
5151
n_threads = Threads.nthreads()
5252

53+
theme(:default, bg = :white)
5354
# bar chart of the time taken vs the time taken by the MATLAB code, also plot the stds as error bars
54-
bar(["MATLAB", "Julia, 1 thread", "Julia, $n_threads threads"], [matlab_time, julia_time_1_thread, julia_time_multi_thread], yerr = [matlab_time_std, julia_time_1_thread_std, julia_time_multi_thread_std], legend = false, dpi=300)
55-
ylabel!("Time for one simulation (s)")
55+
# make a white background with no grid
56+
bar(["MATLAB", "Julia, 1 thread", "Julia, $n_threads threads"], [matlab_time, julia_time_1_thread, julia_time_multi_thread],
57+
yerr = [matlab_time_std, julia_time_1_thread_std, julia_time_multi_thread_std],
58+
legend = false, dpi=300, size=(400, 300), grid = false, ylabel = "Time for one simulation (s)")
5659

5760
# the Julia implementation is faster than the MATLAB implementation, and the multi-threaded version is faster than the single-threaded version.
5861

62+
# increase
63+
5964
# save the image
6065
savefig("benchmark_w_matlab.png")

examples/change_expectations.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ init = Bit.AUSTRIA2010Q1.initial_conditions
1515

1616
Random.seed!(1234)
1717
T = 40
18-
model = Bit.initialise_model(par, init, T)
18+
model = Bit.init_model(par, init, T)
1919
data = Bit.run_one_sim!(model)
2020

2121
# Now we can experiment with changing expectations of the agents in the model.
@@ -30,7 +30,7 @@ end
3030
# run the model again, with the same seed
3131

3232
Random.seed!(1234)
33-
model = Bit.initialise_model(par, init, T)
33+
model = Bit.init_model(par, init, T)
3434
data_back = Bit.run_one_sim!(model)
3535

3636
# plot the results, comparing the two cases as different lines

examples/get_simulations.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ for year in 2010:2019
1212
parameters = load("data/italy/parameters/" * string(year) * "Q" * string(quarter) * ".jld2")
1313
initial_conditions = load("data/italy/initial_conditions/" * string(year) * "Q" * string(quarter) * ".jld2")
1414
T = 12
15-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
15+
model = BeforeIT.init_model(parameters, initial_conditions, T)
1616
n_sims = 4
1717
data_vector = BeforeIT.run_n_sims(model, n_sims)
1818
save("data/italy/simulations/" * string(year) * "Q" * string(quarter) * ".jld2", "data_vector", data_vector)

examples/multithreading_speedup.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using FileIO, Plots, StatsPlots
1313
parameters = Bit.ITALY2010Q1.parameters
1414
initial_conditions = Bit.ITALY2010Q1.initial_conditions
1515
T = 50
16-
model = Bit.initialise_model(parameters, initial_conditions, T);
16+
model = Bit.init_model(parameters, initial_conditions, T);
1717

1818

1919
# The model is in scale 1:2000, so it has around 30,000 households
@@ -31,7 +31,7 @@ println(Threads.nthreads())
3131

3232
@time data = Bit.run_one_sim!(model; multi_threading = false);
3333

34-
model = Bit.initialise_model(parameters, initial_conditions, T);
34+
model = Bit.init_model(parameters, initial_conditions, T);
3535
@time data = Bit.run_one_sim!(model; multi_threading = true);
3636

3737
# Is the speedup in line to what we would expect?

examples/scenario_analysis_via_overload.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ initial_conditions = Bit.AUSTRIA2010Q1.initial_conditions
1212

1313
# initialise the model and the data collector
1414
T = 20
15-
model = Bit.initialise_model(parameters, initial_conditions, T);
16-
data = Bit.initialise_data(model);
15+
model = Bit.init_model(parameters, initial_conditions, T);
16+
data = Bit.init_data(model);
1717

1818
# Simulate the model for T quarters
1919
data_vec_baseline = Bit.run_n_sims(model, 4)

examples/scenario_analysis_via_shock.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ initial_conditions = Bit.AUSTRIA2010Q1.initial_conditions
1212

1313
# initialise the model and the data collector
1414
T = 20
15-
model = Bit.initialise_model(parameters, initial_conditions, T);
15+
model = Bit.init_model(parameters, initial_conditions, T);
1616

1717
# Simulate the model for T quarters
1818
data_vec_baseline = Bit.run_n_sims(model, 4)

main.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ parameters = BeforeIT.AUSTRIA2010Q1.parameters
55
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
66

77
T = 20
8-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
9-
data = BeforeIT.initialise_data(model)
8+
model = BeforeIT.init_model(parameters, initial_conditions, T)
9+
data = BeforeIT.init_data(model)
1010

1111
for t in 1:T
1212
println("Epoch: ", t)

src/model_init/init.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ recursive_namedtuple(x::Any) = x
33
recursive_namedtuple(d::Dict) = MutableNamedTuple(; Dict(k => recursive_namedtuple(v) for (k, v) in d)...)
44

55
"""
6-
initialise_model(parameters, initial_conditions, T, typeInt = Int64, typeFloat = Float64)
6+
init_model(parameters, initial_conditions, T, typeInt = Int64, typeFloat = Float64)
77
88
Initializes the model with given parameters and initial conditions.
99
@@ -18,7 +18,7 @@ Returns:
1818
- model::Model: The initialized model.
1919
2020
"""
21-
function initialise_model(parameters::Dict{String, Any}, initial_conditions::Dict{String, Any}, T, typeInt::DataType = Int64, typeFloat::DataType = Float64)
21+
function init_model(parameters::Dict{String, Any}, initial_conditions::Dict{String, Any}, T, typeInt::DataType = Int64, typeFloat::DataType = Float64)
2222

2323
# properties
2424
properties = BeforeIT.init_properties(parameters, T; typeInt = typeInt, typeFloat = typeFloat)

src/one_simulation.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The simulation runs for a number of epochs specified by `model.prop.T`.
1111
- `data::Data`: The data collected during the simulation.
1212
1313
# Details
14-
The function initializes the data using `BeforeIT.initialise_data(model)`, then iteratively updates the model and data
14+
The function initializes the data using `BeforeIT.init_data(model)`, then iteratively updates the model and data
1515
for each epoch using `BeforeIT.one_epoch!(model)` and `BeforeIT.update_data!(data, model)` respectively.
1616
1717
# Example
@@ -21,7 +21,7 @@ data = run_one_sim!(model)
2121
"""
2222
function run_one_sim!(model; multi_threading = false, shock = NoShock())
2323

24-
data = BeforeIT.initialise_data(model)
24+
data = BeforeIT.init_data(model)
2525

2626
T = model.prop.T
2727

src/utils/data.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ end
3232
"""
3333
Initialise the data arrays
3434
"""
35-
function initialise_data(m)
35+
function init_data(m)
3636
p = m.prop
3737
T = p.T
3838
d = Data([zeros(T + 1) for _ in 1:25]..., zeros(T + 1, p.G), zeros(T + 1, p.G))

test/accounting_identities.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ using Test
88
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
99

1010
T = 1
11-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
12-
data = BeforeIT.initialise_data(model)
11+
model = BeforeIT.init_model(parameters, initial_conditions, T)
12+
data = BeforeIT.init_data(model)
1313

1414
for t in 1:T
1515
BeforeIT.one_epoch!(model; multi_threading = false)

test/deterministic/deterministic_ouput_t1_t5.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
T = 1
55
parameters = BeforeIT.AUSTRIA2010Q1.parameters
66
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
7-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
8-
data = BeforeIT.initialise_data(model)
7+
model = BeforeIT.init_model(parameters, initial_conditions, T)
8+
data = BeforeIT.init_data(model)
99

1010
BeforeIT.one_epoch!(model; multi_threading = false)
1111
BeforeIT.update_data!(data, model)
@@ -30,8 +30,8 @@
3030
T = 5
3131
parameters = BeforeIT.AUSTRIA2010Q1.parameters
3232
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
33-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
34-
data = BeforeIT.initialise_data(model)
33+
model = BeforeIT.init_model(parameters, initial_conditions, T)
34+
data = BeforeIT.init_data(model)
3535
for t in 1:T
3636
BeforeIT.one_epoch!(model; multi_threading = false)
3737
BeforeIT.update_data!(data, model)

test/deterministic/initialize_deterministic.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
parameters = BeforeIT.AUSTRIA2010Q1.parameters
66
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
7-
model = BeforeIT.initialise_model(parameters, initial_conditions, 1)
7+
model = BeforeIT.init_model(parameters, initial_conditions, 1)
88

99
properties = model.prop
1010

test/deterministic/one_epoch_deterministic.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
66

77
T = 1
8-
model = BeforeIT.initialise_model(parameters, initial_conditions, T;)
8+
model = BeforeIT.init_model(parameters, initial_conditions, T;)
99

1010
gov = model.gov # government
1111
cb = model.cb # central bank

test/deterministic/one_run_deterministic.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
initial_conditions1 = deepcopy(initial_conditions)
1212
initial_conditions2 = deepcopy(initial_conditions)
1313

14-
model = BeforeIT.initialise_model(parameters1, initial_conditions1, T;)
15-
data = BeforeIT.initialise_data(model)
14+
model = BeforeIT.init_model(parameters1, initial_conditions1, T;)
15+
data = BeforeIT.init_data(model)
1616
for t in 1:(T - 1)
1717
BeforeIT.one_epoch!(model; multi_threading = false)
1818
BeforeIT.update_data!(data, model)
1919
end
2020

2121

22-
model2 = BeforeIT.initialise_model(parameters2, initial_conditions2, T;)
23-
data2 = BeforeIT.initialise_data(model2)
22+
model2 = BeforeIT.init_model(parameters2, initial_conditions2, T;)
23+
data2 = BeforeIT.init_data(model2)
2424

2525
for t in 1:(T - 1)
2626
BeforeIT.one_epoch!(model2; multi_threading = false)

test/markets/search_and_matching.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using Random
1010
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
1111

1212
T = 1
13-
model = BeforeIT.initialise_model(parameters, initial_conditions, T;)
13+
model = BeforeIT.init_model(parameters, initial_conditions, T;)
1414

1515

1616
gov = model.gov # government

test/model_init/initialise_model.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dir = @__DIR__
55

66
parameters = BeforeIT.STEADY_STATE2010Q1.parameters
77
initial_conditions = BeforeIT.STEADY_STATE2010Q1.initial_conditions
8-
model = BeforeIT.initialise_model(parameters, initial_conditions, 1)
8+
model = BeforeIT.init_model(parameters, initial_conditions, 1)
99

1010
properties = model.prop
1111

test/monte_carlo_evaluations.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ parameters = matread(joinpath(dir, "../data/austria/parameters/2010Q1.mat"))
66
initial_conditions = matread(joinpath(dir, "../data/austria/initial_conditions/2010Q1.mat"))
77

88
T = 20
9-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
10-
data = BeforeIT.initialise_data(model)
9+
model = BeforeIT.init_model(parameters, initial_conditions, T)
10+
data = BeforeIT.init_data(model)
1111

1212
n_sims = 3
1313
data_vector = BeforeIT.run_n_sims(model, n_sims)

test/one_epoch_consistency.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ parameters = matread(joinpath(dir, "../data/steady_state/parameters/2010Q1.mat")
99
initial_conditions = matread(joinpath(dir, "../data/steady_state/initial_conditions/2010Q1.mat"))
1010

1111
T = 1
12-
model = BeforeIT.initialise_model(parameters, initial_conditions, T;)
13-
data = BeforeIT.initialise_data(model)
12+
model = BeforeIT.init_model(parameters, initial_conditions, T;)
13+
data = BeforeIT.init_data(model)
1414

1515

1616
println(BeforeIT.get_accounting_identity_banks(model))

test/steady_state.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ parameters = BeforeIT.STEADY_STATE2010Q1.parameters
55
initial_conditions = BeforeIT.STEADY_STATE2010Q1.initial_conditions
66

77
T = 5
8-
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
9-
data = BeforeIT.initialise_data(model)
8+
model = BeforeIT.init_model(parameters, initial_conditions, T)
9+
data = BeforeIT.init_data(model)
1010

1111
for t in 1:T
1212
println(t)

test/utils/estimations.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using MAT, Random
66

77
parameters = BeforeIT.AUSTRIA2010Q1.parameters
88
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
9-
model = BeforeIT.initialise_model(parameters, initial_conditions, 1)
9+
model = BeforeIT.init_model(parameters, initial_conditions, 1)
1010

1111

1212

0 commit comments

Comments
 (0)