{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4bfce916",
   "metadata": {},
   "source": [
    "\n",
    "<a id='permanent-income-dles'></a>\n",
    "\n",
    "<a id='index-0'></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46390338",
   "metadata": {},
   "source": [
    "# Permanent Income Model using the DLE Class\n",
    "\n",
    "This lecture is part of a suite of lectures that use the quantecon DLE class to instantiate models within the\n",
    "[[Hansen and Sargent, 2013](https://python-advanced.quantecon.org/zreferences.html#id74)] class of models described in detail in [Recursive Models of Dynamic Linear Economies](https://python-advanced.quantecon.org/hs_recursive_models.html).\n",
    "\n",
    "In addition to what’s included in  Anaconda, this lecture uses the quantecon library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e17b389",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "!pip install --upgrade quantecon"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d878bf4e",
   "metadata": {},
   "source": [
    "This lecture adds a third solution method for the\n",
    "linear-quadratic-Gaussian permanent income model with\n",
    "$ \\beta R = 1 $, complementing the other two solution methods described in [Optimal Savings I: The Permanent Income Model](https://python-intro.quantecon.org/perm_income.html) and\n",
    "[Optimal Savings II: LQ Techniques](https://python-intro.quantecon.org/perm_income_cons.html) and [this Jupyter\n",
    "notebook](http://nbviewer.jupyter.org/github/QuantEcon/QuantEcon.notebooks/blob/master/permanent_income.ipynb).\n",
    "\n",
    "The additional solution method uses the **DLE** class.\n",
    "\n",
    "In this way, we  map the permanent\n",
    "income model into the framework of Hansen & Sargent (2013) “Recursive\n",
    "Models of Dynamic Linear Economies” [[Hansen and Sargent, 2013](https://python-advanced.quantecon.org/zreferences.html#id74)].\n",
    "\n",
    "We’ll also require the following imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f132ed59",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from quantecon import DLE\n",
    "\n",
    "np.set_printoptions(suppress=True, precision=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56df6d95",
   "metadata": {},
   "source": [
    "## The Permanent Income Model\n",
    "\n",
    "The LQ permanent income model is an example of a **savings problem**.\n",
    "\n",
    "A consumer has preferences over consumption streams that are ordered by\n",
    "the utility functional\n",
    "\n",
    "\n",
    "<a id='equation-perm-utility'></a>\n",
    "$$\n",
    "E_0 \\sum_{t=0}^\\infty \\beta^t u(c_t) \\tag{21.1}\n",
    "$$\n",
    "\n",
    "where $ E_t $ is the mathematical expectation conditioned on the\n",
    "consumer’s time $ t $ information, $ c_t $ is time $ t $\n",
    "consumption, $ u(c) $ is a strictly concave one-period utility\n",
    "function, and $ \\beta \\in (0,1) $ is a discount factor.\n",
    "\n",
    "The LQ model gets its name partly from assuming that the utility\n",
    "function $ u $ is quadratic:\n",
    "\n",
    "$$\n",
    "u(c) = -.5(c - \\gamma)^2\n",
    "$$\n",
    "\n",
    "where $ \\gamma>0 $ is a bliss level of consumption.\n",
    "\n",
    "The consumer maximizes the utility functional [(21.1)](#equation-perm-utility) by choosing a\n",
    "consumption, borrowing plan $ \\{c_t, b_{t+1}\\}_{t=0}^\\infty $\n",
    "subject to the sequence of budget constraints\n",
    "\n",
    "\n",
    "<a id='equation-max-utility'></a>\n",
    "$$\n",
    "c_t + b_t = R^{-1} b_{t+1}  + y_t, t \\geq 0 \\tag{21.2}\n",
    "$$\n",
    "\n",
    "where $ y_t $ is an exogenous stationary endowment process,\n",
    "$ R $ is a constant gross risk-free interest rate, $ b_t $ is\n",
    "one-period risk-free debt maturing at $ t $, and $ b_0 $ is a\n",
    "given initial condition.\n",
    "\n",
    "We shall assume that $ R^{-1} = \\beta $.\n",
    "\n",
    "Equation [(21.2)](#equation-max-utility) is linear.\n",
    "\n",
    "We use another set of linear equations to model the endowment process.\n",
    "\n",
    "In particular, we assume that the endowment process has the state-space\n",
    "representation\n",
    "\n",
    "\n",
    "<a id='equation-endowment'></a>\n",
    "$$\n",
    "\\begin{aligned} z_{t+1} & = A_{22} z_t + C_2 w_{t+1}  \\cr\n",
    "              y_t & = U_y  z_t  \\cr \\end{aligned} \\tag{21.3}\n",
    "$$\n",
    "\n",
    "where $ w_{t+1} $ is an IID process with mean zero and identity\n",
    "contemporaneous covariance matrix, $ A_{22} $ is a stable matrix,\n",
    "its eigenvalues being strictly below unity in modulus, and $ U_y $\n",
    "is a selection vector that identifies $ y $ with a particular linear\n",
    "combination of the $ z_t $.\n",
    "\n",
    "We impose the following condition on the consumption, borrowing plan:\n",
    "\n",
    "\n",
    "<a id='equation-contraint'></a>\n",
    "$$\n",
    "E_0 \\sum_{t=0}^\\infty \\beta^t b_t^2 < +\\infty \\tag{21.4}\n",
    "$$\n",
    "\n",
    "This condition suffices to rule out Ponzi schemes.\n",
    "\n",
    "(We impose this condition to rule out a borrow-more-and-more plan that\n",
    "would allow the household to enjoy bliss consumption forever)\n",
    "\n",
    "The state vector confronting the household at $ t $ is\n",
    "\n",
    "$$\n",
    "x_t = \\begin{bmatrix} z_t \\\\ b_t \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "where $ b_t $ is its one-period debt falling due at the beginning of\n",
    "period $ t $ and $ z_t $ contains all variables useful for\n",
    "forecasting its future endowment.\n",
    "\n",
    "We assume that $ \\{y_t\\} $ follows a second order univariate\n",
    "autoregressive process:\n",
    "\n",
    "$$\n",
    "y_{t+1} = \\alpha + \\rho_1 y_t + \\rho_2 y_{t-1} + \\sigma w_{t+1}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ce352f0",
   "metadata": {},
   "source": [
    "### Solution with the DLE Class\n",
    "\n",
    "One way of solving this model is to map the problem into the framework\n",
    "outlined in Section 4.8 of [[Hansen and Sargent, 2013](https://python-advanced.quantecon.org/zreferences.html#id74)] by setting up our technology,\n",
    "information and preference matrices as follows:\n",
    "\n",
    "**Technology:**\n",
    "$ \\phi_c=  \\left[ {\\begin{array}{c}  1 \\\\ 0  \\end{array} }  \\right] $\n",
    ",\n",
    "$ \\phi_g=  \\left[ {\\begin{array}{c}  0 \\\\ 1  \\end{array} }  \\right] $\n",
    ",\n",
    "$ \\phi_i=  \\left[ {\\begin{array}{c}  -1 \\\\ -0.00001  \\end{array} }  \\right] $,\n",
    "$ \\Gamma=  \\left[ {\\begin{array}{c}  -1 \\\\ 0  \\end{array} }  \\right] $,\n",
    "$ \\Delta_k = 0 $,  $ \\Theta_k = R $.\n",
    "\n",
    "**Information:**\n",
    "$ A_{22} = \\left[ {\\begin{array}{ccc}  1 & 0 & 0 \\\\ \\alpha & \\rho_1 & \\rho_2 \\\\ 0 & 1 & 0  \\end{array} }  \\right] $,\n",
    "$ C_{2} = \\left[ {\\begin{array}{c}  0 \\\\ \\sigma \\\\ 0  \\end{array} }  \\right] $,\n",
    "$ U_b = \\left[ {\\begin{array}{ccc}  \\gamma & 0 & 0  \\end{array} }  \\right] $,\n",
    "$ U_d = \\left[ {\\begin{array}{ccc}  0 & 1 & 0 \\\\ 0 & 0 & 0  \\end{array} }  \\right] $.\n",
    "\n",
    "**Preferences:** $ \\Lambda = 0 $, $ \\Pi = 1 $,\n",
    "$ \\Delta_h = 0 $, $ \\Theta_h = 0 $.\n",
    "\n",
    "We set parameters\n",
    "\n",
    "$ \\alpha = 10, \\beta = 0.95, \\rho_1 = 0.9, \\rho_2 = 0, \\sigma = 1 $\n",
    "\n",
    "(The value of $ \\gamma $ does not affect the optimal decision rule)\n",
    "\n",
    "The chosen matrices mean that the household’s technology is:\n",
    "\n",
    "$$\n",
    "c_t + k_{t-1} = i_t + y_t\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\frac{k_t}{R} = i_t\n",
    "$$\n",
    "\n",
    "$$\n",
    "l_t^2 = (0.00001)^2i_t\n",
    "$$\n",
    "\n",
    "Combining the first two of these gives the budget constraint of the\n",
    "permanent income model, where $ k_t = b_{t+1} $.\n",
    "\n",
    "The third equation is a very small penalty on debt-accumulation to rule\n",
    "out Ponzi schemes.\n",
    "\n",
    "We set up this instance of the DLE class below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c0cb8bb",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "α, β, ρ_1, ρ_2, σ = 10, 0.95, 0.9, 0, 1\n",
    "\n",
    "γ = np.array([[-1], [0]])\n",
    "ϕ_c = np.array([[1], [0]])\n",
    "ϕ_g = np.array([[0], [1]])\n",
    "ϕ_1 = 1e-5\n",
    "ϕ_i = np.array([[-1], [-ϕ_1]])\n",
    "δ_k = np.array([[0]])\n",
    "θ_k = np.array([[1 / β]])\n",
    "β = np.array([[β]])\n",
    "l_λ = np.array([[0]])\n",
    "π_h = np.array([[1]])\n",
    "δ_h = np.array([[0]])\n",
    "θ_h = np.array([[0]])\n",
    "\n",
    "a22 = np.array([[1,   0,   0],\n",
    "                [α, ρ_1, ρ_2],\n",
    "                [0, 1, 0]])\n",
    "\n",
    "c2 = np.array([[0], [σ], [0]])\n",
    "ud = np.array([[0, 1, 0],\n",
    "               [0, 0, 0]])\n",
    "ub = np.array([[100, 0, 0]])\n",
    "\n",
    "x0 = np.array([[0], [0], [1], [0], [0]])\n",
    "\n",
    "info1 = (a22, c2, ub, ud)\n",
    "tech1 = (ϕ_c, ϕ_g, ϕ_i, γ, δ_k, θ_k)\n",
    "pref1 = (β, l_λ, π_h, δ_h, θ_h)\n",
    "econ1 = DLE(info1, tech1, pref1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a5df1b7",
   "metadata": {},
   "source": [
    "To check the solution of this model with that from the **LQ** problem,\n",
    "we select the $ S_c $ matrix from the DLE class.\n",
    "\n",
    "The solution to the\n",
    "DLE economy has:\n",
    "\n",
    "$$\n",
    "c_t = S_c x_t\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a37b188",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "econ1.Sc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b04e147",
   "metadata": {},
   "source": [
    "The state vector in the DLE class is:\n",
    "\n",
    "$$\n",
    "x_t = \\left[ {\\begin{array}{c}\n",
    " h_{t-1} \\\\ k_{t-1} \\\\ z_t\n",
    " \\end{array} }\n",
    " \\right]\n",
    "$$\n",
    "\n",
    "where $ k_{t-1} $ = $ b_{t} $ is set up to be $ b_t $ in the\n",
    "permanent income model.\n",
    "\n",
    "The state vector in the LQ problem is\n",
    "$ \\begin{bmatrix} z_t \\\\ b_t \\end{bmatrix} $.\n",
    "\n",
    "Consequently, the relevant elements of `econ1.Sc` are the same as in\n",
    "$ -F $ occur when we apply other approaches to the same model in the lecture\n",
    "[Optimal Savings II: LQ Techniques](https://python-intro.quantecon.org/perm_income_cons.html) and [this Jupyter\n",
    "notebook](http://nbviewer.jupyter.org/github/QuantEcon/QuantEcon.notebooks/blob/master/permanent_income.ipynb).\n",
    "\n",
    "The plot below quickly replicates the first two figures of\n",
    "that lecture and that  notebook to confirm that the solutions are the same"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbe41aec",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\n",
    "\n",
    "for i in range(25):\n",
    "    econ1.compute_sequence(x0, ts_length=150)\n",
    "    ax1.plot(econ1.c[0], c='g')\n",
    "    ax1.plot(econ1.d[0], c='b')\n",
    "ax1.plot(econ1.c[0], label='Consumption', c='g')\n",
    "ax1.plot(econ1.d[0], label='Income', c='b')\n",
    "ax1.legend()\n",
    "\n",
    "for i in range(25):\n",
    "    econ1.compute_sequence(x0, ts_length=150)\n",
    "    ax2.plot(econ1.k[0], color='r')\n",
    "ax2.plot(econ1.k[0], label='Debt', c='r')\n",
    "ax2.legend()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "date": 1754298591.7405326,
  "filename": "permanent_income_dles.md",
  "kernelspec": {
   "display_name": "Python",
   "language": "python3",
   "name": "python3"
  },
  "title": "Permanent Income Model using the DLE Class"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}