{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DiscreteDP Example: Bioeconomic Model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "** Eiki Takigawa **\n", "\n", "*Department of Economics, University of Tokyo*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From Miranda and Fackler, Applied Computational Economics and Finance, 2002, Section 7.6.6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia translation of the [python version](http://nbviewer.jupyter.org/github/QuantEcon/QuantEcon.notebooks/blob/master/ddp_ex_MF_7_6_6_py.ipynb)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Plots.PyPlotBackend()" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using QuantEcon\n", "using Plots\n", "pyplot()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "emax = 8 # Energy carrying capacity\n", "n = emax + 1 # Number of states, 0, ..., emax\n", "m = 3 # Number of areas (actions), 0, ..., m-1\n", "e = [2, 4, 5] # Energy offerings\n", "p = [1.0, 0.7, 0.8] # Survival probabilities\n", "q = [0.5, 0.8, 0.7] # Success probabilities\n", "\n", "T = 10 # Time horizon\n", "\n", "# Terminal values\n", "v_term = ones(n)\n", "v_term[1]= 0;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We follow the state-action pairs formulation approach." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "L = n * m # Number of feasible state-action pairs\n", "\n", "s_indices = Vector{Int}(L)\n", "for i in 1:n\n", " s_indices[(i-1) * m + 1 : i * m] = i\n", "end\n", "\n", "a_indices = Vector{Int}(L)\n", "for i in 1:n\n", " a_indices[(i-1) * m + 1 : i * m] = 1:m\n", "end" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Reward vector\n", "R = zeros(L);" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Transition probability array\n", "Q = zeros(L, n)\n", "for (i, s) in enumerate(s_indices)\n", " k = a_indices[i]\n", " if s == 1\n", " Q[i, 1] = 1\n", " elseif s == 2\n", " Q[i, minimum([emax+1, s-1+e[k]])] = p[k] * q[k]\n", " Q[i, 1] = 1 - p[k] * q[k]\n", " else\n", " Q[i, minimum([emax+1, s-1+e[k]])] = p[k] * q[k]\n", " Q[i, s-1] = p[k] * (1 - q[k])\n", " Q[i, 1] = 1 - p[k]\n", " end\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The current version of DiscreteDP does not accept $\\beta = 1$. \n", "So I use a value very close to 1." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Discount factor\n", "beta = 0.99999999;" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a DiscreteDP\n", "ddp = DiscreteDP(R, Q, beta, s_indices, a_indices);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "backward_induction used in the python version does not exist in QuantEcon.jl. \n", "So I simply repeat bellman_operator." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vs = Matrix(T+1, n) \n", "vs[T+1, :] = v_term\n", "for t in T+1:-1:2\n", " vs[t-1, :] = bellman_operator(ddp, vs[t, :])\n", "end" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = bar(0:8, vs[1, :], xlabel=\"Stock of Energy\", xticks=0:1:8,\n", " ylabel=\"Probability\", yticks=0:0.2:1, ylims=(0,1), label=\"\", \n", " title=\"Survivial Probability, Period 0\", )\n", "p2 = bar(0:8, vs[6, :], xlabel=\"Stock of Energy\", xticks=0:1:8,\n", " ylabel=\"Probability\", yticks=0:0.2:1, ylims=(0,1), label=\"\",\n", " title=\"Survivial Probability, Period 5\", )\n", "plot(p1, p2, layout=(1,2), size=(700,230))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.6.0", "language": "julia", "name": "julia-0.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }