ssm.html 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>What are State Space Models? &#8212; State Space Models: A Modern Approach</title>
  7. <link href="../../_static/css/theme.css" rel="stylesheet">
  8. <link href="../../_static/css/index.ff1ffe594081f20da1ef19478df9384b.css" rel="stylesheet">
  9. <link rel="stylesheet"
  10. href="../../_static/vendor/fontawesome/5.13.0/css/all.min.css">
  11. <link rel="preload" as="font" type="font/woff2" crossorigin
  12. href="../../_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
  13. <link rel="preload" as="font" type="font/woff2" crossorigin
  14. href="../../_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
  15. <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
  16. <link rel="stylesheet" type="text/css" href="../../_static/sphinx-book-theme.css?digest=c3fdc42140077d1ad13ad2f1588a4309" />
  17. <link rel="stylesheet" type="text/css" href="../../_static/togglebutton.css" />
  18. <link rel="stylesheet" type="text/css" href="../../_static/copybutton.css" />
  19. <link rel="stylesheet" type="text/css" href="../../_static/mystnb.css" />
  20. <link rel="stylesheet" type="text/css" href="../../_static/sphinx-thebe.css" />
  21. <link rel="stylesheet" type="text/css" href="../../_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
  22. <link rel="stylesheet" type="text/css" href="../../_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
  23. <link rel="preload" as="script" href="../../_static/js/index.be7d3bbb2ef33a8344ce.js">
  24. <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
  25. <script src="../../_static/jquery.js"></script>
  26. <script src="../../_static/underscore.js"></script>
  27. <script src="../../_static/doctools.js"></script>
  28. <script src="../../_static/clipboard.min.js"></script>
  29. <script src="../../_static/copybutton.js"></script>
  30. <script>let toggleHintShow = 'Click to show';</script>
  31. <script>let toggleHintHide = 'Click to hide';</script>
  32. <script>let toggleOpenOnPrint = 'true';</script>
  33. <script src="../../_static/togglebutton.js"></script>
  34. <script>var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
  35. <script src="../../_static/sphinx-book-theme.d59cb220de22ca1c485ebbdc042f0030.js"></script>
  36. <script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"
  37. const thebe_selector = ".thebe,.cell"
  38. const thebe_selector_input = "pre"
  39. const thebe_selector_output = ".output, .cell_output"
  40. </script>
  41. <script async="async" src="../../_static/sphinx-thebe.js"></script>
  42. <script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
  43. <script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
  44. <link rel="index" title="Index" href="../../genindex.html" />
  45. <link rel="search" title="Search" href="../../search.html" />
  46. <link rel="next" title="Inference in discrete SSMs" href="../hmm/hmm_index.html" />
  47. <link rel="prev" title="Scratchpad" href="../scratch.html" />
  48. <meta name="viewport" content="width=device-width, initial-scale=1" />
  49. <meta name="docsearch:language" content="None">
  50. <!-- Google Analytics -->
  51. </head>
  52. <body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
  53. <div class="container-fluid" id="banner"></div>
  54. <div class="container-xl">
  55. <div class="row">
  56. <div class="col-12 col-md-3 bd-sidebar site-navigation show" id="site-navigation">
  57. <div class="navbar-brand-box">
  58. <a class="navbar-brand text-wrap" href="../../index.html">
  59. <h1 class="site-logo" id="site-title">State Space Models: A Modern Approach</h1>
  60. </a>
  61. </div><form class="bd-search d-flex align-items-center" action="../../search.html" method="get">
  62. <i class="icon fas fa-search"></i>
  63. <input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
  64. </form><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
  65. <div class="bd-toc-item active">
  66. <ul class="nav bd-sidenav">
  67. <li class="toctree-l1">
  68. <a class="reference internal" href="../../root.html">
  69. State Space Models: A Modern Approach
  70. </a>
  71. </li>
  72. </ul>
  73. <ul class="current nav bd-sidenav">
  74. <li class="toctree-l1">
  75. <a class="reference internal" href="../scratch.html">
  76. Scratchpad
  77. </a>
  78. </li>
  79. <li class="toctree-l1 current active">
  80. <a class="current reference internal" href="#">
  81. What are State Space Models?
  82. </a>
  83. </li>
  84. <li class="toctree-l1 has-children">
  85. <a class="reference internal" href="../hmm/hmm_index.html">
  86. Inference in discrete SSMs
  87. </a>
  88. <input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" type="checkbox"/>
  89. <label for="toctree-checkbox-1">
  90. <i class="fas fa-chevron-down">
  91. </i>
  92. </label>
  93. <ul>
  94. <li class="toctree-l2">
  95. <a class="reference internal" href="../hmm/hmm.html">
  96. Hidden Markov Models
  97. </a>
  98. </li>
  99. <li class="toctree-l2">
  100. <a class="reference internal" href="../hmm/hmm_filter.html">
  101. HMM filtering (forwards algorithm)
  102. </a>
  103. </li>
  104. <li class="toctree-l2">
  105. <a class="reference internal" href="../hmm/hmm_smoother.html">
  106. HMM smoothing (forwards-backwards algorithm)
  107. </a>
  108. </li>
  109. <li class="toctree-l2">
  110. <a class="reference internal" href="../hmm/hmm_viterbi.html">
  111. Viterbi algorithm
  112. </a>
  113. </li>
  114. <li class="toctree-l2">
  115. <a class="reference internal" href="../hmm/hmm_parallel.html">
  116. Parallel HMM smoothing
  117. </a>
  118. </li>
  119. <li class="toctree-l2">
  120. <a class="reference internal" href="../hmm/hmm_sampling.html">
  121. Forwards-filtering backwards-sampling algorithm
  122. </a>
  123. </li>
  124. </ul>
  125. </li>
  126. <li class="toctree-l1 has-children">
  127. <a class="reference internal" href="../lgssm/lgssm_index.html">
  128. Inference in linear-Gaussian SSMs
  129. </a>
  130. <input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" type="checkbox"/>
  131. <label for="toctree-checkbox-2">
  132. <i class="fas fa-chevron-down">
  133. </i>
  134. </label>
  135. <ul>
  136. <li class="toctree-l2">
  137. <a class="reference internal" href="../lgssm/kalman_filter.html">
  138. Kalman filtering
  139. </a>
  140. </li>
  141. <li class="toctree-l2">
  142. <a class="reference internal" href="../lgssm/kalman_smoother.html">
  143. Kalman (RTS) smoother
  144. </a>
  145. </li>
  146. <li class="toctree-l2">
  147. <a class="reference internal" href="../lgssm/kalman_parallel.html">
  148. Parallel Kalman Smoother
  149. </a>
  150. </li>
  151. <li class="toctree-l2">
  152. <a class="reference internal" href="../lgssm/kalman_sampling.html">
  153. Forwards-filtering backwards sampling
  154. </a>
  155. </li>
  156. </ul>
  157. </li>
  158. <li class="toctree-l1 has-children">
  159. <a class="reference internal" href="../extended/extended_index.html">
  160. Extended (linearized) methods
  161. </a>
  162. <input class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" type="checkbox"/>
  163. <label for="toctree-checkbox-3">
  164. <i class="fas fa-chevron-down">
  165. </i>
  166. </label>
  167. <ul>
  168. <li class="toctree-l2">
  169. <a class="reference internal" href="../extended/extended_filter.html">
  170. Extended Kalman filtering
  171. </a>
  172. </li>
  173. <li class="toctree-l2">
  174. <a class="reference internal" href="../extended/extended_smoother.html">
  175. Extended Kalman smoother
  176. </a>
  177. </li>
  178. <li class="toctree-l2">
  179. <a class="reference internal" href="../extended/extended_parallel.html">
  180. Parallel extended Kalman smoothing
  181. </a>
  182. </li>
  183. </ul>
  184. </li>
  185. <li class="toctree-l1 has-children">
  186. <a class="reference internal" href="../unscented/unscented_index.html">
  187. Unscented methods
  188. </a>
  189. <input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" type="checkbox"/>
  190. <label for="toctree-checkbox-4">
  191. <i class="fas fa-chevron-down">
  192. </i>
  193. </label>
  194. <ul>
  195. <li class="toctree-l2">
  196. <a class="reference internal" href="../unscented/unscented_filter.html">
  197. Unscented filtering
  198. </a>
  199. </li>
  200. <li class="toctree-l2">
  201. <a class="reference internal" href="../unscented/unscented_smoother.html">
  202. Unscented smoothing
  203. </a>
  204. </li>
  205. </ul>
  206. </li>
  207. <li class="toctree-l1">
  208. <a class="reference internal" href="../quadrature/quadrature_index.html">
  209. Quadrature and cubature methods
  210. </a>
  211. </li>
  212. <li class="toctree-l1">
  213. <a class="reference internal" href="../postlin/postlin_index.html">
  214. Posterior linearization
  215. </a>
  216. </li>
  217. <li class="toctree-l1">
  218. <a class="reference internal" href="../adf/adf_index.html">
  219. Assumed Density Filtering
  220. </a>
  221. </li>
  222. <li class="toctree-l1">
  223. <a class="reference internal" href="../vi/vi_index.html">
  224. Variational inference
  225. </a>
  226. </li>
  227. <li class="toctree-l1">
  228. <a class="reference internal" href="../pf/pf_index.html">
  229. Particle filtering
  230. </a>
  231. </li>
  232. <li class="toctree-l1">
  233. <a class="reference internal" href="../smc/smc_index.html">
  234. Sequential Monte Carlo
  235. </a>
  236. </li>
  237. <li class="toctree-l1 has-children">
  238. <a class="reference internal" href="../learning/learning_index.html">
  239. Offline parameter estimation (learning)
  240. </a>
  241. <input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" type="checkbox"/>
  242. <label for="toctree-checkbox-5">
  243. <i class="fas fa-chevron-down">
  244. </i>
  245. </label>
  246. <ul>
  247. <li class="toctree-l2">
  248. <a class="reference internal" href="../learning/em.html">
  249. Expectation Maximization (EM)
  250. </a>
  251. </li>
  252. <li class="toctree-l2">
  253. <a class="reference internal" href="../learning/sgd.html">
  254. Stochastic Gradient Descent (SGD)
  255. </a>
  256. </li>
  257. <li class="toctree-l2">
  258. <a class="reference internal" href="../learning/vb.html">
  259. Variational Bayes (VB)
  260. </a>
  261. </li>
  262. <li class="toctree-l2">
  263. <a class="reference internal" href="../learning/mcmc.html">
  264. Markov Chain Monte Carlo (MCMC)
  265. </a>
  266. </li>
  267. </ul>
  268. </li>
  269. <li class="toctree-l1">
  270. <a class="reference internal" href="../tracking/tracking_index.html">
  271. Multi-target tracking
  272. </a>
  273. </li>
  274. <li class="toctree-l1">
  275. <a class="reference internal" href="../ensemble/ensemble_index.html">
  276. Data assimilation using Ensemble Kalman filter
  277. </a>
  278. </li>
  279. <li class="toctree-l1">
  280. <a class="reference internal" href="../bnp/bnp_index.html">
  281. Bayesian non-parametric SSMs
  282. </a>
  283. </li>
  284. <li class="toctree-l1">
  285. <a class="reference internal" href="../changepoint/changepoint_index.html">
  286. Changepoint detection
  287. </a>
  288. </li>
  289. <li class="toctree-l1">
  290. <a class="reference internal" href="../timeseries/timeseries_index.html">
  291. Timeseries forecasting
  292. </a>
  293. </li>
  294. <li class="toctree-l1">
  295. <a class="reference internal" href="../gp/gp_index.html">
  296. Markovian Gaussian processes
  297. </a>
  298. </li>
  299. <li class="toctree-l1">
  300. <a class="reference internal" href="../ode/ode_index.html">
  301. Differential equations and SSMs
  302. </a>
  303. </li>
  304. <li class="toctree-l1">
  305. <a class="reference internal" href="../control/control_index.html">
  306. Optimal control
  307. </a>
  308. </li>
  309. <li class="toctree-l1">
  310. <a class="reference internal" href="../../bib.html">
  311. Bibliography
  312. </a>
  313. </li>
  314. </ul>
  315. </div>
  316. </nav> <!-- To handle the deprecated key -->
  317. <div class="navbar_extra_footer">
  318. Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
  319. </div>
  320. </div>
  321. <main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
  322. <div class="topbar container-xl fixed-top">
  323. <div class="topbar-contents row">
  324. <div class="col-12 col-md-3 bd-topbar-whitespace site-navigation show"></div>
  325. <div class="col pl-md-4 topbar-main">
  326. <button id="navbar-toggler" class="navbar-toggler ml-0" type="button" data-toggle="collapse"
  327. data-toggle="tooltip" data-placement="bottom" data-target=".site-navigation" aria-controls="navbar-menu"
  328. aria-expanded="true" aria-label="Toggle navigation" aria-controls="site-navigation"
  329. title="Toggle navigation" data-toggle="tooltip" data-placement="left">
  330. <i class="fas fa-bars"></i>
  331. <i class="fas fa-arrow-left"></i>
  332. <i class="fas fa-arrow-up"></i>
  333. </button>
  334. <div class="dropdown-buttons-trigger">
  335. <button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn" aria-label="Download this page"><i
  336. class="fas fa-download"></i></button>
  337. <div class="dropdown-buttons">
  338. <!-- ipynb file if we had a myst markdown file -->
  339. <!-- Download raw file -->
  340. <a class="dropdown-buttons" href="../../_sources/chapters/ssm/ssm.ipynb"><button type="button"
  341. class="btn btn-secondary topbarbtn" title="Download source file" data-toggle="tooltip"
  342. data-placement="left">.ipynb</button></a>
  343. <!-- Download PDF via print -->
  344. <button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
  345. onclick="printPdf(this)" data-toggle="tooltip" data-placement="left">.pdf</button>
  346. </div>
  347. </div>
  348. <!-- Source interaction buttons -->
  349. <div class="dropdown-buttons-trigger">
  350. <button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
  351. aria-label="Connect with source repository"><i class="fab fa-github"></i></button>
  352. <div class="dropdown-buttons sourcebuttons">
  353. <a class="repository-button"
  354. href="https://github.com/probml/ssm-book"><button type="button" class="btn btn-secondary topbarbtn"
  355. data-toggle="tooltip" data-placement="left" title="Source repository"><i
  356. class="fab fa-github"></i>repository</button></a>
  357. <a class="issues-button"
  358. href="https://github.com/probml/ssm-book/issues/new?title=Issue%20on%20page%20%2Fchapters/ssm/ssm.html&body=Your%20issue%20content%20here."><button
  359. type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip" data-placement="left"
  360. title="Open an issue"><i class="fas fa-lightbulb"></i>open issue</button></a>
  361. </div>
  362. </div>
  363. <!-- Full screen (wrap in <a> to have style consistency -->
  364. <a class="full-screen-button"><button type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip"
  365. data-placement="bottom" onclick="toggleFullScreen()" aria-label="Fullscreen mode"
  366. title="Fullscreen mode"><i
  367. class="fas fa-expand"></i></button></a>
  368. <!-- Launch buttons -->
  369. <div class="dropdown-buttons-trigger">
  370. <button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
  371. aria-label="Launch interactive content"><i class="fas fa-rocket"></i></button>
  372. <div class="dropdown-buttons">
  373. <a class="binder-button" href="https://mybinder.org/v2/gh/probml/ssm-book/main?urlpath=tree/chapters/ssm/ssm.ipynb"><button type="button"
  374. class="btn btn-secondary topbarbtn" title="Launch Binder" data-toggle="tooltip"
  375. data-placement="left"><img class="binder-button-logo"
  376. src="../../_static/images/logo_binder.svg"
  377. alt="Interact on binder">Binder</button></a>
  378. <a class="colab-button" href="https://colab.research.google.com/github/probml/ssm-book/blob/main/chapters/ssm/ssm.ipynb"><button type="button" class="btn btn-secondary topbarbtn"
  379. title="Launch Colab" data-toggle="tooltip" data-placement="left"><img class="colab-button-logo"
  380. src="../../_static/images/logo_colab.png"
  381. alt="Interact on Colab">Colab</button></a>
  382. </div>
  383. </div>
  384. </div>
  385. <!-- Table of contents -->
  386. <div class="d-none d-md-block col-md-2 bd-toc show noprint">
  387. <div class="tocsection onthispage pt-5 pb-3">
  388. <i class="fas fa-list"></i> Contents
  389. </div>
  390. <nav id="bd-toc-nav" aria-label="Page">
  391. <ul class="visible nav section-nav flex-column">
  392. <li class="toc-h1 nav-item toc-entry">
  393. <a class="reference internal nav-link" href="#">
  394. What are State Space Models?
  395. </a>
  396. </li>
  397. <li class="toc-h1 nav-item toc-entry">
  398. <a class="reference internal nav-link" href="#hidden-markov-models">
  399. Hidden Markov Models
  400. </a>
  401. <ul class="visible nav section-nav flex-column">
  402. <li class="toc-h2 nav-item toc-entry">
  403. <a class="reference internal nav-link" href="#example-casino-hmm">
  404. Example: Casino HMM
  405. </a>
  406. </li>
  407. </ul>
  408. </li>
  409. <li class="toc-h1 nav-item toc-entry">
  410. <a class="reference internal nav-link" href="#linear-gaussian-ssms">
  411. Linear Gaussian SSMs
  412. </a>
  413. <ul class="visible nav section-nav flex-column">
  414. <li class="toc-h2 nav-item toc-entry">
  415. <a class="reference internal nav-link" href="#example-model-for-2d-tracking">
  416. Example: model for 2d tracking
  417. </a>
  418. </li>
  419. </ul>
  420. </li>
  421. <li class="toc-h1 nav-item toc-entry">
  422. <a class="reference internal nav-link" href="#inferential-goals">
  423. Inferential goals
  424. </a>
  425. <ul class="visible nav section-nav flex-column">
  426. <li class="toc-h2 nav-item toc-entry">
  427. <a class="reference internal nav-link" href="#example-inference-in-the-casino-hmm">
  428. Example: inference in the casino HMM
  429. </a>
  430. </li>
  431. <li class="toc-h2 nav-item toc-entry">
  432. <a class="reference internal nav-link" href="#example-inference-in-the-tracking-ssm">
  433. Example: inference in the tracking SSM
  434. </a>
  435. </li>
  436. </ul>
  437. </li>
  438. </ul>
  439. </nav>
  440. </div>
  441. </div>
  442. </div>
  443. <div id="main-content" class="row">
  444. <div class="col-12 col-md-9 pl-md-3 pr-md-0">
  445. <!-- Table of contents that is only displayed when printing the page -->
  446. <div id="jb-print-docs-body" class="onlyprint">
  447. <h1>What are State Space Models?</h1>
  448. <!-- Table of contents -->
  449. <div id="print-main-content">
  450. <div id="jb-print-toc">
  451. <div>
  452. <h2> Contents </h2>
  453. </div>
  454. <nav aria-label="Page">
  455. <ul class="visible nav section-nav flex-column">
  456. <li class="toc-h1 nav-item toc-entry">
  457. <a class="reference internal nav-link" href="#">
  458. What are State Space Models?
  459. </a>
  460. </li>
  461. <li class="toc-h1 nav-item toc-entry">
  462. <a class="reference internal nav-link" href="#hidden-markov-models">
  463. Hidden Markov Models
  464. </a>
  465. <ul class="visible nav section-nav flex-column">
  466. <li class="toc-h2 nav-item toc-entry">
  467. <a class="reference internal nav-link" href="#example-casino-hmm">
  468. Example: Casino HMM
  469. </a>
  470. </li>
  471. </ul>
  472. </li>
  473. <li class="toc-h1 nav-item toc-entry">
  474. <a class="reference internal nav-link" href="#linear-gaussian-ssms">
  475. Linear Gaussian SSMs
  476. </a>
  477. <ul class="visible nav section-nav flex-column">
  478. <li class="toc-h2 nav-item toc-entry">
  479. <a class="reference internal nav-link" href="#example-model-for-2d-tracking">
  480. Example: model for 2d tracking
  481. </a>
  482. </li>
  483. </ul>
  484. </li>
  485. <li class="toc-h1 nav-item toc-entry">
  486. <a class="reference internal nav-link" href="#inferential-goals">
  487. Inferential goals
  488. </a>
  489. <ul class="visible nav section-nav flex-column">
  490. <li class="toc-h2 nav-item toc-entry">
  491. <a class="reference internal nav-link" href="#example-inference-in-the-casino-hmm">
  492. Example: inference in the casino HMM
  493. </a>
  494. </li>
  495. <li class="toc-h2 nav-item toc-entry">
  496. <a class="reference internal nav-link" href="#example-inference-in-the-tracking-ssm">
  497. Example: inference in the tracking SSM
  498. </a>
  499. </li>
  500. </ul>
  501. </li>
  502. </ul>
  503. </nav>
  504. </div>
  505. </div>
  506. </div>
  507. <div>
  508. <div class="cell docutils container">
  509. <div class="cell_input docutils container">
  510. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># meta-data does not work yet in VScode</span>
  511. <span class="c1"># https://github.com/microsoft/vscode-jupyter/issues/1121</span>
  512. <span class="p">{</span>
  513. <span class="s2">&quot;tags&quot;</span><span class="p">:</span> <span class="p">[</span>
  514. <span class="s2">&quot;hide-cell&quot;</span>
  515. <span class="p">]</span>
  516. <span class="p">}</span>
  517. <span class="c1">### Install necessary libraries</span>
  518. <span class="k">try</span><span class="p">:</span>
  519. <span class="kn">import</span> <span class="nn">jax</span>
  520. <span class="k">except</span><span class="p">:</span>
  521. <span class="c1"># For cuda version, see https://github.com/google/jax#installation</span>
  522. <span class="o">%</span><span class="k">pip</span> install --upgrade &quot;jax[cpu]&quot;
  523. <span class="kn">import</span> <span class="nn">jax</span>
  524. <span class="k">try</span><span class="p">:</span>
  525. <span class="kn">import</span> <span class="nn">distrax</span>
  526. <span class="k">except</span><span class="p">:</span>
  527. <span class="o">%</span><span class="k">pip</span> install --upgrade distrax
  528. <span class="kn">import</span> <span class="nn">distrax</span>
  529. <span class="k">try</span><span class="p">:</span>
  530. <span class="kn">import</span> <span class="nn">jsl</span>
  531. <span class="k">except</span><span class="p">:</span>
  532. <span class="o">%</span><span class="k">pip</span> install git+https://github.com/probml/jsl
  533. <span class="kn">import</span> <span class="nn">jsl</span>
  534. <span class="k">try</span><span class="p">:</span>
  535. <span class="kn">import</span> <span class="nn">rich</span>
  536. <span class="k">except</span><span class="p">:</span>
  537. <span class="o">%</span><span class="k">pip</span> install rich
  538. <span class="kn">import</span> <span class="nn">rich</span>
  539. </pre></div>
  540. </div>
  541. </div>
  542. </div>
  543. <div class="cell docutils container">
  544. <div class="cell_input docutils container">
  545. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
  546. <span class="s2">&quot;tags&quot;</span><span class="p">:</span> <span class="p">[</span>
  547. <span class="s2">&quot;hide-cell&quot;</span>
  548. <span class="p">]</span>
  549. <span class="p">}</span>
  550. <span class="c1">### Import standard libraries</span>
  551. <span class="kn">import</span> <span class="nn">abc</span>
  552. <span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span>
  553. <span class="kn">import</span> <span class="nn">functools</span>
  554. <span class="kn">import</span> <span class="nn">itertools</span>
  555. <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span><span class="p">,</span> <span class="n">Callable</span><span class="p">,</span> <span class="n">NamedTuple</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Tuple</span>
  556. <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
  557. <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
  558. <span class="kn">import</span> <span class="nn">jax</span>
  559. <span class="kn">import</span> <span class="nn">jax.numpy</span> <span class="k">as</span> <span class="nn">jnp</span>
  560. <span class="kn">from</span> <span class="nn">jax</span> <span class="kn">import</span> <span class="n">lax</span><span class="p">,</span> <span class="n">vmap</span><span class="p">,</span> <span class="n">jit</span><span class="p">,</span> <span class="n">grad</span>
  561. <span class="kn">from</span> <span class="nn">jax.scipy.special</span> <span class="kn">import</span> <span class="n">logit</span>
  562. <span class="kn">from</span> <span class="nn">jax.nn</span> <span class="kn">import</span> <span class="n">softmax</span>
  563. <span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span>
  564. <span class="kn">from</span> <span class="nn">jax.random</span> <span class="kn">import</span> <span class="n">PRNGKey</span><span class="p">,</span> <span class="n">split</span>
  565. <span class="kn">import</span> <span class="nn">inspect</span>
  566. <span class="kn">import</span> <span class="nn">inspect</span> <span class="k">as</span> <span class="nn">py_inspect</span>
  567. <span class="kn">import</span> <span class="nn">rich</span>
  568. <span class="kn">from</span> <span class="nn">rich</span> <span class="kn">import</span> <span class="n">inspect</span> <span class="k">as</span> <span class="n">r_inspect</span>
  569. <span class="kn">from</span> <span class="nn">rich</span> <span class="kn">import</span> <span class="nb">print</span> <span class="k">as</span> <span class="n">r_print</span>
  570. <span class="k">def</span> <span class="nf">print_source</span><span class="p">(</span><span class="n">fname</span><span class="p">):</span>
  571. <span class="n">r_print</span><span class="p">(</span><span class="n">py_inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">fname</span><span class="p">))</span>
  572. </pre></div>
  573. </div>
  574. </div>
  575. </div>
  576. <div class="math notranslate nohighlight">
  577. \[ \begin{align}\begin{aligned}\newcommand\floor[1]{\lfloor#1\rfloor}\\\newcommand{\real}{\mathbb{R}}\\% Numbers
  578. \newcommand{\vzero}{\boldsymbol{0}}
  579. \newcommand{\vone}{\boldsymbol{1}}\\% Greek https://www.latex-tutorial.com/symbols/greek-alphabet/
  580. \newcommand{\valpha}{\boldsymbol{\alpha}}
  581. \newcommand{\vbeta}{\boldsymbol{\beta}}
  582. \newcommand{\vchi}{\boldsymbol{\chi}}
  583. \newcommand{\vdelta}{\boldsymbol{\delta}}
  584. \newcommand{\vDelta}{\boldsymbol{\Delta}}
  585. \newcommand{\vepsilon}{\boldsymbol{\epsilon}}
  586. \newcommand{\vzeta}{\boldsymbol{\zeta}}
  587. \newcommand{\vXi}{\boldsymbol{\Xi}}
  588. \newcommand{\vell}{\boldsymbol{\ell}}
  589. \newcommand{\veta}{\boldsymbol{\eta}}
  590. %\newcommand{\vEta}{\boldsymbol{\Eta}}
  591. \newcommand{\vgamma}{\boldsymbol{\gamma}}
  592. \newcommand{\vGamma}{\boldsymbol{\Gamma}}
  593. \newcommand{\vmu}{\boldsymbol{\mu}}
  594. \newcommand{\vmut}{\boldsymbol{\tilde{\mu}}}
  595. \newcommand{\vnu}{\boldsymbol{\nu}}
  596. \newcommand{\vkappa}{\boldsymbol{\kappa}}
  597. \newcommand{\vlambda}{\boldsymbol{\lambda}}
  598. \newcommand{\vLambda}{\boldsymbol{\Lambda}}
  599. \newcommand{\vLambdaBar}{\overline{\vLambda}}
  600. %\newcommand{\vnu}{\boldsymbol{\nu}}
  601. \newcommand{\vomega}{\boldsymbol{\omega}}
  602. \newcommand{\vOmega}{\boldsymbol{\Omega}}
  603. \newcommand{\vphi}{\boldsymbol{\phi}}
  604. \newcommand{\vvarphi}{\boldsymbol{\varphi}}
  605. \newcommand{\vPhi}{\boldsymbol{\Phi}}
  606. \newcommand{\vpi}{\boldsymbol{\pi}}
  607. \newcommand{\vPi}{\boldsymbol{\Pi}}
  608. \newcommand{\vpsi}{\boldsymbol{\psi}}
  609. \newcommand{\vPsi}{\boldsymbol{\Psi}}
  610. \newcommand{\vrho}{\boldsymbol{\rho}}
  611. \newcommand{\vtheta}{\boldsymbol{\theta}}
  612. \newcommand{\vthetat}{\boldsymbol{\tilde{\theta}}}
  613. \newcommand{\vTheta}{\boldsymbol{\Theta}}
  614. \newcommand{\vsigma}{\boldsymbol{\sigma}}
  615. \newcommand{\vSigma}{\boldsymbol{\Sigma}}
  616. \newcommand{\vSigmat}{\boldsymbol{\tilde{\Sigma}}}
  617. \newcommand{\vsigmoid}{\vsigma}
  618. \newcommand{\vtau}{\boldsymbol{\tau}}
  619. \newcommand{\vxi}{\boldsymbol{\xi}}\\
  620. % Lower Roman (Vectors)
  621. \newcommand{\va}{\mathbf{a}}
  622. \newcommand{\vb}{\mathbf{b}}
  623. \newcommand{\vBt}{\mathbf{\tilde{B}}}
  624. \newcommand{\vc}{\mathbf{c}}
  625. \newcommand{\vct}{\mathbf{\tilde{c}}}
  626. \newcommand{\vd}{\mathbf{d}}
  627. \newcommand{\ve}{\mathbf{e}}
  628. \newcommand{\vf}{\mathbf{f}}
  629. \newcommand{\vg}{\mathbf{g}}
  630. \newcommand{\vh}{\mathbf{h}}
  631. %\newcommand{\myvh}{\mathbf{h}}
  632. \newcommand{\vi}{\mathbf{i}}
  633. \newcommand{\vj}{\mathbf{j}}
  634. \newcommand{\vk}{\mathbf{k}}
  635. \newcommand{\vl}{\mathbf{l}}
  636. \newcommand{\vm}{\mathbf{m}}
  637. \newcommand{\vn}{\mathbf{n}}
  638. \newcommand{\vo}{\mathbf{o}}
  639. \newcommand{\vp}{\mathbf{p}}
  640. \newcommand{\vq}{\mathbf{q}}
  641. \newcommand{\vr}{\mathbf{r}}
  642. \newcommand{\vs}{\mathbf{s}}
  643. \newcommand{\vt}{\mathbf{t}}
  644. \newcommand{\vu}{\mathbf{u}}
  645. \newcommand{\vv}{\mathbf{v}}
  646. \newcommand{\vw}{\mathbf{w}}
  647. \newcommand{\vws}{\vw_s}
  648. \newcommand{\vwt}{\mathbf{\tilde{w}}}
  649. \newcommand{\vWt}{\mathbf{\tilde{W}}}
  650. \newcommand{\vwh}{\hat{\vw}}
  651. \newcommand{\vx}{\mathbf{x}}
  652. %\newcommand{\vx}{\mathbf{x}}
  653. \newcommand{\vxt}{\mathbf{\tilde{x}}}
  654. \newcommand{\vy}{\mathbf{y}}
  655. \newcommand{\vyt}{\mathbf{\tilde{y}}}
  656. \newcommand{\vz}{\mathbf{z}}
  657. %\newcommand{\vzt}{\mathbf{\tilde{z}}}\\
  658. % Upper Roman (Matrices)
  659. \newcommand{\vA}{\mathbf{A}}
  660. \newcommand{\vB}{\mathbf{B}}
  661. \newcommand{\vC}{\mathbf{C}}
  662. \newcommand{\vD}{\mathbf{D}}
  663. \newcommand{\vE}{\mathbf{E}}
  664. \newcommand{\vF}{\mathbf{F}}
  665. \newcommand{\vG}{\mathbf{G}}
  666. \newcommand{\vH}{\mathbf{H}}
  667. \newcommand{\vI}{\mathbf{I}}
  668. \newcommand{\vJ}{\mathbf{J}}
  669. \newcommand{\vK}{\mathbf{K}}
  670. \newcommand{\vL}{\mathbf{L}}
  671. \newcommand{\vM}{\mathbf{M}}
  672. \newcommand{\vMt}{\mathbf{\tilde{M}}}
  673. \newcommand{\vN}{\mathbf{N}}
  674. \newcommand{\vO}{\mathbf{O}}
  675. \newcommand{\vP}{\mathbf{P}}
  676. \newcommand{\vQ}{\mathbf{Q}}
  677. \newcommand{\vR}{\mathbf{R}}
  678. \newcommand{\vS}{\mathbf{S}}
  679. \newcommand{\vT}{\mathbf{T}}
  680. \newcommand{\vU}{\mathbf{U}}
  681. \newcommand{\vV}{\mathbf{V}}
  682. \newcommand{\vW}{\mathbf{W}}
  683. \newcommand{\vX}{\mathbf{X}}
  684. %\newcommand{\vXs}{\vX_{\vs}}
  685. \newcommand{\vXs}{\vX_{s}}
  686. \newcommand{\vXt}{\mathbf{\tilde{X}}}
  687. \newcommand{\vY}{\mathbf{Y}}
  688. \newcommand{\vZ}{\mathbf{Z}}
  689. \newcommand{\vZt}{\mathbf{\tilde{Z}}}
  690. \newcommand{\vzt}{\mathbf{\tilde{z}}}\\
  691. %%%%
  692. \newcommand{\hidden}{\vz}
  693. \newcommand{\hid}{\hidden}
  694. \newcommand{\observed}{\vy}
  695. \newcommand{\obs}{\observed}
  696. \newcommand{\inputs}{\vu}
  697. \newcommand{\input}{\inputs}\\\newcommand{\hmmTrans}{\vA}
  698. \newcommand{\hmmObs}{\vB}
  699. \newcommand{\hmmInit}{\vpi}
  700. \newcommand{\hmmhid}{\hidden}
  701. \newcommand{\hmmobs}{\obs}\\\newcommand{\ldsDyn}{\vA}
  702. \newcommand{\ldsObs}{\vC}
  703. \newcommand{\ldsDynIn}{\vB}
  704. \newcommand{\ldsObsIn}{\vD}
  705. \newcommand{\ldsDynNoise}{\vQ}
  706. \newcommand{\ldsObsNoise}{\vR}\\\newcommand{\ssmDyn}{f}
  707. \newcommand{\ssmObs}{h}\end{aligned}\end{align} \]</div>
  708. <div class="tex2jax_ignore mathjax_ignore section" id="what-are-state-space-models">
  709. <span id="sec-ssm-intro"></span><h1>What are State Space Models?<a class="headerlink" href="#what-are-state-space-models" title="Permalink to this headline">¶</a></h1>
  710. <p>A state space model or SSM
  711. is a partially observed Markov model,
  712. in which the hidden state, <span class="math notranslate nohighlight">\(\hidden_t\)</span>,
  713. evolves over time according to a Markov process,
  714. possibly conditional on external inputs or controls <span class="math notranslate nohighlight">\(\input_t\)</span>,
  715. and each hidden state generates some
  716. observations <span class="math notranslate nohighlight">\(\obs_t\)</span> at each time step.
  717. (In this book, we mostly focus on discrete time systems,
  718. although we consider the continuous-time case in XXX.)
  719. We get to see the observations, but not the hidden state.
  720. Our main goal is to infer the hidden state given the observations.
  721. However, we can also use the model to predict future observations,
  722. by first predicting future hidden states, and then predicting
  723. what observations they might generate.
  724. By using a hidden state <span class="math notranslate nohighlight">\(\hidden_t\)</span>
  725. to represent the past observations, <span class="math notranslate nohighlight">\(\obs_{1:t-1}\)</span>,
  726. the model can have ``infinite’’ memory,
  727. unlike a standard Markov model.</p>
  728. <p>Formally we can define an SSM
  729. as the following joint distribution:</p>
  730. <div class="math notranslate nohighlight" id="equation-ssmfull">
  731. <span class="eqno">(2)<a class="headerlink" href="#equation-ssmfull" title="Permalink to this equation">¶</a></span>\[p(\hmmobs_{1:T},\hmmhid_{1:T}|\inputs_{1:T})
  732. = \left[ p(\hmmhid_1|\inputs_1) \prod_{t=2}^{T}
  733. p(\hmmhid_t|\hmmhid_{t-1},\inputs_t) \right]
  734. \left[ \prod_{t=1}^T p(\hmmobs_t|\hmmhid_t, \inputs_t, \hmmobs_{t-1}) \right]\]</div>
  735. <p>where <span class="math notranslate nohighlight">\(p(\hmmhid_t|\hmmhid_{t-1},\inputs_t)\)</span> is the
  736. transition model,
  737. <span class="math notranslate nohighlight">\(p(\hmmobs_t|\hmmhid_t, \inputs_t, \hmmobs_{t-1})\)</span> is the
  738. observation model,
  739. and <span class="math notranslate nohighlight">\(\inputs_{t}\)</span> is an optional input or action.
  740. See <a class="reference internal" href="#ssm-ar"><span class="std std-numref">Figure 3</span></a>
  741. for an illustration of the corresponding graphical model.</p>
  742. <div class="figure align-default" id="ssm-ar">
  743. <a class="reference internal image-reference" href="../../_images/SSM-AR-inputs.png"><img alt="../../_images/SSM-AR-inputs.png" src="../../_images/SSM-AR-inputs.png" style="width: 152.0px; height: 165.0px;" /></a>
  744. <p class="caption"><span class="caption-number">Fig. 3 </span><span class="caption-text">Illustration of an SSM as a graphical model.</span><a class="headerlink" href="#ssm-ar" title="Permalink to this image">¶</a></p>
  745. </div>
  746. <p>We often consider a simpler setting in which there
  747. are no external inputs,
  748. and the observations are conditionally independent of each other
  749. (rather than having Markovian dependencies) given the hidden state.
  750. In this case the joint simplifies to</p>
  751. <div class="math notranslate nohighlight" id="equation-ssmsimplified">
  752. <span class="eqno">(3)<a class="headerlink" href="#equation-ssmsimplified" title="Permalink to this equation">¶</a></span>\[p(\hmmobs_{1:T},\hmmhid_{1:T})
  753. = \left[ p(\hmmhid_1) \prod_{t=2}^{T}
  754. p(\hmmhid_t|\hmmhid_{t-1}) \right]
  755. \left[ \prod_{t=1}^T p(\hmmobs_t|\hmmhid_t \right]\]</div>
  756. <p>See <a class="reference internal" href="#ssm-simplified"><span class="std std-numref">Figure 4</span></a>
  757. for an illustration of the corresponding graphical model.
  758. Compare <a class="reference internal" href="#equation-ssmfull">(2)</a> and <a class="reference internal" href="#equation-ssmsimplified">(3)</a>.</p>
  759. <div class="figure align-default" id="ssm-simplified">
  760. <a class="reference internal image-reference" href="../../_images/SSM-simplified.png"><img alt="../../_images/SSM-simplified.png" src="../../_images/SSM-simplified.png" style="width: 136.0px; height: 98.0px;" /></a>
  761. <p class="caption"><span class="caption-number">Fig. 4 </span><span class="caption-text">Illustration of a simplified SSM.</span><a class="headerlink" href="#ssm-simplified" title="Permalink to this image">¶</a></p>
  762. </div>
  763. </div>
  764. <div class="tex2jax_ignore mathjax_ignore section" id="hidden-markov-models">
  765. <span id="sec-hmm-intro"></span><h1>Hidden Markov Models<a class="headerlink" href="#hidden-markov-models" title="Permalink to this headline">¶</a></h1>
  766. <p>In this section, we discuss the
  767. hidden Markov model or HMM,
  768. which is a state space model in which the hidden states
  769. are discrete, so <span class="math notranslate nohighlight">\(\hmmhid_t \in \{1,\ldots, K\}\)</span>.
  770. The observations may be discrete,
  771. <span class="math notranslate nohighlight">\(\hmmobs_t \in \{1,\ldots, C\}\)</span>,
  772. or continuous,
  773. <span class="math notranslate nohighlight">\(\hmmobs_t \in \real^D\)</span>,
  774. or some combination,
  775. as we illustrate below.
  776. More details can be found in e.g.,
  777. <span id="id1">[<a class="reference internal" href="../../bib.html#id34" title="O. Cappe, E. Moulines, and T. Ryden. Inference in Hidden Markov Models. Springer, 2005.">CMR05</a>, <a class="reference internal" href="../../bib.html#id33" title="A. Fraser. Hidden Markov Models and Dynamical Systems. SIAM Press, 2008.">Fra08</a>, <a class="reference internal" href="../../bib.html#id32" title="L. R. Rabiner. A tutorial on Hidden Markov Models and selected applications in speech recognition. Proc. of the IEEE, 77(2):257–286, 1989.">Rab89</a>]</span>.
  778. For an interactive introduction,
  779. see <a class="reference external" href="https://nipunbatra.github.io/hmm/">https://nipunbatra.github.io/hmm/</a>.</p>
  780. <div class="section" id="example-casino-hmm">
  781. <h2>Example: Casino HMM<a class="headerlink" href="#example-casino-hmm" title="Permalink to this headline">¶</a></h2>
  782. <p>To illustrate HMMs with categorical observation model,
  783. we consider the “Ocassionally dishonest casino” model from <span id="id2">[<a class="reference internal" href="../../bib.html#id3" title="R. Durbin, S. Eddy, A. Krogh, and G. Mitchison. Biological Sequence Analysis: Probabilistic Models of Proteins and Nucleic Acids. Cambridge University Press, 1998.">DEKM98</a>]</span>.
  784. There are 2 hidden states, representing whether the dice being used in the casino is fair or loaded.
  785. Each state defines a distribution over the 6 possible observations.</p>
  786. <p>The transition model is denoted by</p>
  787. <div class="math notranslate nohighlight">
  788. \[p(z_t=j|z_{t-1}=i) = \hmmTrans_{ij}\]</div>
  789. <p>Here the <span class="math notranslate nohighlight">\(i\)</span>’th row of <span class="math notranslate nohighlight">\(\vA\)</span> corresponds to the outgoing distribution from state <span class="math notranslate nohighlight">\(i\)</span>.
  790. This is a row stochastic matrix,
  791. meaning each row sums to one.
  792. We can visualize
  793. the non-zero entries in the transition matrix by creating a state transition diagram,
  794. as shown in
  795. <a class="reference internal" href="#casino-fig"><span class="std std-numref">Figure 5</span></a></p>
  796. <div class="figure align-default" id="casino-fig">
  797. <a class="reference internal image-reference" href="../../_images/casino.png"><img alt="../../_images/casino.png" src="../../_images/casino.png" style="width: 208.5px; height: 142.5px;" /></a>
  798. <p class="caption"><span class="caption-number">Fig. 5 </span><span class="caption-text">Illustration of the casino HMM.</span><a class="headerlink" href="#casino-fig" title="Permalink to this image">¶</a></p>
  799. </div>
  800. <p>The observation model
  801. <span class="math notranslate nohighlight">\(p(\obs_t|\hidden_t=j)\)</span> has the form</p>
  802. <div class="math notranslate nohighlight">
  803. \[p(\obs_t=k|\hidden_t=j) = \hmmObs_{jk} \]</div>
  804. <p>This is represented by the histograms associated with each
  805. state in <a class="reference internal" href="#casino-fig"><span class="std std-ref">Illustration of the casino HMM.</span></a>.</p>
  806. <p>Finally,
  807. the initial state distribution is denoted by</p>
  808. <div class="math notranslate nohighlight">
  809. \[p(z_1=j) = \hmmInit_j\]</div>
  810. <p>Collectively we denote all the parameters by <span class="math notranslate nohighlight">\(\vtheta=(\hmmTrans, \hmmObs, \hmmInit)\)</span>.</p>
  811. <p>Now let us implement this model in code.</p>
  812. <div class="cell docutils container">
  813. <div class="cell_input docutils container">
  814. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># state transition matrix</span>
  815. <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span>
  816. <span class="p">[</span><span class="mf">0.95</span><span class="p">,</span> <span class="mf">0.05</span><span class="p">],</span>
  817. <span class="p">[</span><span class="mf">0.10</span><span class="p">,</span> <span class="mf">0.90</span><span class="p">]</span>
  818. <span class="p">])</span>
  819. <span class="c1"># observation matrix</span>
  820. <span class="n">B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span>
  821. <span class="p">[</span><span class="mi">1</span><span class="o">/</span><span class="mi">6</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">6</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">6</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">6</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">6</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">6</span><span class="p">],</span> <span class="c1"># fair die</span>
  822. <span class="p">[</span><span class="mi">1</span><span class="o">/</span><span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="mi">10</span><span class="p">,</span> <span class="mi">5</span><span class="o">/</span><span class="mi">10</span><span class="p">]</span> <span class="c1"># loaded die</span>
  823. <span class="p">])</span>
  824. <span class="n">pi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">])</span>
  825. <span class="p">(</span><span class="n">nstates</span><span class="p">,</span> <span class="n">nobs</span><span class="p">)</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">B</span><span class="p">)</span>
  826. </pre></div>
  827. </div>
  828. </div>
  829. </div>
  830. <div class="cell docutils container">
  831. <div class="cell_input docutils container">
  832. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">distrax</span>
  833. <span class="kn">from</span> <span class="nn">distrax</span> <span class="kn">import</span> <span class="n">HMM</span>
  834. <span class="n">hmm</span> <span class="o">=</span> <span class="n">HMM</span><span class="p">(</span><span class="n">trans_dist</span><span class="o">=</span><span class="n">distrax</span><span class="o">.</span><span class="n">Categorical</span><span class="p">(</span><span class="n">probs</span><span class="o">=</span><span class="n">A</span><span class="p">),</span>
  835. <span class="n">init_dist</span><span class="o">=</span><span class="n">distrax</span><span class="o">.</span><span class="n">Categorical</span><span class="p">(</span><span class="n">probs</span><span class="o">=</span><span class="n">pi</span><span class="p">),</span>
  836. <span class="n">obs_dist</span><span class="o">=</span><span class="n">distrax</span><span class="o">.</span><span class="n">Categorical</span><span class="p">(</span><span class="n">probs</span><span class="o">=</span><span class="n">B</span><span class="p">))</span>
  837. <span class="nb">print</span><span class="p">(</span><span class="n">hmm</span><span class="p">)</span>
  838. </pre></div>
  839. </div>
  840. </div>
  841. <div class="cell_output docutils container">
  842. <div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
  843. </pre></div>
  844. </div>
  845. <div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;distrax._src.utils.hmm.HMM object at 0x7fb570b7e1f0&gt;
  846. </pre></div>
  847. </div>
  848. </div>
  849. </div>
  850. <p>Let’s sample from the model. We will generate a sequence of latent states, <span class="math notranslate nohighlight">\(\hid_{1:T}\)</span>,
  851. which we then convert to a sequence of observations, <span class="math notranslate nohighlight">\(\obs_{1:T}\)</span>.</p>
  852. <div class="cell docutils container">
  853. <div class="cell_input docutils container">
  854. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">seed</span> <span class="o">=</span> <span class="mi">314</span>
  855. <span class="n">n_samples</span> <span class="o">=</span> <span class="mi">300</span>
  856. <span class="n">z_hist</span><span class="p">,</span> <span class="n">x_hist</span> <span class="o">=</span> <span class="n">hmm</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="n">PRNGKey</span><span class="p">(</span><span class="n">seed</span><span class="p">),</span> <span class="n">seq_len</span><span class="o">=</span><span class="n">n_samples</span><span class="p">)</span>
  857. <span class="n">z_hist_str</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">z_hist</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">))[:</span><span class="mi">60</span><span class="p">]</span>
  858. <span class="n">x_hist_str</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">x_hist</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">))[:</span><span class="mi">60</span><span class="p">]</span>
  859. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Printing sample observed/latent...&quot;</span><span class="p">)</span>
  860. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;x: </span><span class="si">{</span><span class="n">x_hist_str</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
  861. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;z: </span><span class="si">{</span><span class="n">z_hist_str</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
  862. </pre></div>
  863. </div>
  864. </div>
  865. <div class="cell_output docutils container">
  866. <div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Printing sample observed/latent...
  867. x: 633665342652353616444236412331351246651613325161656366246242
  868. z: 222222211111111111111111111111111111111222111111112222211111
  869. </pre></div>
  870. </div>
  871. </div>
  872. </div>
  873. <div class="cell docutils container">
  874. <div class="cell_input docutils container">
  875. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Here is the source code for the sampling algorithm.</span>
  876. <span class="n">print_source</span><span class="p">(</span><span class="n">hmm</span><span class="o">.</span><span class="n">sample</span><span class="p">)</span>
  877. </pre></div>
  878. </div>
  879. </div>
  880. <div class="cell_output docutils container">
  881. <div class="output text_html"><pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"> def sample<span style="font-weight: bold">(</span>self,
  882. *,
  883. seed: chex.PRNGKey,
  884. seq_len: chex.Array<span style="font-weight: bold">)</span> -&gt; Tuple:
  885. <span style="color: #008000; text-decoration-color: #008000">""</span>"Sample from this HMM.
  886. Samples an observation of given length according to this
  887. Hidden Markov Model and gives the sequence of the hidden states
  888. as well as the observation.
  889. Args:
  890. seed: Random key of shape <span style="font-weight: bold">(</span><span style="color: #000080; text-decoration-color: #000080; font-weight: bold">2</span>,<span style="font-weight: bold">)</span> and dtype uint32.
  891. seq_len: The length of the observation sequence.
  892. Returns:
  893. Tuple of hidden state sequence, and observation sequence.
  894. <span style="color: #008000; text-decoration-color: #008000">""</span>"
  895. rng_key, rng_init = jax.random.split<span style="font-weight: bold">(</span>seed<span style="font-weight: bold">)</span>
  896. initial_state = self._init_dist.sample<span style="font-weight: bold">(</span><span style="color: #808000; text-decoration-color: #808000">seed</span>=<span style="color: #800080; text-decoration-color: #800080">rng_init</span><span style="font-weight: bold">)</span>
  897. def draw_state<span style="font-weight: bold">(</span>prev_state, key<span style="font-weight: bold">)</span>:
  898. state = self._trans_dist.sample<span style="font-weight: bold">(</span><span style="color: #808000; text-decoration-color: #808000">seed</span>=<span style="color: #800080; text-decoration-color: #800080">key</span><span style="font-weight: bold">)</span>
  899. return state, state
  900. rng_state, rng_obs = jax.random.split<span style="font-weight: bold">(</span>rng_key<span style="font-weight: bold">)</span>
  901. keys = jax.random.split<span style="font-weight: bold">(</span>rng_state, seq_len - <span style="color: #000080; text-decoration-color: #000080; font-weight: bold">1</span><span style="font-weight: bold">)</span>
  902. _, states = jax.lax.scan<span style="font-weight: bold">(</span>draw_state, initial_state, keys<span style="font-weight: bold">)</span>
  903. states = jnp.append<span style="font-weight: bold">(</span>initial_state, states<span style="font-weight: bold">)</span>
  904. def draw_obs<span style="font-weight: bold">(</span>state, key<span style="font-weight: bold">)</span>:
  905. return self._obs_dist.sample<span style="font-weight: bold">(</span><span style="color: #808000; text-decoration-color: #808000">seed</span>=<span style="color: #800080; text-decoration-color: #800080">key</span><span style="font-weight: bold">)</span>
  906. keys = jax.random.split<span style="font-weight: bold">(</span>rng_obs, seq_len<span style="font-weight: bold">)</span>
  907. obs_seq = jax.vmap<span style="font-weight: bold">(</span>draw_obs, <span style="color: #808000; text-decoration-color: #808000">in_axes</span>=<span style="font-weight: bold">(</span><span style="color: #000080; text-decoration-color: #000080; font-weight: bold">0</span>, <span style="color: #000080; text-decoration-color: #000080; font-weight: bold">0</span><span style="font-weight: bold">))(</span>states, keys<span style="font-weight: bold">)</span>
  908. return states, obs_seq
  909. </pre>
  910. </div></div>
  911. </div>
  912. <p>Our primary goal will be to infer the latent state from the observations,
  913. so we can detect if the casino is being dishonest or not. This will
  914. affect how we choose to gamble our money.
  915. We discuss various ways to perform this inference below.</p>
  916. </div>
  917. </div>
  918. <div class="tex2jax_ignore mathjax_ignore section" id="linear-gaussian-ssms">
  919. <h1>Linear Gaussian SSMs<a class="headerlink" href="#linear-gaussian-ssms" title="Permalink to this headline">¶</a></h1>
  920. <p>Blah blah</p>
  921. <div class="section" id="example-model-for-2d-tracking">
  922. <span id="sec-tracking-lds"></span><h2>Example: model for 2d tracking<a class="headerlink" href="#example-model-for-2d-tracking" title="Permalink to this headline">¶</a></h2>
  923. <p>Blah blah</p>
  924. </div>
  925. </div>
  926. <div class="tex2jax_ignore mathjax_ignore section" id="inferential-goals">
  927. <span id="sec-inference"></span><h1>Inferential goals<a class="headerlink" href="#inferential-goals" title="Permalink to this headline">¶</a></h1>
  928. <div class="figure align-default" id="dbn-inference">
  929. <a class="reference internal image-reference" href="../../_images/dbn-inference-problems.png"><img alt="../../_images/dbn-inference-problems.png" src="../../_images/dbn-inference-problems.png" style="width: 512.0px; height: 359.0px;" /></a>
  930. <p class="caption"><span class="caption-number">Fig. 6 </span><span class="caption-text">Illustration of the different kinds of inference in an SSM.
  931. The main kinds of inference for state-space models.
  932. The shaded region is the interval for which we have data.
  933. The arrow represents the time step at which we want to perform inference.
  934. <span class="math notranslate nohighlight">\(t\)</span> is the current time, <span class="math notranslate nohighlight">\(T\)</span> is the sequence length,
  935. <span class="math notranslate nohighlight">\(\ell\)</span> is the lag and <span class="math notranslate nohighlight">\(h\)</span> is the prediction horizon.</span><a class="headerlink" href="#dbn-inference" title="Permalink to this image">¶</a></p>
  936. </div>
  937. <p>Given the sequence of observations, and a known model,
  938. one of the main tasks with SSMs
  939. to perform posterior inference,
  940. about the hidden states; this is also called
  941. state estimation.
  942. At each time step <span class="math notranslate nohighlight">\(t\)</span>,
  943. there are multiple forms of posterior we may be interested in computing,
  944. including the following:</p>
  945. <ul class="simple">
  946. <li><p>the filtering distribution
  947. <span class="math notranslate nohighlight">\(p(\hmmhid_t|\hmmobs_{1:t})\)</span></p></li>
  948. <li><p>the smoothing distribution
  949. <span class="math notranslate nohighlight">\(p(\hmmhid_t|\hmmobs_{1:T})\)</span> (note that this conditions on future data <span class="math notranslate nohighlight">\(T&gt;t\)</span>)</p></li>
  950. <li><p>the fixed-lag smoothing distribution
  951. <span class="math notranslate nohighlight">\(p(\hmmhid_{t-\ell}|\hmmobs_{1:t})\)</span> (note that this
  952. infers <span class="math notranslate nohighlight">\(\ell\)</span> steps in the past given data up to the present).</p></li>
  953. </ul>
  954. <p>We may also want to compute the
  955. predictive distribution <span class="math notranslate nohighlight">\(h\)</span> steps into the future:</p>
  956. <div class="amsmath math notranslate nohighlight" id="equation-81c90c98-dd2c-4cd3-a4ef-b79cf155d9bf">
  957. <span class="eqno">(4)<a class="headerlink" href="#equation-81c90c98-dd2c-4cd3-a4ef-b79cf155d9bf" title="Permalink to this equation">¶</a></span>\[\begin{align}
  958. p(\hmmobs_{t+h}|\hmmobs_{1:t})
  959. &amp;= \sum_{\hmmhid_{t+h}} p(\hmmobs_{t+h}|\hmmhid_{t+h}) p(\hmmhid_{t+h}|\hmmobs_{1:t})
  960. \end{align}\]</div>
  961. <p>where the hidden state predictive distribution is</p>
  962. <div class="amsmath math notranslate nohighlight" id="equation-ce97a09c-fef8-49e4-ba8b-cacf7c235798">
  963. <span class="eqno">(5)<a class="headerlink" href="#equation-ce97a09c-fef8-49e4-ba8b-cacf7c235798" title="Permalink to this equation">¶</a></span>\[\begin{align}
  964. p(\hmmhid_{t+h}|\hmmobs_{1:t})
  965. &amp;= \sum_{\hmmhid_{t:t+h-1}}
  966. p(\hmmhid_t|\hmmobs_{1:t})
  967. p(\hmmhid_{t+1}|\hmmhid_{t})
  968. p(\hmmhid_{t+2}|\hmmhid_{t+1})
  969. \cdots
  970. p(\hmmhid_{t+h}|\hmmhid_{t+h-1})
  971. \end{align}\]</div>
  972. <p>See \cref{fig:dbn_inf_problems} for a summary of these distributions.</p>
  973. <p>In addition to comuting posterior marginals,
  974. we may want to compute the most probable hidden sequence,
  975. i.e., the joint MAP estimate</p>
  976. <div class="math notranslate nohighlight">
  977. \[\arg \max_{\hmmhid_{1:T}} p(\hmmhid_{1:T}|\hmmobs_{1:T})\]</div>
  978. <p>or sample sequences from the posterior</p>
  979. <div class="math notranslate nohighlight">
  980. \[\hmmhid_{1:T} \sim p(\hmmhid_{1:T}|\hmmobs_{1:T})\]</div>
  981. <p>Algorithms for all these task are discussed in the following chapters,
  982. since the details depend on the form of the SSM.</p>
  983. <div class="section" id="example-inference-in-the-casino-hmm">
  984. <h2>Example: inference in the casino HMM<a class="headerlink" href="#example-inference-in-the-casino-hmm" title="Permalink to this headline">¶</a></h2>
  985. <p>We now illustrate filtering, smoothing and MAP decoding applied
  986. to the casino HMM from <span class="xref std std-ref">sec:casino</span>.</p>
  987. <div class="cell docutils container">
  988. <div class="cell_input docutils container">
  989. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Call inference engine</span>
  990. <span class="n">filtered_dist</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">smoothed_dist</span><span class="p">,</span> <span class="n">loglik</span> <span class="o">=</span> <span class="n">hmm</span><span class="o">.</span><span class="n">forward_backward</span><span class="p">(</span><span class="n">x_hist</span><span class="p">)</span>
  991. <span class="n">map_path</span> <span class="o">=</span> <span class="n">hmm</span><span class="o">.</span><span class="n">viterbi</span><span class="p">(</span><span class="n">x_hist</span><span class="p">)</span>
  992. </pre></div>
  993. </div>
  994. </div>
  995. <div class="cell_output docutils container">
  996. <div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/opt/anaconda3/lib/python3.8/site-packages/jax/_src/numpy/lax_numpy.py:5154: UserWarning: Explicitly requested dtype &lt;class &#39;jax._src.numpy.lax_numpy.int64&#39;&gt; requested in astype is not available, and will be truncated to dtype int32. To enable more dtypes, set the jax_enable_x64 configuration option or the JAX_ENABLE_X64 shell environment variable. See https://github.com/google/jax#current-gotchas for more.
  997. lax_internal._check_user_dtype_supported(dtype, &quot;astype&quot;)
  998. </pre></div>
  999. </div>
  1000. </div>
  1001. </div>
  1002. <div class="cell docutils container">
  1003. <div class="cell_input docutils container">
  1004. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Find the span of timesteps that the simulated systems turns to be in state 1</span>
  1005. <span class="k">def</span> <span class="nf">find_dishonest_intervals</span><span class="p">(</span><span class="n">z_hist</span><span class="p">):</span>
  1006. <span class="n">spans</span> <span class="o">=</span> <span class="p">[]</span>
  1007. <span class="n">x_init</span> <span class="o">=</span> <span class="mi">0</span>
  1008. <span class="k">for</span> <span class="n">t</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">z_hist</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]):</span>
  1009. <span class="k">if</span> <span class="n">z_hist</span><span class="p">[</span><span class="n">t</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">z_hist</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
  1010. <span class="n">x_end</span> <span class="o">=</span> <span class="n">t</span>
  1011. <span class="n">spans</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">x_init</span><span class="p">,</span> <span class="n">x_end</span><span class="p">))</span>
  1012. <span class="k">elif</span> <span class="n">z_hist</span><span class="p">[</span><span class="n">t</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">z_hist</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  1013. <span class="n">x_init</span> <span class="o">=</span> <span class="n">t</span> <span class="o">+</span> <span class="mi">1</span>
  1014. <span class="k">return</span> <span class="n">spans</span>
  1015. </pre></div>
  1016. </div>
  1017. </div>
  1018. </div>
  1019. <div class="cell docutils container">
  1020. <div class="cell_input docutils container">
  1021. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Plot posterior</span>
  1022. <span class="k">def</span> <span class="nf">plot_inference</span><span class="p">(</span><span class="n">inference_values</span><span class="p">,</span> <span class="n">z_hist</span><span class="p">,</span> <span class="n">ax</span><span class="p">,</span> <span class="n">state</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">map_estimate</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
  1023. <span class="n">n_samples</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">inference_values</span><span class="p">)</span>
  1024. <span class="n">xspan</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
  1025. <span class="n">spans</span> <span class="o">=</span> <span class="n">find_dishonest_intervals</span><span class="p">(</span><span class="n">z_hist</span><span class="p">)</span>
  1026. <span class="k">if</span> <span class="n">map_estimate</span><span class="p">:</span>
  1027. <span class="n">ax</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">xspan</span><span class="p">,</span> <span class="n">inference_values</span><span class="p">,</span> <span class="n">where</span><span class="o">=</span><span class="s2">&quot;post&quot;</span><span class="p">)</span>
  1028. <span class="k">else</span><span class="p">:</span>
  1029. <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xspan</span><span class="p">,</span> <span class="n">inference_values</span><span class="p">[:,</span> <span class="n">state</span><span class="p">])</span>
  1030. <span class="k">for</span> <span class="n">span</span> <span class="ow">in</span> <span class="n">spans</span><span class="p">:</span>
  1031. <span class="n">ax</span><span class="o">.</span><span class="n">axvspan</span><span class="p">(</span><span class="o">*</span><span class="n">span</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s2">&quot;tab:gray&quot;</span><span class="p">,</span> <span class="n">edgecolor</span><span class="o">=</span><span class="s2">&quot;none&quot;</span><span class="p">)</span>
  1032. <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n_samples</span><span class="p">)</span>
  1033. <span class="c1"># ax.set_ylim(0, 1)</span>
  1034. <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">1.1</span><span class="p">)</span>
  1035. <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s2">&quot;Observation number&quot;</span><span class="p">)</span>
  1036. </pre></div>
  1037. </div>
  1038. </div>
  1039. </div>
  1040. <div class="cell docutils container">
  1041. <div class="cell_input docutils container">
  1042. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span> <span class="c1"># Filtering</span>
  1043. <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
  1044. <span class="n">plot_inference</span><span class="p">(</span><span class="n">filtered_dist</span><span class="p">,</span> <span class="n">z_hist</span><span class="p">,</span> <span class="n">ax</span><span class="p">)</span>
  1045. <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s2">&quot;p(loaded)&quot;</span><span class="p">)</span>
  1046. <span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s2">&quot;Filtered&quot;</span><span class="p">)</span>
  1047. </pre></div>
  1048. </div>
  1049. </div>
  1050. <div class="cell_output docutils container">
  1051. <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Text(0.5, 1.0, &#39;Filtered&#39;)
  1052. </pre></div>
  1053. </div>
  1054. <img alt="../../_images/ssm_19_1.png" src="../../_images/ssm_19_1.png" />
  1055. </div>
  1056. </div>
  1057. <div class="cell docutils container">
  1058. <div class="cell_input docutils container">
  1059. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Smoothing</span>
  1060. <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
  1061. <span class="n">plot_inference</span><span class="p">(</span><span class="n">smoothed_dist</span><span class="p">,</span> <span class="n">z_hist</span><span class="p">,</span> <span class="n">ax</span><span class="p">)</span>
  1062. <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s2">&quot;p(loaded)&quot;</span><span class="p">)</span>
  1063. <span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s2">&quot;Smoothed&quot;</span><span class="p">)</span>
  1064. </pre></div>
  1065. </div>
  1066. </div>
  1067. <div class="cell_output docutils container">
  1068. <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Text(0.5, 1.0, &#39;Smoothed&#39;)
  1069. </pre></div>
  1070. </div>
  1071. <img alt="../../_images/ssm_20_1.png" src="../../_images/ssm_20_1.png" />
  1072. </div>
  1073. </div>
  1074. <div class="cell docutils container">
  1075. <div class="cell_input docutils container">
  1076. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># MAP estimation</span>
  1077. <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
  1078. <span class="n">plot_inference</span><span class="p">(</span><span class="n">map_path</span><span class="p">,</span> <span class="n">z_hist</span><span class="p">,</span> <span class="n">ax</span><span class="p">,</span> <span class="n">map_estimate</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  1079. <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s2">&quot;MAP state&quot;</span><span class="p">)</span>
  1080. <span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s2">&quot;Viterbi&quot;</span><span class="p">)</span>
  1081. </pre></div>
  1082. </div>
  1083. </div>
  1084. <div class="cell_output docutils container">
  1085. <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Text(0.5, 1.0, &#39;Viterbi&#39;)
  1086. </pre></div>
  1087. </div>
  1088. <img alt="../../_images/ssm_21_1.png" src="../../_images/ssm_21_1.png" />
  1089. </div>
  1090. </div>
  1091. <div class="cell docutils container">
  1092. <div class="cell_input docutils container">
  1093. <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># TODO: posterior samples</span>
  1094. </pre></div>
  1095. </div>
  1096. </div>
  1097. </div>
  1098. </div>
  1099. <div class="section" id="example-inference-in-the-tracking-ssm">
  1100. <h2>Example: inference in the tracking SSM<a class="headerlink" href="#example-inference-in-the-tracking-ssm" title="Permalink to this headline">¶</a></h2>
  1101. <p>We now illustrate filtering, smoothing and MAP decoding applied
  1102. to the 2d tracking HMM from <a class="reference internal" href="#sec-tracking-lds"><span class="std std-ref">Example: model for 2d tracking</span></a>.</p>
  1103. </div>
  1104. </div>
  1105. <script type="text/x-thebe-config">
  1106. {
  1107. requestKernel: true,
  1108. binderOptions: {
  1109. repo: "binder-examples/jupyter-stacks-datascience",
  1110. ref: "master",
  1111. },
  1112. codeMirrorConfig: {
  1113. theme: "abcdef",
  1114. mode: "python"
  1115. },
  1116. kernelOptions: {
  1117. kernelName: "python3",
  1118. path: "./chapters/ssm"
  1119. },
  1120. predefinedOutput: true
  1121. }
  1122. </script>
  1123. <script>kernelName = 'python3'</script>
  1124. </div>
  1125. <!-- Previous / next buttons -->
  1126. <div class='prev-next-area'>
  1127. <a class='left-prev' id="prev-link" href="../scratch.html" title="previous page">
  1128. <i class="fas fa-angle-left"></i>
  1129. <div class="prev-next-info">
  1130. <p class="prev-next-subtitle">previous</p>
  1131. <p class="prev-next-title">Scratchpad</p>
  1132. </div>
  1133. </a>
  1134. <a class='right-next' id="next-link" href="../hmm/hmm_index.html" title="next page">
  1135. <div class="prev-next-info">
  1136. <p class="prev-next-subtitle">next</p>
  1137. <p class="prev-next-title">Inference in discrete SSMs</p>
  1138. </div>
  1139. <i class="fas fa-angle-right"></i>
  1140. </a>
  1141. </div>
  1142. </div>
  1143. </div>
  1144. <footer class="footer">
  1145. <p>
  1146. By Kevin Murphy, Scott Linderman, et al.<br/>
  1147. &copy; Copyright 2021.<br/>
  1148. </p>
  1149. </footer>
  1150. </main>
  1151. </div>
  1152. </div>
  1153. <script src="../../_static/js/index.be7d3bbb2ef33a8344ce.js"></script>
  1154. </body>
  1155. </html>