Skip to content

Commit

Permalink
refactor: initialise_model -> init_model and initialise_data -> init_…
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
AldoGl committed Aug 14, 2024
1 parent 61db895 commit 7c3d889
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 44 deletions.
14 changes: 11 additions & 3 deletions examples/basic_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ initial_conditions = Bit.AUSTRIA2010Q1.initial_conditions
# We can now initialise the model, by specifying in advance the maximum number of epochs.

T = 16
model = Bit.initialise_model(parameters, initial_conditions, T)
model = Bit.init_model(parameters, initial_conditions, T)


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

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

data = Bit.initialise_data(model);
data = Bit.init_data(model);

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

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


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

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

model = Bit.initialise_model(parameters, initial_conditions, T)
model = Bit.init_model(parameters, initial_conditions, T)
data_vector = Bit.run_n_sims(model, 4)

# Note that this will use the number of threads specified when activating the Julia environment.
Expand Down Expand Up @@ -96,3 +97,10 @@ p9 = errorline(
titlefont = 10,
)
plot(p1, p2, p3, p4, p5, p6, p7, p8, p9, layout = (3, 3), legend = false)

plot(p1, p4, p5, p3, p8, p9, layout = (3, 2), legend = false, size = (400, 600), dpi = 300, left_margin = 3Plots.mm)

plot(p1, p4, p5, p3, p8, p9, layout = (2, 3), legend = false, size = (600, 400), dpi = 300)#, left_margin = 3Plots.mm)


savefig("output.png")
15 changes: 10 additions & 5 deletions examples/benchmark_w_matlab.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ T = 12*2

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

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

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

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

theme(:default, bg = :white)
# bar chart of the time taken vs the time taken by the MATLAB code, also plot the stds as error bars
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)
ylabel!("Time for one simulation (s)")
# make a white background with no grid
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, size=(400, 300), grid = false, ylabel = "Time for one simulation (s)")

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

# increase

# save the image
savefig("benchmark_w_matlab.png")
4 changes: 2 additions & 2 deletions examples/change_expectations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ init = Bit.AUSTRIA2010Q1.initial_conditions

Random.seed!(1234)
T = 40
model = Bit.initialise_model(par, init, T)
model = Bit.init_model(par, init, T)
data = Bit.run_one_sim!(model)

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

Random.seed!(1234)
model = Bit.initialise_model(par, init, T)
model = Bit.init_model(par, init, T)
data_back = Bit.run_one_sim!(model)

# plot the results, comparing the two cases as different lines
Expand Down
2 changes: 1 addition & 1 deletion examples/get_simulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ for year in 2010:2019
parameters = load("data/italy/parameters/" * string(year) * "Q" * string(quarter) * ".jld2")
initial_conditions = load("data/italy/initial_conditions/" * string(year) * "Q" * string(quarter) * ".jld2")
T = 12
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
model = BeforeIT.init_model(parameters, initial_conditions, T)
n_sims = 4
data_vector = BeforeIT.run_n_sims(model, n_sims)
save("data/italy/simulations/" * string(year) * "Q" * string(quarter) * ".jld2", "data_vector", data_vector)
Expand Down
4 changes: 2 additions & 2 deletions examples/multithreading_speedup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using FileIO, Plots, StatsPlots
parameters = Bit.ITALY2010Q1.parameters
initial_conditions = Bit.ITALY2010Q1.initial_conditions
T = 50
model = Bit.initialise_model(parameters, initial_conditions, T);
model = Bit.init_model(parameters, initial_conditions, T);


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

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

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

# Is the speedup in line to what we would expect?
4 changes: 2 additions & 2 deletions examples/scenario_analysis_via_overload.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ initial_conditions = Bit.AUSTRIA2010Q1.initial_conditions

# initialise the model and the data collector
T = 20
model = Bit.initialise_model(parameters, initial_conditions, T);
data = Bit.initialise_data(model);
model = Bit.init_model(parameters, initial_conditions, T);
data = Bit.init_data(model);

