Jekyll2020-01-09T02:13:17-08:00https://yugeten.github.io/feed.xmlYuge ShiDPhil student, University of OxfordYuge Shiyshi@robots.ox.ac.ukGaussian Process, not quite for dummies2019-09-05T00:00:00-07:002019-09-05T00:00:00-07:00https://yugeten.github.io/posts/2019/09/gp<h1 id="before-diving-in">Before diving in</h1>
<p>For a long time, I recall having this vague impression about Gaussian Processes (GPs) being able to magically define probability distributions over sets of functions, yet I procrastinated reading up about them for many many moons. However, as always, I’d like to think that this is not just due to my procrastination superpowers. Whenever I look up “Gaussian Process” on Google, I find these well-written tutorials with vivid plots that explain everything up until non-linear regression in detail, but shy away at the very first glimpse of any sort of information theory. The key takeaway is always,</p>
<blockquote>
<p>A Gaussian process is a probability distribution over possible functions that fit a set of points.</p>
</blockquote>
<p>While memorising this sentence does help if some random stranger comes up to you on the street and ask for a definition of Gaussian Process – which I’m sure happens all the time – it doesn’t get you much further beyond that. In what range does the algorithm search for “possible functions”? What gives it the capacity to model things on a continuous, infinite space?</p>
<p>Confused, I turned to the “the Book” in this area, <a href="http://www.gaussianprocess.org/gpml/chapters/RW.pdf"><em>Gaussian Processes for Machine Learning</em></a> by Carl Edward Rasmussen and Christopher K. I. Williams. I have friends working in more statistical areas who swear by this book, but after spending half an hour just to read 2 pages about linear regression I went straight into an existential crisis. I’m sure it’s a great book, but the math is quite out of my league.</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/92-98SYOdlY" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
<p>So what more is there? Thankfully I found the above lecture by Dr. Richard Turner on YouTube, which was a great introduction to GPs, and some of its state-of-the-art approaches. After watching this video, reading the <em>Gaussian Processes for Machine Learning</em> book became a lot easier. So I decided to compile some notes for the lecture, which can now hopefully help other people who are eager to more than just scratch the surface of GPs by reading some “machine learning for dummies” tutorial, but don’t quite have the claws to take on a textbook.</p>
<p><em><strong>Acknowledgement:</strong> the figures in this blog are from Dr. Richard Turner’s talk “Gaussian Processes: From the Basic to the State-of-the-Art”, which I highly recommend! Have a lookie here: <a href="http://cbl.eng.cam.ac.uk/pub/Public/Turner/News/imperial-gp-tutorial.pdf">Portal to slides</a>.</em></p>
<h1 id="motivation-non-linear-regression">Motivation: non-linear regression</h1>
<p>Of course, like almost everything in machine learning, we have to start from regression. Let’s revisit the problem: somebody comes to you with some data points (red points in image below), and we would like to make some prediction of the value of $y$ with a specific $x$.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60500034-8bcb1f00-9cb1-11e9-9028-c2982528a5f2.png" alt="drawing" width="300" /></p>
<p>In non-linear regression, we fit some nonlinear curves to observations. The higher degrees of polynomials you choose, the better it will fit the observations.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60500079-9be2fe80-9cb1-11e9-96ff-8f15bb902bd6.png" alt="drawing" width="300" /></p>
<p>This sort of traditional non-linear regression, however, typically gives you <strong>one</strong> function that it considers to fit these observations the best. But what about the other ones that are also pretty good? What if we observed one more points, and one of those ones end up being a much better fit than the “best” solution?</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60500111-aac9b100-9cb1-11e9-8e18-181598397e29.png" alt="drawing" width="300" /></p>
<p>To solve this problem, we turn to the good Ol’ Gaussians.</p>
<h1 id="the-world-of-gaussians">The world of Gaussians</h1>
<h2 id="recap">Recap</h2>
<p>Here we cover the basics of multivariate Gaussian distribution. If you’re already familiar with this, skip to the next section <em><strong>2D Gaussian Examples</strong></em>.</p>
<p>The Multivariate Gaussian distribution is also known as the joint normal distribution, and is the generalisation of the univariate Gaussian distribution to high dimensional spaces. Formally, the definition is:</p>
<blockquote>
<p>A random variable is said to be k-variate normally distributed if every linear combination of its k components have a univariate normal distribution.</p>
</blockquote>
<p>Mathematically, $X = (X_1, …X_k)^T$ has a multivariate Gaussian distribution if $Y=a_1X_1 + a_2X_2 … + a_kX_k$ is normally distributed for any constant vector ${a} \in \mathcal{R}^k$.</p>
<p><em><strong>Note</strong>: if all k components are independent Gaussian random variables, then $X$ must be multivariate Gaussian (because the sum of independent Gaussian random variables is always Gaussian).</em></p>
<p>_<strong>Another note</strong>: sum of random variables is different from sum of distribution – the sum of two Gaussian distributions gives you a Gaussian mixture, which is not Gaussian except in special cases. _</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60735059-d36bd800-9f49-11e9-870a-9c6958afc964.png" alt="drawing" width="500" /></p>
<h2 id="2d-gaussian-examples">2D Gaussian Examples</h2>
<h3 id="covariance-matrix">Covariance matrix</h3>
<p>Here is an example of a 2D Gaussian distribution with mean 0, with the oval contours denoting points of constant probability.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60500191-ccc33380-9cb1-11e9-8f80-5b695d896000.png" alt="drawing" width="350" /></p>
<p>The covariance matrix, denoted as $\Sigma$, tells us (1) the <strong>variance</strong> of each individual random variable (on diagonal entries) and (2) the <strong>covariance</strong> between the random variables (off diagonal entries). The covariance matrix in above image indicates that $y_1$ and $y_2$ are positively correlated (with $0.7$ covariance), therefore the somewhat “stretchy” shape of the contour. If we keep reducing the covariance while keeping the variance unchanged, the following transition can be observed:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60500266-f0867980-9cb1-11e9-8da6-f7f4fc5ab858.png" alt="drawing" width="220" /><img src="https://user-images.githubusercontent.com/18204038/60500289-faa87800-9cb1-11e9-84d0-50685c54a535.png" alt="drawing" width="220" /><img src="https://user-images.githubusercontent.com/18204038/60500307-03994980-9cb2-11e9-9efe-22cabecc0a27.png" alt="drawing" width="220" /></p>
<p>Note that when $y_1$ is independent from $y_2$ (rightmost plot above), the contours are spherical.</p>
<h3 id="conditioning">Conditioning</h3>
<p>With multivariate Gaussian, another fun thing we can do is conditioning. In 2D, we can demonstrate this graphically:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60500452-478c4e80-9cb2-11e9-8df8-763938a0eca2.png" alt="drawing" width="350" /></p>
<p>We fix the value of $y_1$ to compute the density of $y_2$ along the red line – thereby condition on $y_1$. Note that in here since $y_2 \in \mathcal{N}(\mu, \sigma)$ , by conditioning we get a Gaussian back.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60500477-4f4bf300-9cb2-11e9-9a90-7fc316f64468.png" alt="drawing" width="350" /></p>
<p>We can also visualise how this conditioned gaussian changes as the correlation drop – when correlation is $0$, $y_1$ tells you nothing about $y_2$, so for $y_2$ the mean drop to $0$ and the variance becomes high.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/68397329-eb0ed380-016a-11ea-9720-ae7ce342d3d4.gif" alt="drawing" width="350" /></p>
<p><</p>
<h1 id="high-dimensional-gaussian-a-new-interpretation">High dimensional gaussian: a new interpretation</h1>
<h2 id="2d-gaussian">2D Gaussian</h2>
<p>The oval contour graph of Gaussian, while providing information on the mean and covariance of our multivariate Gaussian distribution, does not really give us much intuition on how the random variables correlate with each other during the sampling process.</p>
<p>Therefore, consider this new interpretation that can be plotted as such:</p>
<p>Take the oval contour graph of the 2D Gaussian (left-top in below image) and choose a <strong>random point</strong> on the graph. Then, plot the value of $y_1$ and $y_2$ of that point on a new graph, at index = $1$ and $2$, respectively.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60428229-4e528d00-9bf0-11e9-8813-9931dd159fb8.png" alt="drawing" width="300" /></p>
<p>Under this setting, we can now visualise the sampling operation in a new way by taking multiple “<strong>random points</strong>” and plot $y_1$ and $y_2$ at index $1$ and $2$ multiple times. Because $y_1$ and $y_2$ are correlated ($0.9$ correlation), as we take multiple samples, the bar on the index graph only “wiggles” ever so slightly as the two endpoints move up and down together.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/68397330-eba76a00-016a-11ea-9950-d1dec3ee1285.gif" alt="drawing" width="300" /></p>
<p>For conditioning, we can simply fix one of the endpoint on the index graph (in below plots, fix $y_1$ to 1) and sample from $y_2$.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/68397339-ee09c400-016a-11ea-94c9-a3121f725deb.gif" alt="drawing" width="300" /></p>
<h2 id="higher-dimensional-gaussian">Higher dimensional Gaussian</h2>
<h3 id="5d-gaussian">5D Gaussian</h3>
<p>Now we can consider a higher dimension Gaussian, starting from 5D — so the covariance matrix is now 5x5.</p>
<p>Take a second to have a good look at the covariance matrix, and notice:</p>
<ol>
<li>All variances (diagonal) are equal to 1;</li>
<li>The further away the indices of two points are, the less correlated they are. For instance, correlation between $y_1$ and $y_2$ is quite high, $y_1$ and $y_3$ lower, $y_1$ and $y_4$ the lowest)</li>
</ol>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/68397343-f06c1e00-016a-11ea-8ad3-8203f940f495.gif" alt="drawing" width="300" /></p>
<p>We can again condition on $y_1$ and take samples for all the other points. Notice that $y_2$ is moving less compared to $y_3$ - $y_5$ because it is more correlated to $y_1$.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/68397344-f104b480-016a-11ea-84f3-32e7485b965e.gif" alt="drawing" width="300" /></p>
<h3 id="20d-gaussian">20D Gaussian</h3>
<p>To make things more intuitive, for 20D Gaussian we replace the numerical covariance matrix by a colour map, with warmer colors indicating higher correlation:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61445672-ba015d80-a945-11e9-95a8-e026a26f2856.png" alt="drawing" width="200" /></p>
<p>This gives us samples that look like this:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/68397345-f235e180-016a-11ea-9ebb-a81bb34d2033.gif" alt="drawing" width="300" /></p>
<p>Now look at what happens to the 20D Gaussian conditioned on $y_1$ and $y_2$:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/68397346-f2ce7800-016a-11ea-96d2-cce9ee1c8116.gif" alt="drawing" width="300" /></p>
<p>Hopefully you may now be thinking: “Ah, this is looking exactly like the nonlinear regression problem we started with!” And yes, indeed, this is exactly like a nonlinear regression problem where $y_1$ and $y_2$ are given as observations. Using this index plot with 20D Gaussian, we can now generate <strong>a family of curves</strong> that fits these observations. Even better, if we generate a number of them, we can compute the mean and variance of the fitting using these randomly generated curves. We visualise this in the plot below.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61446482-1f098300-a947-11e9-8658-18c0b6e4d50d.png" alt="drawing" width="400" /></p>
<p>We can see from the above image that because of how covariance matrix is structured (i.e. closer points have higher correlation), the points closer to the observations has very low uncertainty with non-zero mean, whereas the ones further from them have high uncertainty and zero mean. <em>(Note that in reality, we don’t have to actually take many many many samples to estimate the mean and standard deviation, they are completely analytical.)</em></p>
<p>Here we also offer a slightly more <em>exciting</em> example where we condition on 4 points of the 20D Gaussian (and you wonder why everybody hates statisticians):</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61447393-c2a76300-a948-11e9-98da-5843098f7523.png" alt="drawing" width="200" /></p>
<h2 id="getting-real">Getting “real”</h2>
<p>The problem with this approach for nonlinear regression seems obvious – it feels like all the points on the x-axis have to be integers because they are indices, while in reality, we want to model observations with real values. One immediately obvious solution for this is, we can keep increasing the dimensionality of the Gaussian and calculate many many points close to the observation, but that is a bit clumsy.</p>
<p>The solution lies in how the covariance matrix is generated. Conventionally, $\Sigma$ is calculated using the following 2-step process:</p>
<p><script type="math/tex">\Sigma (x_1, x_2) = K(x_1, x_2) + I \sigma_y^2</script>
<script type="math/tex">K(x_1, x_2) = \sigma^2 e^{-\frac{1}{2l^2}(x_1 - x_2)^2}</script></p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61448254-75c48c00-a94a-11e9-9150-79b03d54c694.png" alt="drawing" width="200" /></p>
<p>The covariance matrices in all the above examples are computed using the Radial Basis Function (RBF) kernel $K(x_1, x_2)$ – all by taking integer values for $x_1$, $x_2$. This RBF kernel ensures the <strong>“smoothness”</strong> of the covariance matrix, by generating a large output values for $x_1$, $x_2$ inputs that are closer to each other and smaller values for inputs that are further away . Note that if $x_1=x_2$, $K(x_1, x_2)=\sigma^2$. We then take K and add $I\sigma_y^2$ for the final covariance matrix to factor in noise – more on this later.</p>
<p>This means in principle, <strong>we can calculate this covariance matrix for any real-valued $x_1$ and $x_2$ by simply plugging them in</strong>. The real-valued $x$s effectively result in an infinite-dimensional Gaussian defined by the covariance matrix.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60430108-61fff280-9bf4-11e9-8ef4-a989734b859f.png" alt="drawing" width="400" /></p>
<p>Now that, is a <strong>Gaussian process</strong> (mic drop).</p>
<h1 id="gaussian-process">Gaussian Process</h1>
<h2 id="textbook-definition">Textbook definition</h2>
<p>From the above derivation, you can view Gaussian process as a generalisation of multivariate Gaussian distribution to infinitely many variables. Here we also provide the textbook definition of GP, in case you had to testify under oath:</p>
<blockquote>
<p>A Gaussian process is a collection of random variables, any finite number of which have consistent Gaussian distributions.</p>
</blockquote>
<p>Just like a Gaussian distribution is specified by its mean and variance, a Gaussian process is completely defined by (1) a mean function $m(x)$ telling you the mean at any point of the input space and (2) a covariance function $K(x, x’)$ that sets the covariance between points. The mean can be any value and the covariance matrix should be positive definite.</p>
<script type="math/tex; mode=display">f(x) \sim \mathcal{G}\mathcal{P}(m(x), K(x, x'))</script>
<h2 id="parametric-vs-non-parametric">Parametric vs. non-parametric</h2>
<p>Note that our Gaussian processes are non-paramatric, as opposed to nonlinear regression models which are parametric. And here’s a secret:</p>
<h3 align="center"> non-parametric model == model with infinite number of parameters </h3>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61577225-40e83e80-aadc-11e9-8b52-d001d25d7181.png" alt="drawing" width="200" /></p>
<p>In a parametric model, we define the function explicitly with some parameters:</p>
<script type="math/tex; mode=display">y(x) = f(x) + \epsilon \sigma_y</script>
<script type="math/tex; mode=display">p(\epsilon) = \mathcal{N}(0,1)</script>
<p>Where $\sigma_y$ is Gaussian noise describing how noisy the fit is to the actual observation (graphically it’ll represent how often the data lies directly on the fitted curve).
We can place a Gaussian process prior over the nonlinear function – meaning, we assume that the parametric function above is drawn from the Gaussian process defined as follow:</p>
<script type="math/tex; mode=display">p(f(x)\mid \theta) = \mathcal{G}\mathcal{P}(0, K(x, x'))</script>
<script type="math/tex; mode=display">K(x, x') = \sigma^2 \text{exp}(-\frac{1}{2l^2}(x-x')^2)</script>
<p>This GP will now generate lots of smooth/wiggly functions, and if you think your parametric function falls into this family of functions that GP generates, this is now a sensible way to perform non-linear regression.</p>
<p>We can also add Gaussian noise $\sigma_y$ directly to the model, since the sum of Gaussian variables is also a Gaussian:</p>
<script type="math/tex; mode=display">p(f(x)\mid \theta) = \mathcal{G}\mathcal{P}(0, K(x, x') + I\sigma_y^2)</script>
<p>In summary, GP regression is exactly the same as regression with parametric models, except you put a prior on the set of functions you’d like to consider for this dataset. The characteristic of this “set of functions” you consider is defined by the kernel of choice ($K(x, x’)$). Note that conventionally the prior has mean 0.</p>
<h2 id="hyperparameters">Hyperparameters</h2>
<p>There are 2 hyperparameters here:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60430623-a2ac3b80-9bf5-11e9-952d-7181bbec066a.png" alt="drawing" width="400" /></p>
<ul>
<li><strong>Vertical scale</strong> $\sigma$: describes how much span the function has vertically;</li>
<li><strong>Horizontal scale</strong> $l$: describes how quickly the correlation between two points drops as the distance between them increases – a high $l$ gives you a <em>smooth</em> function, while lower $l$ results in a <em>wiggly</em> function.</li>
</ul>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61697612-82245c80-ad2f-11e9-8540-cef3b0715617.png" alt="drawing" width="400" /></p>
<p>Luckily, because $p(y \mid \theta)$ is Gaussian, we can compute its likelihood in close form. That means we can just maximise the likelihood of $p(y\mid \theta)$ under these hyperparameters using a gradient optimiser:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61696910-1b527380-ad2e-11e9-8392-5a6bfe0bdfc0.png" alt="drawing" width="150" /></p>
<h1 id="details-for-implementation">Details for implementation</h1>
<p><em><strong>Before we start:</strong> here we are going to stay quite high level – no code will be shown, but you can easily find many implementations of GP on GitHub (personally I like <a href="https://github.com/dfm/gp/blob/master/worksheet.ipynb">this repo</a>, it’s a Jupyter Notebook walk through with step-by-step explanation). However, this part is important to understanding how GP actually works, so try not to skip it.</em></p>
<h2 id="computation">Computation</h2>
<p>Hopefully at this point you are wondering: this smooth function with infinite-dimensional covariance matrix thing all sounds well and good, but how do we actually do computation with an infinite by infinite matrix?</p>
<p><strong>Marginalisation baby!</strong> Imagine you have a multivariate Gaussian over two vector variables $y_1$ and $y_2$, where:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61701083-7b98e380-ad35-11e9-8012-5b86e1299cf5.png" alt="drawing" width="250" /></p>
<p>Here, we partition the mean into the mean of $y_1$, $a$ and the mean of $y_2$, $b$; similarly, for covariance matrix, we have $A$ as the covariance of $y_1$, $B$ that of $y_1$ and $y_2$, $B^T$ that of $y_2$ and $y_1$ and $C$ of $y_2$.
So now, we can easily compute the probability of $y_1$ using the marginalisation property:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61701492-2b6e5100-ad36-11e9-8ad8-7c2478b29d4d.png" alt="drawing" width="500" /></p>
<p>This formation is extremely powerful — it allows us to calculate the likelihood of $y_1$ under the joint distribution of $p(y_1, y_2)$, while completely ignoring $y_2$! We can now generalise from two variables to <strong>infinitely many</strong>, by altering our definition of $y_1$ and $y_2$ to:</p>
<ul>
<li>$y_1$: contains a finite number of variables we are interested in;</li>
<li>$y_2$: contains all the variables we don’t care about, which is infinitely many;
Then similar to the 2-variable case, we can compute the mean and covariance for $y_1$ partition only, without having to worry about the infinite stuff in $y_2$.
This nice little property allows us to think about finite dimensional projection of the underlying infinite object on our computer. We can forget about the infinite stuff happening under the hood.</li>
</ul>
<h2 id="predictions">Predictions</h2>
<p>Taking the above $y_1$, $y_2$ example, but this time imagine all the observations are in partition $y_2$ and all the points we want to make predictions about are in $y_1$ (again, the infinite points are still in the background, let’s imagine we’ve shoved them into some $y_3$ that is omitted here).</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61702038-09290300-ad37-11e9-911a-346d4e5a9727.png" alt="drawing" width="250" /></p>
<p>To make predictions about $y_1$ given observations of $y_2$, we can then use bayes rules to calculate $p(y_1\mid y_2)$:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61702480-c156ab80-ad37-11e9-94e7-8c7989fefd0a.png" alt="drawing" width="150" /></p>
<p>Because $p(y_1)$, $p(y_2)$ and $p(y_1,y_2)$ are all Gaussians, $p(y_1\mid y_2)$ is also Gaussian. We can therefore compute $p(y_1\mid y_2)$ analytically:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61703635-ea783b80-ad39-11e9-9606-54b5995df95c.png" alt="drawing" width="350" /></p>
<blockquote>
<p>Note: here we catch a glimpse of the bottleneck of GP: we can see that this analytical solution involves computing the inverse of the covariance matrix of our observation $C^{-1}$, which, given $n$ observations, is an $O(n^3)$ operation. This is why we use Cholesky decomposition – more on this later.</p>
</blockquote>
<p>To gain some more intuition on the method, we can write out the predictive mean and predictive covariance as such:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61703783-3e832000-ad3a-11e9-9139-3a478550e1be.png" alt="drawing" width="500" /></p>
<p>So the mean of $p(y_1 \mid y_2)$ is linearly related to $y_2$, and the predictive covariance is the prior uncertainty subtracted by the reduction in uncertainty after seeing the observations. Therefore, the more data we see, the more certain we are.</p>
<h2 id="higher-dimensional-data">Higher dimensional data</h2>
<p>You can also do this for higher-dimensional data (though of course at greater computational costs). Here we extend the covariance function to incorporate RBF kernels in 2D data:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60501013-32fc8600-9cb3-11e9-84db-39fd11a947f5.png" alt="drawing" width="500" /></p>
<h2 id="covariance-matrix-selection">Covariance matrix selection</h2>
<p>As one last detail, let’s talk about the different covariance matrices used for GP. I don’t have any authoritative advice on selecting kernels for GP in general, and I believe in practice, most people try a few popular kernels and pick the one that fits their data/problem the best. So here we will only introduce the form of some of the most frequently seen kernels, get a feel for them with some plots and not go into too much detail. (I highly recommend implementing some of them and play around with it though! It’s good coding practice and best way to gain intuitions about these kernels.)</p>
<h3 id="laplacian-function">Laplacian Function</h3>
<p>This function is continuous but non-differentiable. It looks like this:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61705455-adae4380-ad3d-11e9-848b-0f9c49f85b99.png" alt="drawing" width="400" /></p>
<p>If you average over all samples, you get straight lines joining your datapoints, which are called Browninan bridges.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60494726-3c7ff100-9ca7-11e9-864d-e41619f2bd5c.png" alt="drawing" width="150" /><img src="https://user-images.githubusercontent.com/18204038/60494726-3c7ff100-9ca7-11e9-864d-e41619f2bd5c.png" alt="drawing" width="150" /></p>
<h3 id="rational-quadratic">Rational quadratic</h3>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61705556-e0583c00-ad3d-11e9-90d7-177c1cdb5f66.png" alt="drawing" width="400" /></p>
<p>Average over all samples looks like this:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60499570-a18c1480-9cb0-11e9-99a4-87e02f195c9e.png" alt="drawing" width="150" /><img src="https://user-images.githubusercontent.com/18204038/60499556-9b963380-9cb0-11e9-9f1c-825484a88a74.png" alt="drawing" width="150" /></p>
<h3 id="periodic-functions">Periodic functions</h3>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61706968-55794080-ad41-11e9-81ee-21043a296a08.png" alt="drawing" width="400" /></p>
<p>Average over all samples looks like this:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/60494736-430e6880-9ca7-11e9-812b-b7944305d16f.png" alt="drawing" width="150" /><img src="https://user-images.githubusercontent.com/18204038/61706395-e9e2a380-ad3f-11e9-86dc-713f14db3bae.png" alt="drawing" width="150" /></p>
<h3 id="summary">Summary</h3>
<p>There are books that you can look up for appropriate kernels for covariance functions for your particular problem, and rules you can follow to produce more complicated covariance functions (such as, the product of two covariance functions is a valid covariance function). They can give you very different results:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61706573-565da280-ad40-11e9-848a-10f34fa1113b.png" alt="drawing" width="500" /></p>
<p>It is tricky to find the appropriate covariance function, but there are also methods in place for model selection. One of those methods is Bayesian model comparison, defined as follows:</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/61706770-d5eb7180-ad40-11e9-9235-b802afec85c4.png" alt="drawing" width="500" /></p>
<p>However, it does involve a very difficult integral (or sum in discrete case, as showed above) over the hyperparameters of your GP, which makes it impractical, and is also very sensitive to the prior you put over your hyperparameters. In practice, it is more common to use deep Gaussian Processes for automatic kernel design, which optimises the choice of covariance function that is appropriate for your data through training.</p>
<h1 id="the-end">The end</h1>
<p>Hopefully this has been a helpful guide to Gaussian process for you. I want to keep things relatively short and simple here, so I did not delve into the complications of using GPs in practice – in reality GPs suffers from not being able to scale to large datasets, and the choice of kernels can be very tricky. There are some state-of-the-art approaches that tackle with these issues (see <a href="https://arxiv.org/abs/1602.04133">deep GP</a> and <a href="https://arxiv.org/abs/1605.07066">sparse GP</a>), but since I am by no means an expert in this area I will leave you to exploring them.</p>
<p>Thank you for reading! Remember to take your canvas bag to the supermarket, baby whales are dying.</p>
<p align="center"><img src="https://user-images.githubusercontent.com/18204038/64785652-c03a4180-d564-11e9-9662-980fb7ecd522.png" alt="drawing" width="250" /></p>
<h1 id="acknowledgements">Acknowledgements</h1>
<p>I would like to thank Andrey Kurenkov and Hugh Zhang from <a href="https://thegradient.pub/">The Gradient</a> for helping me with the edits of this article.</p>Yuge Shiyshi@robots.ox.ac.ukBefore diving in For a long time, I recall having this vague impression about Gaussian Processes (GPs) being able to magically define probability distributions over sets of functions, yet I procrastinated reading up about them for many many moons. However, as always, I’d like to think that this is not just due to my procrastination superpowers. Whenever I look up “Gaussian Process” on Google, I find these well-written tutorials with vivid plots that explain everything up until non-linear regression in detail, but shy away at the very first glimpse of any sort of information theory. The key takeaway is always, A Gaussian process is a probability distribution over possible functions that fit a set of points.