{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "20f20651",
   "metadata": {},
   "source": [
    "# Competitive Equilibria with Arrow Securities"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "079bbc38",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n",
    "This lecture presents Python code for experimenting with  competitive equilibria of  an infinite-horizon pure exchange economy with\n",
    "\n",
    "- Heterogeneous agents  \n",
    "- Endowments of a single consumption that are person-specific functions of a common Markov state  \n",
    "- Complete markets in one-period Arrow state-contingent securities  \n",
    "- Discounted expected utility preferences  of a kind often used in macroeconomics and finance  \n",
    "- Common expected utility preferences across agents  \n",
    "- Common beliefs among agents  \n",
    "- A constant relative risk aversion (CRRA)  one-period utility function that implies the existence of a representative consumer whose consumption process can be plugged into a formula for the pricing kernel for  one-step Arrow securities and thereby determine equilibrium prices before determining an equilibrium distribution of wealth  \n",
    "- Differences in their  endowments make individuals want to reallocate consumption goods across time and Markov states  \n",
    "\n",
    "\n",
    "We impose  restrictions that allow us to **Bellmanize** competitive equilibrium prices and quantities\n",
    "\n",
    "We use  Bellman equations  to describe\n",
    "\n",
    "- asset prices  \n",
    "- continuation wealth levels for each person  \n",
    "- state-by-state natural debt limits for each person  \n",
    "\n",
    "\n",
    "In the course of presenting the model we shall encounter these important ideas\n",
    "\n",
    "- a **resolvent operator**   widely  used in this class of models  \n",
    "- absence of  **borrowing limits** in finite horizon economies  \n",
    "- state-by-state **borrowing limits** required in infinite horizon economies  \n",
    "- a counterpart of the **law of iterated expectations** known as a **law of iterated values**  \n",
    "- a  **state-variable degeneracy** that prevails within a competitive equilibrium and that opens the way to various appearances of resolvent operators  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "699565c0",
   "metadata": {},
   "source": [
    "## The setting\n",
    "\n",
    "In effect, this lecture implements a Python version of  the model presented in section 9.3.3 of Ljungqvist and Sargent [[Ljungqvist and Sargent, 2018](https://python.quantecon.org/zreferences.html#id187)]."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73aa2887",
   "metadata": {},
   "source": [
    "### Preferences and endowments\n",
    "\n",
    "In each period $ t\\geq 0 $,  a stochastic\n",
    "event $ s_t \\in {\\bf S} $ is realized.\n",
    "\n",
    "Let the history of events up until time $ t $\n",
    "be denoted $ s^t = [s_0, s_{1}, \\ldots, s_{t-1}, s_t] $.\n",
    "\n",
    "(Sometimes  we inadvertently reverse the recording order and  denote a history as $ s^t = [s_t, s_{t-1}, \\ldots, s_1, s_0] $.)\n",
    "\n",
    "The unconditional\n",
    "probability of observing a particular sequence of events $ s^t $ is\n",
    "given by a probability measure $ \\pi_t(s^t) $.\n",
    "\n",
    "For $ t > \\tau $, we write the probability\n",
    "of observing $ s^t $ conditional on the realization of $ s^\\tau $as $ \\pi_t(s^t\\vert s^\\tau) $.\n",
    "\n",
    "We  assume that trading occurs after\n",
    "observing $ s_0 $,\n",
    "which we capture by setting $ \\pi_0(s_0)=1 $ for the initially\n",
    "given value of $ s_0 $.\n",
    "\n",
    "In this lecture we shall follow much macroeconomics and econometrics and assume that\n",
    "$ \\pi_t(s^t) $  is induced by  a Markov process.\n",
    "\n",
    "There are $ K $ consumers named $ k=1, \\ldots , K $.\n",
    "\n",
    "Consumer $ k $\n",
    "owns a stochastic endowment of one good\n",
    "$ y_t^k(s^t) $ that depends on the\n",
    "history $ s^t $.\n",
    "\n",
    "The history $ s^t $ is publicly observable.\n",
    "\n",
    "Consumer $ k $\n",
    "purchases a history-dependent  consumption plan $ c^k =\n",
    " \\{c_t^k(s^t)\\}_{t=0}^\\infty $\n",
    "\n",
    "Consumer $ k $  orders consumption plans by\n",
    "\n",
    "$$\n",
    "U_k(c^k) =\n",
    "   \\sum_{t=0}^\\infty \\sum_{s^t} \\beta^t u_k[c_t^k(s^t)]\n",
    "   \\pi_t(s^t),\n",
    "$$\n",
    "\n",
    "where $ 0 < \\beta < 1 $.\n",
    "\n",
    "The right side is equal to $ E_0 \\sum_{t=0}^\\infty \\beta^t\n",
    "u_k(c_t^k) $, where $ E_0 $ is the mathematical expectation operator,\n",
    "conditioned on $ s_0 $.\n",
    "\n",
    "Here $ u_k(c) $ is an increasing, twice\n",
    "continuously differentiable, strictly concave function of\n",
    "consumption $ c\\geq 0 $  of one good.\n",
    "\n",
    "The utility function of person $ k $ satisfies\n",
    "the Inada condition\n",
    "\n",
    "$$\n",
    "\\lim_{c \\downarrow 0} u'_k(c) = +\\infty.\n",
    "$$\n",
    "\n",
    "This condition implies that each\n",
    "agent chooses strictly positive consumption for every\n",
    "date-history pair $ (t, s^t) $.\n",
    "\n",
    "Those interior solutions enable us to confine our\n",
    "analysis to Euler equations that hold with equality and also guarantee that\n",
    "**natural debt limits**  don’t bind  in economies like ours  with\n",
    "sequential trading of Arrow securities.\n",
    "\n",
    "We adopt the assumption, routinely\n",
    "employed in much of macroeconomics,\n",
    "that  consumers share   probabilities $ \\pi_t(s^t) $  for all $ t $ and $ s^t $.\n",
    "\n",
    "A **feasible allocation** satisfies\n",
    "\n",
    "$$\n",
    "\\sum_i c_t^k(s^t) \\leq \\sum_i y_t^k(s^t)\n",
    "$$\n",
    "\n",
    "for all $ t $ and for all $ s^t $."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eb0dc79",
   "metadata": {},
   "source": [
    "## Recursive Formulation\n",
    "\n",
    "Following descriptions in section 9.3.3 of Ljungqvist and Sargent [[Ljungqvist and Sargent, 2018](https://python.quantecon.org/zreferences.html#id187)] chapter 9, we  set up  a competitive equilibrium of a pure exchange economy with complete markets in one-period Arrow securities.\n",
    "\n",
    "When  endowments $ y^k(s) $ are all functions of a common Markov state $ s $,\n",
    "the pricing kernel takes the form $ Q(s'|s) $, where $ Q(s'| s) $ is the price of one unit of consumption\n",
    "in state $ s' $ at date $ t+1 $ when the Markov state at date $ t $ is $ s $.\n",
    "\n",
    "These enable us to provide a\n",
    "recursive formulation of a consumer’s optimization problem.\n",
    "\n",
    "Consumer $ k $’s state at time $ t $ is its financial wealth $ a^k_t $ and Markov state $ s_t $.\n",
    "\n",
    "Let $ v^k(a,s) $ be the optimal value of consumer $ k $’s problem\n",
    "starting from state $ (a, s) $.\n",
    "\n",
    "- $ v^k(a,s) $ is the maximum expected discounted utility that consumer $ k $ with current financial wealth $ a $ can attain in Markov state $ s $.  \n",
    "\n",
    "\n",
    "The optimal  value function satisfies the Bellman equation\n",
    "\n",
    "$$\n",
    "v^k(a, s) = \\max_{c, \\hat a(s')} \\left\\{ u_k(c) + \\beta \\sum_{s'} v^k[\\hat a(s'),s'] \\pi (s' | s) \\right\\}\n",
    "$$\n",
    "\n",
    "where  maximization is subject to the budget constraint\n",
    "\n",
    "$$\n",
    "c + \\sum_{s'} \\hat a(s') Q(s' | s)\n",
    "     \\leq  y^k(s) + a\n",
    "$$\n",
    "\n",
    "and also the constraints\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "c & \\geq 0, \\\\\n",
    "           -   \\hat a(s') & \\leq \\bar A^k(s'), \\hskip.5cm \\forall s' \\in {\\bf S}\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "with the second constraint evidently being a set of state-by-state debt limits.\n",
    "\n",
    "Note that the value function and decision rule that solve  the Bellman equation implicitly depend\n",
    "on the pricing kernel $ Q(\\cdot \\vert \\cdot) $ because it appears in the agent’s budget constraint.\n",
    "\n",
    "Use the first-order conditions for  the\n",
    "problem on the right of the Bellman  equation and a\n",
    "Benveniste-Scheinkman formula and rearrange to get\n",
    "\n",
    "$$\n",
    "Q(s_{t+1} | s_t ) = {\\beta u'_k(c_{t+1}^k) \\pi(s_{t+1} | s_t)\n",
    "                 \\over u'_k(c_t^k) },\n",
    "$$\n",
    "\n",
    "where it is understood that $ c_t^k = c^k(s_t) $\n",
    "and $ c_{t+1}^k = c^k(s_{t+1}) $.\n",
    "\n",
    "A **recursive competitive equilibrium** is\n",
    "an initial distribution of wealth $ \\vec a_0 $, a set of borrowing limits $ \\{\\bar A^k(s)\\}_{k=1}^K $,\n",
    "a pricing kernel $ Q(s' | s) $, sets of value functions $ \\{v^k(a,s)\\}_{k=1}^K $, and\n",
    "decision rules $ \\{c^k(s), \\hat a^k(s)\\}_{k=1}^K $ such\n",
    "that\n",
    "\n",
    "- The state-by-state borrowing constraints satisfy the recursion  \n",
    "\n",
    "\n",
    "$$\n",
    "\\bar A^k(s) = y^k(s) + \\sum_{s'} Q(s'|s) \\bar A^k(s')\n",
    "$$\n",
    "\n",
    "- For all $ k $, given\n",
    "  $ a^k_0 $, $ \\bar A^k(s) $,  and the pricing kernel, the value functions and decision rules\n",
    "  solve the consumers’ problems;  \n",
    "- For all realizations of $ \\{s_t\\}_{t=0}^\\infty $, the consumption and asset\n",
    "  portfolios $ \\{\\{c^k_t, $\n",
    "  $ \\{\\hat a^k_{t+1}(s')\\}_{s'}\\}_k\\}_t $  satisfy $ \\sum_k c^k_t = \\sum_k y^k(s_t) $ and\n",
    "  $ \\sum_k \\hat a_{t+1}^k(s') = 0 $\n",
    "  for all $ t $ and $ s' $.  \n",
    "- The initial financial wealth vector $ \\vec a_0 $ satisfies $ \\sum_{k=1}^K a_0^k = 0 $.  \n",
    "\n",
    "\n",
    "The third condition asserts that there are  zero net aggregate claims in all Markov states.\n",
    "\n",
    "The fourth condition asserts that the economy is closed and  starts  from a situation in which there\n",
    "are  zero net aggregate claims."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "504d8c47",
   "metadata": {},
   "source": [
    "## State Variable Degeneracy\n",
    "\n",
    "Please see Ljungqvist and Sargent [[Ljungqvist and Sargent, 2018](https://python.quantecon.org/zreferences.html#id187)] for a description of\n",
    "timing protocol for trades  consistent with an  Arrow-Debreu vision in which\n",
    "\n",
    "- at time $ 0 $ there are complete markets in a complete menu of history $ s^t $-contingent claims on consumption at all dates that all trades occur at time zero  \n",
    "- all trades occur once and for all at time $ 0 $  \n",
    "\n",
    "\n",
    "If  an allocation and pricing kernel $ Q $ in   a recursive competitive equilibrium are to be\n",
    "consistent\n",
    "with the equilibrium allocation and price system that prevail in a  corresponding complete markets economy with such history-contingent commodities and\n",
    "all trades occurring at time $ 0 $,\n",
    "we must impose that $ a_0^k = 0 $ for $ k = 1, \\ldots , K $.\n",
    "\n",
    "That  is\n",
    "what assures that at time $ 0 $ the present value of each agent’s consumption equals the present value of his endowment stream,\n",
    "the  single  budget constraint in   arrangement with all trades occurring at time $ 0 $.\n",
    "\n",
    "Starting the system  with $ a_0^k =0 $ for all $ i $ has a striking implication that we  call **state variable degeneracy**.\n",
    "\n",
    "Here is  what we mean by **state variable degeneracy**:\n",
    "\n",
    "Although two state variables $ a,s $ appear in the value function $ v^k(a,s) $, within a recursive competitive equilibrium starting from $ a_0^k = 0 \\ \\forall i $  at initial Markov state  $ s_0 $, two outcomes  prevail:\n",
    "\n",
    "- $ a_0^k = 0 $ for all $ i $ whenever the Markov state $ s_t $ returns to   $ s_0 $.  \n",
    "- Financial wealth $ a $ is an exact function of the Markov state $ s $.  \n",
    "\n",
    "\n",
    "The first finding  asserts that each household  recurrently visits the zero financial wealth state with which it began life.\n",
    "\n",
    "The second finding  asserts that within a competitive equilibrium  the exogenous Markov state is all we require to track an individual.\n",
    "\n",
    "Financial wealth turns out to be redundant because it is an exact function of the Markov state for each individual.\n",
    "\n",
    "This outcome depends critically on there being complete markets in Arrow securities.\n",
    "\n",
    "For example, it does not prevail in the incomplete markets setting of this lecture [The Aiyagari Model](https://python.quantecon.org/aiyagari.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03df1db1",
   "metadata": {},
   "source": [
    "## Markov Asset Prices\n",
    "\n",
    "Let’s start with a brief summary of formulas for computing asset prices in\n",
    "a Markov setting.\n",
    "\n",
    "The setup assumes the following infrastructure\n",
    "\n",
    "- Markov states: $ s \\in S = \\left[\\bar{s}_1, \\ldots, \\bar{s}_n \\right] $ governed by  an $ n $-state Markov chain with transition probability  \n",
    "\n",
    "\n",
    "$$\n",
    "P_{ij} = \\Pr \\left\\{s_{t+1} = \\bar{s}_j \\mid s_t = \\bar{s}_i \\right\\}\n",
    "$$\n",
    "\n",
    "- A collection $ h=1,\\ldots, H $ of  $ n \\times 1 $ vectors of  $ H $ assets that pay off  $ d^h\\left(s\\right) $  in state $ s $  \n",
    "- An $ n \\times n $ matrix  pricing kernel $ Q $ for one-period Arrow securities, where $ Q_{ij} $  = price at time $ t $ in state $ s_t =\n",
    "  \\bar s_i $ of one unit of consumption when $ s_{t+1} = \\bar s_j $ at time $ t+1 $:  \n",
    "\n",
    "\n",
    "$$\n",
    "Q_{ij} = {\\textrm{Price}} \\left\\{s_{t+1} = \\bar{s}_j \\mid s_t = \\bar{s}_i \\right\\}\n",
    "$$\n",
    "\n",
    "- The price of risk-free one-period bond in state $ i $ is $ R_i^{-1} = \\sum_{j}Q_{i,j} $  \n",
    "- The gross rate of return on a one-period risk-free bond Markov state $ \\bar s_i $ is $ R_i = (\\sum_j Q_{i,j})^{-1} $  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba76b2fd",
   "metadata": {},
   "source": [
    "### Exogenous Pricing Kernel\n",
    "\n",
    "At this point, we’ll take the pricing kernel $ Q $ as exogenous, i.e., determined outside the model\n",
    "\n",
    "Two examples would be\n",
    "\n",
    "- $ Q = \\beta P $ where $ \\beta \\in (0,1) $  \n",
    "- $ Q = S P $ where $ S $ is an $ n \\times n $ matrix of *stochastic discount factors*  \n",
    "\n",
    "\n",
    "We’ll write down implications of  Markov asset pricing in a nutshell for two types of assets\n",
    "\n",
    "- the price in Markov state $ s $ at time $ t $ of a **cum dividend** stock that entitles the owner at the beginning of time $ t $ to the time $ t $ dividend and the option to sell the asset at time $ t+1 $.  The price evidently satisfies $ p^h(\\bar s_i) = d^h(\\bar s_i) + \\sum_j Q_{ij} p^h(\\bar s_j) $, which implies that the vector $ p^h $ satisfies $ p^h = d^h + Q p^h $ which implies the formula  \n",
    "\n",
    "\n",
    "$$\n",
    "p^h = (I - Q)^{-1} d^h\n",
    "$$\n",
    "\n",
    "- the price in Markov state $ s $ at time $ t $ of an **ex dividend** stock that entitles the owner at the end  of time $ t $ to the time $ t+1 $ dividend and the option to sell the stock at time $ t+1 $. The  price is  \n",
    "\n",
    "\n",
    "$$\n",
    "p^h = (I - Q)^{-1} Q d^h\n",
    "$$\n",
    "\n",
    ">**Note**\n",
    ">\n",
    ">The matrix geometric sum $ (I - Q)^{-1} = I + Q + Q^2 + \\cdots $\n",
    "is an example of a **resolvent operator**.\n",
    "\n",
    "Below, we describe an equilibrium model with trading of one-period Arrow securities in which the pricing kernel is endogenous.\n",
    "\n",
    "In constructing our model, we’ll repeatedly encounter formulas that remind us of our asset pricing formulas."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f81ed604",
   "metadata": {},
   "source": [
    "### Multi-Step-Forward Transition Probabilities and Pricing Kernels\n",
    "\n",
    "The $ (i,j) $ component of  the $ \\ell $-step ahead transition probability $ P^\\ell $ is\n",
    "\n",
    "$$\n",
    "Prob(s_{t+\\ell} = \\bar s_j | s_t = \\bar s_i)   = P^{\\ell}_{i,j}\n",
    "$$\n",
    "\n",
    "The $ (i,j) $ component of  the $ \\ell $-step ahead pricing kernel $ Q^\\ell $ is\n",
    "\n",
    "$$\n",
    "Q^{(\\ell)}(s_{t+\\ell} = \\bar s_j | s_t = \\bar s_i)   = Q^{\\ell}_{i,j}\n",
    "$$\n",
    "\n",
    "We’ll use these objects to state a useful property in asset pricing theory."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6a346d1",
   "metadata": {},
   "source": [
    "### Laws of Iterated Expectations and Iterated Values\n",
    "\n",
    "A  **law of iterated values** has a mathematical structure that parallels a\n",
    "**law of iterated expectations**\n",
    "\n",
    "We can describe its structure readily in the  Markov setting of this lecture\n",
    "\n",
    "Recall the following recursion satisfied  by $ j $ step ahead transition probabilites\n",
    "for our finite state Markov chain:\n",
    "\n",
    "$$\n",
    "P_j(s_{t+j}| s_t)  = \\sum_{s_{t+1}} P_{j-1}(s_{t+j}| s_{t+1}) P(s_{t+1} | s_t)\n",
    "$$\n",
    "\n",
    "We can use this recursion to verify the law of iterated expectations applied\n",
    "to computing the conditional expectation of a random variable $ d(s_{t+j}) $ conditioned\n",
    "on $ s_t $ via the following string of equalities\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "E \\left[ E d(s_{t+j}) | s_{t+1} \\right] | s_t\n",
    "    & = \\sum_{s_{t+1}} \\left[ \\sum_{s_{t+j}} d(s_{t+j}) P_{j-1}(s_{t+j}| s_{t+1} ) \\right]         P(s_{t+1} | s_t) \\\\\n",
    " & = \\sum_{s_{t+j}}  d(s_{t+j}) \\left[ \\sum_{s_{t+1}} P_{j-1} ( s_{t+j} |s_{t+1}) P(s_{t+1}| s_t) \\right] \\\\\n",
    " & = \\sum_{s_{t+j}} d(s_{t+j}) P_j (s_{t+j} | s_t ) \\\\\n",
    " & = E d(s_{t+j})| s_t\n",
    "    \\end{aligned}\n",
    "$$\n",
    "\n",
    "The pricing kernel for $ j $ step ahead Arrow securities satisfies the recursion\n",
    "\n",
    "$$\n",
    "Q_j(s_{t+j}| s_t)  = \\sum_{s_{t+1}} Q_{j-1}(s_{t+j}| s_{t+1}) Q(s_{t+1} | s_t)\n",
    "$$\n",
    "\n",
    "The time $ t $ **value** in Markov state $ s_t $  of a time $ t+j $  payout $ d(s_{t+j}) $\n",
    "is\n",
    "\n",
    "$$\n",
    "V(d(s_{t+j})|s_t) = \\sum_{s_{t+j}} d(s_{t+j}) Q_j(s_{t+j}| s_t)\n",
    "$$\n",
    "\n",
    "The **law of iterated values** states\n",
    "\n",
    "$$\n",
    "V \\left[ V (d(s_{t+j}) | s_{t+1}) \\right] | s_t  =   V(d(s_{t+j}))| s_t\n",
    "$$\n",
    "\n",
    "We verify it by pursuing the following a string of inequalities that are counterparts to those we used\n",
    "to verify the law of iterated expectations:\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "V \\left[ V  ( d(s_{t+j}) | s_{t+1} ) \\right] | s_t\n",
    "    & = \\sum_{s_{t+1}} \\left[ \\sum_{s_{t+j}} d(s_{t+j}) Q_{j-1}(s_{t+j}| s_{t+1} ) \\right]         Q(s_{t+1} | s_t) \\\\\n",
    " & = \\sum_{s_{t+j}}  d(s_{t+j}) \\left[ \\sum_{s_{t+1}} Q_{j-1} ( s_{t+j} |s_{t+1}) Q(s_{t+1}| s_t) \\right] \\\\\n",
    " & = \\sum_{s_{t+j}} d(s_{t+j}) Q_j (s_{t+j} | s_t ) \\\\\n",
    " & = E V(d(s_{t+j}))| s_t\n",
    "    \\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c514058d",
   "metadata": {},
   "source": [
    "## General Equilibrium\n",
    "\n",
    "Now we are ready to do some fun calculations.\n",
    "\n",
    "We find it interesting to think in terms of analytical **inputs** into and **outputs** from our general equilibrium theorizing."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ecee20",
   "metadata": {},
   "source": [
    "### Inputs\n",
    "\n",
    "- Markov states: $ s \\in S = \\left[\\bar{s}_1, \\ldots, \\bar{s}_n \\right] $ governed by  an $ n $-state Markov chain with transition probability  \n",
    "\n",
    "\n",
    "$$\n",
    "P_{ij} = \\Pr \\left\\{s_{t+1} = \\bar{s}_j \\mid s_t = \\bar{s}_i \\right\\}\n",
    "$$\n",
    "\n",
    "- A collection of  $ K \\times 1 $ vectors of individual $ k $ endowments: $ y^k\\left(s\\right), k=1,\\ldots, K $  \n",
    "- An $ n \\times 1 $ vector of aggregate endowment:  $ y\\left(s\\right) \\equiv \\sum_{k=1}^K y^k\\left(s\\right) $  \n",
    "- A collection of  $ K \\times 1 $ vectors of individual $ k $ consumptions: $ c^k\\left(s\\right), k=1,\\ldots, K $  \n",
    "- A collection of restrictions  on feasible consumption allocations for $ s \\in S $:  \n",
    "\n",
    "\n",
    "$$\n",
    "c\\left(s\\right)= \\sum_{k=1}^K c^k\\left(s\\right)\n",
    "\\leq  y\\left(s\\right)\n",
    "$$\n",
    "\n",
    "- Preferences: a common utility functional across agents $ E_0 \\sum_{t=0}^\\infty \\beta^t u(c^k_t) $ with  CRRA one-period utility function $ u\\left(c\\right) $ and discount factor $ \\beta \\in (0,1) $  \n",
    "\n",
    "\n",
    "The one-period utility function is\n",
    "\n",
    "$$\n",
    "u \\left(c\\right) = \\frac{c^{1-\\gamma}}{1-\\gamma}\n",
    "$$\n",
    "\n",
    "so that\n",
    "\n",
    "$$\n",
    "u^\\prime \\left(c\\right) = c^{-\\gamma}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a632225",
   "metadata": {},
   "source": [
    "### Outputs\n",
    "\n",
    "- An $ n \\times n $ matrix  pricing kernel $ Q $ for one-period Arrow securities, where $ Q_{ij} $  = price at time $ t $ in state $ s_t = \\bar s_i $ of one unit of consumption when $ s_{t+1} = \\bar s_j $ at time $ t+1 $  \n",
    "- pure exchange so that $ c\\left(s\\right) = y\\left(s\\right) $  \n",
    "- a $ K \\times 1 $ vector distribution of wealth vector $ \\alpha $, $ \\alpha_k \\geq 0, \\sum_{k=1}^K \\alpha_k =1 $  \n",
    "- A collection of $ n \\times 1 $ vectors of individual $ k $ consumptions: $ c^k\\left(s\\right), k=1,\\ldots, K $  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d2be227",
   "metadata": {},
   "source": [
    "### $ Q $ is the Pricing Kernel\n",
    "\n",
    "For any agent  $ k \\in \\left[1, \\ldots, K\\right] $, at the equilibrium allocation,\n",
    "the one-period Arrow securities pricing kernel satisfies\n",
    "\n",
    "$$\n",
    "Q_{ij} = \\beta \\left(\\frac{c^k\\left(\\bar{s}_j\\right)}{c^k\\left(\\bar{s}_i\\right)}\\right)^{-\\gamma} P_{ij}\n",
    "$$\n",
    "\n",
    "where $ Q $ is an $ n \\times n $ matrix\n",
    "\n",
    "This follows from agent $ k $’s first-order necessary conditions.\n",
    "\n",
    "But with the CRRA preferences that we have assumed, individual consumptions vary proportionately\n",
    "with aggregate consumption and therefore with the aggregate endowment.\n",
    "\n",
    "- This is a consequence of our preference specification implying that **Engle curves** are affine in wealth and therefore  satisfy conditions for **Gorman aggregation**  \n",
    "\n",
    "\n",
    "Thus,\n",
    "\n",
    "$$\n",
    "c^k \\left(s\\right) = \\alpha_k c\\left(s\\right) = \\alpha_k y\\left(s\\right)\n",
    "$$\n",
    "\n",
    "for an arbitrary   **distribution of wealth**  in the form of an   $ K \\times 1 $ vector $ \\alpha $\n",
    "that satisfies\n",
    "\n",
    "$$\n",
    "\\alpha_k \\in \\left(0, 1\\right), \\quad \\sum_{k=1}^K \\alpha_k = 1\n",
    "$$\n",
    "\n",
    "This means that we can compute the pricing kernel from\n",
    "\n",
    "\n",
    "<a id='equation-eq-qformula'></a>\n",
    "$$\n",
    "Q_{ij} = \\beta \\left(\\frac{y_j}{y_i}\\right)^{-\\gamma} P_{ij} \\tag{71.1}\n",
    "$$\n",
    "\n",
    "Note that $ Q_{ij} $ is independent of vector $ \\alpha $.\n",
    "\n",
    "**Key finding:** We can compute competitive equilibrium **prices** prior to computing a **distribution of wealth**."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e53bf69",
   "metadata": {},
   "source": [
    "### Values\n",
    "\n",
    "Having computed an equilibrium pricing kernel $ Q $, we can compute several **values** that are required\n",
    "to pose or represent the solution of an individual household’s optimum problem.\n",
    "\n",
    "We denote  an $ K \\times 1 $ vector of  state-dependent values of agents’ endowments in Markov state $ s $ as\n",
    "\n",
    "$$\n",
    "A\\left(s\\right)=\\left[\\begin{array}{c}\n",
    "A^{1}\\left(s\\right)\\\\\n",
    " \\vdots\\\\\n",
    "A^{K}\\left(s\\right)\n",
    "\\end{array}\\right], \\quad s \\in \\left[\\bar{s}_1, \\ldots, \\bar{s}_n\\right]\n",
    "$$\n",
    "\n",
    "and an  $ n \\times 1 $ vector of continuation endowment values for each individual $ k $ as\n",
    "\n",
    "$$\n",
    "A^{k}=\\left[\\begin{array}{c}\n",
    "A^{k}\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "A^{k}\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right], \\quad k \\in \\left[1, \\ldots, K\\right]\n",
    "$$\n",
    "\n",
    "$ A^k $ of consumer $ k $ satisfies\n",
    "\n",
    "$$\n",
    "A^k = \\left[I - Q\\right]^{-1} \\left[ y^k\\right]\n",
    "$$\n",
    "\n",
    "where\n",
    "\n",
    "$$\n",
    "y^{k}=\\left[\\begin{array}{c}\n",
    "y^{k}\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "y^{k}\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right] \\equiv \\begin{bmatrix} y^k_1 \\cr \\vdots \\cr y^k_n \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "In a competitive equilibrium of an **infinite horizon** economy with sequential trading of one-period Arrow securities, $ A^k(s) $ serves as a state-by-state vector of **debt limits** on the quantities of one-period  Arrow securities\n",
    "paying off  in state $ s $ at time $ t+1 $ that individual $ k $ can issue at time $ t $.\n",
    "\n",
    "These are often called **natural debt limits**.\n",
    "\n",
    "Evidently, they equal the maximum amount that it is feasible for  individual $ k $ to repay\n",
    "even if he consumes zero goods forevermore.\n",
    "\n",
    "**Remark:** If  we have an Inada condition at zero consumption or just impose that consumption\n",
    "be nonnegative, then in a **finite horizon** economy with sequential trading of one-period Arrow securities there is no need to impose natural debt limits. See the section on a Finite Horizon Economy  below."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b334191",
   "metadata": {},
   "source": [
    "### Continuation Wealth\n",
    "\n",
    "Continuation wealth plays an important role in Bellmanizing a competitive equilibrium with sequential\n",
    "trading of a complete set of one-period Arrow securities.\n",
    "\n",
    "We denote  an $ K \\times 1 $ vector of  state-dependent continuation wealths in Markov state $ s $ as\n",
    "\n",
    "$$\n",
    "\\psi\\left(s\\right)=\\left[\\begin{array}{c}\n",
    "\\psi^{1}\\left(s\\right)\\\\\n",
    "\\vdots\\\\\n",
    "\\psi^{K}\\left(s\\right)\n",
    "\\end{array}\\right], \\quad s \\in \\left[\\bar{s}_1, \\ldots, \\bar{s}_n\\right]\n",
    "$$\n",
    "\n",
    "and an  $ n \\times 1 $ vector of continuation wealths for each individual $ k $ as\n",
    "\n",
    "$$\n",
    "\\psi^{k}=\\left[\\begin{array}{c}\n",
    "\\psi^{k}\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "\\psi^{k}\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right], \\quad k \\in \\left[1, \\ldots, K\\right]\n",
    "$$\n",
    "\n",
    "Continuation wealth  $ \\psi^k $ of consumer $ k $ satisfies\n",
    "\n",
    "\n",
    "<a id='equation-eq-continwealth'></a>\n",
    "$$\n",
    "\\psi^k = \\left[I - Q\\right]^{-1} \\left[\\alpha_k y - y^k\\right] \\tag{71.2}\n",
    "$$\n",
    "\n",
    "where\n",
    "\n",
    "$$\n",
    "y^{k}=\\left[\\begin{array}{c}\n",
    "y^{k}\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "y^{k}\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right],\\quad y=\\left[\\begin{array}{c}\n",
    "y\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "y\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right]\n",
    "$$\n",
    "\n",
    "Note that $ \\sum_{k=1}^K \\psi^k = {0}_{n \\times 1} $.\n",
    "\n",
    "**Remark:** At the initial state $ s_0 \\in \\begin{bmatrix} \\bar s_1, \\ldots, \\bar s_n \\end{bmatrix} $,\n",
    "the continuation wealth $ \\psi^k(s_0) = 0 $ for all agents $ k = 1, \\ldots, K $.  This indicates that\n",
    "the economy begins with  all agents being debt-free and financial-asset-free at time $ 0 $, state $ s_0 $.\n",
    "\n",
    "**Remark:** Note that all agents’ continuation wealths recurrently return to zero when the Markov state returns to whatever value $ s_0 $ it had at time $ 0 $."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37374ef3",
   "metadata": {},
   "source": [
    "### Optimal Portfolios\n",
    "\n",
    "A nifty feature of the model is that an optimal portfolio of  a type $ k $ agent equals the continuation wealth that we just computed.\n",
    "\n",
    "Thus, agent $ k $’s state-by-state purchases of Arrow securities next period depend only on next period’s\n",
    "Markov state and equal\n",
    "\n",
    "\n",
    "<a id='equation-eqn-optport'></a>\n",
    "$$\n",
    "a_k(s) = \\psi^k(s), \\quad s \\in \\left[\\bar s_1, \\ldots, \\bar s_n \\right] \\tag{71.3}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce8dda78",
   "metadata": {},
   "source": [
    "### Equilibrium Wealth Distribution $ \\alpha $\n",
    "\n",
    "With the initial state being  a particular state $ s_0 \\in \\left[\\bar{s}_1, \\ldots, \\bar{s}_n\\right] $,\n",
    "we must have\n",
    "\n",
    "$$\n",
    "\\psi^k\\left(s_0\\right) = 0, \\quad k=1, \\ldots, K\n",
    "$$\n",
    "\n",
    "which means the equilibrium distribution of wealth satisfies\n",
    "\n",
    "\n",
    "<a id='equation-eqn-alphakform'></a>\n",
    "$$\n",
    "\\alpha_k = \\frac{V_z y^k}{V_z y} \\tag{71.4}\n",
    "$$\n",
    "\n",
    "where $ V \\equiv \\left[I - Q\\right]^{-1} $ and $ z $ is the row index corresponding to the initial state $ s_0 $.\n",
    "\n",
    "Since $ \\sum_{k=1}^K V_z y^k = V_z y $,  $ \\sum_{k=1}^K \\alpha_k = 1 $.\n",
    "\n",
    "In summary, here is the logical flow of an algorithm to compute a competitive equilibrium:\n",
    "\n",
    "- compute $ Q $ from the aggregate allocation and  formula [(71.1)](#equation-eq-qformula)  \n",
    "- compute the distribution of wealth $ \\alpha $ from the formula [(71.4)](#equation-eqn-alphakform)  \n",
    "- Using  $ \\alpha $ assign each consumer $ k $ the share  $ \\alpha_k $ of the aggregate endowment at each state  \n",
    "- return to the $ \\alpha $-dependent formula [(71.2)](#equation-eq-continwealth)  and compute continuation wealths  \n",
    "- via formula [(71.3)](#equation-eqn-optport) equate agent $ k $’s portfolio to its continuation wealth state by state  \n",
    "\n",
    "\n",
    "We can also add formulas for optimal value functions in  a competitive equilibrium with trades\n",
    "in a complete set of one-period state-contingent Arrow securities.\n",
    "\n",
    "Call the optimal value functions $ J^k $ for consumer $ k $.\n",
    "\n",
    "For the infinite horizon economy now under study, the formula is\n",
    "\n",
    "$$\n",
    "J^k = (I - \\beta P)^{-1} u(\\alpha_k y)  , \\quad u(c) = \\frac{c^{1-\\gamma}}{1-\\gamma}\n",
    "$$\n",
    "\n",
    "where it is understood that $ u(\\alpha_k y) $ is a vector."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d95d8c94",
   "metadata": {},
   "source": [
    "## Finite Horizon\n",
    "\n",
    "We now describe a finite-horizon version of the economy that operates  for $ T+1 $ periods\n",
    "$ t \\in {\\bf T} = \\{ 0, 1, \\ldots, T\\} $.\n",
    "\n",
    "Consequently, we’ll  want  $ T+1 $ counterparts to objects described above, with one important exception:\n",
    "we won’t need **borrowing limits**.\n",
    "\n",
    "- borrowing limits aren’t required for a finite horizon economy in which a\n",
    "  one-period utility function $ u(c) $ satisfies an Inada condition that sets the marginal utility of consumption at zero consumption to zero.  \n",
    "- Nonnegativity of consumption choices at all $ t \\in {\\bf T} $ automatically\n",
    "  limits borrowing.  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28111eb6",
   "metadata": {},
   "source": [
    "### Continuation Wealths\n",
    "\n",
    "We denote  a $ K \\times 1 $ vector of  state-dependent continuation wealths in Markov state $ s $ at time $ t $ as\n",
    "\n",
    "$$\n",
    "\\psi_t\\left(s\\right)=\\left[\\begin{array}{c}\n",
    "\\psi^{1}\\left(s\\right)\\\\\n",
    "\\vdots\\\\\n",
    "\\psi^{K}\\left(s\\right)\n",
    "\\end{array}\\right], \\quad s \\in \\left[\\bar{s}_1, \\ldots, \\bar{s}_n\\right]\n",
    "$$\n",
    "\n",
    "and an  $ n \\times 1 $ vector of continuation wealths for each individual $ k $ as\n",
    "\n",
    "$$\n",
    "\\psi_t^{k}=\\left[\\begin{array}{c}\n",
    "\\psi_t^{k}\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "\\psi_t^{k}\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right], \\quad k \\in \\left[1, \\ldots, K\\right]\n",
    "$$\n",
    "\n",
    "Continuation wealths  $ \\psi^k $ of consumer $ k $ satisfy\n",
    "\n",
    "\n",
    "<a id='equation-eq-vv'></a>\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\psi_T^k & =  \\left[\\alpha_k y - y^k\\right] \\cr\n",
    "\\psi_{T-1}^k & =  \\left[I + Q \\right] \\left[\\alpha_k y - y^k\\right] \\cr\n",
    "\\vdots \\quad  & \\quad \\quad \\quad \\vdots \\cr\n",
    "\\psi_0^k  & = \\left[I + Q + Q^2 + \\cdots + Q^T \\right] \\left[\\alpha_k y - y^k\\right]\n",
    "\\end{aligned} \\tag{71.5}\n",
    "$$\n",
    "\n",
    "where\n",
    "\n",
    "$$\n",
    "y^{k}=\\left[\\begin{array}{c}\n",
    "y^{k}\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "y^{k}\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right],\\quad y=\\left[\\begin{array}{c}\n",
    "y\\left(\\bar{s}_{1}\\right)\\\\\n",
    "\\vdots\\\\\n",
    "y\\left(\\bar{s}_{n}\\right)\n",
    "\\end{array}\\right]\n",
    "$$\n",
    "\n",
    "Note that $ \\sum_{k=1}^K \\psi_t^k = {0}_{n \\times 1} $ for all $ t \\in {\\bf T} $.\n",
    "\n",
    "**Remark:** At the initial state $ s_0 \\in \\begin{bmatrix} \\bar s_1, \\ldots, \\bar s_n \\end{bmatrix} $,\n",
    "for all agents $ k = 1, \\ldots, K $, continuation wealth $ \\psi_0^k(s_0) = 0 $.  This indicates that\n",
    "the economy begins with  all agents being debt-free and financial-asset-free at time $ 0 $, state $ s_0 $.\n",
    "\n",
    "**Remark:** Note that all agents’ continuation wealths  return to zero when the Markov state returns to whatever value $ s_0 $ it had at time $ 0 $. This will recur if the Markov chain makes the initial state $ s_0 $ recurrent.\n",
    "\n",
    "With the initial state being  a particular state $ s_0 \\in \\left[\\bar{s}_1, \\ldots, \\bar{s}_n\\right] $, we must have\n",
    "\n",
    "$$\n",
    "\\psi_0^k\\left(s_0\\right) = 0, \\quad k=1, \\ldots, K\n",
    "$$\n",
    "\n",
    "which means the equilibrium distribution of wealth satisfies\n",
    "\n",
    "\n",
    "<a id='equation-eq-w'></a>\n",
    "$$\n",
    "\\alpha_k = \\frac{V_z y^k}{V_z y} \\tag{71.6}\n",
    "$$\n",
    "\n",
    "where  now in our finite-horizon economy\n",
    "\n",
    "\n",
    "<a id='equation-eq-ww'></a>\n",
    "$$\n",
    "V = \\left[I + Q + Q^2 + \\cdots + Q^T \\right] \\tag{71.7}\n",
    "$$\n",
    "\n",
    "and $ z $ is the row index corresponding to the initial state $ s_0 $.\n",
    "\n",
    "Since $ \\sum_{k=1}^K V_z y^k = V_z y $,  $ \\sum_{k=1}^K \\alpha_k = 1 $.\n",
    "\n",
    "In summary, here is the logical flow of an algorithm to compute a competitive equilibrium with Arrow securities\n",
    "in our finite-horizon Markov economy:\n",
    "\n",
    "- compute $ Q $ from the aggregate allocation and  formula [(71.1)](#equation-eq-qformula)  \n",
    "- compute the distribution of wealth $ \\alpha $ from  formulas [(71.6)](#equation-eq-w) and [(71.7)](#equation-eq-ww)  \n",
    "- using  $ \\alpha $, assign each consumer $ k $ the share  $ \\alpha_k $ of the aggregate endowment at each state  \n",
    "- return to the $ \\alpha $-dependent formula [(71.5)](#equation-eq-vv) for continuation wealths and compute continuation wealths  \n",
    "- equate agent $ k $’s portfolio to its continuation wealth state by state  \n",
    "\n",
    "\n",
    "While for  the infinite horizon economy, the formula for value functions  is\n",
    "\n",
    "$$\n",
    "J^k = (I - \\beta P)^{-1} u(\\alpha_k y)  , \\quad u(c) = \\frac{c^{1-\\gamma}}{1-\\gamma}\n",
    "$$\n",
    "\n",
    "for the finite horizon economy the formula is\n",
    "\n",
    "$$\n",
    "J_0^k = (I + \\beta P + \\cdots + \\beta^T P^T) u(\\alpha_k y) ,\n",
    "$$\n",
    "\n",
    "where it is understood that $ u(\\alpha_k y) $ is a vector."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45107605",
   "metadata": {},
   "source": [
    "## Python Code\n",
    "\n",
    "We are ready to dive into some Python code.\n",
    "\n",
    "As usual, we start with Python imports."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45738fb5",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bda944f1",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "np.set_printoptions(suppress=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c7c21cc",
   "metadata": {},
   "source": [
    "First, we create a Python class to compute  the objects that comprise a competitive equilibrium\n",
    "with sequential trading of one-period Arrow securities.\n",
    "\n",
    "In addition to infinite-horizon economies, the code is set up to handle finite-horizon economies indexed by horizon $ T $.\n",
    "\n",
    "We’ll study examples of  finite horizon economies after we first  look at  some infinite-horizon economies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbc07766",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "class RecurCompetitive:\n",
    "    \"\"\"\n",
    "    A class that represents a recursive competitive economy\n",
    "    with one-period Arrow securities.\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self,\n",
    "                 s,        # state vector\n",
    "                 P,        # transition matrix\n",
    "                 ys,       # endowments ys = [y1, y2, .., yI]\n",
    "                 γ=0.5,    # risk aversion\n",
    "                 β=0.98,   # discount rate\n",
    "                 T=None):  # time horizon, none if infinite\n",
    "\n",
    "        # preference parameters\n",
    "        self.γ = γ\n",
    "        self.β = β\n",
    "\n",
    "        # variables dependent on state\n",
    "        self.s = s\n",
    "        self.P = P\n",
    "        self.ys = ys\n",
    "        self.y = np.sum(ys, 1)\n",
    "\n",
    "        # dimensions\n",
    "        self.n, self.K = ys.shape\n",
    "\n",
    "        # compute pricing kernel\n",
    "        self.Q = self.pricing_kernel()\n",
    "\n",
    "        # compute price of risk-free one-period bond\n",
    "        self.PRF = self.price_risk_free_bond()\n",
    "\n",
    "        # compute risk-free rate\n",
    "        self.R = self.risk_free_rate()\n",
    "\n",
    "        # V = [I - Q]^{-1} (infinite case)\n",
    "        if T is None:\n",
    "            self.T = None\n",
    "            self.V = np.empty((1, n, n))\n",
    "            self.V[0] = np.linalg.inv(np.eye(n) - self.Q)\n",
    "        # V = [I + Q + Q^2 + ... + Q^T] (finite case)\n",
    "        else:\n",
    "            self.T = T\n",
    "            self.V = np.empty((T+1, n, n))\n",
    "            self.V[0] = np.eye(n)\n",
    "\n",
    "            Qt = np.eye(n)\n",
    "            for t in range(1, T+1):\n",
    "                Qt = Qt.dot(self.Q)\n",
    "                self.V[t] = self.V[t-1] + Qt\n",
    "\n",
    "        # natural debt limit\n",
    "        self.A = self.V[-1] @ ys\n",
    "\n",
    "    def u(self, c):\n",
    "        \"The CRRA utility\"\n",
    "\n",
    "        return c ** (1 - self.γ) / (1 - self.γ)\n",
    "\n",
    "    def u_prime(self, c):\n",
    "        \"The first derivative of CRRA utility\"\n",
    "\n",
    "        return c ** (-self.γ)\n",
    "\n",
    "    def pricing_kernel(self):\n",
    "        \"Compute the pricing kernel matrix Q\"\n",
    "\n",
    "        c = self.y\n",
    "\n",
    "        n = self.n\n",
    "        Q = np.empty((n, n))\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                ratio = self.u_prime(c[j]) / self.u_prime(c[i])\n",
    "                Q[i, j] = self.β * ratio * P[i, j]\n",
    "\n",
    "        self.Q = Q\n",
    "\n",
    "        return Q\n",
    "\n",
    "    def wealth_distribution(self, s0_idx):\n",
    "        \"Solve for wealth distribution α\"\n",
    "\n",
    "        # set initial state\n",
    "        self.s0_idx = s0_idx\n",
    "\n",
    "        # simplify notations\n",
    "        n = self.n\n",
    "        Q = self.Q\n",
    "        y, ys = self.y, self.ys\n",
    "\n",
    "        # row of V corresponding to s0\n",
    "        Vs0 = self.V[-1, s0_idx, :]\n",
    "        α = Vs0 @ self.ys / (Vs0 @ self.y)\n",
    "\n",
    "        self.α = α\n",
    "\n",
    "        return α\n",
    "\n",
    "    def continuation_wealths(self):\n",
    "        \"Given α, compute the continuation wealths ψ\"\n",
    "\n",
    "        diff = np.empty((n, K))\n",
    "        for k in range(K):\n",
    "            diff[:, k] = self.α[k] * self.y - self.ys[:, k]\n",
    "\n",
    "        ψ = self.V @ diff\n",
    "        self.ψ = ψ\n",
    "\n",
    "        return ψ\n",
    "\n",
    "    def price_risk_free_bond(self):\n",
    "        \"Give Q, compute price of one-period risk free bond\"\n",
    "\n",
    "        PRF = np.sum(self.Q, 0)\n",
    "        self.PRF = PRF\n",
    "\n",
    "        return PRF\n",
    "\n",
    "    def risk_free_rate(self):\n",
    "        \"Given Q, compute one-period gross risk-free interest rate R\"\n",
    "\n",
    "        R = np.sum(self.Q, 0)\n",
    "        R = np.reciprocal(R)\n",
    "        self.R = R\n",
    "\n",
    "        return R\n",
    "\n",
    "    def value_functionss(self):\n",
    "        \"Given α, compute the optimal value functions J in equilibrium\"\n",
    "\n",
    "        n, T = self.n, self.T\n",
    "        β = self.β\n",
    "        P = self.P\n",
    "\n",
    "        # compute (I - βP)^(-1) in infinite case\n",
    "        if T is None:\n",
    "            P_seq = np.empty((1, n, n))\n",
    "            P_seq[0] = np.linalg.inv(np.eye(n) - β * P)\n",
    "        # and (I + βP + ... + β^T P^T) in finite case\n",
    "        else:\n",
    "            P_seq = np.empty((T+1, n, n))\n",
    "            P_seq[0] = np.eye(n)\n",
    "\n",
    "            Pt = np.eye(n)\n",
    "            for t in range(1, T+1):\n",
    "                Pt = Pt.dot(P)\n",
    "                P_seq[t] = P_seq[t-1] + Pt * β ** t\n",
    "\n",
    "        # compute the matrix [u(α_1 y), ..., u(α_K, y)]\n",
    "        flow = np.empty((n, K))\n",
    "        for k in range(K):\n",
    "            flow[:, k] = self.u(self.α[k] * self.y)\n",
    "\n",
    "        J = P_seq @ flow\n",
    "\n",
    "        self.J = J\n",
    "\n",
    "        return J"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "331182b3",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "We’ll use our code to construct equilibrium objects in several example economies.\n",
    "\n",
    "Our first several examples will be infinite horizon economies.\n",
    "\n",
    "Our final example will be a finite horizon economy."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b5ffff4",
   "metadata": {},
   "source": [
    "### Example 1\n",
    "\n",
    "Please read the preceding class for default parameter values and the  following Python code for the fundamentals of the economy.\n",
    "\n",
    "Here goes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a100ada",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# dimensions\n",
    "K, n = 2, 2\n",
    "\n",
    "# states\n",
    "s = np.array([0, 1])\n",
    "\n",
    "# transition\n",
    "P = np.array([[.5, .5], [.5, .5]])\n",
    "\n",
    "# endowments\n",
    "ys = np.empty((n, K))\n",
    "ys[:, 0] = 1 - s       # y1\n",
    "ys[:, 1] = s           # y2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "627d9a35",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex1 = RecurCompetitive(s, P, ys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23cb06b8",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# endowments\n",
    "ex1.ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2065d4b",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# pricing kernal\n",
    "ex1.Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a98af91",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Risk free rate R\n",
    "ex1.R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "921019a8",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# natural debt limit, A = [A1, A2, ..., AI]\n",
    "ex1.A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9dfd19c",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 1\n",
    "print(f'α = {ex1.wealth_distribution(s0_idx=0)}')\n",
    "print(f'ψ = \\n{ex1.continuation_wealths()}')\n",
    "print(f'J = \\n{ex1.value_functionss()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dac3809f",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 2\n",
    "print(f'α = {ex1.wealth_distribution(s0_idx=1)}')\n",
    "print(f'ψ = \\n{ex1.continuation_wealths()}')\n",
    "print(f'J = \\n{ex1.value_functionss()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ba29e87",
   "metadata": {},
   "source": [
    "### Example 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf093230",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# dimensions\n",
    "K, n = 2, 2\n",
    "\n",
    "# states\n",
    "s = np.array([1, 2])\n",
    "\n",
    "# transition\n",
    "P = np.array([[.5, .5], [.5, .5]])\n",
    "\n",
    "# endowments\n",
    "ys = np.empty((n, K))\n",
    "ys[:, 0] = 1.5         # y1\n",
    "ys[:, 1] = s           # y2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83d2670f",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex2 = RecurCompetitive(s, P, ys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2b9cec8",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# endowments\n",
    "\n",
    "print(\"ys = \\n\", ex2.ys)\n",
    "\n",
    "# pricing kernal\n",
    "print (\"Q = \\n\", ex2.Q)\n",
    "\n",
    "# Risk free rate R\n",
    "print(\"R = \", ex2.R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f98f1e9d",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# pricing kernal\n",
    "ex2.Q"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9911a51",
   "metadata": {},
   "source": [
    "Note that the pricing kernal in example economies 1 and 2 differ.\n",
    "\n",
    "This comes from differences in the aggregate endowments in state 1 and 2 in example 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "628c6641",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex2.β * ex2.u_prime(3.5) / ex2.u_prime(2.5) * ex2.P[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdac5081",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex2.β * ex2.u_prime(2.5) / ex2.u_prime(3.5) * ex2.P[1,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e129365",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Risk free rate R\n",
    "ex2.R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45a889bc",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# natural debt limit, A = [A1, A2, ..., AI]\n",
    "ex2.A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d5dfd70",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 1\n",
    "print(f'α = {ex2.wealth_distribution(s0_idx=0)}')\n",
    "print(f'ψ = \\n{ex2.continuation_wealths()}')\n",
    "print(f'J = \\n{ex2.value_functionss()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2610b6cd",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 1\n",
    "print(f'α = {ex2.wealth_distribution(s0_idx=1)}')\n",
    "print(f'ψ = \\n{ex2.continuation_wealths()}')\n",
    "print(f'J = \\n{ex2.value_functionss()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8292045",
   "metadata": {},
   "source": [
    "### Example 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15a40726",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# dimensions\n",
    "K, n = 2, 2\n",
    "\n",
    "# states\n",
    "s = np.array([1, 2])\n",
    "\n",
    "# transition\n",
    "λ = 0.9\n",
    "P = np.array([[1-λ, λ], [0, 1]])\n",
    "\n",
    "# endowments\n",
    "ys = np.empty((n, K))\n",
    "ys[:, 0] = [1, 0]         # y1\n",
    "ys[:, 1] = [0, 1]         # y2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "058d104c",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex3 = RecurCompetitive(s, P, ys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a2d7085",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# endowments\n",
    "\n",
    "print(\"ys = \", ex3.ys)\n",
    "\n",
    "# pricing kernel\n",
    "print (\"Q = \", ex3.Q)\n",
    "\n",
    "# Risk free rate R\n",
    "print(\"R = \", ex3.R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bc29f18",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# pricing kernel\n",
    "ex3.Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4563849a",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# natural debt limit, A = [A1, A2, ..., AI]\n",
    "ex3.A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b63b8dca",
   "metadata": {},
   "source": [
    "Note that the natural debt limit for agent $ 1 $ in state $ 2 $ is $ 0 $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35947e47",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 1\n",
    "print(f'α = {ex3.wealth_distribution(s0_idx=0)}')\n",
    "print(f'ψ = \\n{ex3.continuation_wealths()}')\n",
    "print(f'J = \\n{ex3.value_functionss()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a4b01e6",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 1\n",
    "print(f'α = {ex3.wealth_distribution(s0_idx=1)}')\n",
    "print(f'ψ = \\n{ex3.continuation_wealths()}')\n",
    "print(f'J = \\n{ex3.value_functionss()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6873065",
   "metadata": {},
   "source": [
    "For the specification of the Markov chain in example 3, let’s take a look at how the equilibrium allocation changes as a function of transition probability $ \\lambda $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53f47178",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "λ_seq = np.linspace(0, 0.99, 100)\n",
    "\n",
    "# prepare containers\n",
    "αs0_seq = np.empty((len(λ_seq), 2))\n",
    "αs1_seq = np.empty((len(λ_seq), 2))\n",
    "\n",
    "for i, λ in enumerate(λ_seq):\n",
    "    P = np.array([[1-λ, λ], [0, 1]])\n",
    "    ex3 = RecurCompetitive(s, P, ys)\n",
    "\n",
    "    # initial state s0 = 1\n",
    "    α = ex3.wealth_distribution(s0_idx=0)\n",
    "    αs0_seq[i, :] = α\n",
    "\n",
    "    # initial state s0 = 2\n",
    "    α = ex3.wealth_distribution(s0_idx=1)\n",
    "    αs1_seq[i, :] = α"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ae93ad8",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "fig, axs = plt.subplots(1, 2, figsize=(12, 4))\n",
    "\n",
    "for i, αs_seq in enumerate([αs0_seq, αs1_seq]):\n",
    "    for j in range(2):\n",
    "        axs[i].plot(λ_seq, αs_seq[:, j], label=f'α{j+1}')\n",
    "        axs[i].set_xlabel('λ')\n",
    "        axs[i].set_title(f'initial state s0 = {s[i]}')\n",
    "        axs[i].legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9392832",
   "metadata": {},
   "source": [
    "### Example 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bafb225",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# dimensions\n",
    "K, n = 2, 3\n",
    "\n",
    "# states\n",
    "s = np.array([1, 2, 3])\n",
    "\n",
    "# transition\n",
    "λ = .9\n",
    "μ = .9\n",
    "δ = .05\n",
    "\n",
    "# prosperous, moderate, and recession states\n",
    "P = np.array([[1-λ, λ, 0], [μ/2, μ, μ/2], [(1-δ)/2, (1-δ)/2, δ]])\n",
    "\n",
    "# endowments\n",
    "ys = np.empty((n, K))\n",
    "ys[:, 0] = [.25, .75, .2]       # y1\n",
    "ys[:, 1] = [1.25, .25, .2]      # y2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba6890e8",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex4 = RecurCompetitive(s, P, ys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96888b04",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# endowments\n",
    "print(\"ys = \\n\", ex4.ys)\n",
    "\n",
    "# pricing kernal\n",
    "print (\"Q = \\n\", ex4.Q)\n",
    "\n",
    "# Risk free rate R\n",
    "print(\"R = \", ex4.R)\n",
    "\n",
    "# natural debt limit, A = [A1, A2, ..., AI]\n",
    "print(\"A = \\n\", ex4.A)\n",
    "\n",
    "print('')\n",
    "\n",
    "for i in range(1, 4):\n",
    "    # when the initial state is state i\n",
    "    print(f\"when the initial state is state {i}\")\n",
    "    print(f'α = {ex4.wealth_distribution(s0_idx=i-1)}')\n",
    "    print(f'ψ = \\n{ex4.continuation_wealths()}')\n",
    "    print(f'J = \\n{ex4.value_functionss()}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8809380c",
   "metadata": {},
   "source": [
    "### Finite Horizon Example\n",
    "\n",
    "We now  revisit the economy defined in example 1, but set the time horizon to be $ T=10 $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d5626b8",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# dimensions\n",
    "K, n = 2, 2\n",
    "\n",
    "# states\n",
    "s = np.array([0, 1])\n",
    "\n",
    "# transition\n",
    "P = np.array([[.5, .5], [.5, .5]])\n",
    "\n",
    "# endowments\n",
    "ys = np.empty((n, K))\n",
    "ys[:, 0] = 1 - s       # y1\n",
    "ys[:, 1] = s           # y2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ac6ef44",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex1_finite = RecurCompetitive(s, P, ys, T=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f35b529e",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# (I + Q + Q^2 + ... + Q^T)\n",
    "ex1_finite.V[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "440992f4",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# endowments\n",
    "ex1_finite.ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82cee101",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# pricing kernal\n",
    "ex1_finite.Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b3c5f07",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Risk free rate R\n",
    "ex1_finite.R"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f94380d",
   "metadata": {},
   "source": [
    "In the finite time horizon case, `ψ` and `J` are returned as sequences.\n",
    "\n",
    "Components  are ordered from $ t=T $ to $ t=0 $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7040956a",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 2\n",
    "print(f'α = {ex1_finite.wealth_distribution(s0_idx=0)}')\n",
    "print(f'ψ = \\n{ex1_finite.continuation_wealths()}\\n')\n",
    "print(f'J = \\n{ex1_finite.value_functionss()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "448cee39",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# when the initial state is state 2\n",
    "print(f'α = {ex1_finite.wealth_distribution(s0_idx=1)}')\n",
    "print(f'ψ = \\n{ex1_finite.continuation_wealths()}\\n')\n",
    "print(f'J = \\n{ex1_finite.value_functionss()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe51e562",
   "metadata": {},
   "source": [
    "We can check the results with finite horizon converges to the ones with infinite horizon as $ T \\rightarrow \\infty $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f86aa79d",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex1_large = RecurCompetitive(s, P, ys, T=10000)\n",
    "ex1_large.wealth_distribution(s0_idx=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3b719e4",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex1.V, ex1_large.V[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "268f936f",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex1_large.continuation_wealths()\n",
    "ex1.ψ, ex1_large.ψ[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec069eaa",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "ex1_large.value_functionss()\n",
    "ex1.J, ex1_large.J[-1]"
   ]
  }
 ],
 "metadata": {
  "date": 1751374719.675549,
  "filename": "ge_arrow.md",
  "kernelspec": {
   "display_name": "Python",
   "language": "python3",
   "name": "python3"
  },
  "title": "Competitive Equilibria with Arrow Securities"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}