FITC


In [1]:
import sys
# Add the symgp folder path to the sys.path list
module_path = r'/Users/jaduol/Documents/Uni (original)/Part II/IIB/MEng Project/'
if module_path not in sys.path:
    sys.path.append(module_path)

from symgp import SuperMatSymbol, utils, MVG, Variable, SuperDiagMat, Kernel
from sympy import symbols, ZeroMatrix, Identity
from IPython.display import display, Math, Latex

In [2]:
import imp
imp.reload(utils)
imp.reload(kernel)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-2a19cde2f3c6> in <module>()
      1 import imp
      2 imp.reload(utils)
----> 3 imp.reload(kernel)

NameError: name 'kernel' is not defined

In [2]:
m, n, l = symbols('m n l')
s_y = symbols('\u03c3_y')
K = Kernel()

1. Induding prior


In [3]:
u = Variable('u',m,1)
p_u = MVG([u],mean=ZeroMatrix(m,1),cov=K(u,u))

print("p_u:")
display(Latex(utils.matLatex(p_u)))


p_u:
\begin{align*} p\left(\mathbf{u}\right)&= \mathcal{N}\left(\mathbf{u};\mathbf{m}_{\mathbf{u}},\mathbf{\Sigma}_{\mathbf{u}}\right)\\ \mathbf{m}_{\mathbf{u}} &= \mathbf{0}\\ \mathbf{\Sigma}_{\mathbf{u}} &= \mathbf{K}_{\mathbf{u},\mathbf{u}}\\ \end{align*}

2. Training and Test Conditionals


In [4]:
f, fs, y = utils.variables("f f_{*} y",[n, l, n])

q_fgu = MVG([f], mean=K(f,u)*K(u,u).I*u,
                 cov=SuperDiagMat(K(f,f)-K(f,u)*K(u,u).I*K(u,f)),
                 cond_vars=[u],
                 prefix='q_{FITC}')

print("q_fgu:")
display(Latex(utils.matLatex(q_fgu)))


q_fgu:
\begin{align*} q_{FITC}\left(\mathbf{f}|\mathbf{u}\right)&= \mathcal{N}\left(\mathbf{f};\mathbf{m}_{\mathbf{f}|\mathbf{u}},\mathbf{\Sigma}_{\mathbf{f}|\mathbf{u}}\right)\\ \mathbf{m}_{\mathbf{f}|\mathbf{u}} &= \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{u}\\ \mathbf{\Sigma}_{\mathbf{f}|\mathbf{u}} &= \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}]\\ \end{align*}

In [5]:
q_fsgu = MVG([fs], mean=K(fs,u)*K(u,u).I*u,
                   cov=K(fs,fs)-K(fs,u)*K(u,u).I*K(u,fs),
                   cond_vars=[u],
                   prefix='q_{FITC}')

print("q_fgu:")
display(Latex(utils.matLatex(q_fsgu)))


q_fgu:
\begin{align*} q_{FITC}\left(\mathbf{f_{*}}|\mathbf{u}\right)&= \mathcal{N}\left(\mathbf{f_{*}};\mathbf{m}_{\mathbf{f_{*}}|\mathbf{u}},\mathbf{\Sigma}_{\mathbf{f_{*}}|\mathbf{u}}\right)\\ \mathbf{m}_{\mathbf{f_{*}}|\mathbf{u}} &= \mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{u}\\ \mathbf{\Sigma}_{\mathbf{f_{*}}|\mathbf{u}} &= \mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}} - \mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}\\ \end{align*}

In [6]:
# q(f,fs|u)
q_f_fs_g_u = q_fgu*q_fsgu

print("q_f_fs_g_u:")
display(Latex(utils.matLatex(q_f_fs_g_u)))