# Simulate the model for T quarters
data_vec_baseline = Bit.run_n_sims(model, 4)
Expand Down
2 changes: 1 addition & 1 deletion examples/scenario_analysis_via_shock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ initial_conditions = Bit.AUSTRIA2010Q1.initial_conditions

# initialise the model and the data collector
T = 20
model = Bit.initialise_model(parameters, initial_conditions, T);
model = Bit.init_model(parameters, initial_conditions, T);

# Simulate the model for T quarters
data_vec_baseline = Bit.run_n_sims(model, 4)
Expand Down
4 changes: 2 additions & 2 deletions main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

T = 20
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
data = BeforeIT.initialise_data(model)
model = BeforeIT.init_model(parameters, initial_conditions, T)
data = BeforeIT.init_data(model)

for t in 1:T
println("Epoch: ", t)
Expand Down
4 changes: 2 additions & 2 deletions src/model_init/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ recursive_namedtuple(x::Any) = x
recursive_namedtuple(d::Dict) = MutableNamedTuple(; Dict(k => recursive_namedtuple(v) for (k, v) in d)...)

"""
initialise_model(parameters, initial_conditions, T, typeInt = Int64, typeFloat = Float64)
init_model(parameters, initial_conditions, T, typeInt = Int64, typeFloat = Float64)
Initializes the model with given parameters and initial conditions.
Expand All @@ -18,7 +18,7 @@ Returns:
- model::Model: The initialized model.
"""
function initialise_model(parameters::Dict{String, Any}, initial_conditions::Dict{String, Any}, T, typeInt::DataType = Int64, typeFloat::DataType = Float64)
function init_model(parameters::Dict{String, Any}, initial_conditions::Dict{String, Any}, T, typeInt::DataType = Int64, typeFloat::DataType = Float64)

