-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGP_regressors.py
326 lines (250 loc) · 8.96 KB
/
GP_regressors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import numpy as np
"""
Different classes are created for each regression method.
Here we consider the gaussian process regression using the exponential kernel and
Hilbert Space approximations of the Kernel.
"""
class Gaussian_kernel_GPR():
"""
Class for a GP model
"""
def __init__(self, data, sigma2, alpha=1, scale=1):
"""
Initialize your GP class.
Parameters
----------
data : Tuple of regression data input and observation e.g. (x, y).
sigma2 : Float of likelihood variance.
length_scale: Float for kernel lengthscale.
variance: Float for kernel variance.
"""
self.data = data
self.sigma2 = sigma2
self.alpha = alpha
self.scale = scale
def gaussian_kernel(self, X1, X2):
"""
returns the NxM kernel matrix between the two sets of input X1 and X2
arguments:
X1 -- NxD matrix
X2 -- MxD matrix
alpha -- scalar
scale -- scalar
returns NxM matrix
"""
alpha = self.alpha
scale = self.scale
d2 = np.sum((X1[:,None,:]-X2[None,:,:])**2, axis=-1)
K = alpha*np.exp(-0.5*d2/scale**2)
return K
def posterior(self, Xp):
"""
returns the posterior distribution of f evaluated at each of the points in Xp conditioned on (X, y)
using the squared exponential kernel.
"""
kernel = self.gaussian_kernel
x_train, y_train = self.data
sigma2 = self.sigma2
Ksf = kernel(Xp,x_train)
Kff = kernel(x_train,x_train)
Kss = kernel(Xp,Xp)
jitter = 1e-6
mu = Ksf @ np.linalg.inv(Kff + (jitter + sigma2)*np.identity(len(x_train))) @ y_train
Sigma = Kss - [email protected](Kff + sigma2*np.identity(len(x_train)))@Ksf.T
return mu, Sigma
def GK_spectral(self, t):
## Spectral density of the Gaussian Kernel
alpha = self.alpha
scale = self.scale
s = alpha * np.sqrt(2*np.pi*scale**2) * np.exp(-0.5*(scale*t)**2)
return s
class LP_approx_GPR():
"""
Class for a GP model approximating the kernel as Lazaro-Gradilla paper
(2010) http://jmlr.org/papers/v11/lazaro-gredilla10a.html
"""
def __init__(self, data, sigma2, M, alpha=1, scale=1):
"""
Initialize your GP class.
Parameters
----------
data : Tuple of regression data input and observation e.g. (x, y).
sigma2 : Float of likelihood variance.
alpha: Amplitude of the kernel.
scale: Float for kernel lengthscale.
M: Approximation degree.
samples: random samples from the spectral density of the Gaussian Kernel.
"""
self.data = data
self.sigma2 = sigma2
self.alpha = alpha
self.scale = scale
self.M = M
self.samples = np.random.normal(loc=0, scale=(1/(2*np.pi*scale)), size=M)
def kernel_approx(self, X1, X2):
"""
returns the NxM kernel matrix between the two sets of input X1 and X2
arguments:
X1 -- NxD matrix
X2 -- MxD matrix
returns NxM matrix
"""
M = self.M
samples = self.samples
sigma02 = self.alpha
d = np.sum((X1[:,None,:]-X2[None,:,:]), axis=-1)
k = 0
for sr in samples:
Knew = np.cos(2*np.pi*sr*d)
k = Knew + k
return k * sigma02/M
def phi_vector(self, x, samples):
"""
returns the vector of the trigonometric functions evaluated at x.
"""
phi = None
for sr in samples:
phi = np.append(phi, np.array([np.cos(2*np.pi*sr*x), np.sin(2*np.pi*sr*x)]))
phi = phi[1:]
return phi[:,None]
def Phi_matrix(self, X, samples):
"""
returns the matrix of the trigonometric functions evaluated at each of the points in X.
"""
phi_vector = self.phi_vector
return (np.reshape(np.apply_along_axis(phi_vector, 1, X, samples), (len(X) , 2*len(samples))).T).astype('float64')
def posterior(self, Xp):
"""
returns the posterior distribution of f evaluated at each of the points in Xp conditioned on (X, y)
using the approximated squared exponential kernel and the reduced notation.
"""
np.random.seed(123)
x_train, y_train = self.data
sigma2 = self.sigma2
alpha = self.alpha
M = self.M
samples = self.samples
jitter = 1.0E-10
phisf = self.Phi_matrix(Xp, samples)
Phif = self.Phi_matrix(x_train, samples)
A = (alpha * Phif @ Phif.T + (jitter + M * sigma2) * np.identity(2 * M)) / alpha
A_1 = np.linalg.inv(A)
mu = phisf.T @ A_1 @ Phif @ y_train
Var = sigma2 * phisf.T @ A_1 @ phisf
return mu, Var
class HS_approx_GPR():
"""
Class for Hilbert Space approximation of the GPR based on the paper
from Solin and Särkkä (2020) https://link.springer.com/article/10.1007/s11222-019-09886-w
"""
def __init__(self, data, sigma2, M, L, alpha=1, scale=1):
"""
Initialize the Hilbert Space approximation GP class.
Parameters
----------
data : Tuple of regression data input and observation e.g. (x, y).
sigma2 : Float of likelihood variance.
alpha: Amplitude of the kernel.
scale: Float for kernel lengthscale.
M: Approximation degree.
"""
self.data = data
self.sigma2 = sigma2
self.alpha = alpha
self.scale = scale
self.M = M
self.L = L
def phi_j(self, lam_j, x):
"""
returns the eigenfunction j evaluated ax the point x.
"""
L = self.L
return np.sin((lam_j)*(x+L))/(np.sqrt(L))
def spectral_density(self, t):
"""
Evaluates the spectral density of the exponential kernel ar the point t.
"""
## Spectral density of the Gaussian Kernel
alpha = self.alpha
scale = self.scale
s = alpha * np.sqrt(2*np.pi*scale**2) * np.exp(-0.5*(scale*t)**2)
return s
def kernel_approx(self, X1, X2):
"""
returns the NxM kernel matrix between the two sets of input X1 and X2
arguments:
X1 -- NxD matrix
X2 -- MxD matrix
returns NxM matrix
"""
M = self.M
alpha = self.alpha
scale = self.scale
L = self.L
spectral_density = self.spectral_density
phi_j = self.phi_j
k = 0
for j in range(M):
lam_j = np.pi*(j+1)/(2*L)
Phi_x1 = phi_j(X1,lam_j,L)
Phi_x2 = phi_j(X2,lam_j,L)
s_j = spectral_density(lam_j, alpha, scale)
Knew = s_j * np.kron(Phi_x1, Phi_x2.T)
k = Knew + k
return k
def phi_vector(self, x):
"""
Returns the vector of x evaluated at each of the eigenfunctions from 1 to M.
"""
M = self.M
L = self.L
phi_j = self.phi_j
phi = None
for j in range(M):
lam_j = np.pi*(j+1)/(2*L)
phi = np.append(phi, phi_j(lam_j, x))
phi = phi[1:]
return phi[:,None].astype('float64')
def Phi_matrix(self, X):
"""
returns the data matrix of the input points X evaluated at each of the eigenfunctions from 1 to M.
"""
M = self.M
phi_vector = self.phi_vector
return np.reshape(np.apply_along_axis(phi_vector, 1, X), (len(X) , M))
def Lambda(self):
"""
returns the diagonal matrix of the eigenvalues.
"""
M = self.M
L = self.L
spectral_density = self.spectral_density
lams = []
for j in range(M):
lam_j = np.pi*(j+1)/(2*L)
lams.append(lam_j)
lams = np.array(lams)
Lambda = np.diag(spectral_density(lams))
return Lambda
def posterior(self, Xp):
"""
returns the posterior distribution of f evaluated at each of the points in Xp conditioned on (X, y)
using the approximated squared exponential kernel using the Hilbert Space approximation.
"""
np.random.seed(123)
x_train, y_train = self.data
sigma2 = self.sigma2
M = self.M
Phi_matrix = self.Phi_matrix
jitter = 1.0E-10
phisf = Phi_matrix(Xp).T
Phif = Phi_matrix(x_train)
Lambda = self.Lambda()
X = [email protected](Lambda)
X_s = np.sqrt(Lambda)@phisf
y_train = y_train[:,None]
B = X.T @ X + (jitter + sigma2)*np.identity(M)
B_1 = np.linalg.inv(B)
mu = X_s.T @ B_1 @ X.T @ y_train
Var = sigma2 * X_s.T @ B_1 @ X_s
return mu, Var