Regression
Gaussian Process Regression, GPR
2023-12-15
Gaussian process regression is a nonparametric regression method that can model nonlinear relationships. Rather than estimating a fixed set of model coefficients, it uses Bayesian inference based on the joint distribution of the training data and predictions.
The predicted value f* for x* can be calculated as the linear combination of all measurement values y using the weights w.
\(f*=\sum _{\ }^{\ }w_i\left(x*,X\right)y_i\)
When the weight w is set as a function of the given x* and the training data X, following a certain statistical distribution that is inversely proportional to the distance between them (for example, a statistical distance in a Gaussian distribution), this is called Gaussian Process Regression (GPR). In order to calculate the weights, all the training data must always be available, making GPR a method of Lazy Learner.
The following is a graph representing the Gaussian distribution \(N(y) = \exp(-y^T\Sigma^{-1}y/2)\), where y=(y1,y2) is a two-dimensional vector and the matrix \(\Sigma =\begin{bmatrix} 1 & 0.7 \\ 0.7 & 1\end{bmatrix}\) is the covariance matrix.

Any intersects at random y1 (or y2) have the normal distribution, but their mean and variance depend on the covariance matrix. For example, the following figure represents the distribution of y2 on a y1=1 intersect, where y2 follows a normal distribution of p(y2|y1) ~ N(0.7,0.71). In other words, a regression equation passing through y = (1, 0.7) can be represented as a Gaussian distribution using the covariance matrix mentioned above. Alternatively, the GPR created from the training data of y1 can be used to predict the test data of y2.

When we extend this concept, if we create a covariance matrix of dimensions [nxn] corresponding to the training data (y1 ... yn), we can think of creating a regression equation that passes through all the given training data, and this is the fundamental concept of GPR.
The method for performing GPR is as follows.
First, introduce a kernel function to calculate the covariance matrix K(X,X) for the training dataset.
The most commonly used kernel function is the Square Exponential (or Radial Based function), which is defined as follows.
\(K(x,x^*)=\exp -\dfrac{(x-x^*)^2}{2L^2}\)
Here, L represents the scale of distance and indicates the fluctuations in the regression equation. A larger value indicates a flatter regression line, and as the distance between two data points x and x* becomes closer, it approaches 1, while it approaches 0 as the distance increases.
Let's consider the prior distribution of the measured value, f, for the training dataset (X), f ~ N(0, K(X, X)).
\(p(f)\sim \exp (-\frac{1}{2}f^TK^{-1}f)\)
The prior distribution of the test dataset (X*) is denoted as f ~ N(0, K(X*,X*)). Our goal is to calculate the posterior probability of f* after f is observed, which can be obtained using the following Bayesian theorem.
\(p(f,f^*)=p(f^*|f)p(f)\)
which also produces the following:
\(p(f^*|f)=p(f,f^*)/p(f)\)
The p(f, f*) is the joint distribution of f and f* presented as follows:
\(\begin{bmatrix}f\\f^*\end{bmatrix}\sim N\left(0, \begin{bmatrix} K(X,X)&K(X,X^*)\\K(X^*,X)&K(X^*,X^*)\end{bmatrix}\right)\)
First, let's find the inverse of the covariance matrix which we will use to formulate joint Gaussian distribution later.
\(\begin{bmatrix}K(X,X)&K(X,X^*)\\K(X^*,X)&K(X^*,X^*)\end{bmatrix}^{-1}\equiv \begin{bmatrix}K&k\\k&d\end{bmatrix}^{-1}=\begin{bmatrix}A&b\\b^T&c\end{bmatrix}\)
In order to find the unknown b and c above, use the following tricks
\(\begin{bmatrix}A&b\\b^T&c\end{bmatrix}\begin{bmatrix}K&k\\k&d\end{bmatrix}=\begin{bmatrix}I&0\\0&I\end{bmatrix}\)
Expanding the matrix multiplication
\(\begin{bmatrix}AK+bk^T&Ak+bd\\b^TK+ck^T&b^Tk+cd\end{bmatrix}=\begin{bmatrix}I&0\\0&I\end{bmatrix}\)
Comparing both sides,
\(b^TK+ck^T=0\text{ [1xn][nxn]+[1xn]=[1xn]}\)
\(b^Tk+cd=1\text{ [1xn][nx1]+[1x1]=[1x1]}\)
Finally, we can get b and c as follows:
\(c=\dfrac{1-b^Tk}{d}\)
\(b^TK+\dfrac{(1-b^Tk)k^T}{d}=0\rightarrow b^TKd+k^T-b^Tkk^T=0\rightarrow b^T(Kd-kIk^T(=-k^T))\rightarrow b^T=\dfrac{-k^TK^{-1}}{d-k^TK^{-1}k}\)
\(c=\dfrac{1}{d}\left\{1-\dfrac{-k^TK^{-1}}{d-k^TK^{-1}k}k\right\}=\frac{1}{d}\dfrac{d-k^TK^{-1}k+k^TK^{-1}k}{d-k^TK^{-1}k}=\dfrac{1}{d-k^TK^{-1}k}\)
\(\rightarrow c=\dfrac{1}{d-k^TK^{-1}k}\)
Finally, the joint distribution is simplified as follows:
\(p(f,f^*)\sim \exp \left(-\dfrac{1}{2}\begin{bmatrix}f\\f^*\end{bmatrix}^T\begin{bmatrix}A&b\\b^T&c\end{bmatrix}\begin{bmatrix}f\\f^*\end{bmatrix}\right)\)
\(=\exp \begin{pmatrix}-\dfrac{1}{2}[f^TAf+b^Tff^*+f^Tbf^*+cf^{*2}]\end{pmatrix}=\exp \begin{pmatrix}-\dfrac{1}{2}[f^TAf+2b^Tff^*+cf^{*2}]\end{pmatrix}\)
The condition probability p(f*|f) is now calculated from the Bayesian theorem p(f*|f)=p(f,f*)/p(f) aforementioned,
\(p(f^*|f)=p(f,f^*)/p(f)\sim \exp \begin{pmatrix}-\dfrac{1}{2}[f^TAf+2b^Tff^*+cf^{*2}-f^TK^{-1}f]\end{pmatrix}\)
\(=\exp \left(\ -\dfrac{1}{2c^{-1}}\begin{bmatrix}f^{*2}+\dfrac{2b^Tf}{c}f^*+\left(\dfrac{b^Tf}{c}\right)^T\left(\dfrac{b^Tf}{c}\right)+f^T\left(A-K^{-1}-\dfrac{bb^T}{c^2}\right)f\end{bmatrix}\right)\)
\(\sim \exp \left(-\dfrac{1}{2c^{-1}}\begin{bmatrix}f^*+\dfrac{b^Tf}{c}\end{bmatrix}^2\right)+const=N(f^*|-\dfrac{b^Tf}{c},c^{-1})\)
At the ends, the p(f*|f) becomes the normal distribution with the mean = -bTf/c and the variance = c-1 .
By substituting b and c into the distribution formula, we get the final expression.
\(p(f^*|f)\sim N(k^TK^{-1}f,d-k^TK^{-1}k)=N(K(X^*,X)K(X,X)^{-1}f,K(X^*,X^*)-K(X^*,X)K(X,X)^{-1}K(X,X^*))\)
The inference f* for the new data x* using GPR has the normal distribution with the following mean and variance:
Mean: \(K(x^*,X)K(X,X)^{-1}\)
Variance: \(K(x^*,x^*)-K(x^*,X)K(X,X)^{-1}K(X,x^*)\)
The variance of the posterior probability is always smaller than the variance of the prior probability (since the second term is positive), so the result is always improved.
The average value of each _f is equal to y, so it can be expressed in the following:
\(f*=w\left(x*,X\right)y=\sum _{\ }^{\ }w_iy_i\)
In other words, the predicted value for a given x* can be represented as a weight for all training data, and to calculate the weight, kernel calculations with all training data are required.
Looking at the size of the matrices, K(X, X) becomes an [nxn] matrix for n training data, K(x*,X) = [1xn], K(X,x*) = [nx1], K(x*,x*)= [1x1], f = [nx1], so f* = [1xn][nxn][nx1] = [1x1].
For the illustration purpose, we use the following three pairs of single variable (x,y) dataset (-5, -6), (2,1), (5,4) to perform GPR at Excel worksheet.
Step 0. Ready to data and set the Length Scale

Step 1. Calculate the Gaussian Kernel Σ among all the training X and calculate the inverse of the Kernel matrix.
Σ =EXP(-SUMXMY2(INDEX($A$2:$A$4,E$1,1),INDEX($A$2:$A$4,$D2,1))/2/$C$2^2)
Σ-1=MINVERSE(Σ)

Step 2. Obtain the Gaussial Kernel K(X,x*) for the new data, x*, toward all the training data, X.
K(x1,x*) =EXP(-SUMXMY2($A7,$A$2)/2/$C$2/$C$2)
K(x2,x*) =EXP(-SUMXMY2($A7,$A$3)/2/$C$2/$C$2)
K(x3,x*) =EXP(-SUMXMY2($A7,$A$4)/2/$C$2/$C$2)
K(x*,x*) = 1 ≡Kx*x*

Step 3. Compute the mean of the probabilistic distribution of y*(x*), y*|y
y*|y = MMULT(Kxx*,MMULT(Σ-1,y))
Step 4. Calculate the variance of the distribution of y*, σ2
σ2=Kx*x*-MMULT(Kxx*,MMULT(Σ-1,TRANSPOSE(Kxx*)))

The plots below show the GPR results for the two cases of L=2 and L=5. The red line is the mean of y* and the blue dotted lines are the y*+/- σ confidence interval.

