{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using pymrio without a parser (small IO example)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "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\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preperation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import pymrio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First import the pymrio module and other packages needed:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "import pymrio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get external IO table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this example we use the IO table given in *Miller and Blair (2009)*: Table 2.3 (on page 22 in the 2009 edition)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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\n", "DataFrames with multiindex for columns and index." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "_sectors = [\"sector1\", \"sector2\"]\n", "_regions = [\"reg1\"]\n", "_Z_multiindex = pd.MultiIndex.from_product(\n", " [_regions, _sectors], names=[\"region\", \"sector\"]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "Z = pd.DataFrame(\n", " data=np.array([[150, 500], [200, 100]]), index=_Z_multiindex, columns=_Z_multiindex\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regionreg1
sectorsector1sector2
regionsector
reg1sector1150500
sector2200100
\n", "
" ], "text/plain": [ "region reg1 \n", "sector sector1 sector2\n", "region sector \n", "reg1 sector1 150 500\n", " sector2 200 100" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Final demand is treated in the same way:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "_categories = [\"final demand\"]\n", "_fd_multiindex = pd.MultiIndex.from_product(\n", " [_regions, _categories], names=[\"region\", \"category\"]\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "Y = pd.DataFrame(\n", " data=np.array([[350], [1700]]), index=_Z_multiindex, columns=_fd_multiindex\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regionreg1
categoryfinal demand
regionsector
reg1sector1350
sector21700
\n", "
" ], "text/plain": [ "region reg1\n", "category final demand\n", "region sector \n", "reg1 sector1 350\n", " sector2 1700" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Factor inputs are given as 'Payment sectors' in the table:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "F = pd.DataFrame(\n", " data=np.array([[650, 1400]]), index=[\"Payments_sectors\"], columns=_Z_multiindex\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regionreg1
sectorsector1sector2
Payments_sectors6501400
\n", "
" ], "text/plain": [ "region reg1 \n", "sector sector1 sector2\n", "Payments_sectors 650 1400" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "F" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Include tables in the IOSystem object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the next step, an empty instance of an IOSYstem has to be set up." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "io = pymrio.IOSystem()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can add the tables to the IOSystem instance:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "io.Z = Z\n", "io.Y = Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extension are defined as objects within the IOSystem. The Extension instance can be instanced independently (the parameter 'name' is required):" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "factor_input = pymrio.Extension(name=\"Factor Input\", F=F)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "io.factor_input = factor_input" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For consistency and plotting we can add a DataFrame containg the units per row:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "io.factor_input.unit = pd.DataFrame(data=[\"USD\"], index=F.index, columns=[\"unit\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check whats in the system:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'IO System with parameters: Z, Y, meta, factor_input'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(io)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point we have everything to calculate the full IO system." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate the missing parts" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io.calc_all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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):" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regionreg1
sectorsector1sector2
regionsector
reg1sector10.150.25
sector20.200.05
\n", "
" ], "text/plain": [ "region reg1 \n", "sector sector1 sector2\n", "region sector \n", "reg1 sector1 0.15 0.25\n", " sector2 0.20 0.05" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io.A" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regionreg1
sectorsector1sector2
regionsector
reg1sector11.2541250.330033
sector20.2640261.122112
\n", "
" ], "text/plain": [ "region reg1 \n", "sector sector1 sector2\n", "region sector \n", "reg1 sector1 1.254125 0.330033\n", " sector2 0.264026 1.122112" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io.L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update to system for a new final demand" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "To do so we first have to set up the new final demand:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "Ynew = Y.copy()\n", "Ynew[(\"reg1\", \"final_demand\")] = np.array([[600], [1500]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We copy the original IOSystem:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "io_new_fd = io.copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To calculate for the new final demand we have to remove everything from the system except for the coefficients (A,L,S,M)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io_new_fd.reset_all_to_coefficients()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can assign the new final demand and recalculate the system:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "io_new_fd.Y = Ynew" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io_new_fd.calc_all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The new x equalls the xnew values given in *Miller and Blair (2009)* at formula 2.13:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
indout
regionsector
reg1sector12247.524752
sector23841.584158
\n", "
" ], "text/plain": [ " indout\n", "region sector \n", "reg1 sector1 2247.524752\n", " sector2 3841.584158" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io_new_fd.x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As for all IO System, we can have a look at the modification history:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Description: Metadata for pymrio\n", "MRIO Name: IO_copy\n", "System: None\n", "Version: None\n", "File: None\n", "History:\n", "20210224 10:42:06 - MODIFICATION - Calculating accounts for extension factor_input\n", "20210224 10:42:06 - MODIFICATION - Flow matrix Z calculated\n", "20210224 10:42:06 - MODIFICATION - Industry Output x calculated\n", "20210224 10:42:06 - MODIFICATION - Reset full system to coefficients\n", "20210224 10:42:06 - NOTE - IOSystem copy IO_copy based on IO\n", "20210224 10:42:06 - MODIFICATION - Calculating accounts for extension factor_input\n", "20210224 10:42:06 - MODIFICATION - Leontief matrix L calculated\n", "20210224 10:42:06 - MODIFICATION - Coefficient matrix A calculated\n", "20210224 10:42:06 - MODIFICATION - Industry output x calculated" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "io_new_fd.meta" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }