Skip to content

Commit 7c23046

Browse files
committed
Rename model stepping functions
1 parent c8c1356 commit 7c23046

22 files changed

+43
-37
lines changed

Diff for: Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "0.2.0"
55

66
[deps]
77
ChunkSplitters = "ae650224-84b6-46f8-82ea-d812ca08434e"
8+
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
89
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
910
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1011
DynamicSampling = "2083aeaf-6258-5d07-89fc-32cf5060c837"
@@ -22,6 +23,7 @@ StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
2223

2324
[compat]
2425
ChunkSplitters = "3"
26+
CommonSolve = "0.2"
2527
Dates = "1"
2628
Distributions = "0.25"
2729
DynamicSampling = "0.4"

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
6969

7070
T = 20
7171
model = BeforeIT.init_model(parameters, initial_conditions, T)
72-
data = BeforeIT.run_one_sim!(model)
72+
data = BeforeIT.run!(model)
7373
```
7474

7575
This will simulate the model with the original Austrian parametrisation for 20 quarters and save the results in the `data` object.

Diff for: docs/src/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ initial_conditions = BeforeIT.AUSTRIA2010Q1.initial_conditions
3636

3737
T = 20
3838
model = BeforeIT.initialise_model(parameters, initial_conditions, T)
39-
data = BeforeIT.run_one_sim!(model)
39+
data = BeforeIT.run!(model)
4040
```
4141

4242
To plot the results of the simulation, you can use the `Plots` package

Diff for: examples/basic_example.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ data = Bit.init_data(model);
3131
# We can run now the model for a number of epochs and progressively update the data tracker.
3232

3333
for t in 1:T
34-
Bit.run_one_epoch!(model; multi_threading = true)
34+
Bit.step!(model; multi_threading = true)
3535
Bit.update_data!(data, model)
3636
end
3737

3838
# Note that we can equivalently run the model for a number of epochs in the single command
39-
# `data = BeforeIT.run_one_sim!(model)`, but writing the loop explicitely is more instructive.
39+
# `data = BeforeIT.run!(model)`, but writing the loop explicitely is more instructive.
4040

4141
# We can then plot any time series stored in the data tracker, for example
4242

@@ -50,7 +50,7 @@ plot(ps..., layout = (3, 3))
5050
# To run multiple monte-carlo repetitions in parallel we can use
5151

5252
model = Bit.init_model(parameters, initial_conditions, T)
53-
data_vector = Bit.run_n_sims(model, 4)
53+
data_vector = Bit.ensemblerun(model, 4)
5454

5555
# Note that this will use the number of threads specified when activating the Julia environment.
5656
# To discover the number of threads available, you can use the command

Diff for: examples/benchmark_w_matlab.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function run(parameters, initial_conditions, T; multi_threading = false)
1313
data = BeforeIT.init_data(model);
1414

1515
for _ in 1:T
16-
BeforeIT.run_one_epoch!(model; multi_threading = multi_threading)
16+
BeforeIT.step!(model; multi_threading = multi_threading)
1717
BeforeIT.update_data!(data, model)
1818
end
1919
return model, data

Diff for: examples/change_expectations.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ init = Bit.AUSTRIA2010Q1.initial_conditions
1616
Random.seed!(1234)
1717
T = 40
1818
model = Bit.init_model(par, init, T)
19-
data = Bit.run_one_sim!(model)
19+
data = Bit.run!(model)
2020

2121
# Now we can experiment with changing expectations of the agents in the model.
2222
# We will change the function `estimate_next_value` to make the agents expect
@@ -31,7 +31,7 @@ end
3131

3232
Random.seed!(1234)
3333
model = Bit.init_model(par, init, T)
34-
data_back = Bit.run_one_sim!(model)
34+
data_back = Bit.run!(model)
3535

3636
# Plot the results, comparing the two cases as different lines
3737

Diff for: examples/get_simulations.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ for year in 2010:2019
1717
T = 12
1818
model = BeforeIT.init_model(parameters, initial_conditions, T)
1919
n_sims = 4
20-
data_vector = BeforeIT.run_n_sims(model, n_sims)
20+
data_vector = BeforeIT.ensemblerun(model, n_sims)
2121
save("data/italy/simulations/" * string(year) * "Q" * string(quarter) * ".jld2", "data_vector", data_vector)
2222
end
2323
end

Diff for: examples/multithreading_speedup.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ Threads.nthreads()
2626

2727
# Then we need to first compile the code not to count compilation time,
2828
# we can do that just by executing the function one time
29-
Bit.run_one_sim!(model; multi_threading = false);
29+
Bit.run!(model; multi_threading = false);
3030

3131
# Let's now compare the performance of single threading and multi threading
3232
model = Bit.init_model(parameters, initial_conditions, T);
33-
@time data = Bit.run_one_sim!(model; multi_threading = false);
33+
@time data = Bit.run!(model; multi_threading = false);
3434

