forked from profLewis/gpml-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovariance.py
334 lines (303 loc) · 13.3 KB
/
covariance.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
# -*- coding: utf-8 -*-
""" GPML Covariance functions.
Pairwise dependencies between datapoints to be used in a Gaussian process.
Created: Wed Dec 18 16:57:01 2013 by Hannes Nickisch, Philips Research Hamburg.
Modified: $Id: covariance.py 1263 2013-12-13 13:36:13Z hn $
"""
__version__ = "$Id: covariance.py 913 2013-08-15 12:54:33Z hn $"
# TODO: mask
import numpy as np
import hashlib as hl
from functools import partial
import scipy.spatial.distance as ssd
from hyper import HyperIter
class Covariance(HyperIter):
""" Covariance function base class.
"""
def __init__(self,hyp={},name='plain',chil=[]):
self.hyp = hyp
self.name,self.chil = name,chil
def __str__(self):
return "cov/"+self.name
def __repr__(self):
return self.__str__()
def __mul__(self, other):
""" Product of a covariance function and a scalar or another
covariance function.
"""
if isinstance(other,float) or isinstance(other,int):
hyp = dict(log_sf=0.5*np.log(other))
other = Covariance(hyp=hyp,name='const')
if self.name=='prod': # make sure all terms appear on the same level
self.chil.append(other)
return self
else:
return Covariance(name='prod',chil=[other,self])
def __rmul__(self, other): # other is not of type Covariance
return self.__mul__(other)
def __add__(self, other):
""" Sum of a covariance function and a scalar or another
covariance function.
"""
if isinstance(other,float) or isinstance(other,int):
hyp = dict(log_sf=0.5*np.log(other))
other = Covariance(hyp=hyp,name='const')
if self.name=='sum': # make sure all terms appear on the same level
self.chil.append(other)
return self
else:
return Covariance(name='sum',chil=[other,self])
def __radd__(self, other): # other is not of type Covariance
return self.__add__(other)
def evaluate(self,x,z=None, diag=False,deriv=None):
""" Default evaluation function to be overridden by derived classes.
"""
def shape(x,z,diag): # shape of the resulting matrix
n = x.shape[0]
if diag: return n,
else:
if z==None: return n,n
else: return n,z.shape[0]
if self.name in ['prod','sum']:
if deriv==None: # evaluate
if self.name=='prod':
K = np.ones( shape(x,z,diag))
for k in self.chil: K *= k(x,z=z,diag=diag)
elif self.name=='sum' :
K = np.zeros(shape(x,z,diag))
for k in self.chil: K += k(x,z=z,diag=diag)
else: # evaluate derivatives
deriv = int(deriv) # derivative index
if deriv<0 or deriv>self.get_nhyp()-1:
raise Exception('Bad derivative index.')
nhyp = np.cumsum([c.get_nhyp() for c in self.chil])
j = np.nonzero(deriv<nhyp)[0].min() # index of term
if j==0: i = deriv # index inside term
else: i = deriv-nhyp[j-1]
if self.name=='prod':
K = np.ones( shape(x,z,diag))
for l,k in enumerate(self.chil):
if l==j: K *= k(x,z=z,diag=diag,deriv=i)
else: K *= k(x,z=z,diag=diag)
elif self.name=='sum' :
K = self.chil[j](x,z=z,diag=diag,deriv=i)
elif self.name=='const':
K = np.exp(2*self.hyp['log_sf'])*np.ones(shape(x,z,diag))
if deriv!=None: K *= 2.0
else: # unknown name
K = np.zeros(shape(x,z,diag))
return K
def __call__(self, x,z=None, diag=False,deriv=None):
return self.evaluate(x,z=z,diag=diag,deriv=deriv)
def memoise(f=None,n=10,output=False):
""" Add a cache to an existing function depending on numpy arrays.
"""
if f is None: return partial(memoise,n=n,output=output)
def hashfun(x):
""" Hash function also dealing with numpy arrays.
Output is always a string.
"""
if type(np.array([]))==type(x): return hl.sha1(x).hexdigest()
else: return str(hash(x))
cache = {}
def g(*args,**kwargs):
""" Augmented function as returned by the decorator.
"""
for c in cache: # make all cache entries one tick older
v,a = cache[c]; cache[c] = v,a+1
h = '' # overall hash value
for a in args: h += hashfun(a)
for v in kwargs.values(): h += hashfun(v)
x = hashfun(h)
if x not in cache: # cache miss
if output: print "miss(%d)"%len(cache)
if len(cache)>=n: # cache is full -> delete oldest entry
if output: print "delete"
xold,age = None,0
for xact in cache:
if cache[xact][1]>age: xold,age = xact,cache[xact][1]
del cache[xold]
cache[x] = f(*args,**kwargs),0
else: # cache hit, reset age
if output: print "hit(%d)"%len(cache)
cache[x] = cache[x][0],0
return cache[x][0]
return g
@memoise(n=5)
def sq_dist_scipy(x,z):
""" Compute squared distances using scipy.spatial.distance.
"""
return ssd.cdist(x,z,'sqeuclidean')
@memoise(n=5)
def sq_dist_dot(x,z):
""" Compute squared distances by dot products.
"""
d2 = np.sum(x*x,axis=1).reshape(-1,1) + np.sum(z*z,axis=1).reshape(1,-1)
d2 -= 2*np.dot(x,z.T)
return d2
@memoise(n=5)
def sq_dist_loop(x,z):
""" Compute squared distances by a loop over dimensions.
"""
d2 = np.zeros([x.shape[0],z.shape[0]])
for i in range(x.shape[1]):
d2 += (x[:,i].reshape(-1,1)-z[:,i].reshape(1,-1))**2
return d2
def sq_dist(x,z=None,ell=None,sqd=sq_dist_scipy):
""" Compute a matrix of all pairwise squared distances
between two sets of vectors, stored in the row of the two matrices:
x (of size n by D) and z (of size m by D).
"""
if ell==None:
if z==None: return sqd(x,x)
else: return sqd(x,z)
else:
ell = ell.reshape(1,-1)
if z==None: return sqd(x/ell,x/ell)
else: return sqd(x/ell,z/ell)
class noise(Covariance):
def __init__(self):
""" Construct a noise covariance function.
"""
Covariance.__init__(self,hyp={},name='noise')
def evaluate(self, x,z=None, diag=False,deriv=None):
""" Evaluation of noise covariance function.
"""
n = x.shape[0]
if z==None: m = n
else: m = z.shape[0]
if diag:
if deriv==None: K = np.ones(n)
else: K = np.zeros(n)
else:
if deriv==None: K = np.eye(n,m)
else: K = np.zeros([n,m])
return K
class stat(Covariance):
def __init__(self,h=None,dh=None,log_ell=None):
""" Construct a generic stationary covariance function
k(x,z) = h(d2(x,z)), where d2(x,z) is the squared distance between
the data points x and z.
"""
hyp = dict(log_ell=log_ell)
Covariance.__init__(self,hyp=hyp,name='stat')
self.h,self.dh = h,dh
def evaluate(self, x,z=None, diag=False,deriv=None):
""" Evaluation of a generic stationary covariance function.
"""
n,D = x.shape
ell = np.exp(self.hyp['log_ell'])
iso = np.size(ell)==1 # do we have an isotropic covariance function?
if not iso: ell = ell.reshape(1,D)
if diag: K = np.zeros(n)
else: K = sq_dist(x,z,ell=ell)
if deriv==None: K = self.h(K) # covariance evaluation
else: # ell derivative(s)
if iso:
K = -2*self.dh(K)*K
else:
K = -2*self.dh(K)
if diag: K *= 0
else:
i = deriv
xi = (x[:,i]/ell[:,i]).reshape(n,1)
if z==None: K *= sq_dist(xi,None)
else: K *= sq_dist(xi,(z[:,i]/ell[:,i]).reshape(-1,1))
return K
class se(Covariance):
def __init__(self,ell=None,log_ell=None):
""" Construct a squared exponential covariance function.
"""
if log_ell==None: log_ell = np.log(ell)
hyp = dict(log_ell=log_ell)
Covariance.__init__(self,hyp=hyp,name='se')
def evaluate(self, x,z=None, diag=False,deriv=None):
""" Evaluation of squared exponential covariance function.
"""
def h(D2): return np.exp(-0.5*D2)
def dh(D2): return -0.5*np.exp(-0.5*D2)
k = stat(h=h,dh=dh,log_ell=self.hyp['log_ell'])
return k.evaluate(x,z=z,diag=diag,deriv=deriv)
class rq(Covariance):
def __init__(self,ell=None,log_ell=None,al=None,log_al=None):
""" Construct a rational quadratic covariance function.
"""
if log_ell==None: log_ell = np.log(ell)
if log_al ==None: log_al = np.log(al)
hyp = dict(log_ell=log_ell,log_al=log_al)
Covariance.__init__(self,hyp=hyp,name='rq')
def evaluate(self, x,z=None, diag=False,deriv=None):
""" Evaluation of rational quadratic covariance function.
"""
al = np.exp(self.hyp['log_al'])
def g(D2): return 1+0.5*D2/al
def h(D2): return g(D2)**(-al )
def dh(D2): return -0.5*g(D2)**(-al-1)
k = stat(h=h,dh=dh,log_ell=self.hyp['log_ell'])
if deriv==None:
K = k.evaluate(x,z=z,diag=diag,deriv=deriv)
else:
if deriv==0: # log_al comes before log_ell
ell = np.exp(self.hyp['log_ell'])
n,D = x.shape
if diag: D2 = np.zeros(n)
else: D2 = sq_dist(x,z,ell=ell)
G = g(D2)
K = G**(-al) * (0.5*D2/G - al*np.log(G))
else: # length scales
K = k.evaluate(x,z=z,diag=diag,deriv=deriv-1)
return K
class matern(Covariance):
def __init__(self,ell=None,log_ell=None,d=1):
""" Construct a Matérn covariance function, d=1,3,5.
"""
if log_ell==None: log_ell = np.log(ell)
hyp = dict(log_ell=log_ell)
Covariance.__init__(self,hyp=hyp,name='se')
self.d = d
def evaluate(self, x,z=None, diag=False,deriv=None):
""" Evaluation of Matérn covariance function.
"""
d = self.d # degree
if d==1: f,df = lambda t: 1 ,lambda t: 0
elif d==3: f,df = lambda t: 1+t ,lambda t: 1
elif d==5: f,df = lambda t: 1+t*(1+t/3.0),lambda t: 1+2*t/3.0
else: raise Exception('Bad d in covariance.')
g = lambda t: f(t)*np.exp(-t)
dg = lambda t: df(t)*np.exp(-t) - g(t)
def h(D2): return g(np.sqrt(d*D2))
def dh(D2): return dg(np.sqrt(d*D2))*np.sqrt(d) / (2*np.sqrt(D2))
k = stat(h=h,dh=dh,log_ell=self.hyp['log_ell'])
K = k.evaluate(x,z=z,diag=diag,deriv=deriv)
K[np.isnan(K)] = 0
return K
class gabor(Covariance):
def __init__(self,ell=None,log_ell=None,p=None,log_p=None):
""" Construct a squared exponential covariance function.
"""
if log_ell==None: log_ell = np.log(ell)
if log_p==None: log_p = np.log(p)
hyp = dict(log_ell=log_ell,log_p=log_p)
Covariance.__init__(self,hyp=hyp,name='gabor')
def evaluate(self, x,z=None, diag=False,deriv=None):
""" Evaluation of squared exponential covariance function.
"""
p = np.exp(self.hyp['log_p'])
def f(D2): return 2*np.pi/ p * np.sqrt(D2)
def df(D2): return np.pi/(p * np.sqrt(D2))
def h(D2): return np.exp(-0.5*D2)*np.cos(f(D2))
def dh(D2): return -0.5*h(D2) - np.exp(-0.5*D2)*np.sin(f(D2)) * df(D2)
k = stat(h=h,dh=dh,log_ell=self.hyp['log_ell'])
if deriv==None:
K = k.evaluate(x,z=z,diag=diag,deriv=deriv)
else:
if deriv==0: # log_p comes before log_ell
ell = np.exp(self.hyp['log_ell'])
n,D = x.shape
if diag: D2 = np.zeros(n)
else: D2 = sq_dist(x,z,ell=ell)
K = np.exp(-0.5*D2)*np.sin(f(D2))*f(D2)
else: # length scales
K = k.evaluate(x,z=z,diag=diag,deriv=deriv-1)
K[np.isnan(K)] = 0
return K