Introducing the DIY MACROECONOMIC MODEL SIMULATION Website

Karsten Kohler (University of Leeds)

Outline

  1. Overview: what the DIY Macrosimulation website offers
  2. Solving economic models
  3. Numerical simulations
  4. Simulating a model from the website

\(\downarrow\) Download slides \(\downarrow\)

Overview: what the website offers (1/2)

  • a free online ‘textbook’ on macroeconomic model simulation
  • explains how to numerically simulate key macroeconomic models
  • covers a wide array of economic models
  • provides annotated codes in two open-source programming languages: R and Python
  • provides complementary mathematical analyses of models

Overview: what the website offers (2/2)

  • short tutorials introducing R and Python
  • introduction to the simulation of economic models
  • introduction to the mathematical analysis of dynamic models (advanced)
  • lots of applications:
    • more than 5 static models (e.g. Neoclassical Model, IS-LM Model, Post Keynesian Model, …)
    • more than 10 dynamic models (e.g. New Keynesian Model, Conflict Inflation Model, Ricardian Model, …)
  • let’s take a look: https://macrosimulation.org/

Solving economic models (1/2)

  • any (economic) model has 3 key ingredients:

    1. a set of \(N\) equations

    2. a set of \(N\) endogenous variables

    3. a set of fixed coefficients (‘parameters’) and exogenous variables

  • equations specify causal relationships: \(Y=f(X)\)

  • solution to a model tells us how endogenous variables (= outcomes) react to changes in exogenous variables (= causes)

  • useful for explaining major economic events (e.g. 2021-23 Great Inflation)

  • useful for policy analysis (e.g. hypothetical effect of an increase in government spending on unemployment, inflation, etc.)

  • useful for econometric analysis: helps identify exogenous variables and potential sources of bias (e.g. simultaneity, omitted variables)

Solving economic models (2/2)

  • models can be solved either analytically or numerically
  • analytical approaches:
    • method of substitution (algebra)
    • cast model in matrix form \(Ax=b\) and find \(x=A^{-1}b\) (linear algebra)
    • find partial derivatives in nonlinear models via implicit function theorem + total differentiation/Cramer’s rule
  • advantages:
    • rigorous
    • general
  • disadvantages:
    • can be cumbersome
    • not intuitive for everyone
    • becomes practically infeasible for larger models

Numerical simulations: how? (1/3)

  • numerical simulations provide alternative way to solving and analysing economic models
  • can be done with open-source programming languages (e.g. R and Python)
  • advantages:
    • easy to do (less algebra needed), intuitive
    • can be used to confirm analytical solutions
    • can solve more complex and nonlinear models
    • provides nice visualisations
  • disadvantage:
    • need to pick specific parameter values and thus analyse special cases only

Numerical simulations: how? (2/3)

  • let’s start from the simplest possible example
  • a Keynesian goods market model
  • \(N=2\) equations: goods market equilibrium and consumption function
  • \(N=2\) endogenous variables: output \(Y\) and consumption \(C\)
  • one exogenous variable: government spending \(G_0\)

\[ Y= C + I_0 \] \[ C = c_0 + c_1Y \]

  • solution: \(Y^*=\frac{c_0+I_0}{1-c_1}\)

Numerical simulations: how? (3/3)

  • to solve this system numerically we use an approach that is based on iteration:
  1. choose a set of numerical parameter values (e.g. \(c_0=3\), \(c_1=0.8\))

  2. choose (arbitrary but non-zero) initial values for the endogenous variables (e.g. \(Y=C=1)\)

  3. then solve the system of equations many times using a for loop

  4. in this way, solution gets progressively approximated

  • NB: we are only analysing a special case (a specific parameterisation, e.g. \(c_0=3\), \(c_1=0.8\))

Numerical simulations: example (1/5)

  • let’s see how the Keynesian goods market model can be simulated numerically
  • tell me whether you want me to proceed in Python or R

https://vevox.app/#/m/172648080

Numerical simulations: example in R (2/5)

R code

### Simulate Keynesian goods market model via iteration

#Clear the environment 
rm(list=ls(all=TRUE))

# Set number of parameterisations that will be considered
S=2

# Set fixed parameter values
c0=3
c1=0.8

#Create vector in which equilibrium solutions from different parameterisations will be stored
Y_eq=vector(length=S)
C_eq=vector(length=S)

#Create vector with exogenous variable that will change 
G0=vector(length=S)
G0[1]=5
G0[2]=6

# Initialise endogenous variables at arbitrary positive value 
Y=C=1

#Solve this system numerically through 1000 iterations based on the initialisation
for (i in 1:S){
  for (iteration in 1:1000){
  Y = C + G0[i]
  C = c0 + c1*Y
  } # close iterations loop
  
#Save results for different parameterisations in vector
Y_eq[i]=Y
C_eq[i]=C
} # close parameterisations loop


# Verify solution for Y*
Y_eq[1]           # numerical solution

Numerical simulations: example in R (2/5)

[1] 40
(c0+G0[1])/(1-c1) # analytical solution
[1] 40

Numerical simulations: example in R (3/5)

# Bar chart of different equilibrium solutions of Keynesian model
barplot(Y_eq, ylab="Y", main="Output", names.arg=c("Baseline", "Increase in G0"))

Numerical simulations: example in R (3/5)

Numerical simulations: example in Python (4/5)

Python code

### Simulate Keynesian goods market model via iteration

# Load NumPy library
import numpy as np

# Set the number of parameterisations that will be considered
S = 2

# Set fixed parameter values
c0 = 3
c1 = 0.8

# Create numpy arrays in which equilibrium solutions from different parameterisations will be stored
Y_eq = np.zeros(S)
C_eq = np.zeros(S)

# Create a numpy array with the exogenous variable that will change
G0 = np.zeros(S)
G0[0] = 5
G0[1] = 6

# Initialize endogenous variables at an arbitrary positive value
Y = C = 1

# Solve this system numerically through 1000 iterations based on the initialization
for i in range(S):
    for iteration in range(1000):
        Y = C + G0[i]
        C = c0 + c1 * Y

    # Save results for different parameterisations in the numpy arrays
    Y_eq[i] = Y
    C_eq[i] = C


# Verify solution for Y*
Y_eq[0]           # numerical solution

Numerical simulations: example in Python (4/5)

39.999999999999986
(c0+G0[0])/(1-c1) # analytical solution
40.00000000000001

Numerical simulations: example in Python (5/5)

import matplotlib.pyplot as plt

# Create the bar chart
names = ["Baseline", "Increase in G0"]
plt.bar(names, Y_eq, color='gray')

# Add labels and title
plt.ylabel("Y")
plt.title("Output")

# Show the plot
plt.show()

Numerical simulations: example in Python (5/5)

Simulating a model from the website

  • let’s simulate a model from the website
  • take your pick!

https://vevox.app/#/m/172648080