3535
model = Bit.init_model(parameters, initial_conditions, T);
36-
@time data = Bit.run_one_sim!(model; multi_threading = true);
36+
@time data = Bit.run!(model; multi_threading = true);
3737

3838
# Is the speedup in line to what we would expect? Yes!

Diff for: examples/scenario_analysis_via_overload.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data = Bit.init_data(model);
1818

1919
# Simulate the model for T quarters
2020

21-
data_vec_baseline = Bit.run_n_sims(model, 4)
21+
data_vec_baseline = Bit.ensemblerun(model, 4)
2222

2323
# Now, apply a shock to the model and simulate it again
2424
# Here, we do this by overloading the central_bank_rate function with the wanted behaviour
@@ -36,7 +36,7 @@ function central_bank_rate(cb::Bit.CentralBank, model::Bit.Model)
3636
end
3737
end
3838

39-
data_vec_shocked = Bit.run_n_sims(model, 4)
39+
data_vec_shocked = Bit.ensemblerun(model, 4)
4040

4141
# Finally, we can plot baseline and shocked simulations
4242

Diff for: examples/scenario_analysis_via_shock.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ T = 20
1515
model = Bit.init_model(parameters, initial_conditions, T);
1616

1717
# Simulate the model for T quarters
18-
data_vec_baseline = Bit.run_n_sims(model, 4)
18+
data_vec_baseline = Bit.ensemblerun(model, 4)
1919

2020
# Now, apply a shock to the model and simulate it again.
2121
# A shock is simply a function that takes the model and changes some of
@@ -42,7 +42,7 @@ end
4242
# and run a shocked simulation
4343

4444
custom_shock = CustomShock(0.0, 10)
45-
data_vec_shocked = Bit.run_n_sims(model, 4; shock = custom_shock)
45+
data_vec_shocked = Bit.ensemblerun(model, 4; shock = custom_shock)
4646

4747
# Finally, we can plot baseline and shocked simulations
4848

Diff for: main.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data = BeforeIT.init_data(model)
1010

1111
for t in 1:T
1212
println("Epoch: ", t)
13-
BeforeIT.run_one_epoch!(model; multi_threading = true)
13+
BeforeIT.step!(model; multi_threading = true)
1414
BeforeIT.update_data!(data, model)
1515
end
1616

Diff for: src/model_init/abstract.jl

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract type AbstractBank end
1717
abstract type AbstractCentralBank end
1818
abstract type AbstractGovernment end
1919
abstract type AbstractRestOfTheWorld end
20+
abstract type AbstractModel end
2021