q_f_fs_g_u:
\begin{align*} q_{FITC}\left(\mathbf{f},\mathbf{f_{*}}|\mathbf{u}\right)&= \mathcal{N}\left(\left[\begin{smallmatrix}\mathbf{f}\\\mathbf{f_{*}}\end{smallmatrix}\right];\mathbf{m}_{\mathbf{f},\mathbf{f_{*}}|\mathbf{u}},\mathbf{\Sigma}_{\mathbf{f},\mathbf{f_{*}}|\mathbf{u}}\right)\\ \mathbf{m}_{\mathbf{f},\mathbf{f_{*}}|\mathbf{u}} &= \left[\begin{smallmatrix}\mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{u}\\\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{u}\end{smallmatrix}\right]\\ \mathbf{\Sigma}_{\mathbf{f},\mathbf{f_{*}}|\mathbf{u}} &= \left[\begin{smallmatrix}\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}]&\mathbf{0}\\\mathbf{0}&\mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}} - \mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}\end{smallmatrix}\right]\\ \end{align*}

In [7]:
# q(f,fs,u)
q_f_fs_u = q_f_fs_g_u*p_u

print("q_f_fs_u:")
display(Latex(utils.matLatex(q_f_fs_u)))


q_f_fs_u:
\begin{align*} q_{FITC}\left(\mathbf{f},\mathbf{f_{*}},\mathbf{u}\right)&= \mathcal{N}\left(\left[\begin{smallmatrix}\mathbf{f}\\\mathbf{f_{*}}\\\mathbf{u}\end{smallmatrix}\right];\mathbf{m}_{\mathbf{f},\mathbf{f_{*}},\mathbf{u}},\mathbf{\Sigma}_{\mathbf{f},\mathbf{f_{*}},\mathbf{u}}\right)\\ \mathbf{m}_{\mathbf{f},\mathbf{f_{*}},\mathbf{u}} &= \left[\begin{smallmatrix}\mathbf{0}\\\mathbf{0}\\\mathbf{0}\end{smallmatrix}\right]\\ \mathbf{\Sigma}_{\mathbf{f},\mathbf{f_{*}},\mathbf{u}} &= \left[\begin{smallmatrix}\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}&\mathbf{K}_{\mathbf{f},\mathbf{u}}\\\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}}&\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}}\\\mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}&\mathbf{K}_{\mathbf{u},\mathbf{u}}\end{smallmatrix}\right]\\ \end{align*}

In [8]:
# Effective prior: q(f,fs)
q_f_fs = q_f_fs_u.marginalise([u])

print("q_f_fs:")
display(Latex(utils.matLatex(q_f_fs)))


q_f_fs:
\begin{align*} q_{FITC}\left(\mathbf{f},\mathbf{f_{*}}\right)&= \mathcal{N}\left(\left[\begin{smallmatrix}\mathbf{f}\\\mathbf{f_{*}}\end{smallmatrix}\right];\mathbf{m}_{\mathbf{f},\mathbf{f_{*}}},\mathbf{\Sigma}_{\mathbf{f},\mathbf{f_{*}}}\right)\\ \mathbf{m}_{\mathbf{f},\mathbf{f_{*}}} &= \left[\begin{smallmatrix}\mathbf{0}\\\mathbf{0}\end{smallmatrix}\right]\\ \mathbf{\Sigma}_{\mathbf{f},\mathbf{f_{*}}} &= \left[\begin{smallmatrix}\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}\\\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}}\end{smallmatrix}\right]\\ \end{align*}

3. Likelihood


In [9]:
p_ygf = MVG([y],mean=f,cov=s_y**2*Identity(n),cond_vars=[f])

print("p_ygf:")
display(Latex(utils.matLatex(p_ygf)))


p_ygf:
\begin{align*} p\left(\mathbf{y}|\mathbf{f}\right)&= \mathcal{N}\left(\mathbf{y};\mathbf{m}_{\mathbf{y}|\mathbf{f}},\mathbf{\Sigma}_{\mathbf{y}|\mathbf{f}}\right)\\ \mathbf{m}_{\mathbf{y}|\mathbf{f}} &= \mathbf{f}\\ \mathbf{\Sigma}_{\mathbf{y}|\mathbf{f}} &= \sigma_y^{2} \mathbf{I}\\ \end{align*}

4. Inference


In [10]:
# q(f,fs,y)
q_f_fs_y = p_ygf*q_f_fs

