-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into compathelper/new_version/2023-10-11-01-04-44…
…-475-03462036025
- Loading branch information
Showing
18 changed files
with
290 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Consistent | ||
using DataFrames | ||
using ForwardDiff | ||
|
||
function calibrate!(data, model, parameters=Dict()) | ||
x -> model.f!(y, x, ...) + x | ||
function loss(data,) | ||
|
||
end | ||
ForwardDiff.derivative(() -> loss, input) | ||
end | ||
|
||
model = SIM() | ||
df = DataFrame(reverse(lags[:, end-20:end-1], dims=2)', model.endogenous_variables) | ||
df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Consistent | ||
|
||
PC_gdp = @model begin | ||
@endogenous Y YD T V C | ||
@exogenous r G B_h | ||
@parameters α_1 α_2 θ | ||
@equations begin | ||
Y = C + G | ||
YD = Y - T + r[-1] * B_h[-1] | ||
T = θ * (Y + r[-1] * B_h[-1]) | ||
V = V[-1] + (YD - C) | ||
C = α_1 * YD + α_2 * V[-1] | ||
end | ||
end | ||
|
||
PC_hh = @model begin | ||
@endogenous H_h B_h B_s H_s B_cb r | ||
@exogenous r_exo G V YD T | ||
@parameters λ_0 λ_1 λ_2 | ||
@equations begin | ||
H_h = V - B_h | ||
B_h = (λ_0 + λ_1 * r - λ_2 * (YD / V)) * V | ||
B_s = (G + r[-1] * B_s[-1]) - (T + r[-1] * B_cb[-1]) + B_s[-1] | ||
H_s = B_cb - B_cb[-1] + H_s[-1] | ||
B_cb = B_s - B_h | ||
r = r_exo | ||
end | ||
end | ||
|
||
PC_complete = PC_gdp + PC_hh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Consistent | ||
using NLsolve | ||
using DataFrames | ||
using Gadfly | ||
using Pipe | ||
Gadfly.push_theme(:dark) | ||
|
||
function solve(model, lags, exos, params) | ||
nlsolve( | ||
(x, y) -> model.f!(x, y, lags, exos, params), | ||
fill(1.0, length(model.endogenous_variables)), | ||
autodiff = :forward, | ||
).zero | ||
end | ||
|
||
# Setup SIM | ||
model = Consistent.SIM() # load predefined SIM model | ||
params_dict = Consistent.SIM(true) # load default parameters | ||
exos = [20.0] # this is only G | ||
exos = exos[:, :] # bring in matrix format | ||
lags = fill(0.0, length(model.endogenous_variables), 1) # lagged values of endogenous variables are all 0.0 | ||
param_values = map(x -> params_dict[x], model.parameters) # get raw parameter values | ||
solution = solve(model, lags, exos, param_values) # solve first period | ||
|
||
# Solve model for 50 periods | ||
for i in 1:50 | ||
solution = solve(model, lags, exos, param_values) # solve first period | ||
lags = hcat(lags, solution) | ||
end | ||
|
||
# Convert results to DataFrame | ||
df = DataFrame(lags', model.endogenous_variables) | ||
df[!, :period] = 1:nrow(df) | ||
# Select variables, convert to long format, and plot variables | ||
@pipe df |> | ||
select(_, [:Y, :C, :YD, :period]) |> | ||
stack(_, Not(:period), variable_name=:variable) |> | ||
plot( | ||
_, | ||
x=:period, | ||
y=:value, | ||
color=:variable, | ||
Geom.line | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Base: + | ||
|
||
function +(model1::Model, model2::Model) | ||
undetermined = findall(in(model2.endogenous_variables), model1.endogenous_variables) | ||
@assert isempty(undetermined) "The endogenous variables $(model1[undetermined]) appear twice" | ||
exos1 = filter(x -> !(x in model2.endogenous_variables), model1.exogenous_variables) | ||
exos2 = filter( | ||
x -> !((x in model1.endogenous_variables) || (x in exos1)), model2.exogenous_variables | ||
) | ||
equations = vcat(model1.equations, model2.equations) | ||
global Consistent.sfc_model = Model() | ||
sfc_model.endogenous_variables = vcat(model1.endogenous_variables, model2.endogenous_variables) | ||
sfc_model.exogenous_variables = vcat(exos1, exos2) | ||
sfc_model.parameters = vcat(model1.parameters, model2.parameters) | ||
sfc_model.math_operators = model1.math_operators # FIXME: does this make sense? | ||
sfc_model.equations = equations | ||
eval(build_f!(equations)) | ||
deepcopy(sfc_model) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Root-mean-square error of a time series. | ||
""" | ||
rmse(x, y) = sqrt(sum((x .- y).^2) / length(x)) | ||
|
||
""" | ||
RMSE standardized by the mean of our target values. | ||
""" | ||
standardized_rmse(x, y) = rmse(x, y) / mean(y) | ||
|
||
""" | ||
Mean standardized RMSE for a vector of time series. Makes only sense for strictly positive variables. | ||
Note: Without some kind of stationarity this estimation does not really make sense either, | ||
but this is often a general problem of stock-flow consistent models. | ||
""" | ||
function msrmse(x, y) | ||
# loop over columns | ||
average = 0.0 | ||
for i in axes(x, 2) | ||
average += standardized_rmse(x[:, i], y[:, i]) | ||
end | ||
return average | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Note: This is some old model where I don't know the origin anymore. | ||
It is probably related to some Peter Bofinger textbook. | ||
""" | ||
BMW() = @model begin | ||
@endogenous C_s C_d I_s I_d N_s N_d L_s L_d r_l r_m K K_T DA | ||
@exogenous r_exo | ||
@parameters α_0 α_1_w α_1_r α_2 | ||
@equations begin | ||
C_s = C_d | ||
I_s = I_d | ||
N_s = N_d | ||
L_s = L_d - L_d[-1] + L_s[-1] | ||
Y = C_s + I_s | ||
WB_d = Y - r_l[-1] * L_d[-1] - AF | ||
AF = delta * K[-1] | ||
L_d = I_d - AF + L_d[-1] | ||
YD = WB_s + r_m[-1] * M_d[-1] | ||
M_h = YD - C_d + M_h[-1] | ||
M_s = L_s - L_s[-1] - M_s[-1] | ||
r_m = r_l | ||
WB_s = W * N_s | ||
N_d = Y / pr | ||
W = WB_d / N_d | ||
C_d = α_0 + α_1_w * WB_s + α_1_r * r_m[-1] * M_h[-1] + α_2 * M_h | ||
K = I_d - DA - K[-1] | ||
DA = delta * K[-1] | ||
K_T = kappa * Y[-1] | ||
I_d = gamma * (K_T - K[-1]) + DA | ||
r_l = r_exo | ||
end | ||
end |
File renamed without changes.
Oops, something went wrong.