Tutorials › Stochastic Processes › A Two-Observation Gaussian Process Calculation

Gaussian Processes · Tutorial 879 of 994

A Two-Observation Gaussian Process Calculation

Work through exact posterior predictions at two test inputs, and see how the observation values and measurement-noise variance affect the results.

Advanced 10 min read

What You'll Learn

  • Set up the joint Gaussian vectors for two noisy observations and two latent test values.
  • Compute the observation covariance matrix and its inverse using exact fractions.
  • Calculate posterior means from the observed values.
  • Calculate posterior variances and the covariance between test values.
  • Check the resulting covariance matrix and identify a common noise-omission error.

From Noisy Observations to Two Predictions

A Gaussian process calculation becomes concrete when the training and test inputs are fixed. The prior covariance between those inputs determines a joint Gaussian distribution; measurement noise changes the covariance of the observations, but it does not change the prior covariance of the latent test values. Conditioning the joint distribution then gives posterior means and a covariance matrix for the test values. Keeping these pieces separate is essential: the posterior covariance describes uncertainty about the latent process, not uncertainty about a future noisy measurement.

Let \((\Omega,\mathcal{F},\mathbb{P})\) be a probability space, and let \((F(x))_{x\in\mathcal{X}}\) be a Gaussian process on an input set \(\mathcal{X}\). This means that every finite vector of process values has a multivariate Gaussian distribution. Write its mean function as \(m(x)=\mathbb{E}[F(x)]\), and its covariance kernel as \(k(x,x')=\operatorname{Cov}(F(x),F(x'))\). At two distinct training inputs \(x_1,x_2\), suppose the observations are \(Y_i=F(x_i)+\epsilon_i\), where the errors are independent \(N(0,\sigma^2)\) variables, independent of the process, and \(\sigma^2>0\). We want to predict the latent values \(F(z_1),F(z_2)\) at two test inputs.

Definition: Let \(K\) be the \(2\times2\) prior covariance matrix of \((F(x_1),F(x_2))\), let \(K_*\) be the prior covariance matrix of \((F(z_1),F(z_2))\), and let \(B\) be the \(2\times2\) matrix whose entry in row \(r\), column \(i\) is \(k(z_r,x_i)\). The observation covariance matrix is \(C=K+\sigma^2 I\), because the independent measurement errors add variance \(\sigma^2\) to each observation and add no cross-covariance. If \(C\) is invertible, the conditional mean vector and covariance matrix of the latent test values, given the observation vector \(Y=y\), are \(m_*+B C^{-1}(y-m_X)\) and \(K_*-B C^{-1}B^{\mathsf T}\), respectively. Here the vectors are written as columns, \(m_X=(m(x_1),m(x_2))^{\mathsf T}\), and \(m_*=(m(z_1),m(z_2))^{\mathsf T}\).

The observation covariance is invertible under the stated positive-noise assumption. Indeed, \(K\) is a covariance matrix, so \(v^{\mathsf T}Kv\ge0\) for every vector \(v\). Thus for any nonzero \(v\), \(v^{\mathsf T}Cv=v^{\mathsf T}Kv+\sigma^2v^{\mathsf T}v>0\). The calculation below applies to a general Gaussian process, then uses a simple kernel that makes every entry exact.

Why the Conditioning Formula Works

Theorem (Gaussian conditioning with independent measurement noise): Under the setup above, the joint vector of observations and latent test values is Gaussian. If \(\sigma^2>0\), its observation covariance \(C\) is invertible, and the conditional distribution of the latent test vector given \(Y=y\) has mean \(m_*+BC^{-1}(y-m_X)\) and covariance \(K_*-BC^{-1}B^{\mathsf T}\).

Proof. Put \(X=(F(x_1),F(x_2))^{\mathsf T}\), \(Z=(F(z_1),F(z_2))^{\mathsf T}\), and \(Y=X+\epsilon\), where \(\epsilon=(\epsilon_1,\epsilon_2)^{\mathsf T}\). Finite-dimensional Gaussianity and independence of the Gaussian noise imply that \((Z,Y)\) is jointly Gaussian. Its means are \(m_*\) and \(m_X\). Independence of \(\epsilon\) from the process gives \(\operatorname{Cov}(Y)=K+\sigma^2 I=C\) and \(\operatorname{Cov}(Z,Y)=B\).