print("q_f_fs_y:")
display(Latex(utils.matLatex(q_f_fs_y)))


q_f_fs_y:
\begin{align*} q_{FITC}\left(\mathbf{y},\mathbf{f_{*}},\mathbf{f}\right)&= \mathcal{N}\left(\left[\begin{smallmatrix}\mathbf{y}\\\mathbf{f_{*}}\\\mathbf{f}\end{smallmatrix}\right];\mathbf{m}_{\mathbf{y},\mathbf{f_{*}},\mathbf{f}},\mathbf{\Sigma}_{\mathbf{y},\mathbf{f_{*}},\mathbf{f}}\right)\\ \mathbf{m}_{\mathbf{y},\mathbf{f_{*}},\mathbf{f}} &= \left[\begin{smallmatrix}\mathbf{0}\\\mathbf{0}\\\mathbf{0}\end{smallmatrix}\right]\\ \mathbf{\Sigma}_{\mathbf{y},\mathbf{f_{*}},\mathbf{f}} &= \left[\begin{smallmatrix}\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}&\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\\\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}}&\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\\\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}&\mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}&\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\end{smallmatrix}\right]\\ \end{align*}

In [11]:
# q(f,fs|y)
q_f_fs_g_y = q_f_fs_y.condition([y])

print("q_f_fs_g_y:")
display(Latex(utils.matLatex(q_f_fs_g_y)))


q_f_fs_g_y:
\begin{align*} q_{FITC}\left(\mathbf{f_{*}},\mathbf{f}|\mathbf{y}\right)&= \mathcal{N}\left(\left[\begin{smallmatrix}\mathbf{f_{*}}\\\mathbf{f}\end{smallmatrix}\right];\mathbf{m}_{\mathbf{f_{*}},\mathbf{f}|\mathbf{y}},\mathbf{\Sigma}_{\mathbf{f_{*}},\mathbf{f}|\mathbf{y}}\right)\\ \mathbf{m}_{\mathbf{f_{*}},\mathbf{f}|\mathbf{y}} &= \left[\begin{smallmatrix}\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}} \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \mathbf{y}\\\left(\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right) \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \mathbf{y}\end{smallmatrix}\right]\\ \mathbf{\Sigma}_{\mathbf{f_{*}},\mathbf{f}|\mathbf{y}} &= \left[\begin{smallmatrix}\mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}} - \mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}} \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}&\mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}} - \mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}} \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \left(\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)\\\mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}} - \left(\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right) \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}&\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}} - \left(\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right) \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \left(\text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)\end{smallmatrix}\right]\\ \end{align*}

In [12]:
# q(fs|y)
q_fs_g_y = q_f_fs_g_y.marginalise([f])

print("q_fs_g_y:")
display(Latex(utils.matLatex(q_fs_g_y)))


q_fs_g_y:
\begin{align*} q_{FITC}\left(\mathbf{f_{*}}|\mathbf{y}\right)&= \mathcal{N}\left(\mathbf{f_{*}};\mathbf{m}_{\mathbf{f_{*}}|\mathbf{y}},\mathbf{\Sigma}_{\mathbf{f_{*}}|\mathbf{y}}\right)\\ \mathbf{m}_{\mathbf{f_{*}}|\mathbf{y}} &= \mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}} \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \mathbf{y}\\ \mathbf{\Sigma}_{\mathbf{f_{*}}|\mathbf{y}} &= \mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}} - \mathbf{K}_{\mathbf{f_{*}},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}} \left(\sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}] + \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f}}\right)^{-1} \mathbf{K}_{\mathbf{f},\mathbf{u}} \mathbf{K}_{\mathbf{u},\mathbf{u}}^{-1} \mathbf{K}_{\mathbf{u},\mathbf{f_{*}}}\\ \end{align*}

Example of Kernel substitution


In [13]:
import imp
imp.reload(utils)
from symgp import Covariance
M = Covariance(u, name='M_{u,u}', full_expr=K(u,u).I)
Q = Kernel(name='Q', sub_kernels=[K,K], kernel_type='mul', mat=M)
#repl_dict = {K(fs,u)*K(u,u).I*K(u,f): Q(fs,f),
#             K(f,u)*K(u,u).I*K(u,f): Q(f,f),
#             K(f,u)*K(u,u).I*K(u,fs): Q(f,fs)}
repl_dict = [Q]

