Gaussian Processes II
Problem 2.1¶
Suppose we model the relationship of a real-valued response variable to a single real input, , using a Gaussian process model in which the mean is zero and the covariances of the observed responses are given by:
,
with the noise-free covariance function defined by:
,
Suppose we have four training cases, as follows:
| x | y |
|---|---|
| 0.5 | 2.0 |
| 2.8 | 3.3 |
| 1.6 | 3.0 |
| 3.9 | 2.7 |
Recall that the conditional mean of the response in a test case with input , given the responses in the training cases, is:
where:
is the vector of observed responses in training cases,
is the vector of responses in test cases,
is the matrix of covariances for the responses in training cases,
is the vector of covariances of the response in the test case with the responses in training cases.
a) Find the predictive mean for the response in a test case in which the input is . b) Find the predictive mean and variance of the response in a test case in which the input is by means of NumPy.
Problem 2.2¶
Recall that for a Gaussian process model, the predictive distribution for the response in a test case with inputs has mean and variance given by:
where:
is the vector of observed responses in training cases,
is the vector of responses in test cases,
is the matrix of covariances for the responses in training cases,
is the vector of covariances of the response in the test case with the responses in training cases,
is the prior variance of the response in the test case.
a) Suppose we have just one training case, with and . Suppose also that the noise-free covariance function is: and the variance of the noise is . Find the mean and variance of the predictive distribution for the response in a test case for which the value of the input is .
b) Repeat the calculations for (a), but using: What can you conclude from the result of this calculation?
Problem 2.3¶
Below are five functions randomly drawn from five different Gaussian processes. For all five Gaussian processes, the mean function is zero. The covariance functions are one of those listed below. For each of the five covariance functions below, indicate which of the five functions above is most likely to have been drawn from the Gaussian process with that covariance function.
(Note: The five functions a, b, c, d, e correspond to the plots in the document)
a) b) c) d) e)
Problem 2.4¶
Decide which of the given alternatives is correct.
a) Consider the RBF kernel. As we substantially decrease the length scale, keeping the data and other parameters fixed, the more “wiggly” the resulting curves will be. b) In a GP with an RBF kernel, the covariance of two points depends on their relative position, whereas in a GP with a linear kernel, the covariance depends also on their absolute location. c) Conditioning in Gaussian Processes corresponds to integrating over one of the dimensions. d) In conventional regression methods we typically allow for variation in both, the model class and coefficients, whereas in Gaussian Processes regression the model class is fixed.
Problem 2.5¶
We intend to model the house price dataset:
x = np.array([50,55,59,61,79,81,88,90,91,97,99,
105,107,110, 111, 112, 116, 117, 121, 123, 124, 125,
135,141, 142,144,145,149, 150,151])
y = np.array([0.36,0.37,0.28,0.29,0.3,
0.5,0.58,0.61,0.62,0.78,0.77,0.83,0.78,0.84,
0.91,0.95,1.05,0.99,0.97,0.93,0.81, 0.9,1.1,
0.98, 0.88, 1.05, 1.02, 1.1, 1.08, 1.12])a) Using the first two observations of the house price dataset, fit a Gaussian Process with a squared exponential kernel specified by , , and . b) Using the first five observations of the house price dataset, fit a Gaussian Process with a squared exponential kernel specified by , , and . c) Using all observations of the house price dataset, fit a Gaussian Process with a squared exponential kernel specified by , , and .
Problem 2.6¶
In this exercise, you will perform the basic steps of Gaussian Process regression. To this end, you will work with the Credit dataset that you can load with Pandas from credit_data.csv.
a) Data standardization: Standardize the response variable Balance and plot the data points of Balance vs. Limit. Describe the relationship you observe. Does a linear model seem appropriate? b) Choosing priors: Generate 10 samples from a zero-mean Gaussian process defined by a squared exponential kernel with , , and . c) Computing the posterior: Compute the posterior distribution of function values evaluated at each of the points in conditioned on Limit and Balance using the squared exponential kernel with , , and . Plot the prior and posterior distribution of function values. d) Log Marginal Likelihood: Compute the log marginal likelihood for several values of the length scale. Decompose it into three components: the constant term, the determinant term (model complexity), and the quadratic term (data fit). Plot each component separately and identify the length scale at which the log marginal likelihood attains its maximum. e) Mean Log Posterior Predictive Density: Split the dataset Credit into a validation data set (40%) and a training data set (60%). Compute the Mean Log Posterior Predictive Density (MLLPD) on the validation data set for several values of the length scale. Identify the length scale at which the MLLPD attains its maximum.
Short Solutions¶
Solution 2.1 a) The covariance matrix of the training responses is The inverse of this is . The vector of covariances of the test response with the training responses is . So . The predictive mean is .
b)
# Training data
x_train = np.array([0.5, 2.8, 1.6, 3.9])
y_train = np.array([2.0, 3.3, 3.0, 2.7])
# Test input
x_test = np.array([1.2])
def custom_covariance(x, x_prime):
diff = np.abs(x[:, None] - x_prime[None, :])
return np.where(diff < 1, 1 - diff, 0)
# Covariance matrices
K_train = custom_covariance(x_train, x_train)
noise = 0.5**2 * np.eye(len(x_train))
C_train = K_train + noise
k_star = custom_covariance(x_train, x_test)
k_star_star = custom_covariance(x_test, x_test)
C_train_inv = np.linalg.inv(C_train)
mu_pred = np.dot(k_star.T, np.dot(C_train_inv, y_train))
var_pred = k_star_star - np.dot(k_star.T, np.dot(C_train_inv, k_star))
print(f"Predictive mean: {mu_pred}")
print(f"Predictive variance: {var_pred}")
# Predictive mean at x*=1.2: [1.92]
# Predictive variance at x*=1.2: [[0.64]]Solution 2.2 a) The mean of the predictive distribution is The variance is
b) The mean is The variance is But variances cannot be negative! We can conclude that is not a valid covariance function.
Solution 2.3
(d)
(a)
(e)
(b)
(c)
Solution 2.4 a) True b) True c) False d) True
Solution 2.5 (Code solution snippet provided in the text defines kernel functions and plotting. The specific plots for (a), (b), and (c) correspond to fitting with 2 points, 5 points, and all points respectively.)
Solution 2.6
a) The scatter plot shows a positive linear relationship. However, only approximately 75% of the variance is explained by a linear model. A GP is appropriate.
b) - c) Code provided defines create_se_kernel, posterior, and plot_with_uncertainty functions.
d) The log_marginal_likelihood function is implemented to return const_term + det_term + quad_term. The optimal scale value found is around 2154.
e) The MLPPD function calculates the mean log posterior predictive density. The optimal scale value is 2154.4, with a log value of roughly 7.68.