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
end

and 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
end

A 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
end

Define 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}:
   1.8333689901882087e-12
  60.72532518256768
  87.08282549802763
  95.59534342888213
 100.66254250176608
 115.67189288092695
 117.30301238465137
 133.09256380543363
 153.985472178747
 171.91584217268783
 189.39718003769144
 192.74945163393915
 207.21906056257492
 220.46880004318965
 235.2614184733168
 246.4704400028988
 232.80880950446013

Compute the ratio of shocked to baseline GDP

gdp_ratio = mean_gdp_shocked ./ mean_gdp_baseline
17×1 Matrix{Float64}:
 1.0
 1.0020289872851962
 1.0067149595544251
 1.008189401387306
 1.008041358111647
 1.0057351675359494
 1.0041186732722889
 1.0037913543495904
 1.0007124031558838
 1.0009645715279554
 1.0012835298357514
 1.0007906012021022
 1.0002769198983532
 0.9981161349380505
 0.9966502052956695
 0.9957070786891494
 0.996824655510535

the 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.5
17×1 Matrix{Float64}:
 3.580093467121083e-17
 0.0012264706136143385
 0.0016134523739134783
 0.0018334483879692339
 0.00196604555053358
 0.0022929633932877998
 0.002497447120790684
 0.0028611910868437617
 0.0031860807802853303
 0.003348433134492234
 0.0035282524341742863
 0.003619107782205959
 0.0038891710922717474
 0.004071359594843055
 0.004301830357760326
 0.004379512652979051
 0.004213258855000314

Finally, 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",
)
Example block output

We can save the figure using: savefig("gdp_shock.png")