q_fs_g_y.mean.expanded = utils.replace(q_fs_g_y.mean.to_full_expr(), repl_dict)
q_fs_g_y.covar.expanded = utils.replace(q_fs_g_y.covar.to_full_expr(), repl_dict)


subbed_expr:  Q_{f_{*},f}*(σ_y**2*I + diag[K_{f,f} + (-1)*Q_{f,f}] + Q_{f,f})^-1*y
srepr(subbed_expr):  SuperMatMul(KernelMatrix('Q_{f_{*},f}', Symbol('l'), Symbol('n')), Inverse(SuperMatAdd(MatMul(Pow(Symbol('σ_y'), Integer(2)), Identity(Symbol('n'))), SuperDiagMat(SuperMatAdd(KernelMatrix('K_{f,f}', Symbol('n'), Symbol('n')), SuperMatMul(Integer(-1), KernelMatrix('Q_{f,f}', Symbol('n'), Symbol('n'))))), SuperMatMul(KernelMatrix('Q_{f,f}', Symbol('n'), Symbol('n'))))), Variable('y', Symbol('n'), Integer(1)))
subbed_expr:  K_{f_{*},f_{*}} + (-1)*Q_{f_{*},f}*(σ_y**2*I + diag[K_{f,f} + (-1)*Q_{f,f}] + Q_{f,f})^-1*Q_{f,f_{*}}
srepr(subbed_expr):  SuperMatAdd(KernelMatrix('K_{f_{*},f_{*}}', Symbol('l'), Symbol('l')), SuperMatMul(Integer(-1), KernelMatrix('Q_{f_{*},f}', Symbol('l'), Symbol('n')), Inverse(SuperMatAdd(MatMul(Pow(Symbol('σ_y'), Integer(2)), Identity(Symbol('n'))), SuperDiagMat(SuperMatAdd(KernelMatrix('K_{f,f}', Symbol('n'), Symbol('n')), SuperMatMul(Integer(-1), KernelMatrix('Q_{f,f}', Symbol('n'), Symbol('n'))))), SuperMatMul(KernelMatrix('Q_{f,f}', Symbol('n'), Symbol('n'))))), KernelMatrix('Q_{f,f_{*}}', Symbol('n'), Symbol('l'))))

In [14]:
imp.reload(utils)

print("q_fs_g_y (replaced):")
display(Latex(utils.matLatex(q_fs_g_y)))


q_fs_g_y (replaced):
\begin{align*} q_{FITC}\left(\mathbf{f_{*}}|\mathbf{y}\right)&= \mathcal{N}\left(\mathbf{f_{*}};\mathbf{m}_{\mathbf{f_{*}}|\mathbf{y}},\mathbf{\Sigma}_{\mathbf{f_{*}}|\mathbf{y}}\right)\\ \mathbf{m}_{\mathbf{f_{*}}|\mathbf{y}} &= \mathbf{Q}_{\mathbf{f_{*}},\mathbf{f}} \left(\mathbf{Q}_{\mathbf{f},\mathbf{f}} + \sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{Q}_{\mathbf{f},\mathbf{f}}]\right)^{-1} \mathbf{y}\\ \mathbf{\Sigma}_{\mathbf{f_{*}}|\mathbf{y}} &= \mathbf{K}_{\mathbf{f_{*}},\mathbf{f_{*}}} - \mathbf{Q}_{\mathbf{f_{*}},\mathbf{f}} \left(\mathbf{Q}_{\mathbf{f},\mathbf{f}} + \sigma_y^{2} \mathbf{I} + \text{diag}[\mathbf{K}_{\mathbf{f},\mathbf{f}} - \mathbf{Q}_{\mathbf{f},\mathbf{f}}]\right)^{-1} \mathbf{Q}_{\mathbf{f},\mathbf{f_{*}}}\\ \end{align*}

In [ ]: