Saturday, October 28, 2023

Leontief Model Simulation with Python Part 1: Simulating "Dynamic Input-Output Model" by RCD Sir with Python

I have simulated "Dynamic Input-Output Model" by RCD Sir shown in the following YouTube with Python.


This is my first step to experiment simulating Leontief's Input-Output econometric model: The first one is the basic model utilising the inverse matrix to find the output level.

The output is derived from the final demand multiplied by the inverse matrix of an identity matrix minus the coefficient matrix and the capital matrix.

A list of my Python code based on the mathematical modelling is as follows:

# importing necessary tools
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.linalg

print('Simulating the example https://www.youtube.com/watch?v=KmVfmISjayA&t=134s')

Sctrs=['Sec1','Sec2','Sec3']

# A: Input Coefficient Matrix
A = np.array([[0.2, 0.3, 0.2],[0.4,0.1,0.2],[0.1,0.3,0.2]])
Pdf_A = pd.DataFrame(data = A,index = Sctrs,columns = Sctrs)
display('A: Input Coefficient Matrix',Pdf_A)

# B: Capital Coefficient Matrix
B = np.array([[0.1,0.2,0.1],[0.2,0.1,0.2],[0.1,0.2,0.1]])
Pdf_B = pd.DataFrame(data = B,index = Sctrs,columns = Sctrs)
display('B: Capital Coefficient Matrix',Pdf_B)

# G: Diagonal matrix of sector growth rates
G = np.array([[0.2,0,0],[0,0.2,0],[0,0,0.1]])
Pdf_G = pd.DataFrame(data = G,index = Sctrs,columns = Sctrs)
display('G: Diagonal matrix of sector growth rates',Pdf_G)

# F: Final Demand Vector
d = np.array([400,300,500])
Pdf_d = pd.DataFrame(data = d,index = Sctrs)
display('d: Final Demand Vector',Pdf_d)

# Identity Matrix
I=np.identity(len(A))
Pdf_I = pd.DataFrame(data = I,index = Sctrs)
display(f'I: {len(A)} by {len(A)} identity matrix',Pdf_I)


BG=np.dot(B,G)
Pdf_BG = pd.DataFrame(data = BG,index = Sctrs,columns = Sctrs)
display('BG is',Pdf_BG)

print('When the output vector is x \n x = Ax + BGx + d \n and therefore \n (I - A - BG)x = d .')

I_A_BG=I-A-BG
Pdf_I_A_BG = pd.DataFrame(data = I_A_BG,index = Sctrs,columns = Sctrs)
display('(I-A-BG) is',Pdf_I_A_BG)

print('Then \n x = (I - A - BG)^-1 d .')

Inv_I_A_BG=np.linalg.inv(I_A_BG)
Pdf_Inv_I_A_BG = pd.DataFrame(data = Inv_I_A_BG,index = Sctrs,columns = Sctrs)
display('(I-A-BG)^-1 is',Pdf_Inv_I_A_BG)

x= np.dot(Inv_I_A_BG,d)
Pdf_x = pd.DataFrame(data = x,index = Sctrs)
display('x = (I - A - BG)^-1 d =',Pdf_x)