Gaussian Process Regression
An introduction to gaussian process regression.
In this post, I’m going to try and introduce, in a friendly manner, gaussian process regression (GPR). Gaussian process regression is a powerful all-purpose regression method. Some of its applications are in Brownian motion, which describes the motion of particle in a fluid, and in geostatistics, where we’re given an incomplete set of 2D points that’s supposed to span some space and GPR is used to estimate the gaps between our observations to fill in the rest of the terrain.
Some concepts you should know about are the multivariate Gaussian distribution and Bayesian regression. I’ll review them here, but it will only be brief.
The main mathematical structure behind GPR is the multivariate Gaussian distribution, which is a generalization of the Gaussian (Normal) distribution to multiple dimensions. Two important properties of multivariate Gaussians to know for GPR is the conditioning property and the additive property. I’ll first show the conditioning property. If we write our random vector as
where and , then
and .
The conditioning property says that the distribution of given (conditional on) is also multivariate Gaussian! It’s distribution is given by
,
The additive property says that if and then
These are a very intuitive results (although the formula for the conditional distribution is cumbersome) that will be very useful when we talk about GPR more.
The next concept that’s important is Bayesian linear regression. Without getting too involved in the details of Bayesian inference, some important points about it is that you can use your prior knowledge of the dataset to help your predictions and you get a distribution on the predicted value! This is in contrast to other methods which return a single value with no account of the possible uncertainty in that value (these other methods use Maximum Likelihood estimation, which normal linear regression does). Bayesian linear regression uses Maximum a Posteriori estimation for the full distribution of the output, called the posterior distribution. Figure 1 shows the difference between these two methods.
Figure 1: Bayesian and Classical (Frequentist) predictions on linear observations. Both are very similar, but one advantage of the Bayesian approach is that it gives the distribution of our prediction. In the figure, the shaded region represents 2 standard deviations above and below the prediction.
Gaussian Processes
Before we get to Gaussian Process regression, I’m going to introduce Gaussian processes (GPs).
Gaussian processes are formally defined a set of random variables , indexed by elements (normally time or space) from some index set , such that any finite subset of this set is multivariate Gaussian distributed.
These sets can be infinitely sized (think of the index variable - time or space - going on infinitely) and therefore we can think of Gaussian Processes as infinite dimensional extensions of the multivariate Gaussian distribution. But, in practice, we’ll always deal with finite sized sets and we can treat them just as we would multivariate Gaussians:
Thinking of Gaussian Processes as infinite dimensional extensions of multivariate Gaussians allows us see them as distributions over random functions. This distribution is specified by a mean function and a covariance function So another way to denote is as
Just for preciseness, must be a real function and must be a valid kernel function (which means being positive semidefinite) to have a Gaussian Process.
So, in the same way that we can sample a random vector from a multivariate Normal distribution, we can sample a random function from a Gaussian distribution. Note the difference here: a vector is finite-sized, but a function is a mapping with a potentially infinite size codomain. And just like the types of vectors that we sample from a multivariate Normal distribution are determined by its mean vector and covariance matrix, the types of functions we sample from a Gaussian Process are determined by the mean function and the covariance function
The covariance function is much more interesting than the mean function It determines the shape of the function we sample. In this post, I’ll only look at one-dimensional Gaussian Processes with a zero mean function, i.e. A non-zero mean function in the 1-dim case would just correspond to a vertical translation of the function. Below, we’ll see 5 different kernels, and in each of the figures I show a graph with just one function and another graph showing twenty functions sampled from the GP. This isn’t meant to be a thorough introduction to kernel function theory, but it’ll hopefully give you a better understanding of the different types of kernel out there.
Figure 2: Constant valued functions sampled from a Gaussian Process with a constant kernel . Notice how the numbers center around 0 and vary in both directions; this is exactly like sampling a number from Gaussian distribution , except here the numbers are constant functions!
Figure 3: Linear functions are sampled from a Gaussian Process with a linear kernel . Notice, in the right graph, that at each point the values center at 0 and vary in proportion to That is, at varies much more than at The distribution of at is distributed according to
Figure 4: Smooth functions are sampled from a Gaussian Process with a squared exponential kernel . These functions are infinitely differentiable at every point. In this figure, the characteristic length scale is set to 1, but changing would result in either more stable (with higher or more volatile (with smaller ) functions.
Figure 5: Symmetric functions about are sampled from a Gaussian Process with the kernel
Figure 6: Periodic functions are sampled from a Gaussian Process with the kernel
Each of these kernels has different hyperparameters associated with them that can effect the types of functions sampled. For example, in the periodic kernel, , in figure 7, the associated hyperparameter is Making larger would give functions with much higher frequencies of oscillation, while smaller values of would give functions with lower frequencies.
This isn’t by any means an exhaustive introduction to Gaussian Processes. But, hopefully this gives you a good intuitive understanding of what they are and some of the different things you can do with them.
Gaussian Process Regression
In a typical regression problem we are given a dataset , where is a noisy observation of the underlying function . It’s assumed that the noise is (this is important for the derivation).
The goal is to predict the values for future ’s. If we knew what the function was, then we wouldn’t need to do GP regression because we could just plug in our future ’s in and get our prediction. But, we don’t know So, the idea is to sample functions from a Gaussian Process.
In theory, we can sample an infinite number of functions and choose only the ones that fit our data. But, in practice, this is obviously intractable. So, if we write down our model
where and
then we’ll notice that both is multivariate Gaussian distributed (from the definition of Gaussian Processes) and is Gaussian distributed (by assumption), and therefore must also be multivariate Gaussian distributed from the additive property of multivariate Gaussians, i.e.
If we have our training dataset and we also have the test dataset for which we want to predict , then a reasonable assumption is that both and come from the same distribution, namely
.
where denotes training data and denotes test data. From the conditioning property of multivariate Gaussians, we can get the conditional distribution of given as
where
and
And that’s it. We can now get our estimate and our uncertainties
So, essentially Gaussian Process regression is using the conditioning property of multivariate Gaussians. Of course, we can also incorporate our prior knowledge of the data by specifying the mean function and the covariance function
Below is the segment of Python code that’s calculates the estimate and the covariance The algorithm I use is taken from Rasmussen et al, chapter 2, where instead of directly taking the inverse of the prediction kernel matrix, they calculate the Cholesky decomposition (i.e. the square root of the matrix).
n = len(X)
K = variance( X, k ) // returns variance of X defined by kernel function k
L = np.linalg.cholesky( K + noise*np.identity(n) )
# predictive mean
alpha = np.linalg.solve( np.transpose(L), np.linalg.solve( L, Y ) )
k_hat = covariance( X, x_hat, k ) // returns covariance of X and x_hat defined by kernel function k
mu_hat = np.matmul( np.transpose(k_hat), alpha )
# predictive variance
v = np.linalg.solve( L, k_hat )
k_hathat = variance( x_hat, k )
a = np.matmul( np.transpose(v), v )
covar_hat = k_hathat - np.matmul( np.transpose(v), v )
var_hat = covar_hat.diagonal()
We did it! Okay, well, at least the math is done. But, it’s always nice to see these types of things graphically. Figure 7 below shows Gaussian process regression with a squared exponential kernel in work. Specifically, the evolution of the posterior distribution as more observations are made. the evolution of posterior distribution on predictions as more observations are made.
Before any observations, the mean prediction is zero and shaded area is 2 standard deviations from the mean (1.96 in this case). After the first observation is made, the prediction curve changes to accomodate the new observation and the variance shrinks near the region at that point. Subsequent observations produce bigger changes and smaller uncertainties. After ten observations are made, we can already see a pretty nice curve and prediction, with low uncertainties near regions with some points.
Figure 7: These pictures shows how the posterior distribution of the prediction changes as more observations are made. The GP here uses the squared exponential kernel.
Below are some figures where I play around with Gaussian Process Regression with different types of observations and different kernels. Figure 8 shows linear observations predicted using a GP with a linear kernel. This can also be interpreted as just doing Bayesian linear regression and shows how Gaussian Process Regression is more general than BLR.
Figure 8: This plot shows GP regression with a linear kernel o observations corresponding to a noisy
In Figure 9, I model with injected noise. Here I show a couple different kernels I tried for this: the squared exponential kernel and the symmetric kernel. In my opinion, the difference in performance between the two isn’t significant, but, in my opinion the symmetric one does look a bit nice. Although, disclosure, I didn’t optimize any of the hyperparameters in this post.
Figure 9: Gaussian processes regression using the squared exponential kernel and the symmetric kernel. The observations are based on the function
Figure 10 shows GPR with the squared exponential kernel and the periodic kernel to model noisy observations of I find it neat that beyond the range of observations the periodic kernel is able to follow much better. Which makes sense, because the squared exponential kernel has no reason to continue with the periodic pattern beyond the range where it sees no data. So, in most problems, I don’t think this would be a huge deal breaker. It doesn’t seem to make sense to have to make predictions on data that isn’t closely related to your training set. So, the squared exponential kernel seems to the best all-purpose kernel for the types of data that I looked at.
Figure 10: Gaussian processes regression using the squared exponential kernel and the periodic kernel. The observations are based on the function
Conclusion
Gaussian Process regression is powerful all-purpose, general tool for Bayesian regression. Although I didn’t show it here, GPR in geostatistics (where it’s 2-dimensional) is particularly cool visually. It makes me think of GPR as a tool to fill in the blanks in between observations for continuous setting.
The time complexity of GPR is based on the implementation above (this comes from inverting the covariance matrix). This is fine for small datasets, but we start looking at datasets on the order of millions of events, then this becomes ugly. There are faster methods for doing GPR, but they are beyond the scope of this post (you can find a textbook on this stuff in the reference pages below).
Aside - Reflection: I think it was pretty cool to do this sort of introduction/explanation style post. My first try at something like this! If you see anything in this post that needs correcting, please let me know so I can fix it (contact info in About page). Thanks for reading this also! Hopefully, this stuff makes a little more sense to you :)
References
- Carl E. Rasmussen and Christopher K. I. Williams. Gaussian Processes for Machine Learning. MIT Press, 2006. Online: http://www.gaussianprocess.org/gpml/