{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Markov Chain" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Definition" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At time n+1, the distribution is:[[0.15 0.8 0.05]]\n" ] } ], "source": [ "A = [ 0.9 0.15 0.25 ; 0.075 0.8 0.25 ; 0.025 0.05 0.5 ];\n", "x_1 = [0,1,0];\n", "B1 = A*x_1;\n", "println(\"At time n+1, the distribution is:\",\"[\",B1',\"]\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In line 1 we construct a 3*3 matrix. \n", "In line 2 we set a stochastic row vector x_1. \n", "The distribution over states is line 3 which A multiplied by x_1." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At time n+2, the distribution is:[[0.2675 0.66375 0.06875]]\n" ] } ], "source": [ "A = [ 0.9 0.15 0.25 ; 0.075 0.8 0.25 ; 0.025 0.05 0.5 ];\n", "x_1 = [0,1,0];\n", "B2 = A*B1;\n", "println(\"At time n+2, the distribution is:\",\"[\",B2',\"]\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar function with before, calculate the distribution at time n+2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At time n+3, the distribution is:[[0.3575 0.56825 0.07425]]\n" ] } ], "source": [ "A = [ 0.9 0.15 0.25 ; 0.075 0.8 0.25 ; 0.025 0.05 0.5 ];\n", "x_1 = [0,1,0];\n", "B3 = A*B2;\n", "println(\"At time n+3, the distribution is:\",\"[\",B3',\"]\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar function with before, calculate the distribution at time n+3" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At time n+4, the distribution is:[[0.42555 0.499975 0.074475]]\n" ] } ], "source": [ "A = [ 0.9 0.15 0.25 ; 0.075 0.8 0.25 ; 0.025 0.05 0.5 ];\n", "x_1 = [0,1,0];\n", "B4 = A*B3;\n", "println(\"At time n+4, the distribution is:\",\"[\",B4',\"]\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar function with before, calculate the distribution at time n+4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Examples of Markov chains" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "50\n", "\n", "\n", "100\n", "\n", "\n", "150\n", "\n", "\n", "200\n", "\n", "\n", "0.00\n", "\n", "\n", "0.25\n", "\n", "\n", "0.50\n", "\n", "\n", "0.75\n", "\n", "\n", "1.00\n", "\n", "\n", "Time t\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Bull Market\n", "\n", "\n", "\n", "Bear Market\n", "\n", "\n", "\n", "Stagnant Market\n", "\n", "\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T = 210 ;\n", "A = [ 0.9 0.15 0.25 ; 0.075 0.8 0.25 ; 0.025 0.05 0.5 ];\n", "x_1 = [0,1,0];\n", "state_traj = [x_1 zeros(3,T-1) ]; # State trajectory\n", "for t=1:T-1 # Dynamics recursion\n", " state_traj[:,t+1] = A*state_traj[:,t];\n", "end\n", "using Plots\n", "plot(1:T, state_traj', xlabel = \"Time t\",\n", " label = [\"Bull Market\", \"Bear Market\", \"Stagnant Market\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstration 1: \n", "In line 1 we set the calculate time as 210. \n", "In line 2 we construct a 3 * 3 matrix. \n", "In line 3 we set a stochastic row vector x_1. \n", "In line 4 we conbine the x_1 vector and zero matrix to be a new matrix state_traj. \n", "In line 5~7, we use dynamics recursion to update the state_traj matrix's value by A*state_traj. \n", "In line 8~9, we import and use plot function to draw markov chain prediction charm." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "50\n", "\n", "\n", "100\n", "\n", "\n", "150\n", "\n", "\n", "200\n", "\n", "\n", "0.1\n", "\n", "\n", "0.2\n", "\n", "\n", "0.3\n", "\n", "\n", "0.4\n", "\n", "\n", "0.5\n", "\n", "\n", "0.6\n", "\n", "\n", "Time t\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Bull Market\n", "\n", "\n", "\n", "Bear Market\n", "\n", "\n", "\n", "Stagnant Market\n", "\n", "\n" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T = 210 ;\n", "A = [ 0.9 0.15 0.25 ; 0.075 0.8 0.25 ; 0.025 0.05 0.5 ];\n", "x_2 = [0.3,0.4,0.3];\n", "state_traj = [x_2 zeros(3,T-1) ]; # State trajectory\n", "for t=1:T-1 # Dynamics recursion\n", " state_traj[:,t+1] = A*state_traj[:,t];\n", "end\n", "using Plots\n", "plot(1:T, state_traj', xlabel = \"Time t\",\n", " label = [\"Bull Market\", \"Bear Market\", \"Stagnant Market\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstration 2: \n", "In line 1 we set the calculate time as 210. \n", "In line 2 we construct a 3 * 3 matrix. \n", "In line 3 we set a stochastic row vector x_2. \n", "In line 4 we conbine the x_2 vector and zero matrix to be a new matrix state_traj. \n", "In line 5~7, we use dynamics recursion to update the state_traj matrix's value by Astate_traj. \n", "In line 8~9, we import and use plot function to draw markov chain prediction charm." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "50\n", "\n", "\n", "100\n", "\n", "\n", "150\n", "\n", "\n", "200\n", "\n", "\n", "0.1\n", "\n", "\n", "0.2\n", "\n", "\n", "0.3\n", "\n", "\n", "0.4\n", "\n", "\n", "0.5\n", "\n", "\n", "Time t\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Bull Market\n", "\n", "\n", "\n", "Bear Market\n", "\n", "\n", "\n", "Stagnant Market\n", "\n", "\n" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T = 210 ;\n", "A = [ 0.9 0.15 0.25 ; 0.075 0.8 0.25 ; 0.025 0.05 0.5 ];\n", "x_3 = [0.5,0.2,0.1];\n", "state_traj = [x_3 zeros(3,T-1) ]; # State trajectory\n", "for t=1:T-1 # Dynamics recursion\n", " state_traj[:,t+1] = A*state_traj[:,t];\n", "end\n", "using Plots\n", "plot(1:T, state_traj', xlabel = \"Time t\",\n", " label = [\"Bull Market\", \"Bear Market\", \"Stagnant Market\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstration 3:\n", "In line 1 we set the calculate time as 210. \n", "In line 2 we construct a 3 * 3 matrix. \n", "In line 3 we set a stochastic row vector x_3. \n", "In line 4 we conbine the x_3 vector and zero matrix to be a new matrix state_traj. \n", "In line 5~7, we use dynamics recursion to update the state_traj matrix's value by Astate_traj. \n", "In line 8~9, we import and use plot function to draw markov chain prediction charm. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.0.3 (4 threads)", "language": "julia", "name": "julia-1.0k" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.0.3" } }, "nbformat": 4, "nbformat_minor": 2 }