Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve data structs #31

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/one_simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ end


"""
run_n_sims(model, n_sims; shock = NoShock())
run_n_sims(model, n_sims; shock = NoShock(), multi_threading = true)

A function that runs `n_sims` simulations in parallel with multiple threading and returns a vector of
data objects of dimension `n_sims`.
Expand All @@ -48,14 +48,26 @@ data objects of dimension `n_sims`.
# Returns
- `data_vector`: A vector containing the data objects collected during each simulation.
"""
function run_n_sims(model, n_sims; shock = NoShock())
function run_n_sims(model, n_sims; multi_threading = true, shock = NoShock())

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

Threads.@threads for i in 1:n_sims
model_i = deepcopy(model)
data = run_one_sim!(model_i; shock = shock)
data_vector[i] = data
if multi_threading
Threads.@threads for i in 1:n_sims
model_i = deepcopy(model)
data = run_one_sim!(model_i; shock = shock)
data_vector[i] = data
end
else
for i in 1:n_sims
model_i = deepcopy(model)
data = run_one_sim!(model_i; shock = shock)
data_vector[i] = data
end
end

# transform the vector of data objects into a DataVector
data_vector = BeforeIT.DataVector(data_vector)

return data_vector
end
115 changes: 85 additions & 30 deletions src/utils/data.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,69 @@
"""
@data(AV = Vector{Float64}, AM = Matrix{Float64})

A macro that defines default types for data structures used in the code.

# Arguments
- `AV::Type`: The default type for vectors. Defaults to `Vector{Float64}`.
- `AM::Type`: The default type for matrices. Defaults to `Matrix{Float64}`.

# Usage
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.
"""
macro data(AV = Vector{Float64}, AM = Matrix{Float64})
return esc(quote
nominal_gdp::$AV
real_gdp::$AV
nominal_gva::$AV
real_gva::$AV
nominal_household_consumption::$AV
real_household_consumption::$AV
nominal_government_consumption::$AV
real_government_consumption::$AV
nominal_capitalformation::$AV
real_capitalformation::$AV
nominal_fixed_capitalformation::$AV
real_fixed_capitalformation::$AV
nominal_fixed_capitalformation_dwellings::$AV
real_fixed_capitalformation_dwellings::$AV
nominal_exports::$AV
real_exports::$AV
nominal_imports::$AV
real_imports::$AV
operating_surplus::$AV
compensation_employees::$AV
wages::$AV
taxes_production::$AV
gdp_deflator_growth_ea::$AV
real_gdp_ea::$AV
euribor::$AV
nominal_sector_gva::$AM
real_sector_gva::$AM
end)
end


# Define the Data struct
struct Data{T, AV <: AbstractVector{T}, AM <: AbstractMatrix{T}}
nominal_gdp::AV
real_gdp::AV
nominal_gva::AV
real_gva::AV
nominal_household_consumption::AV
real_household_consumption::AV
nominal_government_consumption::AV
real_government_consumption::AV
nominal_capitalformation::AV
real_capitalformation::AV
nominal_fixed_capitalformation::AV
real_fixed_capitalformation::AV
nominal_fixed_capitalformation_dwellings::AV
real_fixed_capitalformation_dwellings::AV
nominal_exports::AV
real_exports::AV
nominal_imports::AV
real_imports::AV
operating_surplus::AV
compensation_employees::AV
wages::AV
taxes_production::AV
gdp_deflator_growth_ea::AV
real_gdp_ea::AV
euribor::AV
nominal_sector_gva::AM
real_sector_gva::AM
@data AV AM
end

# Define the DataVector struct
struct DataVector
vector::Vector{Data}
end

# Define the getproperty function for the DataVector struct
# This function allows for the extraction of fields from the Data struct
# by using the dot syntax, e.g., data_vector.nominal_gdp
function Base.getproperty(dv::DataVector, name::Symbol)
if name in fieldnames(BeforeIT.Data)
# If the field name exists in the `a` struct, extract it from all elements
return hcat([getproperty(d, name) for d in dv.vector]...)
else
# Fallback to default behavior for other fields
return getfield(dv, name)
end
end


Expand Down Expand Up @@ -92,6 +130,26 @@ function _update_data_init!(d, m)
end


"""
update_data!(d, m)

Update the data `d` with the model `m`.

# Arguments
- `d`: The data structure to be updated.
- `m`: The model used to update the data.

# Returns
- Nothing. The function updates the data structure `d` in place.

# Example

```julia
data = BeforeIT.init_data(model)
one_epoch!(model)
BeforeIT.update_data!(data, model)
```
"""
function update_data!(d, m)
p = m.prop
t = m.agg.t
Expand Down Expand Up @@ -176,6 +234,3 @@ function update_data!(d, m)
d.real_gdp_ea[t] = m.rotw.Y_EA
end

# overloading "getproperty" to allow for easy access to data in a vector of data structs
import Base: getproperty
getproperty(a::Vector{Data}, v::Symbol) = hcat([getproperty(d, v) for d in a]...)
5 changes: 3 additions & 2 deletions src/utils/plot_data_vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const quantity_titles = Dict(
:gdp_deflator => "gdp deflator",
)

function plot_data_vector(data_vector::Vector{Data}; titlefont = 9, quantities = default_quantities)
Te = length(data_vector[1].wages)
function plot_data_vector(data_vector::DataVector; titlefont = 9, quantities = default_quantities)
Te = length(data_vector.vector[1].wages)

ps = []

for q in quantities
Expand Down
Loading