2122
macro worker(T = Vector{Float64}, I = Vector{Int})
2223
return esc(quote

Diff for: src/model_init/agents.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ This is a Model type. It is used to store all the agents of the economy.
222222
- `rotw`: RestOfTheWorld
223223
- `agg`: Aggregates
224224
"""
225-
mutable struct Model
225+
mutable struct Model <: AbstractModel
226226
w_act::AbstractWorkers
227227
w_inact::AbstractWorkers
228228
firms::AbstractFirms

Diff for: src/one_epoch.jl

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

2+
import CommonSolve
3+
using CommonSolve: step!
4+
export step!
25

36
"""
4-
run_one_epoch!(model; multi_threading = false)
7+
step!(model; multi_threading = false)
58
69
This function simulates a single epoch the economic model, updating various components of the model based
710
the interactions between different economic agents. It accepts a `model` object, which encapsulates the state for the
@@ -17,7 +20,7 @@ Key operations performed include:
1720
1821
The function updates the model in-place and does not return any value.
1922
"""
20-
function run_one_epoch!(model; multi_threading = false, shock = NoShock())
23+
function CommonSolve.step!(model::AbstractModel; multi_threading = false, shock = NoShock())
2124

2225
gov = model.gov # government
2326
cb = model.cb # central bank

Diff for: src/one_simulation.jl

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
run_one_sim!(model; shock = NoShock())
2+
run!(model; shock = NoShock())
33
44
Run a single simulation based on the provided `model`.
55
The simulation runs for a number of epochs specified by `model.prop.T`.
@@ -12,21 +12,21 @@ The simulation runs for a number of epochs specified by `model.prop.T`.
1212
1313
# Details
1414
The function initializes the data using `BeforeIT.init_data(model)`, then iteratively updates the model and data
15-
for each epoch using `BeforeIT.run_one_epoch!(model)` and `BeforeIT.update_data!(data, model)` respectively.
15+
for each epoch using `BeforeIT.step!(model)` and `BeforeIT.update_data!(data, model)` respectively.
1616
1717
# Example
1818
```julia
1919
model = BeforeIT.init_model(parameters, initial_conditions, T)
20-
data = run_one_sim!(model)
20+
data = run!(model)
2121
"""
22-
function run_one_sim!(model; multi_threading = false, shock = NoShock())
22+
function run!(model::AbstractModel; multi_threading = false, shock = NoShock())
2323

2424
data = BeforeIT.init_data(model)
2525

2626
T = model.prop.T
2727

2828
for _ in 1:T
29-
BeforeIT.run_one_epoch!(model; multi_threading = multi_threading, shock = shock)
29+
BeforeIT.step!(model; multi_threading = multi_threading, shock = shock)
3030
BeforeIT.update_data!(data, model)
3131
end
3232

@@ -36,7 +36,7 @@ end
3636

3737

3838
"""
39-
run_n_sims(model, n_sims; shock = NoShock(), multi_threading = true)
39+
ensemblerun(model, n_sims; shock = NoShock(), multi_threading = true)
4040
4141
A function that runs `n_sims` simulations in parallel with multiple threading and returns a vector of
4242
data objects of dimension `n_sims`.
@@ -48,20 +48,20 @@ data objects of dimension `n_sims`.
4848
# Returns
4949
- `data_vector`: A vector containing the data objects collected during each simulation.
5050
"""
51-
function run_n_sims(model, n_sims; multi_threading = true, shock = NoShock())
51+
function ensemblerun(model::AbstractModel, n_sims; multi_threading = true, shock = NoShock())
5252

5353
data_vector = Vector{BeforeIT.Data}(undef, n_sims)
5454

5555
if multi_threading
5656
Threads.@threads for i in 1:n_sims
5757
model_i = deepcopy(model)
58-
data = run_one_sim!(model_i; shock = shock)
58+
data = run!(model_i; shock = shock)
5959
data_vector[i] = data
6060
end
6161
else
6262
for i in 1:n_sims
6363
model_i = deepcopy(model)
64-
data = run_one_sim!(model_i; shock = shock)
64+
data = run!(model_i; shock = shock)
6565
data_vector[i] = data
6666
end
6767
end

Diff for: src/precompile.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using PrecompileTools
88
@compile_workload let
99
model = BeforeIT.init_model(parameters, initial_conditions, T)
1010
data = BeforeIT.init_data(model);
11-
BeforeIT.run_one_epoch!(model)
11+
BeforeIT.step!(model)
1212
BeforeIT.update_data!(data, model)
1313
end
1414
end

Diff for: src/utils/data.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Update the data `d` with the model `m`.
146146
147147
```julia
148148
data = BeforeIT.init_data(model)
149-
run_one_epoch!(model)
149+
step!(model)
150150
BeforeIT.update_data!(data, model)
151151
```
152152
"""

Diff for: test/accounting_identities.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using Test
1212
data = BeforeIT.init_data(model)
1313

1414
for t in 1:T
15-
BeforeIT.run_one_epoch!(model; multi_threading = false)
15+
BeforeIT.step!(model; multi_threading = false)
1616
BeforeIT.update_data!(data, model)
1717
end
1818

Diff for: test/deterministic/deterministic_ouput_t1_t5.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
model = BeforeIT.init_model(parameters, initial_conditions, T)
88
data = BeforeIT.init_data(model)
99

10-
BeforeIT.run_one_epoch!(model; multi_threading = false)
10+
BeforeIT.step!(model; multi_threading = false)
1111
BeforeIT.update_data!(data, model)
1212

1313
# import results from matlab run
@@ -33,7 +33,7 @@
3333
model = BeforeIT.init_model(parameters, initial_conditions, T)
3434
data = BeforeIT.init_data(model)
3535
for t in 1:T
36-
BeforeIT.run_one_epoch!(model; multi_threading = false)
36+
BeforeIT.step!(model; multi_threading = false)
3737
BeforeIT.update_data!(data, model)
3838
end
3939

Diff for: test/deterministic/one_run_deterministic.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
model = BeforeIT.init_model(parameters, initial_conditions, T;)
99
data = BeforeIT.init_data(model)
1010
for t in 1:(T - 1)
11-
BeforeIT.run_one_epoch!(model; multi_threading = m)
11+
BeforeIT.step!(model; multi_threading = m)
1212
BeforeIT.update_data!(data, model)
1313
end
1414
return model, data

Diff for: test/monte_carlo_evaluations.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ model = BeforeIT.init_model(parameters, initial_conditions, T)
1010
data = BeforeIT.init_data(model)
1111

1212
n_sims = 3
13-
data_vector = BeforeIT.run_n_sims(model, n_sims)
13+
data_vector = BeforeIT.ensemblerun(model, n_sims)
1414

1515
@test length(data_vector) == n_sims
1616
@test typeof(data_vector) == Vector{BeforeIT.Data}

Diff for: test/steady_state.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data = BeforeIT.init_data(model)
1010

1111
for t in 1:T
1212
println(t)
13-
BeforeIT.run_one_epoch!(model; multi_threading = false)
13+
BeforeIT.step!(model; multi_threading = false)
1414
BeforeIT.update_data!(data, model)
1515
end
1616

0 commit comments

Comments
 (0)