{ "metadata": { "name": "", "signature": "sha256:255fb4cc32bceceb22af0c76e038632c61c56f21c88996a9bb34c0a881d0830b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import division\n", "import numpy as np\n", "import quantecon as qe" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 38 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Economic Dynamics, Chapter 4: Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercises using Hamilton's Markov chain:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "P = [[0.971, 0.029, 0 ],\n", " [0.145, 0.778, 0.077],\n", " [0 , 0.508, 0.492]]\n", "mc_H = qe.MarkovChain(P)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "mc_H.P" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 40, "text": [ "array([[ 0.971, 0.029, 0. ],\n", " [ 0.145, 0.778, 0.077],\n", " [ 0. , 0.508, 0.492]])" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "states = {'NG': 0, 'MR': 1, 'SR': 2}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 41 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "4.2 Finite State Markov Chains" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "4.2.2 Marginal Distributions" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us use the following function from the previous exercise set:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def cross_sectional_dist(mc, init, T, num_reps=100):\n", " \"\"\"\n", " Return a distribution of visits at time T across num_reps sample paths\n", " of mc with an initial state init.\n", " \n", " \"\"\"\n", " x = np.empty(num_reps, dtype=int)\n", " for i in range(num_reps):\n", " x[i] = mc.simulate(init=init, sample_size=T+1)[-1]\n", " bins = np.arange(mc.n+1)\n", " hist, bin_edges = np.histogram(x, bins=bins)\n", " dist = hist/len(x)\n", " return dist" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "psi = [0, 0, 1]\n", "psi_10 = cross_sectional_dist(mc_H, init=psi, T=10)\n", "print (psi_10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 0.57 0.41 0.02]\n" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "psi_10[states['NG']]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 54, "text": [ "0.56999999999999995" ] } ], "prompt_number": 54 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "4.2.3 Other Identities" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.12" ] }, { "cell_type": "code", "collapsed": false, "input": [ "profits = [1000, 0, -1000]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "t = 5\n", "exp_profits_vec = np.linalg.matrix_power(mc_H.P, t).dot(profits)\n", "print (exp_profits_vec)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 885.34763268 388.68267371 217.74304877]\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "exp_profits_vec[states['SR']]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "217.74304876607994" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.13" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = 1000\n", "exp_profits_vec = np.linalg.matrix_power(mc_H.P, t).dot(profits)\n", "print (exp_profits_vec)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 788.16 788.16 788.16]\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.14" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = 5\n", "psi=[0.2,0.2,0.6]\n", "prob_t_ini=np.dot(psi,np.linalg.matrix_power(mc_H.P, t))\n", "\n", "exp_profits = prob_t_ini.dot(profits)\n", "print (exp_profits)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "385.451890538\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "4.2.4 Constructing Joint Distributions" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.15" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def path_prob(mc, init, X):\n", " \"\"\"\n", " calucualtion of the probablility of a path x\n", " \n", " \"\"\"\n", " \n", " prob=init[X[0]]\n", " for t in range(len(X)-1):\n", " prob=prob*mc[X[t],X[t+1]]\n", " return prob\n", "\n", "Path=[0,1,0]\n", "path_010=path_prob(mc_H.P, init=psi, X=Path)\n", "print(path_010)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.000841\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.16" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.18" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Y_0=[1,1,1]\n", "Y_1=[1,2,1]\n", "Y_2=[1,1,2]\n", "Y_3=[1,2,2]\n", "Y_4=[2,1,1]\n", "Y_5=[2,1,2]\n", "Y_6=[2,2,1]\n", "Y_7=[2,2,2]\n", "\n", "Y=[Y_0,Y_1,Y_2,Y_3,Y_4,Y_5,Y_6,Y_7]\n", "\n", "path_recession= np.empty(len(Y))\n", "for i in range(len(Y)):\n", " path_recession[i]=path_prob(mc_H.P, init=psi, X=Y[i])\n", "\n", "print(path_recession)\n", "\n", "I=[1,1,1,1,1,1,1,1]\n", "Pr_recession=np.dot(path_recession,I)\n", "\n", "print(Pr_recession)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 0.1210568 0.0078232 0.0119812 0.0075768 0.2371344 0.0234696\n", " 0.1499616 0.1452384]\n", "0.704242\n" ] } ], "prompt_number": 26 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.19" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.20" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "Feasible_Path_0=[0,0,0] \n", "Feasible_Path_1=[0,0,1] \n", "Feasible_Path_2=[0,0,2]\n", "Feasible_Path_3=[0,1,0] \n", "Feasible_Path_4=[0,1,1] \n", "Feasible_Path_5=[0,1,2] \n", "Feasible_Path_6=[0,2,0] \n", "Feasible_Path_7=[0,2,1] \n", "Feasible_Path_8=[0,2,2] \n", " \n", "Feasible_Path_9=[1,0,0] \n", "Feasible_Path_10=[1,0,1] \n", "Feasible_Path_11=[1,0,2]\n", "Feasible_Path_12=[1,1,0] \n", "Feasible_Path_13=[1,1,1] \n", "Feasible_Path_14=[1,1,2] \n", "Feasible_Path_15=[1,2,0] \n", "Feasible_Path_16=[1,2,1] \n", "Feasible_Path_17=[1,2,2]\n", "\n", "Feasible_Path_18=[2,0,0] \n", "Feasible_Path_19=[2,0,1] \n", "Feasible_Path_20=[2,0,2]\n", "Feasible_Path_21=[2,1,0] \n", "Feasible_Path_22=[2,1,1] \n", "Feasible_Path_23=[2,1,2] \n", "Feasible_Path_24=[2,2,0] \n", "Feasible_Path_25=[2,2,1] \n", "Feasible_Path_26=[2,2,2]\n", "\n", "Feasible_Path=[Feasible_Path_0,Feasible_Path_1,Feasible_Path_2,Feasible_Path_3\n", " ,Feasible_Path_4,Feasible_Path_5,Feasible_Path_6,Feasible_Path_7\n", " ,Feasible_Path_8,Feasible_Path_9,Feasible_Path_10,Feasible_Path_11\n", " ,Feasible_Path_12,Feasible_Path_13,Feasible_Path_14,Feasible_Path_15\n", " ,Feasible_Path_16,Feasible_Path_17,Feasible_Path_18,Feasible_Path_19\n", " ,Feasible_Path_20,Feasible_Path_21,Feasible_Path_22,Feasible_Path_23\n", " ,Feasible_Path_24,Feasible_Path_25,Feasible_Path_26,]\n", "\n", "\n", "rho=1/(1+0.05)\n", "T=2\n", "\n", "\n", "profits_path=np.empty(len(Feasible_Path))\n", "for j in range(len(Feasible_Path)):\n", " for i in range(T):\n", " profits_path[j]=profits_path[j]+rho**i*profits[Feasible_Path[j][i]]\n", "\n", "Feasible_path_prob=np.empty(len(Feasible_Path))\n", "\n", "for j in range(len(Feasible_Path)):\n", " Feasible_path_prob[j]=path_prob(mc_H.P, init=psi, X=Feasible_Path[j]) \n", "\n", "\n", "print(profits_path)\n", "print(Feasible_path_prob) \n", "\n", "NPV=np.dot(Feasible_path_prob,profits_path)\n", "\n", "print(NPV)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 1952.38095238 1952.38095238 1952.38095238 1000. 1000. 1000.\n", " 47.61904762 47.61904762 47.61904762 952.38095238 952.38095238\n", " 952.38095238 0. 0. 0. -952.38095238\n", " -952.38095238 -952.38095238 -47.61904762 -47.61904762 -47.61904762\n", " -1000. -1000. -1000. -1952.38095238 -1952.38095238\n", " -1952.38095238]\n", "[ 0.1885682 0.0056318 0. 0.000841 0.0045124 0.0004466 0. 0.\n", " 0. 0.028159 0.000841 0. 0.022562 0.1210568\n", " 0.0119812 0. 0.0078232 0.0075768 0. 0. 0.\n", " 0.044196 0.2371344 0.0234696 0. 0.1499616 0.1452384]\n", "-483.238095238\n" ] } ], "prompt_number": 25 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.22" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.2.23" ] }, { "cell_type": "code", "collapsed": false, "input": [ "T=2\n", "NPV=0\n", "\n", "prob_t_ini=np.dot(psi,np.linalg.matrix_power(mc_H.P, t))\n", "exp_profits = prob_t_ini.dot(profits)\n", "\n", "for t in range(T):\n", " NPV=NPV+rho**t*exp_profits\n", "print(NPV)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-170.638095238\n" ] } ], "prompt_number": 29 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "4.3 Stability of Finite State MCs" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "4.3.1 Stationary Distributions" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.3.9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "vecs = mc_H.stationary_distributions\n", "print(vecs)\n", "\n", "exp_profits_st=vecs.dot(profits)\n", "print(exp_profits_st)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 0.8128 0.16256 0.02464]]\n", "[ 788.16]\n" ] } ], "prompt_number": 31 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.3.10" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import ones, identity,transpose\n", "from numpy.linalg import solve\n", "\n", "I=identity(3)\n", "Q,b=ones((3,3)),ones((3,1))\n", "\n", "A=transpose(I-mc_H.P+Q)\n", "print (solve(A,b))\n", "\n", "\n", "psi = solve(A,b)\n", "psi_10 = cross_sectional_dist(mc_H, init=psi, T=20)\n", "print (psi_10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 0.8128 ]\n", " [ 0.16256]\n", " [ 0.02464]]\n", "[ 0.83 0.15 0.02]\n" ] } ], "prompt_number": 72 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "4.3.3 Stability" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.3.27" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "4.3.4 The Law of Large Numbers" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.3.34" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Exercise 4.3.36" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }