Scenario analysis via custom shocks
In this tutorial we will illustrate how to perform a scenario analysis by running the model multiple times under a specific shock and comparing the results with the unshocked model.
import BeforeIT as Bit
import StatsBase: mean, std
using Plots
parameters = Bit.AUSTRIA2010Q1.parameters;
initial_conditions = Bit.AUSTRIA2010Q1.initial_conditions;Initialise the model
model = Bit.Model(parameters, initial_conditions);Simulate the baseline model for T quarters, N_reps times, and collect the data
T = 16
n_sims = 64
model_vec_baseline = Bit.ensemblerun!((deepcopy(model) for _ in 1:n_sims), T);Now, apply a shock to the model and simulate it again. A shock is simply a function that takes the model and changes some of its parameters for a specific time period. We do this by first defining a "struct" with useful attributes. For example, we can define an productivity and a consumption shock with the following structs
struct ProductivityShock
productivity_multiplier::Float64 # productivity multiplier
end
struct ConsumptionShock
consumption_multiplier::Float64 # productivity multiplier
final_time::Int
endand then by making the structs callable functions that change the parameters of the model, this is done in Julia using the syntax below
A permanent change in the labour productivities by the factor s.productivity_multiplier
function (s::ProductivityShock)(model::Bit.Model)
return model.firms.alpha_bar_i .= model.firms.alpha_bar_i .* s.productivity_multiplier
endA temporary change in the propensity to consume model.prop.psi by the factor s.consumption_multiplier
function (s::ConsumptionShock)(model::Bit.Model)
return if model.agg.t == 1
model.prop.psi = model.prop.psi * s.consumption_multiplier
elseif model.agg.t == s.final_time
model.prop.psi = model.prop.psi / s.consumption_multiplier
end
endDefine specific shocks, for example a 2% increase in productivity
productivity_shock = ProductivityShock(1.02)Main.ProductivityShock(1.02)or a 4 quarters long 2% increase in consumption
consumption_shock = ConsumptionShock(1.02, 4)Main.ConsumptionShock(1.02, 4)Simulate the model with the shock
model_vec_shocked = Bit.ensemblerun!((deepcopy(model) for _ in 1:n_sims), T; shock! = consumption_shock);extract the data vectors from the model vectors
data_vector_baseline = Bit.DataVector(model_vec_baseline);
data_vector_shocked = Bit.DataVector(model_vec_shocked);Compute mean and standard error of GDP for the baseline and shocked simulations
mean_gdp_baseline = mean(data_vector_baseline.real_gdp, dims = 2)
mean_gdp_shocked = mean(data_vector_shocked.real_gdp, dims = 2)
sem_gdp_baseline = std(data_vector_baseline.real_gdp, dims = 2) / sqrt(n_sims)
sem_gdp_shocked = std(data_vector_shocked.real_gdp, dims = 2) / sqrt(n_sims)17×1 Matrix{Float64}:
0.0
54.178551573201446
82.91933796143158
95.11942722585636
106.36328736732303
119.65557363749025
134.53889233145634
135.40442517823536
154.44872798962763
162.90936102108964
173.61614793717027
179.57079506301835
194.29748701890023
195.54496731119104
196.74230501594982
201.77516716809902
203.73629321955215Compute the ratio of shocked to baseline GDP
gdp_ratio = mean_gdp_shocked ./ mean_gdp_baseline17×1 Matrix{Float64}:
1.0
1.0005044948991615
1.0063228078231257
1.0119629778508967
1.0128090350859247
1.0084721785895934
1.0067764435811717
1.0050759659874198
1.0037874749479425
1.0040913781022742
1.0056582190115584
1.005460938301129
1.006862965949022
1.007394219366327
1.0066704173154808
1.0066019695818653
1.00636219805734the standard error on a ratio of two variables is computed with the error propagation formula
sem_gdp_ratio = gdp_ratio .* ((sem_gdp_baseline ./ mean_gdp_baseline) .^ 2 .+ (sem_gdp_shocked ./ mean_gdp_shocked) .^ 2) .^ 0.517×1 Matrix{Float64}:
0.0
0.0010015840821505971
0.0015186606337788894
0.001972656522190506
0.0022077710288480037
0.0024376803738066907
0.0025593084173382453
0.00267851290378329
0.002942307216345477
0.003146175065308616
0.0033990800945012476
0.0035383788140848377
0.003639491259613024
0.003702705391577889
0.0037627583911360014
0.0038701871644736363
0.004112044116890584Finally, we can plot the impulse response curve
plot(
1:(T + 1),
gdp_ratio,
ribbon = sem_gdp_ratio,
fillalpha = 0.2,
label = "",
xlabel = "quarters",
ylabel = "GDP change",
)We can save the figure using: savefig("gdp_shock.png")