{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "30a9b425",
   "metadata": {},
   "source": [
    "# Input-Output Models"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "999746e8",
   "metadata": {},
   "source": [
    "## Overview\n",
    "\n",
    "This lecture requires the following imports and installs before we proceed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bee3a1bf",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "!pip install quantecon_book_networks\n",
    "!pip install quantecon\n",
    "!pip install pandas-datareader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7df53c9",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import networkx as nx\n",
    "import matplotlib.pyplot as plt\n",
    "import quantecon_book_networks\n",
    "import quantecon_book_networks.input_output as qbn_io\n",
    "import quantecon_book_networks.plotting as qbn_plt\n",
    "import quantecon_book_networks.data as qbn_data\n",
    "import matplotlib as mpl\n",
    "from matplotlib.patches import Polygon\n",
    "\n",
    "quantecon_book_networks.config(\"matplotlib\")\n",
    "mpl.rcParams.update(mpl.rcParamsDefault)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f62416f",
   "metadata": {},
   "source": [
    "The following figure illustrates a network of linkages among 15 sectors\n",
    "obtained from the US Bureau of Economic Analysis’s 2021 Input-Output Accounts\n",
    "Data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed354938",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "def build_coefficient_matrices(Z, X):\n",
    "    \"\"\"\n",
    "    Build coefficient matrices A and F from Z and X via\n",
    "\n",
    "        A[i, j] = Z[i, j] / X[j]\n",
    "        F[i, j] = Z[i, j] / X[i]\n",
    "\n",
    "    \"\"\"\n",
    "    A, F = np.empty_like(Z), np.empty_like(Z)\n",
    "    n = A.shape[0]\n",
    "    for i in range(n):\n",
    "        for j in range(n):\n",
    "            A[i, j] = Z[i, j] / X[j]\n",
    "            F[i, j] = Z[i, j] / X[i]\n",
    "\n",
    "    return A, F\n",
    "\n",
    "ch2_data = qbn_data.production()\n",
    "codes = ch2_data[\"us_sectors_15\"][\"codes\"]\n",
    "Z = ch2_data[\"us_sectors_15\"][\"adjacency_matrix\"]\n",
    "X = ch2_data[\"us_sectors_15\"][\"total_industry_sales\"]\n",
    "A, F = build_coefficient_matrices(Z, X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4bfecca",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "centrality = qbn_io.eigenvector_centrality(A)\n",
    "\n",
    "# Remove self-loops\n",
    "for i in range(A.shape[0]):\n",
    "    A[i][i] = 0\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(8, 10))\n",
    "plt.axis(\"off\")\n",
    "color_list = qbn_io.colorise_weights(centrality,beta=False)\n",
    "\n",
    "qbn_plt.plot_graph(A, X, ax, codes,\n",
    "              layout_type='spring',\n",
    "              layout_seed=5432167,\n",
    "              tol=0.0,\n",
    "              node_color_list=color_list)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00bb0bf7",
   "metadata": {},
   "source": [
    "|Label|Sector|Label|Sector|Label|Sector|\n",
    "|:--------------:|:--------------:|:--------------:|:--------------:|:--------------:|:--------------:|\n",
    "|ag|Agriculture|wh|Wholesale|pr|Professional Services|\n",
    "|mi|Mining|re|Retail|ed|Education & Health|\n",
    "|ut|Utilities|tr|Transportation|ar|Arts & Entertainment|\n",
    "|co|Construction|in|Information|ot|Other Services (exc govt)|\n",
    "|ma|Manufacturing|fi|Finance|go|Government|\n",
    "An arrow from $ i $ to $ j $ means that some of sector $ i $’s output serves as an input to production of  sector $ j $.\n",
    "\n",
    "Economies are characterised by many such links.\n",
    "\n",
    "A basic framework for their analysis is\n",
    "[Leontief’s](https://en.wikipedia.org/wiki/Wassily_Leontief) input-output model.\n",
    "\n",
    "After  introducing the input-output model, we describe some of its connections to [linear programming lecture](https://intro.quantecon.org/lp_intro.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d89bc1cf",
   "metadata": {},
   "source": [
    "## Input-output analysis\n",
    "\n",
    "Let\n",
    "\n",
    "- $ x_0 $ be the amount of a single exogenous input to production, say labor  \n",
    "- $ x_j, j = 1,\\ldots n $ be the gross output of final good $ j $  \n",
    "- $ d_j, j = 1,\\ldots n $ be the net output of final good $ j $ that is available for final consumption  \n",
    "- $ z_{ij} $ be the quantity of good $ i $ allocated to be an input to producing good $ j $ for $ i=1, \\ldots n $, $ j = 1, \\ldots n $  \n",
    "- $ z_{0j} $ be the quantity of labor allocated to producing good $ j $.  \n",
    "- $ a_{ij} $ be the number of units of good $ i $ required to produce one unit of good $ j $, $ i=0, \\ldots, n, j= 1, \\ldots n $.  \n",
    "- $ w >0 $ be an exogenous wage of labor, denominated in dollars per unit of labor  \n",
    "- $ p $ be an $ n \\times 1 $ vector of prices of produced goods $ i = 1, \\ldots , n $.  \n",
    "\n",
    "\n",
    "The technology for producing  good $ j \\in \\{1, \\ldots , n\\} $ is  described by the **Leontief** function\n",
    "\n",
    "$$\n",
    "x_j = \\min_{i \\in \\{0, \\ldots , n \\}} \\left( \\frac{z_{ij}}{a_{ij}}\\right)\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec4d910c",
   "metadata": {},
   "source": [
    "### Two goods\n",
    "\n",
    "To illustrate, we begin by setting $ n =2 $ and formulating\n",
    "the following network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bee6a40",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "G = nx.DiGraph()\n",
    "\n",
    "nodes= (1, 2, 'c')\n",
    "edges = ((1, 1), (1, 2), (2, 1), (2, 2), (1, 'c'), (2, 'c'))\n",
    "edges1 = ((1, 1), (1, 2), (2, 1), (2, 2), (1, 'c'))\n",
    "edges2 = [(2,'c')]\n",
    "G.add_nodes_from(nodes)\n",
    "G.add_edges_from(edges)\n",
    "\n",
    "pos_list = ([0, 0], [2, 0], [1, -1])\n",
    "pos = dict(zip(G.nodes(), pos_list))\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "plt.axis(\"off\")\n",
    "\n",
    "nx.draw_networkx_nodes(G, pos=pos, node_size=800,\n",
    "                       node_color='white', edgecolors='black')\n",
    "nx.draw_networkx_labels(G, pos=pos)\n",
    "nx.draw_networkx_edges(G,pos=pos, edgelist=edges1,\n",
    "                       node_size=300, connectionstyle='arc3,rad=0.2',\n",
    "                       arrowsize=10, min_target_margin=15)\n",
    "nx.draw_networkx_edges(G, pos=pos, edgelist=edges2,\n",
    "                       node_size=300, connectionstyle='arc3,rad=-0.2',\n",
    "                       arrowsize=10, min_target_margin=15)\n",
    "\n",
    "plt.text(0.055, 0.125, r'$z_{11}$')\n",
    "plt.text(1.825, 0.125, r'$z_{22}$')\n",
    "plt.text(0.955, 0.1, r'$z_{21}$')\n",
    "plt.text(0.955, -0.125, r'$z_{12}$')\n",
    "plt.text(0.325, -0.5, r'$d_{1}$')\n",
    "plt.text(1.6, -0.5, r'$d_{2}$')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f70a75e",
   "metadata": {},
   "source": [
    "*Feasible allocations must satisfy*\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "(1 - a_{11}) x_1 - a_{12} x_2 & \\geq d_1 \\cr\n",
    "-a_{21} x_1 + (1 - a_{22}) x_2 & \\geq d_2 \\cr\n",
    "a_{01} x_1 + a_{02} x_2 & \\leq x_0\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "This can be graphically represented as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f894a0c7",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.grid()\n",
    "\n",
    "# Draw constraint lines\n",
    "ax.hlines(0, -1, 400)\n",
    "ax.vlines(0, -1, 200)\n",
    "\n",
    "ax.plot(np.linspace(55, 380, 100), (50-0.9*np.linspace(55, 380, 100))/(-1.46), color=\"r\")\n",
    "ax.plot(np.linspace(-1, 400, 100), (60+0.16*np.linspace(-1, 400, 100))/0.83, color=\"r\")\n",
    "ax.plot(np.linspace(250, 395, 100), (62-0.04*np.linspace(250, 395, 100))/0.33, color=\"b\")\n",
    "\n",
    "ax.text(130, 38, r\"$(1-a_{11})x_1 + a_{12}x_2 \\geq d_1$\", size=10)\n",
    "ax.text(10, 105, r\"$-a_{21}x_1 + (1-a_{22})x_2 \\geq d_2$\", size=10)\n",
    "ax.text(150, 150, r\"$a_{01}x_1 +a_{02}x_2 \\leq x_0$\", size=10)\n",
    "\n",
    "# Draw the feasible region\n",
    "feasible_set = Polygon(np.array([[301, 151],\n",
    "                                 [368, 143],\n",
    "                                 [250, 120]]),\n",
    "                       color=\"cyan\")\n",
    "ax.add_patch(feasible_set)\n",
    "\n",
    "# Draw the optimal solution\n",
    "ax.plot(250, 120, \"*\", color=\"black\")\n",
    "ax.text(260, 115, \"solution\", size=10)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9e05c07",
   "metadata": {},
   "source": [
    "More generally,   constraints on production are\n",
    "\n",
    "\n",
    "<a id='equation-eq-inout-1'></a>\n",
    "$$\n",
    "\\begin{aligned}\n",
    "(I - A) x &  \\geq d \\cr\n",
    "a_0^\\top x & \\leq x_0\n",
    "\\end{aligned} \\tag{40.1}\n",
    "$$\n",
    "\n",
    "where $ A $ is the $ n \\times n $ matrix with typical element $ a_{ij} $ and $ a_0^\\top = \\begin{bmatrix} a_{01} & \\cdots & a_{0n} \\end{bmatrix} $.\n",
    "\n",
    "If we solve the first block of equations of [(40.1)](#equation-eq-inout-1) for gross output $ x $ we get\n",
    "\n",
    "\n",
    "<a id='equation-eq-inout-2'></a>\n",
    "$$\n",
    "x = (I -A)^{-1} d \\equiv L d \\tag{40.2}\n",
    "$$\n",
    "\n",
    "where the matrix $ L = (I-A)^{-1} $ is sometimes called  a **Leontief Inverse**.\n",
    "\n",
    "To assure that the solution $ X $ of [(40.2)](#equation-eq-inout-2) is a positive vector, the  following **Hawkins-Simon conditions** suffice:\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\det (I - A) > 0 \\text{ and} \\;\\;\\; \\\\\n",
    "(I-A)_{ij} > 0 \\text{ for all } i=j\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23422189",
   "metadata": {},
   "source": [
    "### \n",
    "\n",
    "For example a two-good economy described by\n",
    "\n",
    "\n",
    "<a id='equation-eq-inout-ex'></a>\n",
    "$$\n",
    "A =\n",
    "\\begin{bmatrix}\n",
    "    0.1 & 40 \\\\\n",
    "    0.01 & 0\n",
    "\\end{bmatrix}\n",
    "\\text{ and }\n",
    "d =\n",
    "\\begin{bmatrix}\n",
    "    50 \\\\\n",
    "    2\n",
    "\\end{bmatrix} \\tag{40.3}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c9c14ae",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "A = np.array([[0.1, 40],\n",
    "             [0.01, 0]])\n",
    "d = np.array([50, 2]).reshape((2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eef600c2",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "I = np.identity(2)\n",
    "B = I - A\n",
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "263bc7a4",
   "metadata": {},
   "source": [
    "Let’s check the **Hawkins-Simon conditions**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9afaee5b",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "np.linalg.det(B) > 0 # checking Hawkins-Simon conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "103525ad",
   "metadata": {},
   "source": [
    "Now, let’s compute the **Leontief inverse** matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "499ff52b",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "L = np.linalg.inv(B) # obtaining Leontief inverse matrix\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94cbe64f",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "x = L @ d   # solving for gross output\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a3170f6",
   "metadata": {},
   "source": [
    "## Production possibility frontier\n",
    "\n",
    "The second equation of [(40.1)](#equation-eq-inout-1) can be written\n",
    "\n",
    "$$\n",
    "a_0^\\top x = x_0\n",
    "$$\n",
    "\n",
    "or\n",
    "\n",
    "\n",
    "<a id='equation-eq-inout-frontier'></a>\n",
    "$$\n",
    "A_0^\\top d = x_0 \\tag{40.4}\n",
    "$$\n",
    "\n",
    "where\n",
    "\n",
    "$$\n",
    "A_0^\\top = a_0^\\top (I - A)^{-1}\n",
    "$$\n",
    "\n",
    "For $ i \\in \\{1, \\ldots , n\\} $, the $ i $th component  of $ A_0 $ is the amount of labor that is required to produce one unit of final output of good $ i $.\n",
    "\n",
    "Equation [(40.4)](#equation-eq-inout-frontier) sweeps out a  **production possibility frontier** of final consumption bundles $ d $ that can be produced with exogenous labor input $ x_0 $."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef051337",
   "metadata": {},
   "source": [
    "## \n",
    "\n",
    "Consider the example in [(40.3)](#equation-eq-inout-ex).\n",
    "\n",
    "Suppose we are now given\n",
    "\n",
    "$$\n",
    "a_0^\\top = \\begin{bmatrix}\n",
    "4 & 100\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "Then we can find $ A_0^\\top $ by"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "327321c7",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "a0 = np.array([4, 100])\n",
    "A0 = a0 @ L\n",
    "A0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06f10c90",
   "metadata": {},
   "source": [
    "Thus, the production possibility frontier for this economy is\n",
    "\n",
    "$$\n",
    "10d_1 + 500d_2 = x_0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6030cb7",
   "metadata": {},
   "source": [
    "## Prices\n",
    "\n",
    "[[Dorfman *et al.*, 1958](https://intro.quantecon.org/zreferences.html#id59)] argue that relative prices of the $ n $ produced goods must satisfy\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "p_1 = a_{11}p_1 + a_{21}p_2 + a_{01}w \\\\\n",
    "p_2 = a_{12}p_1 + a_{22}p_2 + a_{02}w\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "More generally,\n",
    "\n",
    "$$\n",
    "p = A^\\top p + a_0 w\n",
    "$$\n",
    "\n",
    "which states that the price of each final good equals the total cost\n",
    "of production, which consists of costs of intermediate inputs $ A^\\top p $\n",
    "plus costs of labor $ a_0 w $.\n",
    "\n",
    "This equation can be written as\n",
    "\n",
    "\n",
    "<a id='equation-eq-inout-price'></a>\n",
    "$$\n",
    "(I - A^\\top) p = a_0 w \\tag{40.5}\n",
    "$$\n",
    "\n",
    "which implies\n",
    "\n",
    "$$\n",
    "p = (I - A^\\top)^{-1} a_0 w\n",
    "$$\n",
    "\n",
    "Notice how  [(40.5)](#equation-eq-inout-price) with [(40.1)](#equation-eq-inout-1) forms a\n",
    "**conjugate pair**  through the appearance of operators\n",
    "that are transposes of one another.\n",
    "\n",
    "This connection surfaces again in a classic linear program and its dual."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8590c368",
   "metadata": {},
   "source": [
    "## Linear programs\n",
    "\n",
    "A **primal** problem is\n",
    "\n",
    "$$\n",
    "\\min_{x} w a_0^\\top x\n",
    "$$\n",
    "\n",
    "subject to\n",
    "\n",
    "$$\n",
    "(I - A) x \\geq d\n",
    "$$\n",
    "\n",
    "The associated **dual** problem is\n",
    "\n",
    "$$\n",
    "\\max_{p} p^\\top d\n",
    "$$\n",
    "\n",
    "subject to\n",
    "\n",
    "$$\n",
    "(I -A)^\\top p \\leq a_0 w\n",
    "$$\n",
    "\n",
    "The primal problem chooses a feasible production plan to minimize costs for delivering a pre-assigned vector of final goods consumption $ d $.\n",
    "\n",
    "The dual problem chooses prices to maximize the value of a pre-assigned vector of final goods $ d $ subject to prices covering costs of production.\n",
    "\n",
    "By the [strong duality theorem](https://en.wikipedia.org/wiki/Dual_linear_program#Strong_duality),\n",
    "optimal value of the primal and dual problems coincide:\n",
    "\n",
    "$$\n",
    "w a_0^\\top x^* = p^* d\n",
    "$$\n",
    "\n",
    "where $ ^* $’s denote optimal choices for the primal and dual problems.\n",
    "\n",
    "The dual problem can be graphically represented as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83f1a88b",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.grid()\n",
    "\n",
    "# Draw constraint lines\n",
    "ax.hlines(0, -1, 50)\n",
    "ax.vlines(0, -1, 250)\n",
    "\n",
    "ax.plot(np.linspace(4.75, 49, 100), (4-0.9*np.linspace(4.75, 49, 100))/(-0.16), color=\"r\")\n",
    "ax.plot(np.linspace(0, 50, 100), (33+1.46*np.linspace(0, 50, 100))/0.83, color=\"r\")\n",
    "\n",
    "ax.text(15, 175, r\"$(1-a_{11})p_1 - a_{21}p_2 \\leq a_{01}w$\", size=10)\n",
    "ax.text(30, 85, r\"$-a_{12}p_1 + (1-a_{22})p_2 \\leq a_{02}w$\", size=10)\n",
    "\n",
    "# Draw the feasible region\n",
    "feasible_set = Polygon(np.array([[17, 69],\n",
    "                                 [4, 0],\n",
    "                                 [0,0],\n",
    "                                 [0, 40]]),\n",
    "                       color=\"cyan\")\n",
    "ax.add_patch(feasible_set)\n",
    "\n",
    "# Draw the optimal solution\n",
    "ax.plot(17, 69, \"*\", color=\"black\")\n",
    "ax.text(18, 60, \"dual solution\", size=10)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c900ddb",
   "metadata": {},
   "source": [
    "## Leontief inverse\n",
    "\n",
    "We have discussed that gross output $ x $ is given by [(40.2)](#equation-eq-inout-2), where $ L $ is called the Leontief Inverse.\n",
    "\n",
    "Recall the [Neumann Series Lemma](https://intro.quantecon.org/eigen_II.html) which states that $ L $ exists if the spectral radius $ r(A)<1 $.\n",
    "\n",
    "In fact\n",
    "\n",
    "$$\n",
    "L = \\sum_{i=0}^{\\infty} A^i\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91cb8b65",
   "metadata": {},
   "source": [
    "### Demand shocks\n",
    "\n",
    "Consider the impact of a demand shock $ \\Delta d $ which shifts demand from $ d_0 $ to $ d_1 = d_0 + \\Delta d $.\n",
    "\n",
    "Gross output shifts from $ x_0 = Ld_0 $ to $ x_1 = Ld_1 $.\n",
    "\n",
    "If $ r(A) < 1 $ then a solution exists and\n",
    "\n",
    "$$\n",
    "\\Delta x = L \\Delta d = \\Delta d + A(\\Delta d) + A^2 (\\Delta d) + \\cdots\n",
    "$$\n",
    "\n",
    "This illustrates that an element $ l_{ij} $ of $ L $ shows the total impact on sector $ i $ of a unit change in demand of good $ j $."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ebc7ebe",
   "metadata": {},
   "source": [
    "## Applications of graph theory\n",
    "\n",
    "We can further study input-output networks through applications of [graph theory](https://intro.quantecon.org/networks.html).\n",
    "\n",
    "An input-output network can be represented by a weighted directed graph induced by the adjacency matrix $ A $.\n",
    "\n",
    "The set of nodes $ V = [n] $ is the list of sectors and the set of edges is given by\n",
    "\n",
    "$$\n",
    "E = \\{(i,j) \\in V \\times V : a_{ij}>0\\}\n",
    "$$\n",
    "\n",
    "In Fig. 40.1 weights are indicated by the widths of the arrows, which are proportional to the corresponding input-output coefficients.\n",
    "\n",
    "We can now use centrality measures to rank sectors and discuss their importance relative to the other sectors."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b06dc0c",
   "metadata": {},
   "source": [
    "### Eigenvector centrality\n",
    "\n",
    "Eigenvector centrality of a node $ i $ is measured by\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "    e_i = \\frac{1}{r(A)} \\sum_{1 \\leq j \\leq n} a_{ij} e_j\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "We plot a bar graph of hub-based eigenvector centrality for the sectors represented in Fig. 40.1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97b09475",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.bar(codes, centrality, color=color_list, alpha=0.6)\n",
    "ax.set_ylabel(\"eigenvector centrality\", fontsize=12)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4556c56",
   "metadata": {},
   "source": [
    "A higher measure indicates higher importance as a supplier.\n",
    "\n",
    "As a result demand shocks in most sectors will significantly impact activity in sectors with high eigenvector centrality.\n",
    "\n",
    "The above figure indicates that manufacturing is the most dominant sector in the US economy."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2e9c8c1",
   "metadata": {},
   "source": [
    "### Output multipliers\n",
    "\n",
    "Another way to rank sectors in input-output networks is via output multipliers.\n",
    "\n",
    "The **output multiplier** of sector $ j $ denoted by $ \\mu_j $ is usually defined as the\n",
    "total sector-wide impact of a unit change of demand in sector $ j $.\n",
    "\n",
    "Earlier when disussing demand shocks we concluded that for $ L = (l_{ij}) $ the element\n",
    "$ l_{ij} $ represents the impact on sector $ i $ of a unit change in demand in sector $ j $.\n",
    "\n",
    "Thus,\n",
    "\n",
    "$$\n",
    "\\mu_j = \\sum_{j=1}^n l_{ij}\n",
    "$$\n",
    "\n",
    "This can be written as $ \\mu^\\top = \\mathbb{1}^\\top L $ or\n",
    "\n",
    "$$\n",
    "\\mu^\\top = \\mathbb{1}^\\top (I-A)^{-1}\n",
    "$$\n",
    "\n",
    "Please note that here we use $ \\mathbb{1} $ to represent a vector of ones.\n",
    "\n",
    "High ranking sectors within this measure are important buyers of intermediate goods.\n",
    "\n",
    "A demand shock in such sectors will cause a large impact on the whole production network.\n",
    "\n",
    "The following figure displays the output multipliers for the sectors represented\n",
    "in Fig. 40.1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf245f26",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "A, F = build_coefficient_matrices(Z, X)\n",
    "omult = qbn_io.katz_centrality(A, authority=True)\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "omult_color_list = qbn_io.colorise_weights(omult,beta=False)\n",
    "ax.bar(codes, omult, color=omult_color_list, alpha=0.6)\n",
    "ax.set_ylabel(\"Output multipliers\", fontsize=12)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "766db454",
   "metadata": {},
   "source": [
    "We observe that manufacturing and agriculture are highest ranking sectors."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b90d2ad",
   "metadata": {},
   "source": [
    "## Exercises"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9109bf3f",
   "metadata": {},
   "source": [
    "## Exercise 40.1\n",
    "\n",
    "[[Dorfman *et al.*, 1958](https://intro.quantecon.org/zreferences.html#id59)] Chapter 9 discusses an example with the following\n",
    "parameter settings:\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix}\n",
    "     0.1 & 1.46 \\\\\n",
    "     0.16 & 0.17\n",
    "    \\end{bmatrix}\n",
    "\\text{ and }\n",
    "a_0 = \\begin{bmatrix} .04 & .33 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "$$\n",
    "x = \\begin{bmatrix} 250 \\\\ 120 \\end{bmatrix}\n",
    "\\text{ and }\n",
    "x_0 = 50\n",
    "$$\n",
    "\n",
    "$$\n",
    "d = \\begin{bmatrix} 50 \\\\ 60 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "Describe how they infer the input-output coefficients in $ A $ and $ a_0 $ from the following hypothetical underlying “data” on agricultural and  manufacturing industries:\n",
    "\n",
    "$$\n",
    "z = \\begin{bmatrix} 25 & 175 \\\\\n",
    "         40 &   20 \\end{bmatrix}\n",
    "\\text{ and }\n",
    "z_0 = \\begin{bmatrix} 10 & 40 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "where $ z_0 $ is a vector of labor services used in each industry."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19d31a4e",
   "metadata": {},
   "source": [
    "## Solution to[ Exercise 40.1](https://intro.quantecon.org/#io_ex1)\n",
    "\n",
    "For each $ i = 0,1,2 $ and $ j = 1,2 $\n",
    "\n",
    "$$\n",
    "a_{ij} = \\frac{z_{ij}}{x_j}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63e3253a",
   "metadata": {},
   "source": [
    "## Exercise 40.2\n",
    "\n",
    "Derive the production possibility frontier for the economy characterized in the previous exercise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a166054a",
   "metadata": {},
   "source": [
    "## Solution to[ Exercise 40.2](https://intro.quantecon.org/#io_ex2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48810c0a",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "A = np.array([[0.1, 1.46],\n",
    "              [0.16, 0.17]])\n",
    "a_0 = np.array([0.04, 0.33])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acc581b7",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "I = np.identity(2)\n",
    "B = I - A\n",
    "L = np.linalg.inv(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d659fe51",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "A_0 = a_0 @ L\n",
    "A_0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12a9b5f4",
   "metadata": {},
   "source": [
    "Thus the production possibility frontier is given by\n",
    "\n",
    "$$\n",
    "0.17 d_1 + 0.69 d_2 = 50\n",
    "$$"
   ]
  }
 ],
 "metadata": {
  "date": 1750037600.2752945,
  "filename": "input_output.md",
  "kernelspec": {
   "display_name": "Python",
   "language": "python3",
   "name": "python3"
  },
  "title": "Input-Output Models"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}