{ "cells": [ { "cell_type": "markdown", "id": "9570e2b0", "metadata": {}, "source": [ "<div id=\"qe-notebook-header\" align=\"right\" style=\"text-align:right;\">\n", " <a href=\"https://quantecon.org/\" title=\"quantecon.org\">\n", " <img style=\"width:250px;display:inline;\" width=\"250px\" src=\"https://assets.quantecon.org/img/qe-menubar-logo.svg\" alt=\"QuantEcon\">\n", " </a>\n", "</div>" ] }, { "cell_type": "markdown", "id": "b5dd58a9", "metadata": {}, "source": [ "# Samuelson Multiplier-Accelerator" ] }, { "cell_type": "markdown", "id": "1eaa3c94", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Samuelson Multiplier-Accelerator](#Samuelson-Multiplier-Accelerator) \n", " - [Overview](#Overview) \n", " - [Details](#Details) \n", " - [Implementation](#Implementation) \n", " - [Stochastic Shocks](#Stochastic-Shocks) \n", " - [Government Spending](#Government-Spending) \n", " - [Wrapping Everything Into a Class](#Wrapping-Everything-Into-a-Class) \n", " - [Using the LinearStateSpace Class](#Using-the-LinearStateSpace-Class) \n", " - [Pure Multiplier Model](#Pure-Multiplier-Model) \n", " - [Summary](#Summary) " ] }, { "cell_type": "markdown", "id": "74905cca", "metadata": {}, "source": [ "In addition to what’s in Anaconda, this lecture will need the following libraries:" ] }, { "cell_type": "code", "execution_count": null, "id": "32a9653c", "metadata": { "hide-output": false }, "outputs": [], "source": [ "!pip install quantecon" ] }, { "cell_type": "markdown", "id": "53b1c167", "metadata": {}, "source": [ "## Overview\n", "\n", "This lecture creates non-stochastic and stochastic versions of Paul Samuelson’s celebrated multiplier accelerator model [[Samuelson, 1939](https://python.quantecon.org/zreferences.html#id89)].\n", "\n", "In doing so, we extend the example of the Solow model class in [our second OOP lecture](https://python-programming.quantecon.org/python_oop.html).\n", "\n", "Our objectives are to\n", "\n", "- provide a more detailed example of OOP and classes \n", "- review a famous model \n", "- review linear difference equations, both deterministic and stochastic \n", "\n", "\n", "Let’s start with some standard imports:" ] }, { "cell_type": "code", "execution_count": null, "id": "71547b38", "metadata": { "hide-output": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "plt.rcParams[\"figure.figsize\"] = (11, 5) #set default figure size\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "9106458c", "metadata": {}, "source": [ "We’ll also use the following for various tasks described below:" ] }, { "cell_type": "code", "execution_count": null, "id": "20cb7930", "metadata": { "hide-output": false }, "outputs": [], "source": [ "from quantecon import LinearStateSpace\n", "import cmath\n", "import math\n", "import sympy\n", "from sympy import Symbol, init_printing\n", "from cmath import sqrt" ] }, { "cell_type": "markdown", "id": "b7572b5c", "metadata": {}, "source": [ "### Samuelson’s Model\n", "\n", "Samuelson used a *second-order linear difference equation* to\n", "represent a model of national output based on three components:\n", "\n", "- a *national output identity* asserting that national output or national income is the\n", " sum of consumption plus investment plus government purchases. \n", "- a Keynesian *consumption function* asserting that consumption at\n", " time $ t $ is equal to a constant times national output at time $ t-1 $. \n", "- an investment *accelerator* asserting that investment at time\n", " $ t $ equals a constant called the *accelerator coefficient*\n", " times the difference in output between period $ t-1 $ and\n", " $ t-2 $. \n", "\n", "\n", "Consumption plus investment plus government purchases\n", "constitute *aggregate demand,* which automatically calls forth an\n", "equal amount of *aggregate supply*.\n", "\n", "(To read about linear difference equations see [here](https://en.wikipedia.org/wiki/Linear_difference_equation) or chapter IX of [[Sargent, 1987](https://python.quantecon.org/zreferences.html#id211)].)\n", "\n", "Samuelson used the model to analyze how particular values of the\n", "marginal propensity to consume and the accelerator coefficient might\n", "give rise to transient *business cycles* in national output.\n", "\n", "Possible dynamic properties include\n", "\n", "- smooth convergence to a constant level of output \n", "- damped business cycles that eventually converge to a constant level\n", " of output \n", "- persistent business cycles that neither dampen nor explode \n", "\n", "\n", "Later we present an extension that\n", "adds a random shock to the right side of the national income\n", "identity representing random fluctuations in aggregate demand.\n", "\n", "This modification makes national output become governed by a second-order\n", "*stochastic linear difference equation* that, with appropriate parameter values,\n", "gives rise to recurrent irregular business cycles.\n", "\n", "(To read about stochastic linear difference equations see chapter XI of\n", "[[Sargent, 1987](https://python.quantecon.org/zreferences.html#id211)].)" ] }, { "cell_type": "markdown", "id": "9c198afe", "metadata": {}, "source": [ "## Details\n", "\n", "Let’s assume that\n", "\n", "- $ \\{G_t\\} $ is a sequence of levels of government expenditures –\n", " we’ll start by setting $ G_t = G $ for all $ t $. \n", "- $ \\{C_t\\} $ is a sequence of levels of aggregate consumption\n", " expenditures, a key endogenous variable in the model. \n", "- $ \\{I_t\\} $ is a sequence of rates of investment, another key\n", " endogenous variable. \n", "- $ \\{Y_t\\} $ is a sequence of levels of national income, yet\n", " another endogenous variable. \n", "\n", "\n", "- $ a $ is the marginal propensity to consume in the Keynesian\n", " consumption function $ C_t = a Y_{t-1} + \\gamma $. \n", "- $ b $ is the “accelerator coefficient” in the “investment\n", " accelerator” $ I_t = b (Y_{t-1} - Y_{t-2}) $. \n", "- $ \\{\\epsilon_{t}\\} $ is an IID sequence standard normal random variables. \n", "- $ \\sigma \\geq 0 $ is a “volatility”\n", " parameter — setting $ \\sigma = 0 $ recovers the non-stochastic case\n", " that we’ll start with. \n", "\n", "\n", "The model combines the consumption function\n", "\n", "\n", "<a id='equation-consumption'></a>\n", "$$\n", "C_t = a Y_{t-1} + \\gamma \\tag{33.1}\n", "$$\n", "\n", "with the investment accelerator\n", "\n", "\n", "<a id='equation-accelerator'></a>\n", "$$\n", "I_t = b (Y_{t-1} - Y_{t-2}) \\tag{33.2}\n", "$$\n", "\n", "and the national income identity\n", "\n", "\n", "<a id='equation-income-identity'></a>\n", "$$\n", "Y_t = C_t + I_t + G_t \\tag{33.3}\n", "$$\n", "\n", "- The parameter $ a $ is peoples’ *marginal propensity to consume*\n", " out of income - equation [(33.1)](#equation-consumption) asserts that people consume a fraction of\n", " $ a \\in (0,1) $ of each additional dollar of income. \n", "- The parameter $ b > 0 $ is the investment accelerator coefficient - equation\n", " [(33.2)](#equation-accelerator) asserts that people invest in physical capital when\n", " income is increasing and disinvest when it is decreasing. \n", "\n", "\n", "Equations [(33.1)](#equation-consumption), [(33.2)](#equation-accelerator), and [(33.3)](#equation-income-identity)\n", "imply the following second-order linear difference equation for national income:\n", "\n", "$$\n", "Y_t = (a+b) Y_{t-1} - b Y_{t-2} + (\\gamma + G_t)\n", "$$\n", "\n", "or\n", "\n", "\n", "<a id='equation-second-order'></a>\n", "$$\n", "Y_t = \\rho_1 Y_{t-1} + \\rho_2 Y_{t-2} + (\\gamma + G_t) \\tag{33.4}\n", "$$\n", "\n", "where $ \\rho_1 = (a+b) $ and $ \\rho_2 = -b $.\n", "\n", "To complete the model, we require two **initial conditions**.\n", "\n", "If the model is to generate time series for $ t=0, \\ldots, T $, we\n", "require initial values\n", "\n", "$$\n", "Y_{-1} = \\bar Y_{-1}, \\quad Y_{-2} = \\bar Y_{-2}\n", "$$\n", "\n", "We’ll ordinarily set the parameters $ (a,b) $ so that starting from\n", "an arbitrary pair of initial conditions\n", "$ (\\bar Y_{-1}, \\bar Y_{-2}) $, national income $ Y_t $ converges to\n", "a constant value as $ t $ becomes large.\n", "\n", "We are interested in studying\n", "\n", "- the transient fluctuations in $ Y_t $ as it converges to its\n", " **steady state** level \n", "- the **rate** at which it converges to a steady state level \n", "\n", "\n", "The deterministic version of the model described so far — meaning that\n", "no random shocks hit aggregate demand — has only transient fluctuations.\n", "\n", "We can convert the model to one that has persistent irregular\n", "fluctuations by adding a random shock to aggregate demand." ] }, { "cell_type": "markdown", "id": "51349b35", "metadata": {}, "source": [ "### Stochastic Version of the Model\n", "\n", "We create a **random** or **stochastic** version of the model by adding\n", "a random process of **shocks** or **disturbances**\n", "$ \\{\\sigma \\epsilon_t \\} $ to the right side of equation [(33.4)](#equation-second-order),\n", "leading to the **second-order scalar linear stochastic difference\n", "equation**:\n", "\n", "\n", "<a id='equation-second-stochastic'></a>\n", "$$\n", "Y_t = G_t + a (1-b) Y_{t-1} - a b Y_{t-2} + \\sigma \\epsilon_{t} \\tag{33.5}\n", "$$" ] }, { "cell_type": "markdown", "id": "8a7ba34d", "metadata": {}, "source": [ "### Mathematical Analysis of the Model\n", "\n", "To get started, let’s set $ G_t \\equiv 0 $, $ \\sigma = 0 $, and\n", "$ \\gamma = 0 $.\n", "\n", "Then we can write equation [(33.5)](#equation-second-stochastic) as\n", "\n", "$$\n", "Y_t = \\rho_1 Y_{t-1} + \\rho_2 Y_{t-2}\n", "$$\n", "\n", "or\n", "\n", "\n", "<a id='equation-second-stochastic2'></a>\n", "$$\n", "Y_{t+2} - \\rho_1 Y_{t+1} - \\rho_2 Y_t = 0 \\tag{33.6}\n", "$$\n", "\n", "To discover the properties of the solution of [(33.6)](#equation-second-stochastic2),\n", "it is useful first to form the **characteristic polynomial**\n", "for [(33.6)](#equation-second-stochastic2):\n", "\n", "\n", "<a id='equation-polynomial'></a>\n", "$$\n", "z^2 - \\rho_1 z - \\rho_2 \\tag{33.7}\n", "$$\n", "\n", "where $ z $ is possibly a complex number.\n", "\n", "We want to find the two **zeros** (a.k.a. **roots**) – namely\n", "$ \\lambda_1, \\lambda_2 $ – of the characteristic polynomial.\n", "\n", "These are two special values of $ z $, say $ z= \\lambda_1 $ and\n", "$ z= \\lambda_2 $, such that if we set $ z $ equal to one of\n", "these values in expression [(33.7)](#equation-polynomial),\n", "the characteristic polynomial [(33.7)](#equation-polynomial) equals zero:\n", "\n", "\n", "<a id='equation-polynomial-sol'></a>\n", "$$\n", "z^2 - \\rho_1 z - \\rho_2 = (z- \\lambda_1 ) (z -\\lambda_2) = 0 \\tag{33.8}\n", "$$\n", "\n", "Equation [(33.8)](#equation-polynomial-sol) is said to **factor** the characteristic polynomial.\n", "\n", "When the roots are complex, they will occur as a complex conjugate pair.\n", "\n", "When the roots are complex, it is convenient to represent them in the\n", "polar form\n", "\n", "$$\n", "\\lambda_1 = r e^{i \\omega}, \\ \\lambda_2 = r e^{-i \\omega}\n", "$$\n", "\n", "where $ r $ is the *amplitude* of the complex number and\n", "$ \\omega $ is its *angle* or *phase*.\n", "\n", "These can also be represented as\n", "\n", "$$\n", "\\lambda_1 = r (cos (\\omega) + i \\sin (\\omega))\n", "$$\n", "\n", "$$\n", "\\lambda_2 = r (cos (\\omega) - i \\sin(\\omega))\n", "$$\n", "\n", "(To read about the polar form, see\n", "[here](https://www.khanacademy.org/math/precalculus/x9e81a4f98389efdf:complex/x9e81a4f98389efdf:complex-mul-div-polar/a/complex-number-polar-form-review))\n", "\n", "Given **initial conditions** $ Y_{-1}, Y_{-2} $, we want to generate\n", "a **solution** of the difference equation [(33.6)](#equation-second-stochastic2).\n", "\n", "It can be represented as\n", "\n", "$$\n", "Y_t = \\lambda_1^t c_1 + \\lambda_2^t c_2\n", "$$\n", "\n", "where $ c_1 $ and $ c_2 $ are constants that depend on the two\n", "initial conditions and on $ \\rho_1, \\rho_2 $.\n", "\n", "When the roots are complex, it is useful to pursue the following calculations.\n", "\n", "Notice that\n", "\n", "$$\n", "\\begin{aligned}\n", " Y_t & = & c_1 (r e^{i \\omega})^t + c_2 (r e^{-i \\omega})^t \\\\\n", " & = & c_1 r^t e^{i\\omega t} + c_2 r^t e^{-i \\omega t} \\\\\n", " & = & c_1 r^t [\\cos(\\omega t) + i \\sin(\\omega t) ] + c_2 r^t [\\cos(\\omega t) - i \\sin(\\omega t) ] \\\\\n", " & = & (c_1 + c_2) r^t \\cos(\\omega t) + i (c_1 - c_2) r^t \\sin(\\omega t)\n", " \\end{aligned}\n", "$$\n", "\n", "The only way that $ Y_t $ can be a real number for each $ t $ is if $ c_1 + c_2 $ is a real number and $ c_1 - c_2 $ is an imaginary number.\n", "\n", "This happens only when $ c_1 $ and $ c_2 $ are complex conjugates, in which case they can be written in the polar forms\n", "\n", "$$\n", "c_1 = v e^{i \\theta}, \\ \\ c_2 = v e^{- i \\theta}\n", "$$\n", "\n", "So we can write\n", "\n", "$$\n", "\\begin{aligned}\n", " Y_t & = & v e^{i \\theta} r^t e^{i \\omega t} + v e ^{- i \\theta} r^t e^{-i \\omega t} \\\\\n", " & = & v r^t [ e^{i(\\omega t + \\theta)} + e^{-i (\\omega t +\\theta)}] \\\\\n", " & = & 2 v r^t \\cos (\\omega t + \\theta)\n", " \\end{aligned}\n", "$$\n", "\n", "where $ v $ and $ \\theta $ are constants that must be chosen to satisfy initial conditions for $ Y_{-1}, Y_{-2} $.\n", "\n", "This formula shows that when the roots are complex, $ Y_t $ displays\n", "oscillations with **period** $ \\check p =\n", "\\frac{2 \\pi}{\\omega} $ and **damping factor** $ r $.\n", "\n", "We say that $ \\check p $ is the **period** because in that amount of time the cosine wave $ \\cos(\\omega t + \\theta) $ goes through exactly one complete cycles.\n", "\n", "(Draw a cosine function to convince yourself of this please)\n", "\n", "**Remark:** Following [[Samuelson, 1939](https://python.quantecon.org/zreferences.html#id89)], we want to choose the parameters\n", "$ a, b $ of the model so that the absolute values (of the possibly\n", "complex) roots $ \\lambda_1, \\lambda_2 $ of the characteristic\n", "polynomial are both strictly less than one:\n", "\n", "$$\n", "| \\lambda_j | < 1 \\quad \\quad \\text{for } j = 1, 2\n", "$$\n", "\n", "**Remark:** When both roots $ \\lambda_1, \\lambda_2 $ of the characteristic polynomial have\n", "absolute values strictly less than one, the absolute value of the larger\n", "one governs the rate of convergence to the steady state of the non\n", "stochastic version of the model." ] }, { "cell_type": "markdown", "id": "474bf067", "metadata": {}, "source": [ "### Things This Lecture Does\n", "\n", "We write a function to generate simulations of a $ \\{Y_t\\} $ sequence as a function of time.\n", "\n", "The function requires that we put in initial conditions for $ Y_{-1}, Y_{-2} $.\n", "\n", "The function checks that $ a, b $ are set so that $ \\lambda_1, \\lambda_2 $ are less than\n", "unity in absolute value (also called “modulus”).\n", "\n", "The function also tells us whether the roots are complex, and, if they are complex, returns both their real and complex parts.\n", "\n", "If the roots are both real, the function returns their values.\n", "\n", "We use our function written to simulate paths that are stochastic (when $ \\sigma >0 $).\n", "\n", "We have written the function in a way that allows us to input $ \\{G_t\\} $ paths of a few simple forms, e.g.,\n", "\n", "- one time jumps in $ G $ at some time \n", "- a permanent jump in $ G $ that occurs at some time \n", "\n", "\n", "We proceed to use the Samuelson multiplier-accelerator model as a laboratory to make a simple OOP example.\n", "\n", "The “state” that determines next period’s $ Y_{t+1} $ is now not just the current value $ Y_t $ but also the once lagged value $ Y_{t-1} $.\n", "\n", "This involves a little more bookkeeping than is required in the Solow model class definition.\n", "\n", "We use the Samuelson multiplier-accelerator model as a vehicle for teaching how we can gradually add more features to the class.\n", "\n", "We want to have a method in the class that automatically generates a simulation, either non-stochastic ($ \\sigma=0 $) or stochastic ($ \\sigma > 0 $).\n", "\n", "We also show how to map the Samuelson model into a simple instance of the `LinearStateSpace` class described [here](https://python.quantecon.org/linear_models.html).\n", "\n", "We can use a `LinearStateSpace` instance to do various things that we did above with our homemade function and class.\n", "\n", "Among other things, we show by example that the eigenvalues of the matrix $ A $ that we use to form the instance of the `LinearStateSpace` class for the Samuelson model equal the roots of the characteristic polynomial [(33.7)](#equation-polynomial) for the Samuelson multiplier accelerator model.\n", "\n", "Here is the formula for the matrix $ A $ in the linear state space system in the case that government expenditures are a constant $ G $:\n", "\n", "$$\n", "A = \\begin{bmatrix} 1 & 0 & 0 \\cr\n", " \\gamma + G & \\rho_1 & \\rho_2 \\cr\n", " 0 & 1 & 0 \\end{bmatrix}\n", "$$" ] }, { "cell_type": "markdown", "id": "fbb02aca", "metadata": {}, "source": [ "## Implementation\n", "\n", "We’ll start by drawing an informative graph from page 189 of [[Sargent, 1987](https://python.quantecon.org/zreferences.html#id211)]" ] }, { "cell_type": "code", "execution_count": null, "id": "1a6503d7", "metadata": { "hide-output": false }, "outputs": [], "source": [ "def param_plot():\n", "\n", " \"\"\"This function creates the graph on page 189 of\n", " Sargent Macroeconomic Theory, second edition, 1987.\n", " \"\"\"\n", "\n", " fig, ax = plt.subplots(figsize=(10, 6))\n", " ax.set_aspect('equal')\n", "\n", " # Set axis\n", " xmin, ymin = -3, -2\n", " xmax, ymax = -xmin, -ymin\n", " plt.axis([xmin, xmax, ymin, ymax])\n", "\n", " # Set axis labels\n", " ax.set(xticks=[], yticks=[])\n", " ax.set_xlabel(r'$\\rho_2$', fontsize=16)\n", " ax.xaxis.set_label_position('top')\n", " ax.set_ylabel(r'$\\rho_1$', rotation=0, fontsize=16)\n", " ax.yaxis.set_label_position('right')\n", "\n", " # Draw (t1, t2) points\n", " ρ1 = np.linspace(-2, 2, 100)\n", " ax.plot(ρ1, -abs(ρ1) + 1, c='black')\n", " ax.plot(ρ1, np.full_like(ρ1, -1), c='black')\n", " ax.plot(ρ1, -(ρ1**2 / 4), c='black')\n", "\n", " # Turn normal axes off\n", " for spine in ['left', 'bottom', 'top', 'right']:\n", " ax.spines[spine].set_visible(False)\n", "\n", " # Add arrows to represent axes\n", " axes_arrows = {'arrowstyle': '<|-|>', 'lw': 1.3}\n", " ax.annotate('', xy=(xmin, 0), xytext=(xmax, 0), arrowprops=axes_arrows)\n", " ax.annotate('', xy=(0, ymin), xytext=(0, ymax), arrowprops=axes_arrows)\n", "\n", " # Annotate the plot with equations\n", " plot_arrowsl = {'arrowstyle': '-|>', 'connectionstyle': \"arc3, rad=-0.2\"}\n", " plot_arrowsr = {'arrowstyle': '-|>', 'connectionstyle': \"arc3, rad=0.2\"}\n", " ax.annotate(r'$\\rho_1 + \\rho_2 < 1$', xy=(0.5, 0.3), xytext=(0.8, 0.6),\n", " arrowprops=plot_arrowsr, fontsize='12')\n", " ax.annotate(r'$\\rho_1 + \\rho_2 = 1$', xy=(0.38, 0.6), xytext=(0.6, 0.8),\n", " arrowprops=plot_arrowsr, fontsize='12')\n", " ax.annotate(r'$\\rho_2 < 1 + \\rho_1$', xy=(-0.5, 0.3), xytext=(-1.3, 0.6),\n", " arrowprops=plot_arrowsl, fontsize='12')\n", " ax.annotate(r'$\\rho_2 = 1 + \\rho_1$', xy=(-0.38, 0.6), xytext=(-1, 0.8),\n", " arrowprops=plot_arrowsl, fontsize='12')\n", " ax.annotate(r'$\\rho_2 = -1$', xy=(1.5, -1), xytext=(1.8, -1.3),\n", " arrowprops=plot_arrowsl, fontsize='12')\n", " ax.annotate(r'${\\rho_1}^2 + 4\\rho_2 = 0$', xy=(1.15, -0.35),\n", " xytext=(1.5, -0.3), arrowprops=plot_arrowsr, fontsize='12')\n", " ax.annotate(r'${\\rho_1}^2 + 4\\rho_2 < 0$', xy=(1.4, -0.7),\n", " xytext=(1.8, -0.6), arrowprops=plot_arrowsr, fontsize='12')\n", "\n", " # Label categories of solutions\n", " ax.text(1.5, 1, 'Explosive\\n growth', ha='center', fontsize=16)\n", " ax.text(-1.5, 1, 'Explosive\\n oscillations', ha='center', fontsize=16)\n", " ax.text(0.05, -1.5, 'Explosive oscillations', ha='center', fontsize=16)\n", " ax.text(0.09, -0.5, 'Damped oscillations', ha='center', fontsize=16)\n", "\n", " # Add small marker to y-axis\n", " ax.axhline(y=1.005, xmin=0.495, xmax=0.505, c='black')\n", " ax.text(-0.12, -1.12, '-1', fontsize=10)\n", " ax.text(-0.12, 0.98, '1', fontsize=10)\n", "\n", " return fig\n", "\n", "param_plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "91a1be2c", "metadata": {}, "source": [ "The graph portrays regions in which the $ (\\lambda_1, \\lambda_2) $\n", "root pairs implied by the $ (\\rho_1 = (a+b), \\rho_2 = - b) $\n", "difference equation parameter pairs in the Samuelson model are such that:\n", "\n", "- $ (\\lambda_1, \\lambda_2) $ are complex with modulus less than\n", " $ 1 $ - in this case, the $ \\{Y_t\\} $ sequence displays damped\n", " oscillations. \n", "- $ (\\lambda_1, \\lambda_2) $ are both real, but one is strictly\n", " greater than $ 1 $ - this leads to explosive growth. \n", "- $ (\\lambda_1, \\lambda_2) $ are both real, but one is strictly\n", " less than $ -1 $ - this leads to explosive oscillations. \n", "- $ (\\lambda_1, \\lambda_2) $ are both real and both are less than\n", " $ 1 $ in absolute value - in this case, there is smooth\n", " convergence to the steady state without damped cycles. \n", "\n", "\n", "Later we’ll present the graph with a red mark showing the particular\n", "point implied by the setting of $ (a,b) $." ] }, { "cell_type": "markdown", "id": "9fbd7f0b", "metadata": {}, "source": [ "### Function to Describe Implications of Characteristic Polynomial" ] }, { "cell_type": "code", "execution_count": null, "id": "d9fdbec4", "metadata": { "hide-output": false }, "outputs": [], "source": [ "def categorize_solution(ρ1, ρ2):\n", "\n", " \"\"\"This function takes values of ρ1 and ρ2 and uses them\n", " to classify the type of solution\n", " \"\"\"\n", "\n", " discriminant = ρ1 ** 2 + 4 * ρ2\n", " if ρ2 > 1 + ρ1 or ρ2 < -1:\n", " print('Explosive oscillations')\n", " elif ρ1 + ρ2 > 1:\n", " print('Explosive growth')\n", " elif discriminant < 0:\n", " print('Roots are complex with modulus less than one; \\\n", "therefore damped oscillations')\n", " else:\n", " print('Roots are real and absolute values are less than one; \\\n", "therefore get smooth convergence to a steady state')" ] }, { "cell_type": "code", "execution_count": null, "id": "8655ebf6", "metadata": { "hide-output": false }, "outputs": [], "source": [ "### Test the categorize_solution function\n", "\n", "categorize_solution(1.3, -.4)" ] }, { "cell_type": "markdown", "id": "f1940e39", "metadata": {}, "source": [ "### Function for Plotting Paths\n", "\n", "A useful function for our work below is" ] }, { "cell_type": "code", "execution_count": null, "id": "b93caacb", "metadata": { "hide-output": false }, "outputs": [], "source": [ "def plot_y(function=None):\n", "\n", " \"\"\"Function plots path of Y_t\"\"\"\n", "\n", " plt.subplots(figsize=(10, 6))\n", " plt.plot(function)\n", " plt.xlabel('Time $t$')\n", " plt.ylabel('$Y_t$', rotation=0)\n", " plt.grid()\n", " plt.show()" ] }, { "cell_type": "markdown", "id": "30e145d5", "metadata": {}, "source": [ "### Manual or “by hand” Root Calculations\n", "\n", "The following function calculates roots of the characteristic polynomial\n", "using high school algebra.\n", "\n", "(We’ll calculate the roots in other ways later)\n", "\n", "The function also plots a $ Y_t $ starting from initial conditions\n", "that we set" ] }, { "cell_type": "code", "execution_count": null, "id": "f4b44ce7", "metadata": { "hide-output": false }, "outputs": [], "source": [ "# This is a 'manual' method\n", "\n", "def y_nonstochastic(y_0=100, y_1=80, α=.92, β=.5, γ=10, n=80):\n", "\n", " \"\"\"Takes values of parameters and computes the roots of characteristic\n", " polynomial. It tells whether they are real or complex and whether they\n", " are less than unity in absolute value.It also computes a simulation of\n", " length n starting from the two given initial conditions for national\n", " income\n", " \"\"\"\n", "\n", " roots = []\n", "\n", " ρ1 = α + β\n", " ρ2 = -β\n", "\n", " print(f'ρ_1 is {ρ1}')\n", " print(f'ρ_2 is {ρ2}')\n", "\n", " discriminant = ρ1 ** 2 + 4 * ρ2\n", "\n", " if discriminant == 0:\n", " roots.append(-ρ1 / 2)\n", " print('Single real root: ')\n", " print(''.join(str(roots)))\n", " elif discriminant > 0:\n", " roots.append((-ρ1 + sqrt(discriminant).real) / 2)\n", " roots.append((-ρ1 - sqrt(discriminant).real) / 2)\n", " print('Two real roots: ')\n", " print(''.join(str(roots)))\n", " else:\n", " roots.append((-ρ1 + sqrt(discriminant)) / 2)\n", " roots.append((-ρ1 - sqrt(discriminant)) / 2)\n", " print('Two complex roots: ')\n", " print(''.join(str(roots)))\n", "\n", " if all(abs(root) < 1 for root in roots):\n", " print('Absolute values of roots are less than one')\n", " else:\n", " print('Absolute values of roots are not less than one')\n", "\n", " def transition(x, t): return ρ1 * x[t - 1] + ρ2 * x[t - 2] + γ\n", "\n", " y_t = [y_0, y_1]\n", "\n", " for t in range(2, n):\n", " y_t.append(transition(y_t, t))\n", "\n", " return y_t\n", "\n", "plot_y(y_nonstochastic())" ] }, { "cell_type": "markdown", "id": "49971b03", "metadata": {}, "source": [ "### Reverse-Engineering Parameters to Generate Damped Cycles\n", "\n", "The next cell writes code that takes as inputs the modulus $ r $ and\n", "phase $ \\phi $ of a conjugate pair of complex numbers in polar form\n", "\n", "$$\n", "\\lambda_1 = r \\exp(i \\phi), \\quad \\lambda_2 = r \\exp(- i \\phi)\n", "$$\n", "\n", "- The code assumes that these two complex numbers are the roots of the\n", " characteristic polynomial \n", "- It then reverse-engineers $ (a,b) $ and $ (\\rho_1, \\rho_2) $,\n", " pairs that would generate those roots " ] }, { "cell_type": "code", "execution_count": null, "id": "a0de6e63", "metadata": { "hide-output": false }, "outputs": [], "source": [ "### code to reverse-engineer a cycle\n", "### y_t = r^t (c_1 cos(ϕ t) + c2 sin(ϕ t))\n", "###\n", "\n", "def f(r, ϕ):\n", " \"\"\"\n", " Takes modulus r and angle ϕ of complex number r exp(j ϕ)\n", " and creates ρ1 and ρ2 of characteristic polynomial for which\n", " r exp(j ϕ) and r exp(- j ϕ) are complex roots.\n", "\n", " Returns the multiplier coefficient a and the accelerator coefficient b\n", " that verifies those roots.\n", " \"\"\"\n", " g1 = cmath.rect(r, ϕ) # Generate two complex roots\n", " g2 = cmath.rect(r, -ϕ)\n", " ρ1 = g1 + g2 # Implied ρ1, ρ2\n", " ρ2 = -g1 * g2\n", " b = -ρ2 # Reverse-engineer a and b that validate these\n", " a = ρ1 - b\n", " return ρ1, ρ2, a, b\n", "\n", "## Now let's use the function in an example\n", "## Here are the example parameters\n", "\n", "r = .95\n", "period = 10 # Length of cycle in units of time\n", "ϕ = 2 * math.pi/period\n", "\n", "## Apply the function\n", "\n", "ρ1, ρ2, a, b = f(r, ϕ)\n", "\n", "print(f\"a, b = {a}, {b}\")\n", "print(f\"ρ1, ρ2 = {ρ1}, {ρ2}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "920ebb3b", "metadata": { "hide-output": false }, "outputs": [], "source": [ "## Print the real components of ρ1 and ρ2\n", "\n", "ρ1 = ρ1.real\n", "ρ2 = ρ2.real\n", "\n", "ρ1, ρ2" ] }, { "cell_type": "markdown", "id": "b91ea1c8", "metadata": {}, "source": [ "### Root Finding Using Numpy\n", "\n", "Here we’ll use numpy to compute the roots of the characteristic\n", "polynomial" ] }, { "cell_type": "code", "execution_count": null, "id": "9ebf1b12", "metadata": { "hide-output": false }, "outputs": [], "source": [ "r1, r2 = np.roots([1, -ρ1, -ρ2])\n", "\n", "p1 = cmath.polar(r1)\n", "p2 = cmath.polar(r2)\n", "\n", "print(f\"r, ϕ = {r}, {ϕ}\")\n", "print(f\"p1, p2 = {p1}, {p2}\")\n", "# print(f\"g1, g2 = {g1}, {g2}\")\n", "\n", "print(f\"a, b = {a}, {b}\")\n", "print(f\"ρ1, ρ2 = {ρ1}, {ρ2}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2fc1e2b2", "metadata": { "hide-output": false }, "outputs": [], "source": [ "##=== This method uses numpy to calculate roots ===#\n", "\n", "\n", "def y_nonstochastic(y_0=100, y_1=80, α=.9, β=.8, γ=10, n=80):\n", "\n", " \"\"\" Rather than computing the roots of the characteristic\n", " polynomial by hand as we did earlier, this function\n", " enlists numpy to do the work for us\n", " \"\"\"\n", "\n", " # Useful constants\n", " ρ1 = α + β\n", " ρ2 = -β\n", "\n", " categorize_solution(ρ1, ρ2)\n", "\n", " # Find roots of polynomial\n", " roots = np.roots([1, -ρ1, -ρ2])\n", " print(f'Roots are {roots}')\n", "\n", " # Check if real or complex\n", " if all(isinstance(root, complex) for root in roots):\n", " print('Roots are complex')\n", " else:\n", " print('Roots are real')\n", "\n", " # Check if roots are less than one\n", " if all(abs(root) < 1 for root in roots):\n", " print('Roots are less than one')\n", " else:\n", " print('Roots are not less than one')\n", "\n", " # Define transition equation\n", " def transition(x, t): return ρ1 * x[t - 1] + ρ2 * x[t - 2] + γ\n", "\n", " # Set initial conditions\n", " y_t = [y_0, y_1]\n", "\n", " # Generate y_t series\n", " for t in range(2, n):\n", " y_t.append(transition(y_t, t))\n", "\n", " return y_t\n", "\n", "plot_y(y_nonstochastic())" ] }, { "cell_type": "markdown", "id": "0941a0df", "metadata": {}, "source": [ "### Reverse-Engineered Complex Roots: Example\n", "\n", "The next cell studies the implications of reverse-engineered complex\n", "roots.\n", "\n", "We’ll generate an **undamped** cycle of period 10" ] }, { "cell_type": "code", "execution_count": null, "id": "1bffa097", "metadata": { "hide-output": false }, "outputs": [], "source": [ "r = 1 # Generates undamped, nonexplosive cycles\n", "\n", "period = 10 # Length of cycle in units of time\n", "ϕ = 2 * math.pi/period\n", "\n", "## Apply the reverse-engineering function f\n", "\n", "ρ1, ρ2, a, b = f(r, ϕ)\n", "\n", "# Drop the imaginary part so that it is a valid input into y_nonstochastic\n", "a = a.real\n", "b = b.real\n", "\n", "print(f\"a, b = {a}, {b}\")\n", "\n", "ytemp = y_nonstochastic(α=a, β=b, y_0=20, y_1=30)\n", "plot_y(ytemp)" ] }, { "cell_type": "markdown", "id": "49f9bbee", "metadata": {}, "source": [ "### Digression: Using Sympy to Find Roots\n", "\n", "We can also use sympy to compute analytic formulas for the roots" ] }, { "cell_type": "code", "execution_count": null, "id": "80bf12c0", "metadata": { "hide-output": false }, "outputs": [], "source": [ "init_printing()\n", "\n", "r1 = Symbol(\"ρ_1\")\n", "r2 = Symbol(\"ρ_2\")\n", "z = Symbol(\"z\")\n", "\n", "sympy.solve(z**2 - r1*z - r2, z)" ] }, { "cell_type": "code", "execution_count": null, "id": "09164554", "metadata": { "hide-output": false }, "outputs": [], "source": [ "a = Symbol(\"α\")\n", "b = Symbol(\"β\")\n", "r1 = a + b\n", "r2 = -b\n", "\n", "sympy.solve(z**2 - r1*z - r2, z)" ] }, { "cell_type": "markdown", "id": "6457e221", "metadata": {}, "source": [ "## Stochastic Shocks\n", "\n", "Now we’ll construct some code to simulate the stochastic version of the\n", "model that emerges when we add a random shock process to aggregate\n", "demand" ] }, { "cell_type": "code", "execution_count": null, "id": "d965e68a", "metadata": { "hide-output": false }, "outputs": [], "source": [ "def y_stochastic(y_0=0, y_1=0, α=0.8, β=0.2, γ=10, n=100, σ=5):\n", "\n", " \"\"\"This function takes parameters of a stochastic version of\n", " the model and proceeds to analyze the roots of the characteristic\n", " polynomial and also generate a simulation.\n", " \"\"\"\n", "\n", " # Useful constants\n", " ρ1 = α + β\n", " ρ2 = -β\n", "\n", " # Categorize solution\n", " categorize_solution(ρ1, ρ2)\n", "\n", " # Find roots of polynomial\n", " roots = np.roots([1, -ρ1, -ρ2])\n", " print(roots)\n", "\n", " # Check if real or complex\n", " if all(isinstance(root, complex) for root in roots):\n", " print('Roots are complex')\n", " else:\n", " print('Roots are real')\n", "\n", " # Check if roots are less than one\n", " if all(abs(root) < 1 for root in roots):\n", " print('Roots are less than one')\n", " else:\n", " print('Roots are not less than one')\n", "\n", " # Generate shocks\n", " ϵ = np.random.normal(0, 1, n)\n", "\n", " # Define transition equation\n", " def transition(x, t): return ρ1 * \\\n", " x[t - 1] + ρ2 * x[t - 2] + γ + σ * ϵ[t]\n", "\n", " # Set initial conditions\n", " y_t = [y_0, y_1]\n", "\n", " # Generate y_t series\n", " for t in range(2, n):\n", " y_t.append(transition(y_t, t))\n", "\n", " return y_t\n", "\n", "plot_y(y_stochastic())" ] }, { "cell_type": "markdown", "id": "cb3c99f3", "metadata": {}, "source": [ "Let’s do a simulation in which there are shocks and the characteristic polynomial has complex roots" ] }, { "cell_type": "code", "execution_count": null, "id": "a1acb67e", "metadata": { "hide-output": false }, "outputs": [], "source": [ "r = .97\n", "\n", "period = 10 # Length of cycle in units of time\n", "ϕ = 2 * math.pi/period\n", "\n", "### Apply the reverse-engineering function f\n", "\n", "ρ1, ρ2, a, b = f(r, ϕ)\n", "\n", "# Drop the imaginary part so that it is a valid input into y_nonstochastic\n", "a = a.real\n", "b = b.real\n", "\n", "print(f\"a, b = {a}, {b}\")\n", "plot_y(y_stochastic(y_0=40, y_1 = 42, α=a, β=b, σ=2, n=100))" ] }, { "cell_type": "markdown", "id": "aa36d21e", "metadata": {}, "source": [ "## Government Spending\n", "\n", "This function computes a response to either a permanent or one-off increase\n", "in government expenditures" ] }, { "cell_type": "code", "execution_count": null, "id": "a2adf818", "metadata": { "hide-output": false }, "outputs": [], "source": [ "def y_stochastic_g(y_0=20,\n", " y_1=20,\n", " α=0.8,\n", " β=0.2,\n", " γ=10,\n", " n=100,\n", " σ=2,\n", " g=0,\n", " g_t=0,\n", " duration='permanent'):\n", "\n", " \"\"\"This program computes a response to a permanent increase\n", " in government expenditures that occurs at time 20\n", " \"\"\"\n", "\n", " # Useful constants\n", " ρ1 = α + β\n", " ρ2 = -β\n", "\n", " # Categorize solution\n", " categorize_solution(ρ1, ρ2)\n", "\n", " # Find roots of polynomial\n", " roots = np.roots([1, -ρ1, -ρ2])\n", " print(roots)\n", "\n", " # Check if real or complex\n", " if all(isinstance(root, complex) for root in roots):\n", " print('Roots are complex')\n", " else:\n", " print('Roots are real')\n", "\n", " # Check if roots are less than one\n", " if all(abs(root) < 1 for root in roots):\n", " print('Roots are less than one')\n", " else:\n", " print('Roots are not less than one')\n", "\n", " # Generate shocks\n", " ϵ = np.random.normal(0, 1, n)\n", "\n", " def transition(x, t, g):\n", "\n", " # Non-stochastic - separated to avoid generating random series\n", " # when not needed\n", " if σ == 0:\n", " return ρ1 * x[t - 1] + ρ2 * x[t - 2] + γ + g\n", "\n", " # Stochastic\n", " else:\n", " ϵ = np.random.normal(0, 1, n)\n", " return ρ1 * x[t - 1] + ρ2 * x[t - 2] + γ + g + σ * ϵ[t]\n", "\n", " # Create list and set initial conditions\n", " y_t = [y_0, y_1]\n", "\n", " # Generate y_t series\n", " for t in range(2, n):\n", "\n", " # No government spending\n", " if g == 0:\n", " y_t.append(transition(y_t, t))\n", "\n", " # Government spending (no shock)\n", " elif g != 0 and duration == None:\n", " y_t.append(transition(y_t, t))\n", "\n", " # Permanent government spending shock\n", " elif duration == 'permanent':\n", " if t < g_t:\n", " y_t.append(transition(y_t, t, g=0))\n", " else:\n", " y_t.append(transition(y_t, t, g=g))\n", "\n", " # One-off government spending shock\n", " elif duration == 'one-off':\n", " if t == g_t:\n", " y_t.append(transition(y_t, t, g=g))\n", " else:\n", " y_t.append(transition(y_t, t, g=0))\n", " return y_t" ] }, { "cell_type": "markdown", "id": "4af23519", "metadata": {}, "source": [ "A permanent government spending shock can be simulated as follows" ] }, { "cell_type": "code", "execution_count": null, "id": "17319ef8", "metadata": { "hide-output": false }, "outputs": [], "source": [ "plot_y(y_stochastic_g(g=10, g_t=20, duration='permanent'))" ] }, { "cell_type": "markdown", "id": "7b723d6b", "metadata": {}, "source": [ "We can also see the response to a one time jump in government expenditures" ] }, { "cell_type": "code", "execution_count": null, "id": "85afa080", "metadata": { "hide-output": false }, "outputs": [], "source": [ "plot_y(y_stochastic_g(g=500, g_t=50, duration='one-off'))" ] }, { "cell_type": "markdown", "id": "4e216932", "metadata": {}, "source": [ "## Wrapping Everything Into a Class\n", "\n", "Up to now, we have written functions to do the work.\n", "\n", "Now we’ll roll up our sleeves and write a Python class called `Samuelson`\n", "for the Samuelson model" ] }, { "cell_type": "code", "execution_count": null, "id": "9fcf64f1", "metadata": { "hide-output": false }, "outputs": [], "source": [ "class Samuelson():\n", "\n", " \"\"\"This class represents the Samuelson model, otherwise known as the\n", " multiple-accelerator model. The model combines the Keynesian multiplier\n", " with the accelerator theory of investment.\n", "\n", " The path of output is governed by a linear second-order difference equation\n", "\n", " .. math::\n", "\n", " Y_t = + \\alpha (1 + \\beta) Y_{t-1} - \\alpha \\beta Y_{t-2}\n", "\n", " Parameters\n", " ----------\n", " y_0 : scalar\n", " Initial condition for Y_0\n", " y_1 : scalar\n", " Initial condition for Y_1\n", " α : scalar\n", " Marginal propensity to consume\n", " β : scalar\n", " Accelerator coefficient\n", " n : int\n", " Number of iterations\n", " σ : scalar\n", " Volatility parameter. It must be greater than or equal to 0. Set\n", " equal to 0 for a non-stochastic model.\n", " g : scalar\n", " Government spending shock\n", " g_t : int\n", " Time at which government spending shock occurs. Must be specified\n", " when duration != None.\n", " duration : {None, 'permanent', 'one-off'}\n", " Specifies type of government spending shock. If none, government\n", " spending equal to g for all t.\n", "\n", " \"\"\"\n", "\n", " def __init__(self,\n", " y_0=100,\n", " y_1=50,\n", " α=1.3,\n", " β=0.2,\n", " γ=10,\n", " n=100,\n", " σ=0,\n", " g=0,\n", " g_t=0,\n", " duration=None):\n", "\n", " self.y_0, self.y_1, self.α, self.β = y_0, y_1, α, β\n", " self.n, self.g, self.g_t, self.duration = n, g, g_t, duration\n", " self.γ, self.σ = γ, σ\n", " self.ρ1 = α + β\n", " self.ρ2 = -β\n", " self.roots = np.roots([1, -self.ρ1, -self.ρ2])\n", "\n", " def root_type(self):\n", " if all(isinstance(root, complex) for root in self.roots):\n", " return 'Complex conjugate'\n", " elif len(self.roots) > 1:\n", " return 'Double real'\n", " else:\n", " return 'Single real'\n", "\n", " def root_less_than_one(self):\n", " if all(abs(root) < 1 for root in self.roots):\n", " return True\n", "\n", " def solution_type(self):\n", " ρ1, ρ2 = self.ρ1, self.ρ2\n", " discriminant = ρ1 ** 2 + 4 * ρ2\n", " if ρ2 >= 1 + ρ1 or ρ2 <= -1:\n", " return 'Explosive oscillations'\n", " elif ρ1 + ρ2 >= 1:\n", " return 'Explosive growth'\n", " elif discriminant < 0:\n", " return 'Damped oscillations'\n", " else:\n", " return 'Steady state'\n", "\n", " def _transition(self, x, t, g):\n", "\n", " # Non-stochastic - separated to avoid generating random series\n", " # when not needed\n", " if self.σ == 0:\n", " return self.ρ1 * x[t - 1] + self.ρ2 * x[t - 2] + self.γ + g\n", "\n", " # Stochastic\n", " else:\n", " ϵ = np.random.normal(0, 1, self.n)\n", " return self.ρ1 * x[t - 1] + self.ρ2 * x[t - 2] + self.γ + g \\\n", " + self.σ * ϵ[t]\n", "\n", " def generate_series(self):\n", "\n", " # Create list and set initial conditions\n", " y_t = [self.y_0, self.y_1]\n", "\n", " # Generate y_t series\n", " for t in range(2, self.n):\n", "\n", " # No government spending\n", " if self.g == 0:\n", " y_t.append(self._transition(y_t, t))\n", "\n", " # Government spending (no shock)\n", " elif self.g != 0 and self.duration == None:\n", " y_t.append(self._transition(y_t, t))\n", "\n", " # Permanent government spending shock\n", " elif self.duration == 'permanent':\n", " if t < self.g_t:\n", " y_t.append(self._transition(y_t, t, g=0))\n", " else:\n", " y_t.append(self._transition(y_t, t, g=self.g))\n", "\n", " # One-off government spending shock\n", " elif self.duration == 'one-off':\n", " if t == self.g_t:\n", " y_t.append(self._transition(y_t, t, g=self.g))\n", " else:\n", " y_t.append(self._transition(y_t, t, g=0))\n", " return y_t\n", "\n", " def summary(self):\n", " print('Summary\\n' + '-' * 50)\n", " print(f'Root type: {self.root_type()}')\n", " print(f'Solution type: {self.solution_type()}')\n", " print(f'Roots: {str(self.roots)}')\n", "\n", " if self.root_less_than_one() == True:\n", " print('Absolute value of roots is less than one')\n", " else:\n", " print('Absolute value of roots is not less than one')\n", "\n", " if self.σ > 0:\n", " print('Stochastic series with σ = ' + str(self.σ))\n", " else:\n", " print('Non-stochastic series')\n", "\n", " if self.g != 0:\n", " print('Government spending equal to ' + str(self.g))\n", "\n", " if self.duration != None:\n", " print(self.duration.capitalize() +\n", " ' government spending shock at t = ' + str(self.g_t))\n", "\n", " def plot(self):\n", " fig, ax = plt.subplots(figsize=(10, 6))\n", " ax.plot(self.generate_series())\n", " ax.set(xlabel='Iteration', xlim=(0, self.n))\n", " ax.set_ylabel('$Y_t$', rotation=0)\n", " ax.grid()\n", "\n", " # Add parameter values to plot\n", " paramstr = f'$\\\\alpha={self.α:.2f}$ \\n $\\\\beta={self.β:.2f}$ \\n \\\n", " $\\\\gamma={self.γ:.2f}$ \\n $\\\\sigma={self.σ:.2f}$ \\n \\\n", " $\\\\rho_1={self.ρ1:.2f}$ \\n $\\\\rho_2={self.ρ2:.2f}$'\n", " props = dict(fc='white', pad=10, alpha=0.5)\n", " ax.text(0.87, 0.05, paramstr, transform=ax.transAxes,\n", " fontsize=12, bbox=props, va='bottom')\n", "\n", " return fig\n", "\n", " def param_plot(self):\n", "\n", " # Uses the param_plot() function defined earlier (it is then able\n", " # to be used standalone or as part of the model)\n", "\n", " fig = param_plot()\n", " ax = fig.gca()\n", "\n", " # Add λ values to legend\n", " for i, root in enumerate(self.roots):\n", " if isinstance(root, complex):\n", " # Need to fill operator for positive as string is split apart\n", " operator = ['+', '']\n", " label = rf'$\\lambda_{i+1} = {sam.roots[i].real:.2f} {operator[i]} {sam.roots[i].imag:.2f}i$'\n", " else:\n", " label = rf'$\\lambda_{i+1} = {sam.roots[i].real:.2f}$'\n", " ax.scatter(0, 0, 0, label=label) # dummy to add to legend\n", "\n", " # Add ρ pair to plot\n", " ax.scatter(self.ρ1, self.ρ2, 100, 'red', '+',\n", " label=r'$(\\ \\rho_1, \\ \\rho_2 \\ )$', zorder=5)\n", "\n", " plt.legend(fontsize=12, loc=3)\n", "\n", " return fig" ] }, { "cell_type": "markdown", "id": "ad901ea3", "metadata": {}, "source": [ "### Illustration of Samuelson Class\n", "\n", "Now we’ll put our Samuelson class to work on an example" ] }, { "cell_type": "code", "execution_count": null, "id": "fb4fd491", "metadata": { "hide-output": false }, "outputs": [], "source": [ "sam = Samuelson(α=0.8, β=0.5, σ=2, g=10, g_t=20, duration='permanent')\n", "sam.summary()" ] }, { "cell_type": "code", "execution_count": null, "id": "5653ab0e", "metadata": { "hide-output": false }, "outputs": [], "source": [ "sam.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "9d0b790c", "metadata": {}, "source": [ "### Using the Graph\n", "\n", "We’ll use our graph to show where the roots lie and how their location\n", "is consistent with the behavior of the path just graphed.\n", "\n", "The red $ + $ sign shows the location of the roots" ] }, { "cell_type": "code", "execution_count": null, "id": "f7b461d2", "metadata": { "hide-output": false }, "outputs": [], "source": [ "sam.param_plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "f4f2216a", "metadata": {}, "source": [ "## Using the LinearStateSpace Class\n", "\n", "It turns out that we can use the [QuantEcon.py](http://quantecon.org/quantecon-py)\n", "[LinearStateSpace](https://github.com/QuantEcon/QuantEcon.py/blob/master/quantecon/lss.py) class to do\n", "much of the work that we have done from scratch above.\n", "\n", "Here is how we map the Samuelson model into an instance of a\n", "`LinearStateSpace` class" ] }, { "cell_type": "code", "execution_count": null, "id": "dc0cfc85", "metadata": { "hide-output": false }, "outputs": [], "source": [ "\"\"\"This script maps the Samuelson model in the the\n", "``LinearStateSpace`` class\n", "\"\"\"\n", "α = 0.8\n", "β = 0.9\n", "ρ1 = α + β\n", "ρ2 = -β\n", "γ = 10\n", "σ = 1\n", "g = 10\n", "n = 100\n", "\n", "A = [[1, 0, 0],\n", " [γ + g, ρ1, ρ2],\n", " [0, 1, 0]]\n", "\n", "G = [[γ + g, ρ1, ρ2], # this is Y_{t+1}\n", " [γ, α, 0], # this is C_{t+1}\n", " [0, β, -β]] # this is I_{t+1}\n", "\n", "μ_0 = [1, 100, 50]\n", "C = np.zeros((3,1))\n", "C[1] = σ # stochastic\n", "\n", "sam_t = LinearStateSpace(A, C, G, mu_0=μ_0)\n", "\n", "x, y = sam_t.simulate(ts_length=n)\n", "\n", "fig, axes = plt.subplots(3, 1, sharex=True, figsize=(12, 8))\n", "titles = ['Output ($Y_t$)', 'Consumption ($C_t$)', 'Investment ($I_t$)']\n", "colors = ['darkblue', 'red', 'purple']\n", "for ax, series, title, color in zip(axes, y, titles, colors):\n", " ax.plot(series, color=color)\n", " ax.set(title=title, xlim=(0, n))\n", " ax.grid()\n", "\n", "axes[-1].set_xlabel('Iteration')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "59827db0", "metadata": {}, "source": [ "### Other Methods in the `LinearStateSpace` Class\n", "\n", "Let’s plot **impulse response functions** for the instance of the\n", "Samuelson model using a method in the `LinearStateSpace` class" ] }, { "cell_type": "code", "execution_count": null, "id": "a3b8832d", "metadata": { "hide-output": false }, "outputs": [], "source": [ "imres = sam_t.impulse_response()\n", "imres = np.asarray(imres)\n", "y1 = imres[:, :, 0]\n", "y2 = imres[:, :, 1]\n", "y1.shape" ] }, { "cell_type": "markdown", "id": "09d3291c", "metadata": {}, "source": [ "Now let’s compute the zeros of the characteristic polynomial by simply\n", "calculating the eigenvalues of $ A $" ] }, { "cell_type": "code", "execution_count": null, "id": "acb8b4cb", "metadata": { "hide-output": false }, "outputs": [], "source": [ "A = np.asarray(A)\n", "w, v = np.linalg.eig(A)\n", "print(w)" ] }, { "cell_type": "markdown", "id": "1079a5c3", "metadata": {}, "source": [ "### Inheriting Methods from `LinearStateSpace`\n", "\n", "We could also create a subclass of `LinearStateSpace` (inheriting all its\n", "methods and attributes) to add more functions to use" ] }, { "cell_type": "code", "execution_count": null, "id": "34efd1da", "metadata": { "hide-output": false }, "outputs": [], "source": [ "class SamuelsonLSS(LinearStateSpace):\n", "\n", " \"\"\"\n", " This subclass creates a Samuelson multiplier-accelerator model\n", " as a linear state space system.\n", " \"\"\"\n", " def __init__(self,\n", " y_0=100,\n", " y_1=50,\n", " α=0.8,\n", " β=0.9,\n", " γ=10,\n", " σ=1,\n", " g=10):\n", "\n", " self.α, self.β = α, β\n", " self.y_0, self.y_1, self.g = y_0, y_1, g\n", " self.γ, self.σ = γ, σ\n", "\n", " # Define intial conditions\n", " self.μ_0 = [1, y_0, y_1]\n", "\n", " self.ρ1 = α + β\n", " self.ρ2 = -β\n", "\n", " # Define transition matrix\n", " self.A = [[1, 0, 0],\n", " [γ + g, self.ρ1, self.ρ2],\n", " [0, 1, 0]]\n", "\n", " # Define output matrix\n", " self.G = [[γ + g, self.ρ1, self.ρ2], # this is Y_{t+1}\n", " [γ, α, 0], # this is C_{t+1}\n", " [0, β, -β]] # this is I_{t+1}\n", "\n", " self.C = np.zeros((3, 1))\n", " self.C[1] = σ # stochastic\n", "\n", " # Initialize LSS with parameters from Samuelson model\n", " LinearStateSpace.__init__(self, self.A, self.C, self.G, mu_0=self.μ_0)\n", "\n", " def plot_simulation(self, ts_length=100, stationary=True):\n", "\n", " # Temporarily store original parameters\n", " temp_mu = self.mu_0\n", " temp_Sigma = self.Sigma_0\n", "\n", " # Set distribution parameters equal to their stationary\n", " # values for simulation\n", " if stationary == True:\n", " try:\n", " self.mu_x, self.mu_y, self.Sigma_x, self.Sigma_y, self.Sigma_yx = \\\n", " self.stationary_distributions()\n", " self.mu_0 = self.mu_x\n", " self.Sigma_0 = self.Sigma_x\n", " # Exception where no convergence achieved when\n", " #calculating stationary distributions\n", " except ValueError:\n", " print('Stationary distribution does not exist')\n", "\n", " x, y = self.simulate(ts_length)\n", "\n", " fig, axes = plt.subplots(3, 1, sharex=True, figsize=(12, 8))\n", " titles = ['Output ($Y_t$)', 'Consumption ($C_t$)', 'Investment ($I_t$)']\n", " colors = ['darkblue', 'red', 'purple']\n", " for ax, series, title, color in zip(axes, y, titles, colors):\n", " ax.plot(series, color=color)\n", " ax.set(title=title, xlim=(0, n))\n", " ax.grid()\n", "\n", " axes[-1].set_xlabel('Iteration')\n", "\n", " # Reset distribution parameters to their initial values\n", " self.mu_0 = temp_mu\n", " self.Sigma_0 = temp_Sigma\n", "\n", " return fig\n", "\n", " def plot_irf(self, j=5):\n", "\n", " x, y = self.impulse_response(j)\n", "\n", " # Reshape into 3 x j matrix for plotting purposes\n", " yimf = np.array(y).flatten().reshape(j+1, 3).T\n", "\n", " fig, axes = plt.subplots(3, 1, sharex=True, figsize=(12, 8))\n", " labels = ['$Y_t$', '$C_t$', '$I_t$']\n", " colors = ['darkblue', 'red', 'purple']\n", " for ax, series, label, color in zip(axes, yimf, labels, colors):\n", " ax.plot(series, color=color)\n", " ax.set(xlim=(0, j))\n", " ax.set_ylabel(label, rotation=0, fontsize=14, labelpad=10)\n", " ax.grid()\n", "\n", " axes[0].set_title('Impulse Response Functions')\n", " axes[-1].set_xlabel('Iteration')\n", "\n", " return fig\n", "\n", " def multipliers(self, j=5):\n", " x, y = self.impulse_response(j)\n", " return np.sum(np.array(y).flatten().reshape(j+1, 3), axis=0)" ] }, { "cell_type": "markdown", "id": "7e6d5225", "metadata": {}, "source": [ "### Illustrations\n", "\n", "Let’s show how we can use the `SamuelsonLSS`" ] }, { "cell_type": "code", "execution_count": null, "id": "8188c5f5", "metadata": { "hide-output": false }, "outputs": [], "source": [ "samlss = SamuelsonLSS()" ] }, { "cell_type": "code", "execution_count": null, "id": "6baf8e98", "metadata": { "hide-output": false }, "outputs": [], "source": [ "samlss.plot_simulation(100, stationary=False)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "61dd6566", "metadata": { "hide-output": false }, "outputs": [], "source": [ "samlss.plot_simulation(100, stationary=True)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "f4bca1e2", "metadata": { "hide-output": false }, "outputs": [], "source": [ "samlss.plot_irf(100)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "b6e6a709", "metadata": { "hide-output": false }, "outputs": [], "source": [ "samlss.multipliers()" ] }, { "cell_type": "markdown", "id": "56cb940a", "metadata": {}, "source": [ "## Pure Multiplier Model\n", "\n", "Let’s shut down the accelerator by setting $ b=0 $ to get a pure\n", "multiplier model\n", "\n", "- the absence of cycles gives an idea about why Samuelson included the\n", " accelerator " ] }, { "cell_type": "code", "execution_count": null, "id": "abe569a8", "metadata": { "hide-output": false }, "outputs": [], "source": [ "pure_multiplier = SamuelsonLSS(α=0.95, β=0)" ] }, { "cell_type": "code", "execution_count": null, "id": "b5b348bf", "metadata": { "hide-output": false }, "outputs": [], "source": [ "pure_multiplier.plot_simulation()" ] }, { "cell_type": "code", "execution_count": null, "id": "856d35a7", "metadata": { "hide-output": false }, "outputs": [], "source": [ "pure_multiplier = SamuelsonLSS(α=0.8, β=0)" ] }, { "cell_type": "code", "execution_count": null, "id": "68f62f58", "metadata": { "hide-output": false }, "outputs": [], "source": [ "pure_multiplier.plot_simulation()" ] }, { "cell_type": "code", "execution_count": null, "id": "13cf60d9", "metadata": { "hide-output": false }, "outputs": [], "source": [ "pure_multiplier.plot_irf(100)" ] }, { "cell_type": "markdown", "id": "e091522d", "metadata": {}, "source": [ "## Summary\n", "\n", "In this lecture, we wrote functions and classes to represent non-stochastic and\n", "stochastic versions of the Samuelson (1939) multiplier-accelerator model, described\n", "in [[Samuelson, 1939](https://python.quantecon.org/zreferences.html#id89)].\n", "\n", "We saw that different parameter values led to different output paths, which\n", "could either be stationary, explosive, or oscillating.\n", "\n", "We also were able to represent the model using the [QuantEcon.py](http://quantecon.org/quantecon-py)\n", "[LinearStateSpace](https://github.com/QuantEcon/QuantEcon.py/blob/master/quantecon/lss.py) class." ] } ], "metadata": { "date": 1751441158.6457133, "filename": "samuelson.md", "kernelspec": { "display_name": "Python", "language": "python3", "name": "python3" }, "title": "Samuelson Multiplier-Accelerator" }, "nbformat": 4, "nbformat_minor": 5 }