# A Visual Exploration of Gaussian Processes

How to turn a collection of small building blocks into a versatile tool for solving regression problems.

## Introduction

Even if you have spent some time reading about machine learning, chances are that you have never heard of Gaussian processes. And if you have, rehearsing the basics is always a good way to refresh your memory. With this blog post we want to give an introduction to Gaussian processes and make the mathematical intuition behind them more approachable.

Gaussian processes are a powerful tool in the machine learning toolbox . They allow us to make predictions about our data by incorporating prior knowledge. Their most obvious area of application are regression problems, for example in robotics, but they can also be extended to classification and clustering tasks . Just a quick recap: the goal of regression is to find a function that describes a given set of data points as closely as possible. This process is called fitting a function to the data. For a given set of training points, there are potentially infinitely many functions that fit the data. Gaussian processes offer an elegant solution to this problem by assigning a probability to each of these functions. The mean of this probability distribution then represents the most probable characterization of the data. Furthermore, using a probabilistic approach allows us to incorporate the confidence of the prediction into the regression result.

We will first explore the mathematical foundation they are built on, and you can follow along using interactive figures and hands-on examples. They help to explain the impact of individual components, and show the flexibility of Gaussian processes. After following this article we hope that you will have a visual intuition on how Gaussian processes work and how you can configure them for different types of data.

## Multivariate Gaussian distributions

Before we can explore Gaussian processes, we need to understand the mathematical concepts they are based on. As the name suggests, the Gaussian distribution (which is often also referred to as normal distribution) is the basic building block of Gaussian processes. In particular, we are interested in the multivariate case of this distribution, where each random variable is distributed normally and their joint distribution is also Gaussian. In general, the multivariate Gaussian distribution is defined by a mean vector \mu and a covariance matrix \Sigma .

The mean vector \mu describes the expected value of the distribution. Each of its components describes the mean of the corresponding dimension. \Sigma models the variance along each dimension and determines how the different random variables are correlated. The covariance matrix is always symmetric and positive semi-definite. The diagonal of \Sigma consists of the standard deviation \sigma_i of the i-th random variable, whereas the off-diagonal elements describe correlations between each of the components \sigma_{ij}.

X = \begin{bmatrix} X_1 \\ X_2 \\ \vdots \\ X_n \end{bmatrix} \sim \mathcal{N}(\mu, \Sigma)

We say X follows a normal distribution. The covariance matrix \Sigma describes the shape of the distribution. It is defined in terms of the expected value E:

\Sigma = \text{Cov}(X_i, X_j) = E \left[ (X_i - \mu_i)(X_j - \mu_j)^T \right]

Visually, the distribution is centered arround the mean and the covariance matrix defines its shape. The following figure shows the influence of these parameters on a two-dimensional Gaussian distribution. The standard deviations for each random variable are on the diagonal of the covariance matrix, while the other values show the covariance between them.

Gaussian distributions are widely used to model the real world: either as a surrogate when the original distributions are unknown, or in the context of the central limit theoremOne of the implications of this theorem is that a collection of independent, identically distributed random variables with finite variance are together distributed normally. A good introduction to the central limit theorem is given by this video from Khan Academy. . In the next section we will take a closer look at how to manipulate Gaussian distributions and extract useful information from them.

### Marginalization and Conditioning

Gaussian distributions have the nice algebraic property of being closed under conditioning and marginalization. This means that the resulting distributions from these operations are also Gaussian, which makes many problems in statistics and machine learning tractable. In the following we will take a closer look at both of these operations, as they are the foundation for Gaussian processes.

Marginalization and conditioning both work on subsets of the original distribution and we will use the following notation:

\begin{bmatrix} X \\ Y \end{bmatrix} \sim \mathcal{N}(\mu, \Sigma) = \mathcal{N} \left( \begin{bmatrix} \mu_X \\ \mu_Y \end{bmatrix}, \begin{bmatrix} \Sigma_{XX} \, \Sigma_{XY} \\ \Sigma_{YX} \, \Sigma_{YY} \end{bmatrix} \right)

With X and Y representing subsets of original random variables.

Through marginalization we can extract partial information from multivariate probability distributions. In particular, given a normal probability distribution P(X,Y) over vectors of random variables X, and Y, we can determine their marginalized probability distributions in the following way:

\begin{aligned} X &\sim \mathcal{N}(\mu_X, \Sigma_{XX}) \\ Y &\sim \mathcal{N}(\mu_Y, \Sigma_{YY}) \end{aligned}

The interpretation of this equation is straight forward: each partition X and Y only depends on its corresponding entries in \mu and \Sigma. To marginalize out a random variable from a Gaussian distribution we can simply drop the variables from \mu and \Sigma.

p_X(x) = \int_y p_{X,Y}(x,y)dy = \int_y p_{X|Y}(x|y) p_Y(y) dy

The way to interpret this equation is that if we are interested in the probability of X = x, we need to consider all possible outcomes of Y that can jointly lead to the result The corresponding Wikipedia article has a good description of the marginal distribution, including several examples. .

Another important operation for Gaussian processes is conditioning. It is used to determine the probability of one variable depending on another variable. Similar to marginalization, this operation is also closed and yields a modified Gaussian distribution. This operation is the cornerstone of Gaussian processes since it allows Bayesian inference, which we will talk about in the next section. Conditioning is defined by:

\begin{aligned} X|Y &\sim \mathcal{N}(\:\mu_X + \Sigma_{XY}\Sigma_{YY}^{-1}(Y - \mu_Y),\: \Sigma_{XX}-\Sigma_{XY}\Sigma_{YY}^{-1}\Sigma_{YX}\:) \\ Y|X &\sim \mathcal{N}(\:\mu_Y + \Sigma_{YX}\Sigma_{XX}^{-1}(X - \mu_X),\: \Sigma_{YY}-\Sigma_{YX}\Sigma_{XX}^{-1}\Sigma_{XY}\:) \\ \end{aligned}

Note that the new mean only depends on the conditioned variable, while the covariance matrix is independent from this variable.

Now that we have worked through the necessary equations, we will think about how we can understand the two operations visually. While marginalization and conditioning can be applied to multivariate distributions of many dimensions, it makes sense to consider the two-dimensional case as shown in the following figure. Marginalization can be seen as integrating along one of the dimensions of the Gaussian distribution, which is in line with the general definition of the marginal distribution. Conditioning also has a nice geometric interpretation — we can imagine it as making a cut through the multivariate distribution, yielding a new Gaussian distribution with fewer dimensions.

## Gaussian Processes

Now that we have recalled some of the basic properties of multivariate Gaussian distributions, we will combine them together to define Gaussian processes, and show how they can be used to tackle regression problems.

First, we will move from the continuous view to the discrete representation of a function: rather than finding an implicit function, we are interested in predicting the function values at concrete points, which we call test points X. Respectively, we will denote the training data as Y. Now, the key idea behind Gaussian processes is that all function values stem from a multivariate Gaussian distribution. That means that the joint probability distribution P(X,Y) spans the space of possible function values for the function that we want to predict. And this joint distribution of test and training data has |X| + |Y| dimensions.

In order to perform regression on the training data, we will treat this problem as Bayesian inference. The essential idea of Bayesian inference is to update the current hypothesis as new information becomes available. In the case of Gaussian processes, this information is the training data. Thus, we are interested in the conditional probability P(X|Y). Finally, we recall that Gaussian distributions are closed under conditioning — so P(X|Y) is also distributed normally.

Now that we have the basic framework of Gaussian processes together, there is only one thing missing: how do we set up this distribution and define the mean \mu and the covariance matrix \Sigma? For this we will use what is called a kernel k. We will talk about this in detail in the next section. But before we come to this, let us reflect on how we can use multivariate Gaussian distributions to estimate function values. The following figure shows an example of this using ten test points at which we want to predict our function:

In Gaussian processes we treat each test point as a random variable. A multivariate Gaussian distribution has the same number of dimensions as the number of random variables. Since we want to predict the function values at |X|=N test points, the corresponding multivariate Gaussian distribution is also N -dimensional. Making a prediction using a Gaussian process ultimately boils down to drawing samples from this distribution. We then interpret the i-th component of the resulting vector as the function value corresponding to the i-th test point.

### Kernels

Recall that in order to set up our distribution, we need to define \mu and \Sigma. In Gaussian processes it is often assumed that \mu = 0, which simplifies the necessary equations for conditioning. We can always assume such a distribution, even if \mu \neq 0, and add \mu back to the resulting function values after the prediction step. So configuring \mu is straight forward — it gets more interesting when we look at the other parameter of the distribution.

The clever step of Gaussian processes is how we set up the covariance matrix \Sigma. The covariance matrix will not only describe the shape of our distribution, but ultimately determines the characteristics of the function that we want to predict. We generate the covariance matrix by evaluating the kernel k, which is often also called covariance function, pairwise on all the points. The kernel receives two points t,t' \in \mathbb{R}^n as an input and returns a similarity measure between those points in the form of a scalar:

k: \mathbb{R}^n \times \mathbb{R}^n \rightarrow \mathbb{R},\quad \Sigma = \text{Cov}(X,X') = k(t,t')

We evaluate this function for each pairwise combination of the test points to retrieve the covariance matrix. This step is also depicted in the figure above. In order to get a better intution for the role of the kernel, let's think about what the entries in the covariance matrix describe. The entry \Sigma_{ij} describes how much influence the i-th and j-th point have on each other. This follows from the definition of the multivariate Gaussian distribution, which states that \Sigma_{ij} defines the correlation between the i-th and the j-th random variable. Since the kernel describes the similarity between the values of our function, it controls the possible shape that a fitted function can adopt. Note that when we choose a kernel, we need to make sure that the resulting matrix adheres to the properties of a covariance matrix.

Kernels are widely used in machine learning, for example in support vector machines. The reason for this is that they allow similarity measures that go far beyond the standard euclidean distance (L2-distance). Many of these kernels conceptually embed the input points into a higher dimensional space in which they then measure the similarityIf the kernel follows Mercer's theorem it can be used to define a Hilbert space. More information on this can be found on Wikipedia.. The following figure shows examples of some common kernels for Gaussian processes. For each kernel, the covariance matrix has been created from N=25 linear spaced values ranging from [-5,5]. Each entry in the matrix shows the covariance between points in the range of [0,1].

Kernels can be separated into stationary and non-stationary kernels. Stationary kernels, such as the RBF or the periodic kernel, are functions invariant to translations, and the covariance of two points is only dependent on their relative position. Non-stationary kernels, such as the linear kernel, do not have this constraint and depend on an absolute location. The stationary nature of the RBF kernel can be observed in the banding around the diagonal of its covariance matrix (as shown in this figure). Increasing the length parameter increases the banding, as points further away from each other become more correlated. For the periodic kernel, we have an additional parameter P that determines the periodicity, which controls the distance between each repetition of the function. In contrast, the parameter C of the linear kernel allows us to change the point on which all functions hinge.

There are many more kernels that can describe different classes of functions, which can be used to model the desired shape of the function. A good overview of different kernels is given by Duvenaud. It is also possible to combine several kernels — but we will get to this later.

### Prior Distribution

We will now shift our focus back to the original task of regression. As we have mentioned earlier, Gaussian processes define a probability distribution over possible functions. Because this distribution is a multivariate Gaussian distribution, the distribution of functions is normal. Recall that we usually assume \mu=0. For now, let's consider the case where we have not yet observed any training data. In the context of Bayesian inference, this is called the prior distribution P(X).

If we have not yet observed any training examples, this distribution revolves around \mu=0, according to our original assumption. The prior distribution will have the same dimensionality as the number of test points N = |X|. We will use the kernel to set up the covariance matrix, which has the dimensions N \times N.

In the previous section we have looked at examples of different kernels. Since the kernel is used to define the entries of the covariance matrix, it also determines which type of functions from the space of all possible functions are more probable. As the prior distribution does not yet contain any additional information, it is perfect to visualize the influence of the kernel on the distribution of functions. The following figure shows samples of potential functions from prior distributions that were created using different kernels:

Adjusting the parameters allows you to control the shape of the resulting functions. This also varies the confidence of the prediction. When decreasing the variance \sigma, a common parameter for all kernels, sampled functions are more concentrated around the mean \mu. For the Linear kernel, setting the variance \sigma_b=0 results in a set of functions constrained to perfectly intersect the offset point c. If we set \sigma_b=0.2 we can model uncertainty, resulting in functions that pass close to c.

### Posterior Distribution

So what happens if we observe training data? Let's get back to the model of Bayesian inference, which states that we can incorporate this additional information into our model, yielding the posterior distribution P(X|Y). We will now take a closer look at how to do this for Gaussian processes.

First, we form the joint distribution P(X,Y) between the test points X and the training points Y. The result is a multivariate Gaussian distribution with dimensions |Y| + |X|. As you can see in the figure below, we concatenate the training and the test points to compute the corresponding covariance matrix.

For the next step we need one operation on Gaussian distributions that we have defined earlier. Using conditioning we can find P(X|Y) from P(X,Y). The dimensions of this new distribution matches the number of test points N and the distribution is also normal. It is important to note, that conditioning leads to derived versions of the mean and the standard deviation: X|Y \sim \mathcal{N}(\mu', \Sigma'). More details can be found in the related section on conditioning multivariate Gaussian distributions. The intuition behind this step is that the training points constrain the set of functions to those that pass through the training points.