Define the residual \(R=Z-m_*-BC^{-1}(Y-m_X)\). It is a linear transformation of a jointly Gaussian vector, hence is jointly Gaussian with \(Y\). Directly expanding its covariance with \(Y\) gives \(\operatorname{Cov}(R,Y)=B-BC^{-1}C=0\). Jointly Gaussian vectors with zero cross-covariance are independent, the standard independence property of Gaussian vectors. Therefore \(R\) is independent of \(Y\), and its conditional mean given \(Y=y\) is its unconditional mean, zero. Rearranging the definition of \(R\) gives the stated conditional mean. Expanding the residual covariance gives \(\operatorname{Cov}(R)=K_*-BC^{-1}B^{\mathsf T}\): the two cross terms are each \(BC^{-1}B^{\mathsf T}\), while the covariance of \(BC^{-1}(Y-m_X)\) is \(BC^{-1}CC^{-1}B^{\mathsf T}=BC^{-1}B^{\mathsf T}\). Since \(R\) is independent of \(Y\), this residual covariance is also the conditional covariance. \(\square\)

Corollary: The posterior covariance \(K_*-BC^{-1}B^{\mathsf T}\) is positive semidefinite. For any vector \(a\), its quadratic form equals the variance of \(a^{\mathsf T}R\), which is nonnegative by the definition of variance. In particular, each posterior latent variance is nonnegative. This check is useful for catching arithmetic errors, but nonnegative diagonal entries alone do not guarantee that a proposed covariance matrix is valid.

Proof. The residual \(R\) in the theorem has covariance \(K_*-BC^{-1}B^{\mathsf T}\). For any fixed vector \(a\), \(a^{\mathsf T}(K_*-BC^{-1}B^{\mathsf T})a=\operatorname{Var}(a^{\mathsf T}R)\ge0\). This is exactly the definition of positive semidefiniteness. \(\square\)

A Kernel with Exact Covariances

