|
| 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 |
1 | 47 | 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 |
29 | 67 | end
|
30 | 68 |
|
31 | 69 |
|
@@ -92,6 +130,26 @@ function _update_data_init!(d, m)
|
92 | 130 | end
|
93 | 131 |
|
94 | 132 |
|
| 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 | +""" |
95 | 153 | function update_data!(d, m)
|
96 | 154 | p = m.prop
|
97 | 155 | t = m.agg.t
|
@@ -176,6 +234,3 @@ function update_data!(d, m)
|
176 | 234 | d.real_gdp_ea[t] = m.rotw.Y_EA
|
177 | 235 | end
|
178 | 236 |
|
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]...) |
|
0 commit comments