Skip to content

Commit f3fc684

Browse files
authored
Merge branch 'bancaditalia:main' into main
2 parents 2f905f7 + 1bc5d1f commit f3fc684

File tree

4 files changed

+114
-38
lines changed

4 files changed

+114
-38
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ julia --proj test/runtests.jl
139139
</a><br />
140140
<p>Paris 1: Pantheon - Sorbonne</p>
141141
</td>
142+
<td align="center">
143+
<a href="https://github.com/Tortar">
144+
<img src="https://avatars.githubusercontent.com/Tortar" width="100px;" alt="Adriano Meligrana"/><br />
145+
<sub><b>Adriano Meligrana</b></sub>
146+
</a><br />
147+
<p>University of Turin</p>
148+
<p>Email: <a href="mailto:[email protected]:">[email protected]</a></p>
149+
</td>
142150
</tr>
143151
</table>
144152

src/one_simulation.jl

+18-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ end
3636

3737

3838
"""
39-
run_n_sims(model, n_sims; shock = NoShock())
39+
run_n_sims(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,14 +48,26 @@ 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; shock = NoShock())
51+
function run_n_sims(model, n_sims; multi_threading = true, shock = NoShock())
5252

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

55-
Threads.@threads for i in 1:n_sims
56-
model_i = deepcopy(model)
57-
data = run_one_sim!(model_i; shock = shock)
58-
data_vector[i] = data
55+
if multi_threading
56+
Threads.@threads for i in 1:n_sims
57+
model_i = deepcopy(model)
58+
data = run_one_sim!(model_i; shock = shock)
59+
data_vector[i] = data
60+
end
61+
else
62+
for i in 1:n_sims
63+
model_i = deepcopy(model)
64+
data = run_one_sim!(model_i; shock = shock)
65+
data_vector[i] = data
66+
end
5967
end
68+
69+
# transform the vector of data objects into a DataVector
70+
data_vector = BeforeIT.DataVector(data_vector)
71+
6072
return data_vector
6173
end

src/utils/data.jl

+85-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,69 @@
1+
"""
2+
@data(AV = Vector{Float64}, AM = Matrix{Float64})
3+
4+
A macro that defines default types for data structures used in the code.
5+
6+
# Arguments
7+
- `AV::Type`: The default type for vectors. Defaults to `Vector{Float64}`.
8+
- `AM::Type`: The default type for matrices. Defaults to `Matrix{Float64}`.
9+
10+
# Usage
11+
This macro can be used to standardize the types of vectors and matrices across the codebase, ensuring consistency and reducing the need for repetitive type declarations.
12+
"""
13+
macro data(AV = Vector{Float64}, AM = Matrix{Float64})
14+
return esc(quote
15+
nominal_gdp::$AV
16+
real_gdp::$AV
17+
nominal_gva::$AV
18+
real_gva::$AV
19+
nominal_household_consumption::$AV
20+
real_household_consumption::$AV
21+
nominal_government_consumption::$AV
22+
real_government_consumption::$AV
23+
nominal_capitalformation::$AV
24+
real_capitalformation::$AV
25+
nominal_fixed_capitalformation::$AV
26+
real_fixed_capitalformation::$AV
27+
nominal_fixed_capitalformation_dwellings::$AV
28+
real_fixed_capitalformation_dwellings::$AV
29+
nominal_exports::$AV
30+
real_exports::$AV
31+
nominal_imports::$AV
32+
real_imports::$AV
33+
operating_surplus::$AV
34+
compensation_employees::$AV
35+
wages::$AV
36+
taxes_production::$AV
37+
gdp_deflator_growth_ea::$AV
38+
real_gdp_ea::$AV
39+
euribor::$AV
40+
nominal_sector_gva::$AM
41+
real_sector_gva::$AM
42+
end)
43+
end
44+
45+
46+
# Define the Data struct
147
struct Data{T, AV <: AbstractVector{T}, AM <: AbstractMatrix{T}}
2-
nominal_gdp::AV
3-
real_gdp::AV
4-
nominal_gva::AV
5-
real_gva::AV
6-
nominal_household_consumption::AV
7-
real_household_consumption::AV
8-
nominal_government_consumption::AV
9-
real_government_consumption::AV
10-
nominal_capitalformation::AV
11-
real_capitalformation::AV
12-
nominal_fixed_capitalformation::AV
13-
real_fixed_capitalformation::AV
14-
nominal_fixed_capitalformation_dwellings::AV
15-
real_fixed_capitalformation_dwellings::AV
16-
nominal_exports::AV
17-
real_exports::AV
18-
nominal_imports::AV
19-
real_imports::AV
20-
operating_surplus::AV
21-
compensation_employees::AV
22-
wages::AV
23-
taxes_production::AV
24-
gdp_deflator_growth_ea::AV
25-
real_gdp_ea::AV
26-
euribor::AV
27-
nominal_sector_gva::AM
28-
real_sector_gva::AM
48+
@data AV AM
49+
end
50+
51+
# Define the DataVector struct
52+
struct DataVector
53+
vector::Vector{Data}
54+
end
55+
56+
# Define the getproperty function for the DataVector struct
57+
# This function allows for the extraction of fields from the Data struct
58+
# by using the dot syntax, e.g., data_vector.nominal_gdp
59+
function Base.getproperty(dv::DataVector, name::Symbol)
60+
if name in fieldnames(BeforeIT.Data)
61+
# If the field name exists in the `a` struct, extract it from all elements
62+
return hcat([getproperty(d, name) for d in dv.vector]...)
63+
else
64+
# Fallback to default behavior for other fields
65+
return getfield(dv, name)
66+
end
2967
end
3068

3169

@@ -92,6 +130,26 @@ function _update_data_init!(d, m)
92130
end
93131

94132

133+
"""
134+
update_data!(d, m)
135+
136+
Update the data `d` with the model `m`.
137+
138+
# Arguments
139+
- `d`: The data structure to be updated.
140+
- `m`: The model used to update the data.
141+
142+
# Returns
143+
- Nothing. The function updates the data structure `d` in place.
144+
145+
# Example
146+
147+
```julia
148+
data = BeforeIT.init_data(model)
149+
one_epoch!(model)
150+
BeforeIT.update_data!(data, model)
151+
```
152+
"""
95153
function update_data!(d, m)
96154
p = m.prop
97155
t = m.agg.t
@@ -176,6 +234,3 @@ function update_data!(d, m)
176234
d.real_gdp_ea[t] = m.rotw.Y_EA
177235
end
178236

179-
# overloading "getproperty" to allow for easy access to data in a vector of data structs
180-
import Base: getproperty
181-
getproperty(a::Vector{Data}, v::Symbol) = hcat([getproperty(d, v) for d in a]...)

src/utils/plot_data_vector.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ const quantity_titles = Dict(
1313
:gdp_deflator => "gdp deflator",
1414
)
1515

16-
function plot_data_vector(data_vector::Vector{Data}; titlefont = 9, quantities = default_quantities)
17-
Te = length(data_vector[1].wages)
16+
function plot_data_vector(data_vector::DataVector; titlefont = 9, quantities = default_quantities)
17+
Te = length(data_vector.vector[1].wages)
18+
1819
ps = []
1920

2021
for q in quantities

0 commit comments

Comments
 (0)