# properties
properties = BeforeIT.init_properties(parameters, T; typeInt = typeInt, typeFloat = typeFloat)
Expand Down
4 changes: 2 additions & 2 deletions src/one_simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The simulation runs for a number of epochs specified by `model.prop.T`.
- `data::Data`: The data collected during the simulation.
# Details
The function initializes the data using `BeforeIT.initialise_data(model)`, then iteratively updates the model and data
The function initializes the data using `BeforeIT.init_data(model)`, then iteratively updates the model and data
for each epoch using `BeforeIT.one_epoch!(model)` and `BeforeIT.update_data!(data, model)` respectively.
# Example
Expand All @@ -21,7 +21,7 @@ data = run_one_sim!(model)
"""
function run_one_sim!(model; multi_threading = false, shock = NoShock())

data = BeforeIT.initialise_data(model)
data = BeforeIT.init_data(model)

T = model.prop.T

Expand Down
2 changes: 1 addition & 1 deletion src/utils/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end
"""
Initialise the data arrays
"""
function initialise_data(m)
function init_data(m)
p = m.prop
T = p.T
d = Data([zeros(T + 1) for _ in 1:25]..., zeros(T + 1, p.G), zeros(T + 1, p.G))
Expand Down
4 changes: 2 additions & 2 deletions test/accounting_identities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using Test
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

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

for t in 1:T
BeforeIT.one_epoch!(model; multi_threading = false)
Expand Down
8 changes: 4 additions & 4 deletions test/deterministic/deterministic_ouput_t1_t5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
T = 1
parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
data = BeforeIT.initialise_data(model)
model = BeforeIT.init_model(parameters, initial_conditions, T)
data = BeforeIT.init_data(model)

BeforeIT.one_epoch!(model; multi_threading = false)
BeforeIT.update_data!(data, model)
Expand All @@ -30,8 +30,8 @@
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)
model = BeforeIT.init_model(parameters, initial_conditions, T)
data = BeforeIT.init_data(model)
for t in 1:T
BeforeIT.one_epoch!(model; multi_threading = false)
BeforeIT.update_data!(data, model)
Expand Down
2 changes: 1 addition & 1 deletion test/deterministic/initialize_deterministic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
model = BeforeIT.initialise_model(parameters, initial_conditions, 1)
model = BeforeIT.init_model(parameters, initial_conditions, 1)

properties = model.prop

Expand Down
2 changes: 1 addition & 1 deletion test/deterministic/one_epoch_deterministic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

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

gov = model.gov # government
cb = model.cb # central bank
Expand Down
8 changes: 4 additions & 4 deletions test/deterministic/one_run_deterministic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
initial_conditions1 = deepcopy(initial_conditions)
initial_conditions2 = deepcopy(initial_conditions)

model = BeforeIT.initialise_model(parameters1, initial_conditions1, T;)
data = BeforeIT.initialise_data(model)
model = BeforeIT.init_model(parameters1, initial_conditions1, T;)
data = BeforeIT.init_data(model)
for t in 1:(T - 1)
BeforeIT.one_epoch!(model; multi_threading = false)
BeforeIT.update_data!(data, model)
end


model2 = BeforeIT.initialise_model(parameters2, initial_conditions2, T;)
data2 = BeforeIT.initialise_data(model2)
model2 = BeforeIT.init_model(parameters2, initial_conditions2, T;)
data2 = BeforeIT.init_data(model2)

for t in 1:(T - 1)
BeforeIT.one_epoch!(model2; multi_threading = false)
Expand Down
2 changes: 1 addition & 1 deletion test/markets/search_and_matching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using Random
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions

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


gov = model.gov # government
Expand Down
2 changes: 1 addition & 1 deletion test/model_init/initialise_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dir = @__DIR__

parameters = BeforeIT.STEADY_STATE2010Q1.parameters
initial_conditions = BeforeIT.STEADY_STATE2010Q1.initial_conditions
model = BeforeIT.initialise_model(parameters, initial_conditions, 1)
model = BeforeIT.init_model(parameters, initial_conditions, 1)

properties = model.prop

Expand Down
4 changes: 2 additions & 2 deletions test/monte_carlo_evaluations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ parameters = matread(joinpath(dir, "../data/austria/parameters/2010Q1.mat"))
initial_conditions = matread(joinpath(dir, "../data/austria/initial_conditions/2010Q1.mat"))

T = 20
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
data = BeforeIT.initialise_data(model)
model = BeforeIT.init_model(parameters, initial_conditions, T)
data = BeforeIT.init_data(model)

n_sims = 3
data_vector = BeforeIT.run_n_sims(model, n_sims)
Expand Down
4 changes: 2 additions & 2 deletions test/one_epoch_consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ parameters = matread(joinpath(dir, "../data/steady_state/parameters/2010Q1.mat")
initial_conditions = matread(joinpath(dir, "../data/steady_state/initial_conditions/2010Q1.mat"))

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


println(BeforeIT.get_accounting_identity_banks(model))
Expand Down
4 changes: 2 additions & 2 deletions test/steady_state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ parameters = BeforeIT.STEADY_STATE2010Q1.parameters
initial_conditions = BeforeIT.STEADY_STATE2010Q1.initial_conditions

T = 5
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
data = BeforeIT.initialise_data(model)
model = BeforeIT.init_model(parameters, initial_conditions, T)
data = BeforeIT.init_data(model)

for t in 1:T
println(t)
Expand Down
2 changes: 1 addition & 1 deletion test/utils/estimations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using MAT, Random

parameters = BeforeIT.AUSTRIA2010Q1.parameters
initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
model = BeforeIT.initialise_model(parameters, initial_conditions, 1)
model = BeforeIT.init_model(parameters, initial_conditions, 1)



Expand Down

0 comments on commit 7c3d889

Please sign in to comment.