{ "metadata": { "name": "", "signature": "sha256:48a49797a062d3c22eaaebbfe4ed0ccbe380f194e3d651a6a837cab4107ce770" }, "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\n", "from mc_tools import mc_compute_stationary, mc_sample_path\n", "from quantecon.mc_tools import MarkovChain\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Scaled Gaussian Elimination" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def ScaledGE(A):\n", " A = np.array(A, dtype=float)\n", " n = A.shape[0]\n", " x = np.zeros(n) \n", "#Reduction stage\n", "# 1st: scaling column vector for i-th column\n", "# 2nd: reduction.Obtain a submatrix for each i\n", " for i in range(n-1):\n", " \n", " A[i+1:n, i] /= -A[i,i]\n", " A[i+1:n, i+1:n] += np.dot(A[i+1:n, i:i+1], A[i:i+1, i+1:n])\n", " \n", "#Backward Substitution \n", " x[n-1]=1\n", " for i in range(n-2, -1, -1):\n", " x[i] = np.sum((x[j] * A[j, i] for j in range(i+1, n)))\n", " \n", " x /= np.sum(x)\n", "\n", " return x \n", " " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "GTH algorithm" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def GTH(A):\n", " A = np.array(A, dtype=float)\n", " n = A.shape[0]\n", " x = np.zeros(n)\n", "#Reduction stage\n", "# 1st: scaling column vector for i-th column.Scaling is conducted by sum of other components in each row.\n", "# 2nd: reduction.Obtain a submatrix for each i\n", "# Key point is sum of row vector equals to 0. Keep an attention to this point.\n", " for i in range(n-1):\n", " scale = np.sum(A[i, i+1:n])\n", " \n", " A[i+1:n, i] /= scale\n", " A[i+1:n, i+1:n] += np.dot(A[i+1:n, i:i+1], A[i:i+1, i+1:n])\n", "\n", " x[n-1] = 1\n", " for i in range(n-2, -1, -1):\n", " x[i] = np.sum((x[j] * A[j, i] for j in range(i+1, n)))\n", "\n", " x /= np.sum(x)\n", "\n", " return x" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Apply the algorithms" ] }, { "cell_type": "code", "collapsed": false, "input": [ "q=0.5\n", "\n", "for i in range(8,18):\n", " e=1e-1**i\n", "\n", " P =np.array([[-(q+e), q , e ],\n", " [q , -(q+e), e ],\n", " [e , e , -2*e]])\n", " \n", " print('e = 1e-{0}'.format(i))\n", " print(ScaledGE(P))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "e = 1e-8\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-9\n", "[ 0.33333334 0.33333334 0.33333333]\n", "e = 1e-10\n", "[ 0.33333332 0.33333332 0.33333335]\n", "e = 1e-11\n", "[ 0.33333332 0.33333332 0.33333335]\n", "e = 1e-12\n", "[ 0.33333579 0.33333579 0.33332842]\n", "e = 1e-13\n", "[ 0.33329879 0.33329879 0.33340243]\n", "e = 1e-14\n", "[ 0.33342217 0.33342217 0.33315567]\n", "e = 1e-15\n", "[ 0.33342217 0.33342217 0.33315567]\n", "e = 1e-16\n", "[ 0.32152035 0.32152035 0.3569593 ]\n", "e = 1e-17\n", "[ nan nan -0.]\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "q=0.5\n", "\n", "for i in range(8,18):\n", " e=1e-1**i\n", "\n", " P =np.array([[-(q+e), q , e ],\n", " [q , -(q+e), e ],\n", " [e , e , -2*e]])\n", " \n", " print('e = 1e-{0}'.format(i))\n", " print(GTH(P))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "e = 1e-8\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-9\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-10\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-11\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-12\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-13\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-14\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-15\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-16\n", "[ 0.33333333 0.33333333 0.33333333]\n", "e = 1e-17\n", "[ 0.33333333 0.33333333 0.33333333]\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "KMR model analysis" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Parameter\n", "p = 1/3\n", "N = 3" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "def KMR_2x2_P_sequential(N, p, epsilon):\n", " P = np.zeros((N+1, N+1), dtype=float)\n", " \n", " P[0, 0], P[0, 1] = 1 - epsilon * (1/2), epsilon * (1/2) \n", " \n", " P[N, N-1], P[N, N] = epsilon * (1/2), 1 - epsilon * (1/2) \n", " \n", " \n", " for x in range(1, N):\n", " \n", " if x/N >p:\n", " P[x, x-1] = (x/N)*epsilon * (1/2)\n", " P[x, x+1] = (1-(x/N))*(1 - epsilon*(1/2))\n", " P[x, x] = 1 - P[x, x-1] - P[x, x+1] \n", " elif x/N