Analogous to the prior distribution, we could obtain a prediction by sampling from this distribution. But, since sampling involves randomness, we would have no guarantee that the result is a good fit to the data. In order to make a better prediction we can use the other basic operation of Gaussian distributions.

Through marginalization of each random variable, we can extract the respective mean function value \mu'_i and standard deviation \sigma'_i = \Sigma'_{ii} for i-th test point. In contrast to the prior distribution, we set the mean to \mu=0, so in this context it is not really of importance. But when we condition the joint distribution of the test and training data the resulting distribution will most likely have a non-zero mean \mu' \neq 0. Extracting \mu' and \sigma' does not only lead to a more meaningful prediction, it also makes a statement about the confidence of he prediction.

The following figure shows an example of the conditional distribution. At first, no training points have been observed. Accordingly, the mean prediction remains at 0 and the standard deviation is the same for each test point. By hovering over the covariance matrix you can see the influence of each point on the current test point. As long as no training points have been observed, the influence of neighboring points is limited locally.

The training points can be activated by clicking on them, which leads to a constrained distribution. This change is reflected in the entries of the covariance matrix, and leads to an adjustment of the mean and the standard deviation of the predicted function. As we would expect, the uncertainty of the prediction is small in regions close to the training data and grows as we move further away from those points.

In the constrained covariance matrix, we can see that the correlation of neighbouring points is affected by the training data. If a predicted point lies on the training data, there is no correlation with other points. Therefore, the function must pass directly through it. Predicted values further away are also affected by the training data — proportional to their distance.

### Combining different kernels

As described earlier, the power of Gaussian processes lies in the choice of the kernel function. This property allows an expert to introduce domain knowledge into the process and lends Gaussian processes their flexibility to capture trends in the training data. For example, by choosing a suitable bandwidth for the RBF kernel, an expert can control how smooth the resulting function will be.

A big benefit that kernels provide is that they can be combined together, resulting in a more specialized kernel. This gives domain experts the ability to include further information, leading to a more accurate prediction. The usual way to combine kernels is to multiply them with each other. Let's consider two kernels, an RBF kernel k_{\text{rbf}} and a periodic kernel k_{\text{per}}, for example. This is how we would combine the two:

k^{\ast}(t,t') = k_{\text{rbf}}(t,t') \cdot k_{\text{per}}(t,t')

In the figure below, the original training data has an ascending trend with a periodic deviation. Using only a linear kernel, it is possible to achieve a normal linear regression of the points. At first sight the RBF kernel accurately approximates the points. But since the RBF kernel is stationary it will always return to \mu=0 in regions further away from observed training data. This decreases the accuracy for predictions that reach further into the past or the future. Only by using a combination of kernels, both the periodic nature and the non-zero mean trend of the data is maintained. This procedure can be used, for example, in the analysis of weather data.

## Conclusion

With this article, you should have obtained an overview of Gaussian processes, and developed a deeper understanding on how they work. As we have seen, Gaussian processes offer a flexible framework for regression and several extensions exist that make them even more versatile. If we need to deal with real-world data, we will often find measurements that are afflicted with uncertainty and errors. Using Gaussian processes, we can define a kernel function that fits our data and add uncertainty to the prediction. For example, one particular extension to Gaussian processes from McHutchon et al. , adapts Gaussian processes to be compatible to such noisy inputs.

Even though we mostly talk about Gaussian processes in the context of regression, they can be adapted for different purposes, e.g. model-peeling and hypothesis testing. By comparing different kernels on the dataset, domain experts can introduce additional knowledge through appropriate combination and parameterization of the kernel. As this might not be possible in many cases, learning specialized kernel functions from the underlying data using deep learning is an area of ongoing research. Furthermore, links between Bayesian inference, Gaussian processes and deep learning have been described in several papers.

If we have sparked your interest, we have compiled a list of further blog posts on the topic of Gaussian processes. In addition, we have linked two Python notebooks that will give you some hands-on experience and help you to get started right away.

### Acknowledgments

We are very grateful to Carla Avolio and Marc Spicker for their feedback on the manuscript. In addition, we want to thank Jonas Körner for helping with the implementation of the figure explaining the multivariate Gaussian distribution. Furthermore, we would like to thank the German Research Foundation (DFG) for financial support within project A01 of the SFB-TRR 161 and within the Research Unit FOR 2111 with grant number DFG-431/16.