-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernels.py
357 lines (282 loc) · 8.79 KB
/
kernels.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import numpy as np
import numpy.random as rnd
import scipy.linalg as la
def confidenceInterval(cov):
return 1.96 * np.diag(cov)
# This will be used to modify for use with non-stationary Kernels
def multVarDiffMat(x,y):
p,n = x.shape
q = len(y)
X = np.zeros((p,q,n))
Y = np.zeros((p,q,n))
for i in range(p):
for j in range(q):
X[i,j] = x[i]
Y[i,j] = y[j]
return X-Y
def diffMat(t1,t2):
"""
Scalar diffmat
Use multivar diffmat for multivariable case
"""
if len(t1.shape) > 1:
T = multVarDiffMat(t1,t2)
else:
T = np.outer(t1,np.ones(len(t2))) - np.outer(np.ones(len(t1)),t2)
return T
class gaussianProcess:
def __init__(self,x=None,y=None):
self.set_data(x,y)
def k(self,x1,x2):
return np.zeros((len(x1),len(x2)))
def set_data(self,x=None,y=None):
if x is None:
self.x = None
else:
self.x = x
self.K = self.k(x,x)
self.Kchol = la.cholesky(self.K,lower=True)
self.scaled_data = la.solve(self.K,y)
def prediction(self,x):
"""
Assuming that x is a numpy array of prediction values
"""
if self.x is None:
mean = np.zeros(len(x))
cov = self.k(x,x)
else:
kVec = self.k(self.x,x)
mean = np.dot(kVec.T,self.scaled_data)
schurFactor = la.solve(self.Kchol,kVec)
cov = self.k(x,x) - np.dot(schurFactor.T,schurFactor)
return mean,cov
def sample(self,x,NumSamples=1):
mean,cov = self.prediction(x)
gain = la.cholesky(cov,lower=True)
if NumSamples == 1:
WShape = (len(x),)
meanMat = mean
else:
WShape = (len(x),NumSamples)
meanMat = np.outer(mean,np.ones(NumSamples))
W = rnd.randn(*WShape)
return meanMat + np.dot(gain,W)
# This will ONLY work for stationary kernels
# Fixing above to work on both
class gaussian(gaussianProcess):
def __init__(self,theta=1.,maxVar=1.):
self.theta = theta
self.maxVar = maxVar
gaussianProcess.__init__(self)
def k(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return self.maxVar * np.exp(-self.theta * t**2.)
def dk(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return -2. * self.theta * t * self.k(t)
def ddk(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return ((2*self.theta*t)**2. - 2*self.theta) * self.k(t)
class inverseMultiquadric(gaussianProcess):
def __init__(self,theta=1.,maxVar=1.):
self.theta = theta
self.maxVar = maxVar
def kNormed(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return 1./(1.+self.theta * t**2.)
def k(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return self.maxVar * self.kNormed(t)
def dk(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return -2. * self.maxVar * self.theta * t * (self.kNormed(t)**2.)
def ddk(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
val1 = -2 * self.maxVar * self.theta * (self.kNormed(t)**2.)
val2 = 2 * self.maxVar * \
((2.*self.theta*t)**2.) * (self.kNormed(t)**3.)
return val1 + val2
class matern1(gaussianProcess):
def __init__(self,theta=1.,maxVar=1.):
self.theta = theta
self.maxVar = maxVar
def eFun(self,t):
return self.maxVar * np.exp(-self.theta * np.abs(t))
def k(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return (1.+self.theta*np.abs(t)) * self.eFun(t)
def dk(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
return -self.theta**2. * np.abs(t) * self.eFun(t)
# Not Twice Differentiable at 0.
class matern2(gaussianProcess):
def __init__(self,theta=1.,maxVar=1.):
self.theta = theta
self.maxVar = maxVar
p0 = np.array([theta**2.,3.*theta,3.])
p1 = np.polyadd(np.polyder(p0),-theta*p0)
p2 = np.polyadd(np.polyder(p1),-theta*p1)
self.p0 = p0
self.p1 = p1
self.p2 = p2
def eFun(self,t):
return self.maxVar * np.exp(-self.theta * np.abs(t))
def k(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
pVal = np.polyval(self.p0,np.abs(t))
return pVal * self.eFun(t)
def dk(self,t1,t2=None):
if t2 is None:
t = t1
else:
t = diffMat(t1,t2)
pVal = np.polyval(self.p1,np.abs(t))
return pVal * self.eFun(t)
# Also not twice differentiable.
#### Multivariate Gaussian code
def mpolysort(p):
if len(p) == 0:
return []
DList = []
CList = []
for m in p:
c,d = m
CList.append(c)
DList.append(d)
CArr = np.array(CList)
DArr = np.array(DList,dtype=int)
DTup = tuple([DL for DL in DArr.T])
ind = np.lexsort(DTup[::-1])
DSorted = DArr[ind]
CSorted = CArr[ind]
cCur = None
pSorted = []
for c,d in zip(CSorted,DSorted):
if cCur is None:
cCur = c
dCur = d
else:
if np.max(np.abs(d-dCur)) == 0:
# Same degree. Just add the coefficients
cCur += c
else:
# New degree. Put in the value and increment.
pSorted.append((cCur,dCur))
cCur = c
dCur = d
# Put in the final value
pSorted.append((cCur,dCur))
return pSorted
def monomialval(x,m):
V = np.ones(x.shape[:-1])
for i in range(len(m)):
V = V * (x[...,i] ** m[i])
return V
def mpolyval(x,p):
V = np.zeros(x.shape[:-1])
for m in p:
c,d = m
V = V + c * monomialval(x,d)
return V
def mpolyderind(p,ind):
pDer = []
for m in p:
c,d = m
if d[ind] > 0:
cDer = d[ind] * c
dDer = np.array(d,copy=True)
dDer[ind] = d[ind] - 1
pDer.append((cDer,dDer))
return mpolysort(pDer)
def mpolyder(p,indexArray):
curIndArray = np.array(indexArray,copy=True)
pDer = None
for i in range(len(indexArray)):
while curIndArray[i] > 0:
if pDer is None:
pDer = mpolyderind(p,i)
else:
pDer = mpolyderind(pDer,i)
curIndArray[i] -= 1
return pDer
def mpolysmul(p,s):
"""
multiply a polynomial by a scalar
"""
sp = []
for c,d in p:
sp.append((s*c,d))
return sp
def mpolyadd(p1,p2):
pBoth = p1 + p2
return mpolysort(pBoth)
def mpolymul(p1,p2):
pTot = []
for c1,d1 in p1:
for c2,d2 in p2:
pTot.append((c1*c2,np.array(d1)+np.array(d2)))
return mpolysort(pTot)
normSquareMat = lambda M : np.sum(M**2.,axis=2)
class mvGaussian:
def __init__(self,theta=1.,maxVar=1.):
self.theta = theta
self.maxVar = theta
def k0(self,x,y):
D = multVarDiffMat(x,y)
M = normSquareMat(D)
return self.maxVar * np.exp(-self.theta*M)
def k(self,x,y,XDerivativeIndex = None,YDerivativeIndex = None):
if (XDerivativeIndex is None) and (YDerivativeIndex is None):
# No Derivatives
return self.k0(x,y)
else:
n = x.shape[-1]
p = [(1.,np.zeros(n,dtype=int))]
TotalDerivativeIndex = np.zeros(n,dtype=int)
psign = 1.
if XDerivativeIndex is not None:
TotalDerivativeIndex += XDerivativeIndex
if YDerivativeIndex is not None:
TotalDerivativeIndex += YDerivativeIndex
psign = (-1.)**(np.sum(YDerivativeIndex))
p = [(psign,np.zeros(n,dtype=int))]
for i in range(n):
eVec = np.zeros(n,dtype=int)
eVec[i] = 1
for j in range(TotalDerivativeIndex[i]):
pDer = mpolyderind(p,i)
pMul = mpolymul(p,[(-2.*self.theta,eVec)])
p = mpolyadd(pDer,pMul)
D = multVarDiffMat(x,y)
pVal = mpolyval(D,p)
return pVal * self.k0(x,y)