Pymrio provides parsing function to load existing MRIO databases. However, it is also possible to assign data directly to the attributes of an IOSystem instance.
This tutorial exemplify this functionality. The tables used here are taken from Miller and Blair (2009): Miller, Ronald E, and Peter D Blair. Input-Output Analysis: Foundations and Extensions. Cambridge (England); New York: Cambridge University Press, 2009. ISBN: 978-0-521-51713-3
First import the pymrio module and other packages needed:
In [1]:
import pymrio
import pandas as pd
import numpy as np
For this example we use the IO table given in Miller and Blair (2009): Table 2.3 (on page 22 in the 2009 edition).
This table contains an interindustry trade flow matrix, final demand columns for household demand and exports and a value added row. The latter we consider as an extensions (factor inputs). To assign these values to the IOSystem attributes, the tables must be pandas DataFrames with multiindex for columns and index.
First we set up the Z matrix by defining the index of rows and columns. The example IO tables contains only domestic tables, but since pymrio was designed with multi regions IO tables in mind, also a region index is needed.
In [2]:
_sectors = ['sector1', 'sector2']
_regions = ['reg1']
_Z_multiindex = pd.MultiIndex.from_product(
[_regions, _sectors], names = [u'region', u'sector'])
Next we setup the total Z matrix. Here we just put in the name the values manually. However, pandas provides several possibility to ease the data input.
In [3]:
Z = pd.DataFrame(
data = np.array([
[150,500],
[200,100]]),
index = _Z_multiindex,
columns = _Z_multiindex
)
In [4]:
Z
Out[4]:
Final demand is treated in the same way:
In [5]:
_categories = ['final demand']
_fd_multiindex = pd.MultiIndex.from_product(
[_regions, _categories], names = [u'region', u'category'])
In [6]:
Y = pd.DataFrame(
data=np.array([[350], [1700]]),
index = _Z_multiindex,
columns = _fd_multiindex)
In [7]:
Y
Out[7]:
Factor inputs are given as 'Payment sectors' in the table:
In [8]:
F = pd.DataFrame(
data = np.array([[650, 1400]]),
index = ['Payments_sectors'],
columns = _Z_multiindex)
In [9]:
F
Out[9]:
In the next step, an empty instance of an IOSYstem has to be set up.
In [10]:
io = pymrio.IOSystem()
Now we can add the tables to the IOSystem instance:
In [11]:
io.Z = Z
io.Y = Y
Extension are defined as objects within the IOSystem. The Extension instance can be instanced independently (the parameter 'name' is required):
In [12]:
factor_input = pymrio.Extension(name = 'Factor Input', F=F)
In [13]:
io.factor_input = factor_input
For consistency and plotting we can add a DataFrame containg the units per row:
In [14]:
io.factor_input.unit = pd.DataFrame(data = ['USD'], index = F.index, columns = ['unit'])
We can check whats in the system:
In [15]:
str(io)
Out[15]:
At this point we have everything to calculate the full IO system.
In [16]:
io.calc_all()
Out[16]:
This gives, among others, the A and L matrix which we can compare with the tables given in Miller and Blair (2009) (Table 2.4 and L given on the next page afterwards):
In [17]:
io.A
Out[17]:
In [18]:
io.L
Out[18]:
The example in Miller and Blair (2009) goes on with using the L matrix to calculate the new industry output x for a changing finald demand Y. This step can easly reproduced with the pymrio module.
To do so we first have to set up the new final demand:
In [19]:
Ynew = Y.copy()
Ynew[('reg1','final_demand')] = np.array([[600],
[1500]])
We copy the original IOSystem:
In [20]:
io_new_fd = io.copy()
To calculate for the new final demand we have to remove everything from the system except for the coefficients (A,L,S,M)
In [21]:
io_new_fd.reset_all_to_coefficients()
Out[21]:
Now we can assign the new final demand and recalculate the system:
In [22]:
io_new_fd.Y = Ynew
In [23]:
io_new_fd.calc_all()
Out[23]:
The new x equalls the xnew values given in Miller and Blair (2009) at formula 2.13:
In [24]:
io_new_fd.x
Out[24]:
As for all IO System, we can have a look at the modification history:
In [25]:
io_new_fd.meta
Out[25]: