{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# State-Space Model\n", "\n", "We may wish to consider another Bayesian setting, but under a markovian setting that evolves over time, namely the latent state space model:\n", "\n", "$$\n", "\\theta \\sim \\mathcal{N}(m, P)\n", "$$\n", "$$\n", "x\\mid \\theta \\;\\sim\\; \\mathcal{N}\\bigl(C\\,\\theta,\\;Q\\bigr)\n", "$$\n", "$$\n", "y\\mid x \\;\\sim\\; \\mathcal{N}\\bigl(A\\,x,\\;R\\bigr)\n", "$$\n", "\n", "Where $y$ is observed data, $x$ is latent, and we wish to infer on $\\theta$\n", "\n", "We first compute the likelihood of y, given by\n", "\n", "$$\n", "p(y\\mid \\theta)\n", "\\;=\\;\\int p(y\\mid x)\\,p(x\\mid \\theta)\\,dx.\n", "$$\n", "\n", "$$\n", "= \\int \\mathcal{N}\\bigl(y; A\\,x,\\;R\\bigr) \\mathcal{N}\\bigl(x; C\\,\\theta,\\;Q\\bigr) dx\n", "$$\n", "\n", "$$\n", "= \\mathcal{N}\\!\\Bigl(\n", "y;\\;A\\,C\\,\\theta,\\;A\\,Q\\,A^\\top + R\n", "\\Bigr)\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we construct the posterior \n", "\n", "$$\n", "p(\\theta\\mid y)\n", "\\propto\n", "p(\\theta) \\; p(y\\mid \\theta)\n", "$$\n", "\n", "$$\n", "\\propto\n", "\\mathcal{N}\\bigl(\\theta; m \\; P )\n", "\\;\n", "\\mathcal{N}\\!\\Bigl(\n", "y;\\;A\\,C\\,\\theta,\\;A\\,Q\\,A^\\top + R\n", "\\Bigr)\n", "$$\n", "\n", "Using established result, we know\n", "\n", "$$\n", "p(\\theta\\mid y) = \\mathcal{N}(y; M, \\Sigma)\n", "$$\n", "\n", "Where the posterior covariance is given by\n", "\n", "$$\n", "\\Sigma \\;=\\;\n", "P - P (AC)^\\top \\Bigl( A\\,Q\\,A^\\top + R + (AC) P (AC) ^\\top \\Bigr)^{-1} (AC) P\n", "$$\n", "\n", "Or equivalently in a more elegant form\n", "\n", "$$\n", "\\Sigma \\;=\\;\n", "\\bigl[P^{-1} + C^\\top A^\\top (A\\,Q\\,A^\\top + R)^{-1} A\\,C\\bigr]^{-1}.\n", "$$ \n", "\n", "And the posterior mean $M$ is given by\n", "\n", "$$\n", "M = m + P (AC)^\\top \\Bigl( A\\,Q\\,A^\\top + R + (AC) P (AC) ^\\top \\Bigr)^{-1} (y - ACm)\n", "$$\n", "\n", "or more elegantly\n", "\n", "$$\n", "M = \\Sigma\\bigl[P^{-1}m + C^\\top A^\\top (A\\,Q\\,A^\\top + R)^{-1}y\\bigr]\n", "$$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an example, consider the following parameters:\n", "\n", "$$\n", "m = \\begin{pmatrix}2 \\\\[4pt] 0\\end{pmatrix},\\quad \n", "P = \\begin{pmatrix}1 & 0\\\\[4pt] 0 & 1\\end{pmatrix},\\quad\n", "C = \\begin{pmatrix}1 & 1\\\\[4pt] 1 & 1.01\\end{pmatrix}\n", "$$\n", "\n", "$$\n", "Q = \\begin{pmatrix}0.01 & 0\\\\[4pt] 0 & 0.01\\end{pmatrix}\n", "A = \\begin{pmatrix}1 & 0 \\\\[4pt]\\varepsilon & 0\\end{pmatrix},\\quad \n", "R = \\,\\begin{pmatrix}\\varepsilon & 0\\\\[4pt] 0 & \\varepsilon\\end{pmatrix},\\quad\n", "$$\n", "\n", "And we observed \n", "$\n", "y = \\begin{pmatrix}1\\\\[3pt]1\\end{pmatrix}.\n", "$\n", "\n", "In this case we know the posterior is \n", "$$\n", "p(\\theta\\mid y) = \\mathcal{N}(y; M, \\Sigma)\n", "$$\n", "Where $M, \\Sigma$ depends on $\\epsilon$ and is computed as follow:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Epsilon | \n", "Posterior Mean | \n", "Posterior Variance | \n", "
---|---|---|---|
0 | \n", "0.001 | \n", "[1.5032, -0.4968] | \n", "[[0.5027, -0.4973], [-0.4973, 0.5027]] | \n", "
1 | \n", "0.010 | \n", "[1.5099, -0.4901] | \n", "[[0.505, -0.495], [-0.495, 0.505]] | \n", "
2 | \n", "0.100 | \n", "[1.5681, -0.4319] | \n", "[[0.5258, -0.4742], [-0.4742, 0.5258]] | \n", "
3 | \n", "1.000 | \n", "[1.6016, -0.3984] | \n", "[[0.6016, -0.3984], [-0.3984, 0.6016]] | \n", "