{
 "cells": [
  {
   "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",
    "```{figure} /figures/SSM-AR-inputs.png\n",
    ":height: 150px\n",
    ":name: fig:ssm-ar\n",
    "\n",
    "Illustration of an SSM as a graphical model.\n",
    "```\n",
    "\n",
    "\n",
    "Formally we can define an SSM \n",
    "as the following joint distribution:\n",
    "```{math}\n",
    ":label: eq:SSM-ar\n",
    "p(\\obs_{1:T},\\hidden_{1:T}|\\inputs_{1:T})\n",
    " = \\left[ p(\\hidden_1|\\inputs_1) \\prod_{t=2}^{T}\n",
    " p(\\hidden_t|\\hidden_{t-1},\\inputs_t) \\right]\n",
    " \\left[ \\prod_{t=1}^T p(\\obs_t|\\hidden_t, \\inputs_t, \\obs_{t-1}) \\right]\n",
    "```\n",
    "where $p(\\hidden_t|\\hidden_{t-1},\\inputs_t)$ is the\n",
    "transition model,\n",
    "$p(\\obs_t|\\hidden_t, \\inputs_t, \\obs_{t-1})$ is the\n",
    "observation model,\n",
    "and $\\inputs_{t}$ is an optional input or action.\n",
    "See {numref}`fig:ssm-ar` \n",
    "for an illustration of the corresponding graphical model.\n",
    "\n",
    "\n",
    "We often consider a simpler setting in which the\n",
    " 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: eq:SSM-input\n",
    "p(\\obs_{1:T},\\hidden_{1:T}|\\inputs_{1:T})\n",
    " = \\left[ p(\\hidden_1|\\inputs_1) \\prod_{t=2}^{T}\n",
    " p(\\hidden_t|\\hidden_{t-1},\\inputs_t) \\right]\n",
    " \\left[ \\prod_{t=1}^T p(\\obs_t|\\hidden_t, \\inputs_t) \\right]\n",
    "```\n",
    "Sometimes there are no external inputs, so the model further\n",
    "simplifies to the following unconditional generative model: \n",
    "```{math}\n",
    ":label: eq:SSM-no-input\n",
    "p(\\obs_{1:T},\\hidden_{1:T})\n",
    " = \\left[ p(\\hidden_1) \\prod_{t=2}^{T}\n",
    " p(\\hidden_t|\\hidden_{t-1}) \\right]\n",
    " \\left[ \\prod_{t=1}^T p(\\obs_t|\\hidden_t) \\right]\n",
    "```\n",
    "See {numref}`ssm-simplified` \n",
    "for an illustration of the corresponding graphical model.\n",
    "\n",
    "\n",
    "```{figure} /figures/SSM-simplified.png\n",
    ":height: 150px\n",
    ":name: ssm-simplified\n",
    "\n",
    "Illustration of a simplified SSM.\n",
    "```\n",
    "\n",
    "SSMs are widely used in many areas of science, engineering, finance, economics, etc.\n",
    "The main applications are state estimation (i.e., inferring the underlying hidden state of the system given the observation),\n",
    "forecasting (i.e., predicting future states and observations), and control (i.e., inferring the sequence of inputs that will\n",
    "give rise to a desired target state). We will discuss these applications in later chapters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "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
}
