{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "84e6656d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ed48cc4",
   "metadata": {},
   "source": [
    "# n-step transition probability\n",
    "## 1. by sampling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a1b2d0b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_state(s):\n",
    "    \"\"\"\n",
    "        Does one step of the Markov chain. \n",
    "    \"\"\"\n",
    "    \n",
    "    if s==0:\n",
    "        if random.random() < 0.99: return 0\n",
    "        else: return 1\n",
    "    elif s==1:\n",
    "        if random.random() < 0.9: return 0\n",
    "        else: return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "31449c13",
   "metadata": {},
   "outputs": [],
   "source": [
    "def many_updates(s,n):\n",
    "    for _ in range(n):\n",
    "        s = update_state(s)\n",
    "        \n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "1f4b042f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.98879, 0.01121)"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 10**5\n",
    "L = [many_updates(0,100) for _ in range(n)]\n",
    "\n",
    "L.count(0)/n, L.count(1)/n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba7c058b",
   "metadata": {},
   "source": [
    "## 2. By Chapman-Kolmogorov formula"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5729abf5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.99, 0.01],\n",
       "        [0.9 , 0.1 ]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P = np.matrix([[0.99, .01], [.9, .1]]); P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "7bb226a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.9891, 0.0109],\n",
       "        [0.981 , 0.019 ]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P*P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "faeff6a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.98901099, 0.01098901],\n",
       "        [0.98901099, 0.01098901]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P**100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "2d3555e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.989010989010989"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "90./91"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47b8ff79",
   "metadata": {},
   "source": [
    "# Stationary distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "b55a3796",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [1 1]]\n",
      "[[1. 0.]\n",
      " [0. 1.]]\n",
      "[1 1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "matrix([[0.98901099, 0.01098901]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "J = np.matrix([[1,1],[1,1]])\n",
    "I = np.identity(2)\n",
    "j = np.array([1,1])\n",
    "print(J); print(I); print(j)\n",
    "pi = j*(I-P+J)**(-1); pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "4707d2a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.0000000e+00, 3.6429193e-17]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi*P - pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "eccdf6bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "2a368992",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 0]), matrix([[0.99, 0.01]]), matrix([[0.9891, 0.0109]]))"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, x*P, x*P*P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "82749348",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.98901105 0.01098895]]\n"
     ]
    }
   ],
   "source": [
    "xlast = x\n",
    "for _ in range(100):\n",
    "    xnext = xlast*P\n",
    "    if np.max(np.abs(xnext - xlast)) < 0.000001:\n",
    "        print(xnext)\n",
    "        break\n",
    "    xlast = xnext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "da75dd62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.010000000000000009"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(np.abs(x*P-x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1693082",
   "metadata": {},
   "source": [
    "TODO: use numpy to solve system of linear equations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a076fa67",
   "metadata": {},
   "source": [
    "# Problem 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "1c3b78d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "La = []\n",
    "num = 10**6\n",
    "for _ in range(num):\n",
    "    s = many_updates(0,100)\n",
    "    t = update_state(s)\n",
    "    La.append((s,t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "af2cfdba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.979239"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([1 for (s,t) in La if s==0 and t==0])/num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "7ba61d5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[0.98901099, 0.01098901]]),\n",
       " matrix([[0.99, 0.01],\n",
       "         [0.9 , 0.1 ]]))"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi, P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "dcffa0fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9791208791208792"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(pi[0,0])*(P[0,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "8c9b8014",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.009913"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([1 for (s,t) in La if s==0 and t==1])/num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "722cd412",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.009890109890109891"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(pi[0,0])*(P[0,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de9180b3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "af8e8f2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "Lb = []\n",
    "num = 10**6\n",
    "for _ in range(num):\n",
    "    s = many_updates(0,100)\n",
    "    t = update_state(s)\n",
    "    u = many_updates(t,99)\n",
    "    Lb.append((s,t,u))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "a69c8126",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.968491"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([1 for (s,t,u) in Lb if s==0 and t==0 and u==0])/num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "18f31619",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9683613090206498"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi[0,0]*P[0,0]*pi[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c9ee7a5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89fb15af",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2067ad8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