For the examples, take \(\mathcal{X}=\mathbb{R}\), zero prior mean, and \(k(x,x')=1+xx'\). This is a valid covariance kernel: for any finite inputs \(u_1,\ldots,u_n\) and real coefficients \(a_1,\ldots,a_n\),

$$ \sum_{i=1}^n\sum_{j=1}^n a_i a_j k(u_i,u_j) = \left(\sum_{i=1}^n a_i\right)^2+ \left(\sum_{i=1}^n a_i u_i\right)^2 \ge 0. $$

Thus every finite covariance matrix generated by this kernel is positive semidefinite. Equivalently, the process can be represented as \(F(x)=A+Bx\), where \(A\) and \(B\) are independent standard normal variables. The examples use training inputs \(-1,1\) and test inputs \(0,2\). With \(k(x,x')=1+xx'\), the training covariance entries are \(k(-1,-1)=2\), \(k(-1,1)=0\), \(k(1,-1)=0\), and \(k(1,1)=2\). The test covariance entries are \(k(0,0)=1\), \(k(0,2)=1\), \(k(2,0)=1\), and \(k(2,2)=5\). The cross-covariance entries, with test inputs in rows and training inputs in columns, are \(k(0,-1)=1\), \(k(0,1)=1\), \(k(2,-1)=-1\), and \(k(2,1)=3\).

Worked Example: Two Observations with Noise Variance One-Half

Observe \(y_1=1\) at \(x_1=-1\) and \(y_2=2\) at \(x_2=1\), with independent measurement errors of variance \(\sigma^2=\tfrac12\). Find the posterior means, variances, and covariance of \(F(0)\) and \(F(2)\).

The matrices from the kernel calculations above are \(K=\begin{bmatrix}2&0\\0&2\end{bmatrix}\), \(K_*=\begin{bmatrix}1&1\\1&5\end{bmatrix}\), and \(B=\begin{bmatrix}1&1\\-1&3\end{bmatrix}\). The observation covariance is

$$ C=K+\tfrac12 I= \begin{bmatrix}\tfrac52&0\\0&\tfrac52\end{bmatrix}, \qquad C^{-1}= \begin{bmatrix}\tfrac25&0\\0&\tfrac25\end{bmatrix}. $$

For the posterior mean, \(m_X=m_*=0\) and \(y=(1,2)^{\mathsf T}\). First, \(C^{-1}y=(\tfrac25,\tfrac45)^{\mathsf T}\). Multiplying by \(B\) gives the mean at \(0\) as \(\tfrac25+\tfrac45=\tfrac65\), and the mean at \(2\) as \(-\tfrac25+3\cdot\tfrac45=-\tfrac25+\tfrac{12}5=2\). The posterior mean vector is therefore \((\tfrac65,2)^{\mathsf T}\).

For the covariance, multiplication gives \(BB^{\mathsf T}=\begin{bmatrix}2&2\\2&10\end{bmatrix}\). Since \(C^{-1}=\tfrac25 I\), the subtracted matrix is \(BC^{-1}B^{\mathsf T}=\tfrac25BB^{\mathsf T}=\begin{bmatrix}\tfrac45&\tfrac45\\\tfrac45&4\end{bmatrix}\). Hence

$$ K_*-BC^{-1}B^{\mathsf T} = \begin{bmatrix}1&1\\1&5\end{bmatrix} - \begin{bmatrix}\tfrac45&\tfrac45\\\tfrac45&4\end{bmatrix} = \begin{bmatrix}\tfrac15&\tfrac15\\\tfrac15&1\end{bmatrix}. $$

The latent posterior variances are \(\tfrac15\) at \(0\) and \(1\) at \(2\), and their posterior covariance is \(\tfrac15\). As a check, the determinant of the posterior covariance is \(\tfrac15\cdot1-(\tfrac15)^2=\tfrac4{25}>0\), and both diagonal entries are positive. Substituting the mean back into the formula also checks it: \(B(\tfrac25,\tfrac45)^{\mathsf T}=(\tfrac65,2)^{\mathsf T}\), as calculated.

Worked Example: Changing the Observed Values

Keep the same four inputs and noise variance \(\tfrac12\), but suppose the observations are \(y_1=2\) and \(y_2=-1\). Recalculate the posterior predictions.

The observation covariance remains \(C=\begin{bmatrix}\tfrac52&0\\0&\tfrac52\end{bmatrix}\), so \(C^{-1}=\begin{bmatrix}\tfrac25&0\\0&\tfrac25\end{bmatrix}\). Now \(C^{-1}y=(\tfrac45,-\tfrac25)^{\mathsf T}\). Thus the posterior mean at \(0\) is \(\tfrac45-\tfrac25=\tfrac25\), and at \(2\) it is \(-\tfrac45+3(-\tfrac25)=-\tfrac45-\tfrac65=-2\). The mean vector is \((\tfrac25,-2)^{\mathsf T}\).

The inputs, kernel, and noise variance have not changed, so \(K_*\) and \(BC^{-1}B^{\mathsf T}\) are still \(\begin{bmatrix}1&1\\1&5\end{bmatrix}\) and \(\begin{bmatrix}\tfrac45&\tfrac45\\\tfrac45&4\end{bmatrix}\). Subtracting gives posterior covariance \(\begin{bmatrix}\tfrac15&\tfrac15\\\tfrac15&1\end{bmatrix}\). Check the mean by multiplying \(B(\tfrac45,-\tfrac25)^{\mathsf T}\): the first component is \(\tfrac45-\tfrac25=\tfrac25\), and the second is \(-\tfrac45-\tfrac65=-2\). This illustrates the distinction between the posterior mean, which depends on the observed values, and posterior covariance, which in this Gaussian model depends on the inputs, kernel, and noise level, not on the realized observations.

Worked Example: Increasing the Noise Variance

Return to observations \(y=(1,2)^{\mathsf T}\), but set the measurement-noise variance to \(1\). Find the predictions at \(0\) and \(2\), showing how this change affects the calculation.

The training covariance is still \(K=\begin{bmatrix}2&0\\0&2\end{bmatrix}\), but now \(C=K+I=\begin{bmatrix}3&0\\0&3\end{bmatrix}\) and \(C^{-1}=\begin{bmatrix}\tfrac13&0\\0&\tfrac13\end{bmatrix}\). Thus \(C^{-1}y=(\tfrac13,\tfrac23)^{\mathsf T}\). Multiplication by \(B\) gives the posterior means \(\tfrac13+\tfrac23=1\) at \(0\) and \(-\tfrac13+3\cdot\tfrac23=-\tfrac13+2=\tfrac53\) at \(2\).

From the entries of \(B\), \(BB^{\mathsf T}=\begin{bmatrix}1^2+1^2&1(-1)+1(3)\\1(-1)+1(3)&(-1)^2+3^2\end{bmatrix}=\begin{bmatrix}2&2\\2&10\end{bmatrix}\). Therefore \(BC^{-1}B^{\mathsf T}=\tfrac13BB^{\mathsf T}=\begin{bmatrix}\tfrac23&\tfrac23\\\tfrac23&\tfrac{10}3\end{bmatrix}\). Subtracting this from \(K_*=\begin{bmatrix}1&1\\1&5\end{bmatrix}\) yields posterior covariance \(\begin{bmatrix}\tfrac13&\tfrac13\\\tfrac13&\tfrac53\end{bmatrix}\). Its determinant is \(\tfrac13\cdot\tfrac53-(\tfrac13)^2=\tfrac59-\tfrac19=\tfrac49>0\). Finally, \(B(\tfrac13,\tfrac23)^{\mathsf T}=(1,\tfrac53)^{\mathsf T}\), checking the mean. Compared with noise variance \(\tfrac12\), the larger noise pulls the mean at \(0\) from \(\tfrac65\) to \(1\), and raises the posterior variance there from \(\tfrac15\) to \(\tfrac13\).

A Common Error: Leaving Noise Out

The observation covariance is not just \(K\). It is \(K+\sigma^2I\), because the observed values include independent measurement errors. In the first worked example, omitting the noise would incorrectly use \(K^{-1}=\tfrac12I\) instead of \(C^{-1}=\tfrac25I\). The resulting incorrect mean at \(0\) would be \((1,1)\tfrac12(1,2)^{\mathsf T}=\tfrac32\), rather than the correct \(\tfrac65\). The incorrect variance at \(0\) would be \(1-(1,1)\tfrac12 I(1,1)^{\mathsf T}=1-\tfrac12(2)=0\), rather than \(\tfrac15\). This is not a harmless change in notation: it treats the noisy observations as if they were exact latent process values.

Another useful distinction is between a latent prediction and a future noisy observation. The variance calculated above is for \(F(z_r)\). If a new independent measurement \(F(z_r)+\epsilon_{\mathrm{new}}\) is predicted, its variance is the latent posterior variance plus \(\sigma^2\). Its covariance with a latent value at a different test input is unchanged, because the new measurement error is independent. Always identify which quantity is being predicted before reporting uncertainty.

Check Your Understanding

Use the kernel \(k(x,x')=1+xx'\) and the conditioning formulas to answer these questions. Show the matrix products and check the resulting covariance matrices.

  1. With training inputs \(-1,1\), test inputs \(0,2\), noise variance \(\tfrac12\), and observations \((0,0)^{\mathsf T}\), find the posterior mean vector and covariance matrix.
  2. For the same inputs and noise variance, calculate the observation covariance matrix when the measurement errors are independent with variance \(\tfrac12\). Why is it not equal to the training covariance matrix?
  3. Using observations \((1,2)^{\mathsf T}\) and noise variance \(1\), verify the posterior mean at \(0\) by substituting into \(BC^{-1}y\).
  4. Explain why the covariance between the two latent test values remains the same when the observed values change but the inputs, kernel, and noise variance are fixed.
  5. In the first worked example, what variance would be reported for a future noisy measurement at input \(0\), rather than for the latent value \(F(0)\)?