{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting jax[cpu]\n",
      "  Downloading jax-0.3.5.tar.gz (946 kB)\n",
      "\u001b[K     |████████████████████████████████| 946 kB 2.7 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting absl-py\n",
      "  Downloading absl_py-1.0.0-py3-none-any.whl (126 kB)\n",
      "\u001b[K     |████████████████████████████████| 126 kB 47.7 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting numpy>=1.19\n",
      "  Downloading numpy-1.22.3-cp38-cp38-macosx_10_14_x86_64.whl (17.6 MB)\n",
      "\u001b[K     |████████████████████████████████| 17.6 MB 47.5 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting opt_einsum\n",
      "  Using cached opt_einsum-3.3.0-py3-none-any.whl (65 kB)\n",
      "Collecting scipy>=1.2.1\n",
      "  Downloading scipy-1.8.0-cp38-cp38-macosx_12_0_universal2.macosx_10_9_x86_64.whl (55.3 MB)\n",
      "\u001b[K     |████████████████████████████████| 55.3 MB 73.1 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting typing_extensions\n",
      "  Using cached typing_extensions-4.1.1-py3-none-any.whl (26 kB)\n",
      "Collecting jaxlib==0.3.5\n",
      "  Downloading jaxlib-0.3.5-cp38-none-macosx_10_9_x86_64.whl (70.5 MB)\n",
      "\u001b[K     |████████████████████████████████| 70.5 MB 723 kB/s  eta 0:00:01\n",
      "\u001b[?25hCollecting flatbuffers<3.0,>=1.12\n",
      "  Using cached flatbuffers-2.0-py2.py3-none-any.whl (26 kB)\n",
      "Requirement already satisfied: six in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from absl-py->jax[cpu]) (1.16.0)\n",
      "Building wheels for collected packages: jax\n",
      "  Building wheel for jax (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for jax: filename=jax-0.3.5-py3-none-any.whl size=1095861 sha256=6886baa70817bbac3b5797b3720dcb81e49097f61cee7d2e1255823ea32ccad8\n",
      "  Stored in directory: /Users/kpmurphy/Library/Caches/pip/wheels/05/30/aa/908988293721511b4b29e0aadf9b5d133d0f14f6c0a188e764\n",
      "Successfully built jax\n",
      "Installing collected packages: numpy, typing-extensions, scipy, opt-einsum, flatbuffers, absl-py, jaxlib, jax\n",
      "Successfully installed absl-py-1.0.0 flatbuffers-2.0 jax-0.3.5 jaxlib-0.3.5 numpy-1.22.3 opt-einsum-3.3.0 scipy-1.8.0 typing-extensions-4.1.1\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Collecting git+https://github.com/probml/jsl\n",
      "  Cloning https://github.com/probml/jsl to /private/var/folders/mn/vt7cgfsx6zs9vblhvbbk7pf8003xtr/T/pip-req-build-i8seqdiw\n",
      "  Running command git clone -q https://github.com/probml/jsl /private/var/folders/mn/vt7cgfsx6zs9vblhvbbk7pf8003xtr/T/pip-req-build-i8seqdiw\n",
      "Collecting chex\n",
      "  Downloading chex-0.1.2-py3-none-any.whl (72 kB)\n",
      "\u001b[K     |████████████████████████████████| 72 kB 1.3 MB/s eta 0:00:011\n",
      "\u001b[?25hCollecting dataclasses\n",
      "  Using cached dataclasses-0.6-py3-none-any.whl (14 kB)\n",
      "Requirement already satisfied: jaxlib in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from jsl==0.0.0) (0.3.5)\n",
      "Requirement already satisfied: jax in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from jsl==0.0.0) (0.3.5)\n",
      "Collecting matplotlib\n",
      "  Downloading matplotlib-3.5.1-cp38-cp38-macosx_10_9_x86_64.whl (7.3 MB)\n",
      "\u001b[K     |████████████████████████████████| 7.3 MB 3.9 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting tensorflow_probability\n",
      "  Using cached tensorflow_probability-0.16.0-py2.py3-none-any.whl (6.3 MB)\n",
      "Collecting dm-tree>=0.1.5\n",
      "  Using cached dm_tree-0.1.6-cp38-cp38-macosx_10_14_x86_64.whl (95 kB)\n",
      "Requirement already satisfied: absl-py>=0.9.0 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from chex->jsl==0.0.0) (1.0.0)\n",
      "Requirement already satisfied: numpy>=1.18.0 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from chex->jsl==0.0.0) (1.22.3)\n",
      "Collecting toolz>=0.9.0\n",
      "  Downloading toolz-0.11.2-py3-none-any.whl (55 kB)\n",
      "\u001b[K     |████████████████████████████████| 55 kB 11.3 MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: six in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from absl-py>=0.9.0->chex->jsl==0.0.0) (1.16.0)\n",
      "Requirement already satisfied: typing-extensions in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from jax->jsl==0.0.0) (4.1.1)\n",
      "Requirement already satisfied: scipy>=1.2.1 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from jax->jsl==0.0.0) (1.8.0)\n",
      "Requirement already satisfied: opt-einsum in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from jax->jsl==0.0.0) (3.3.0)\n",
      "Requirement already satisfied: flatbuffers<3.0,>=1.12 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from jaxlib->jsl==0.0.0) (2.0)\n",
      "Collecting cycler>=0.10\n",
      "  Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB)\n",
      "Collecting kiwisolver>=1.0.1\n",
      "  Downloading kiwisolver-1.4.2-cp38-cp38-macosx_10_9_x86_64.whl (65 kB)\n",
      "\u001b[K     |████████████████████████████████| 65 kB 8.5 MB/s  eta 0:00:01\n",
      "\u001b[?25hCollecting fonttools>=4.22.0\n",
      "  Downloading fonttools-4.32.0-py3-none-any.whl (900 kB)\n",
      "\u001b[K     |████████████████████████████████| 900 kB 35.8 MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: pyparsing>=2.2.1 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from matplotlib->jsl==0.0.0) (3.0.7)\n",
      "Requirement already satisfied: packaging>=20.0 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from matplotlib->jsl==0.0.0) (21.3)\n",
      "Requirement already satisfied: python-dateutil>=2.7 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from matplotlib->jsl==0.0.0) (2.8.2)\n",
      "Collecting pillow>=6.2.0\n",
      "  Downloading Pillow-9.1.0-cp38-cp38-macosx_10_9_x86_64.whl (3.1 MB)\n",
      "\u001b[K     |████████████████████████████████| 3.1 MB 76.6 MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting gast>=0.3.2\n",
      "  Downloading gast-0.5.3-py3-none-any.whl (19 kB)\n",
      "Requirement already satisfied: decorator in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from tensorflow_probability->jsl==0.0.0) (5.1.1)\n",
      "Collecting cloudpickle>=1.3\n",
      "  Downloading cloudpickle-2.0.0-py3-none-any.whl (25 kB)\n",
      "Building wheels for collected packages: jsl\n",
      "  Building wheel for jsl (setup.py) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for jsl: filename=jsl-0.0.0-py3-none-any.whl size=77852 sha256=e7365293dc97e2b3e72bf42cc19db7d7e355abec312fc4d87961fa2044fa06f0\n",
      "  Stored in directory: /private/var/folders/mn/vt7cgfsx6zs9vblhvbbk7pf8003xtr/T/pip-ephem-wheel-cache-63vxzlng/wheels/ed/8b/bf/0105dc839fecf1fc8db14f7267a6ce5ee876324b58565b359f\n",
      "Successfully built jsl\n",
      "Installing collected packages: toolz, pillow, kiwisolver, gast, fonttools, dm-tree, cycler, cloudpickle, tensorflow-probability, matplotlib, dataclasses, chex, jsl\n",
      "Successfully installed chex-0.1.2 cloudpickle-2.0.0 cycler-0.11.0 dataclasses-0.6 dm-tree-0.1.6 fonttools-4.32.0 gast-0.5.3 jsl-0.0.0 kiwisolver-1.4.2 matplotlib-3.5.1 pillow-9.1.0 tensorflow-probability-0.16.0 toolz-0.11.2\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Collecting rich\n",
      "  Downloading rich-12.2.0-py3-none-any.whl (229 kB)\n",
      "\u001b[K     |████████████████████████████████| 229 kB 2.9 MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: typing-extensions<5.0,>=4.0.0 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from rich) (4.1.1)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /opt/anaconda3/envs/scripts/lib/python3.8/site-packages (from rich) (2.11.2)\n",
      "Collecting commonmark<0.10.0,>=0.9.0\n",
      "  Using cached commonmark-0.9.1-py2.py3-none-any.whl (51 kB)\n",
      "Installing collected packages: commonmark, rich\n",
      "Successfully installed commonmark-0.9.1 rich-12.2.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "# meta-data does not work yet in VScode\n",
    "# https://github.com/microsoft/vscode-jupyter/issues/1121\n",
    "\n",
    "{\n",
    "    \"tags\": [\n",
    "        \"hide-cell\"\n",
    "    ]\n",
    "}\n",
    "\n",
    "\n",
    "### Install necessary libraries\n",
    "\n",
    "try:\n",
    "    import jax\n",
    "except:\n",
    "    # For cuda version, see https://github.com/google/jax#installation\n",
    "    %pip install --upgrade \"jax[cpu]\" \n",
    "    import jax\n",
    "\n",
    "try:\n",
    "    import jsl\n",
    "except:\n",
    "    %pip install git+https://github.com/probml/jsl\n",
    "    import jsl\n",
    "\n",
    "try:\n",
    "    import rich\n",
    "except:\n",
    "    %pip install rich\n",
    "    import rich\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "{\n",
    "    \"tags\": [\n",
    "        \"hide-cell\"\n",
    "    ]\n",
    "}\n",
    "\n",
    "\n",
    "### Import standard libraries\n",
    "\n",
    "import abc\n",
    "from dataclasses import dataclass\n",
    "import functools\n",
    "import itertools\n",
    "\n",
    "from typing import Any, Callable, NamedTuple, Optional, Union, Tuple\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "import jax\n",
    "import jax.numpy as jnp\n",
    "from jax import lax, vmap, jit, grad\n",
    "from jax.scipy.special import logit\n",
    "from jax.nn import softmax\n",
    "from functools import partial\n",
    "from jax.random import PRNGKey, split\n",
    "\n",
    "import inspect\n",
    "import inspect as py_inspect\n",
    "import rich\n",
    "\n",
    "from rich import inspect as r_inspect\n",
    "from rich import print as r_print\n",
    "\n",
    "def print_source(fname):\n",
    "    r_print(py_inspect.getsource(fname))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{math}\n",
    "\n",
    "\\newcommand\\floor[1]{\\lfloor#1\\rfloor}\n",
    "\n",
    "\\newcommand{\\real}{\\mathbb{R}}\n",
    "\n",
    "% Numbers\n",
    "\\newcommand{\\vzero}{\\boldsymbol{0}}\n",
    "\\newcommand{\\vone}{\\boldsymbol{1}}\n",
    "\n",
    "% Greek https://www.latex-tutorial.com/symbols/greek-alphabet/\n",
    "\\newcommand{\\valpha}{\\boldsymbol{\\alpha}}\n",
    "\\newcommand{\\vbeta}{\\boldsymbol{\\beta}}\n",
    "\\newcommand{\\vchi}{\\boldsymbol{\\chi}}\n",
    "\\newcommand{\\vdelta}{\\boldsymbol{\\delta}}\n",
    "\\newcommand{\\vDelta}{\\boldsymbol{\\Delta}}\n",
    "\\newcommand{\\vepsilon}{\\boldsymbol{\\epsilon}}\n",
    "\\newcommand{\\vzeta}{\\boldsymbol{\\zeta}}\n",
    "\\newcommand{\\vXi}{\\boldsymbol{\\Xi}}\n",
    "\\newcommand{\\vell}{\\boldsymbol{\\ell}}\n",
    "\\newcommand{\\veta}{\\boldsymbol{\\eta}}\n",
    "%\\newcommand{\\vEta}{\\boldsymbol{\\Eta}}\n",
    "\\newcommand{\\vgamma}{\\boldsymbol{\\gamma}}\n",
    "\\newcommand{\\vGamma}{\\boldsymbol{\\Gamma}}\n",
    "\\newcommand{\\vmu}{\\boldsymbol{\\mu}}\n",
    "\\newcommand{\\vmut}{\\boldsymbol{\\tilde{\\mu}}}\n",
    "\\newcommand{\\vnu}{\\boldsymbol{\\nu}}\n",
    "\\newcommand{\\vkappa}{\\boldsymbol{\\kappa}}\n",
    "\\newcommand{\\vlambda}{\\boldsymbol{\\lambda}}\n",
    "\\newcommand{\\vLambda}{\\boldsymbol{\\Lambda}}\n",
    "\\newcommand{\\vLambdaBar}{\\overline{\\vLambda}}\n",
    "%\\newcommand{\\vnu}{\\boldsymbol{\\nu}}\n",
    "\\newcommand{\\vomega}{\\boldsymbol{\\omega}}\n",
    "\\newcommand{\\vOmega}{\\boldsymbol{\\Omega}}\n",
    "\\newcommand{\\vphi}{\\boldsymbol{\\phi}}\n",
    "\\newcommand{\\vvarphi}{\\boldsymbol{\\varphi}}\n",
    "\\newcommand{\\vPhi}{\\boldsymbol{\\Phi}}\n",
    "\\newcommand{\\vpi}{\\boldsymbol{\\pi}}\n",
    "\\newcommand{\\vPi}{\\boldsymbol{\\Pi}}\n",
    "\\newcommand{\\vpsi}{\\boldsymbol{\\psi}}\n",
    "\\newcommand{\\vPsi}{\\boldsymbol{\\Psi}}\n",
    "\\newcommand{\\vrho}{\\boldsymbol{\\rho}}\n",
    "\\newcommand{\\vtheta}{\\boldsymbol{\\theta}}\n",
    "\\newcommand{\\vthetat}{\\boldsymbol{\\tilde{\\theta}}}\n",
    "\\newcommand{\\vTheta}{\\boldsymbol{\\Theta}}\n",
    "\\newcommand{\\vsigma}{\\boldsymbol{\\sigma}}\n",
    "\\newcommand{\\vSigma}{\\boldsymbol{\\Sigma}}\n",
    "\\newcommand{\\vSigmat}{\\boldsymbol{\\tilde{\\Sigma}}}\n",
    "\\newcommand{\\vsigmoid}{\\vsigma}\n",
    "\\newcommand{\\vtau}{\\boldsymbol{\\tau}}\n",
    "\\newcommand{\\vxi}{\\boldsymbol{\\xi}}\n",
    "\n",
    "\n",
    "% Lower Roman (Vectors)\n",
    "\\newcommand{\\va}{\\mathbf{a}}\n",
    "\\newcommand{\\vb}{\\mathbf{b}}\n",
    "\\newcommand{\\vBt}{\\mathbf{\\tilde{B}}}\n",
    "\\newcommand{\\vc}{\\mathbf{c}}\n",
    "\\newcommand{\\vct}{\\mathbf{\\tilde{c}}}\n",
    "\\newcommand{\\vd}{\\mathbf{d}}\n",
    "\\newcommand{\\ve}{\\mathbf{e}}\n",
    "\\newcommand{\\vf}{\\mathbf{f}}\n",
    "\\newcommand{\\vg}{\\mathbf{g}}\n",
    "\\newcommand{\\vh}{\\mathbf{h}}\n",
    "%\\newcommand{\\myvh}{\\mathbf{h}}\n",
    "\\newcommand{\\vi}{\\mathbf{i}}\n",
    "\\newcommand{\\vj}{\\mathbf{j}}\n",
    "\\newcommand{\\vk}{\\mathbf{k}}\n",
    "\\newcommand{\\vl}{\\mathbf{l}}\n",
    "\\newcommand{\\vm}{\\mathbf{m}}\n",
    "\\newcommand{\\vn}{\\mathbf{n}}\n",
    "\\newcommand{\\vo}{\\mathbf{o}}\n",
    "\\newcommand{\\vp}{\\mathbf{p}}\n",
    "\\newcommand{\\vq}{\\mathbf{q}}\n",
    "\\newcommand{\\vr}{\\mathbf{r}}\n",
    "\\newcommand{\\vs}{\\mathbf{s}}\n",
    "\\newcommand{\\vt}{\\mathbf{t}}\n",
    "\\newcommand{\\vu}{\\mathbf{u}}\n",
    "\\newcommand{\\vv}{\\mathbf{v}}\n",
    "\\newcommand{\\vw}{\\mathbf{w}}\n",
    "\\newcommand{\\vws}{\\vw_s}\n",
    "\\newcommand{\\vwt}{\\mathbf{\\tilde{w}}}\n",
    "\\newcommand{\\vWt}{\\mathbf{\\tilde{W}}}\n",
    "\\newcommand{\\vwh}{\\hat{\\vw}}\n",
    "\\newcommand{\\vx}{\\mathbf{x}}\n",
    "%\\newcommand{\\vx}{\\mathbf{x}}\n",
    "\\newcommand{\\vxt}{\\mathbf{\\tilde{x}}}\n",
    "\\newcommand{\\vy}{\\mathbf{y}}\n",
    "\\newcommand{\\vyt}{\\mathbf{\\tilde{y}}}\n",
    "\\newcommand{\\vz}{\\mathbf{z}}\n",
    "%\\newcommand{\\vzt}{\\mathbf{\\tilde{z}}}\n",
    "\n",
    "\n",
    "% Upper Roman (Matrices)\n",
    "\\newcommand{\\vA}{\\mathbf{A}}\n",
    "\\newcommand{\\vB}{\\mathbf{B}}\n",
    "\\newcommand{\\vC}{\\mathbf{C}}\n",
    "\\newcommand{\\vD}{\\mathbf{D}}\n",
    "\\newcommand{\\vE}{\\mathbf{E}}\n",
    "\\newcommand{\\vF}{\\mathbf{F}}\n",
    "\\newcommand{\\vG}{\\mathbf{G}}\n",
    "\\newcommand{\\vH}{\\mathbf{H}}\n",
    "\\newcommand{\\vI}{\\mathbf{I}}\n",
    "\\newcommand{\\vJ}{\\mathbf{J}}\n",
    "\\newcommand{\\vK}{\\mathbf{K}}\n",
    "\\newcommand{\\vL}{\\mathbf{L}}\n",
    "\\newcommand{\\vM}{\\mathbf{M}}\n",
    "\\newcommand{\\vMt}{\\mathbf{\\tilde{M}}}\n",
    "\\newcommand{\\vN}{\\mathbf{N}}\n",
    "\\newcommand{\\vO}{\\mathbf{O}}\n",
    "\\newcommand{\\vP}{\\mathbf{P}}\n",
    "\\newcommand{\\vQ}{\\mathbf{Q}}\n",
    "\\newcommand{\\vR}{\\mathbf{R}}\n",
    "\\newcommand{\\vS}{\\mathbf{S}}\n",
    "\\newcommand{\\vT}{\\mathbf{T}}\n",
    "\\newcommand{\\vU}{\\mathbf{U}}\n",
    "\\newcommand{\\vV}{\\mathbf{V}}\n",
    "\\newcommand{\\vW}{\\mathbf{W}}\n",
    "\\newcommand{\\vX}{\\mathbf{X}}\n",
    "%\\newcommand{\\vXs}{\\vX_{\\vs}}\n",
    "\\newcommand{\\vXs}{\\vX_{s}}\n",
    "\\newcommand{\\vXt}{\\mathbf{\\tilde{X}}}\n",
    "\\newcommand{\\vY}{\\mathbf{Y}}\n",
    "\\newcommand{\\vZ}{\\mathbf{Z}}\n",
    "\\newcommand{\\vZt}{\\mathbf{\\tilde{Z}}}\n",
    "\\newcommand{\\vzt}{\\mathbf{\\tilde{z}}}\n",
    "\n",
    "\n",
    "%%%%\n",
    "\\newcommand{\\hidden}{\\vz}\n",
    "\\newcommand{\\obs}{\\vy}\n",
    "\\newcommand{\\inputs}{\\vu}\n",
    "\\newcommand{\\input}{\\inputs}\n",
    "\n",
    "\\newcommand{\\hmmTrans}{\\vA}\n",
    "\\newcommand{\\hmmObs}{\\vB}\n",
    "\\newcommand{\\hmmInit}{\\vpi}\n",
    "\\newcommand{\\hmmhid}{\\hidden}\n",
    "\\newcommand{\\hmmobs}{\\obs}\n",
    "\n",
    "\\newcommand{\\ldsDyn}{\\vA}\n",
    "\\newcommand{\\ldsObs}{\\vC}\n",
    "\\newcommand{\\ldsDynIn}{\\vB}\n",
    "\\newcommand{\\ldsObsIn}{\\vD}\n",
    "\\newcommand{\\ldsDynNoise}{\\vQ}\n",
    "\\newcommand{\\ldsObsNoise}{\\vR}\n",
    "\n",
    "\\newcommand{\\ssmDyn}{f}\n",
    "\\newcommand{\\ssmObs}{h}\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(sec:ssm-intro)=\n",
    "# What are State Space Models?\n",
    "\n",
    "\n",
    "A state space model or SSM\n",
    "is a partially observed Markov model,\n",
    "in which the hidden state,  $\\hidden_t$,\n",
    "evolves over time according to a Markov process,\n",
    "possibly conditional on external inputs or controls $\\input_t$,\n",
    "and each hidden state generates some\n",
    "observations $\\obs_t$ at each time step.\n",
    "(In this book, we mostly focus on discrete time systems,\n",
    "although  we consider the continuous-time case in  XXX.)\n",
    "We get to see the observations, but not the hidden state.\n",
    "Our main goal is to infer the hidden state given the observations.\n",
    "However, we can also use the model to predict future observations,\n",
    "by first predicting future hidden states, and then predicting\n",
    "what observations they might generate.\n",
    "By using  a hidden state $\\hidden_t$\n",
    "to represent the past observations, $\\obs_{1:t-1}$,\n",
    "the  model can have ``infinite'' memory,\n",
    "unlike a standard Markov model.\n",
    "\n",
    "Formally we can define an SSM \n",
    "as the following joint distribution:\n",
    "```{math}\n",
    ":label: SSMfull\n",
    "p(\\hmmobs_{1:T},\\hmmhid_{1:T}|\\inputs_{1:T})\n",
    " = \\left[ p(\\hmmhid_1|\\inputs_1) \\prod_{t=2}^{T}\n",
    " p(\\hmmhid_t|\\hmmhid_{t-1},\\inputs_t) \\right]\n",
    " \\left[ \\prod_{t=1}^T p(\\hmmobs_t|\\hmmhid_t, \\inputs_t, \\hmmobs_{t-1}) \\right]\n",
    "```\n",
    "where $p(\\hmmhid_t|\\hmmhid_{t-1},\\inputs_t)$ is the\n",
    "transition model,\n",
    "$p(\\hmmobs_t|\\hmmhid_t, \\inputs_t, \\hmmobs_{t-1})$ is the\n",
    "observation model,\n",
    "and $\\inputs_{t}$ is an optional input or action.\n",
    "See {numref}`Figure %s <ssm-ar>` \n",
    "for an illustration of the corresponding graphical model.\n",
    "\n",
    "\n",
    "```{figure} /figures/SSM-AR-inputs.png\n",
    ":scale: 100%\n",
    ":name: ssm-ar\n",
    "\n",
    "Illustration of an SSM as a graphical model.\n",
    "```\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We often consider a simpler setting in which there\n",
    "are no external inputs,\n",
    "and the observations are conditionally independent of each other\n",
    "(rather than having Markovian dependencies) given the hidden state.\n",
    "In this case the joint simplifies to \n",
    "```{math}\n",
    ":label: SSMsimplified\n",
    "p(\\hmmobs_{1:T},\\hmmhid_{1:T})\n",
    " = \\left[ p(\\hmmhid_1) \\prod_{t=2}^{T}\n",
    " p(\\hmmhid_t|\\hmmhid_{t-1}) \\right]\n",
    " \\left[ \\prod_{t=1}^T p(\\hmmobs_t|\\hmmhid_t \\right]\n",
    "```\n",
    "See {numref}`Figure %s <ssm-simplified>` \n",
    "for an illustration of the corresponding graphical model.\n",
    "Compare {eq}`SSMfull` and {eq}`SSMsimplified`.\n",
    "\n",
    "\n",
    "```{figure} /figures/SSM-simplified.png\n",
    ":scale: 100%\n",
    ":name: ssm-simplified\n",
    "\n",
    "Illustration of a simplified SSM.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(sec:hmm-intro)=\n",
    "# Hidden Markov Models\n",
    "\n",
    "In this section, we discuss the\n",
    "hidden Markov model or HMM,\n",
    "which is a state space model in which the hidden states\n",
    "are discrete, so $\\hmmhid_t \\in \\{1,\\ldots, K\\}$.\n",
    "The observations may be discrete,\n",
    "$\\hmmobs_t \\in \\{1,\\ldots, C\\}$,\n",
    "or continuous,\n",
    "$\\hmmobs_t \\in \\real^D$,\n",
    "or some combination,\n",
    "as we illustrate below.\n",
    "More details can be found in e.g., \n",
    "{cite}`Rabiner89,Fraser08,Cappe05`.\n",
    "For an interactive introduction,\n",
    "see https://nipunbatra.github.io/hmm/."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(sec:casino-ex)=\n",
    "### Example: Casino HMM\n",
    "\n",
    "To illustrate HMMs with categorical observation model,\n",
    "we consider the \"Ocassionally dishonest casino\" model from {cite}`Durbin98`.\n",
    "There are 2 hidden states, representing whether the dice being used in the casino is fair or loaded.\n",
    "Each state defines a distribution over the 6 possible observations.\n",
    "\n",
    "The transition model is denoted by\n",
    "```{math}\n",
    "p(z_t=j|z_{t-1}=i) = \\hmmTrans_{ij}\n",
    "```\n",
    "Here the $i$'th row of $\\vA$ corresponds to the outgoing distribution from state $i$.\n",
    "This is  a row stochastic matrix,\n",
    "meaning each row sums to one.\n",
    "We can visualize\n",
    "the non-zero entries in the transition matrix by creating a state transition diagram,\n",
    "as shown in {ref}`casino-fig`.\n",
    "\n",
    "```{figure} /figures/casino.png\n",
    ":scale: 50%\n",
    ":name: casino-fig\n",
    "\n",
    "Illustration of the casino HMM.\n",
    "```\n",
    "\n",
    "The  observation model\n",
    "$p(\\obs_t|\\hiddden_t=j)$ has the form\n",
    "```{math}\n",
    "p(\\obs_t=k|\\hidden_t=j) = \\hmmObs_{jk} \n",
    "```\n",
    "This is represented by the histograms associated with each\n",
    "state in {ref}`casino-fig`.\n",
    "\n",
    "Finally,\n",
    "the initial state distribution is denoted by\n",
    "```{math}\n",
    "p(z_1=j) = \\hmmInit_j\n",
    "```\n",
    "\n",
    "Collectively we denote all the parameters by $\\vtheta=(\\hmmTrans, \\hmmObs, \\hmmInit)$.\n",
    "\n",
    "Now let us implement this model code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)\n"
     ]
    }
   ],
   "source": [
    "# state transition matrix\n",
    "A = np.array([\n",
    "    [0.95, 0.05],\n",
    "    [0.10, 0.90]\n",
    "])\n",
    "\n",
    "# observation matrix\n",
    "B = np.array([\n",
    "    [1/6, 1/6, 1/6, 1/6, 1/6, 1/6], # fair die\n",
    "    [1/10, 1/10, 1/10, 1/10, 1/10, 5/10] # loaded die\n",
    "])\n",
    "\n",
    "pi = np.array([0.5, 0.5])\n",
    "\n",
    "(nstates, nobs) = np.shape(B)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
