-
Notifications
You must be signed in to change notification settings - Fork 4
/
gpr1step.py
48 lines (36 loc) · 1.28 KB
/
gpr1step.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
from Utils.reverseYuleWalkerES import reverseYuleWalkerES
def gpr1step(
logtheta,
covfunc,
T,
dt,
):
epsilon = 1e-8
minLen = 20 # Must be at least 2, otherwise indexing errors will result.
D = 1
if len(covfunc.hyp) != len(logtheta):
error('Error: Number of parameters do not agree with covariance function in gpr1step'
)
# TODO check condition number of cov matrix
covfunc.hyp = logtheta
Kss = covfunc.evaluate(np.atleast_2d(range(T)) / dt, 'diag')[0, 0]
x = (np.atleast_2d(range(1, T + 1)) / dt).T
z = np.zeros((1, 1))
Kstar = covfunc.evaluate(x, z)
alpha = reverseYuleWalkerES(Kss, Kstar, minLen, epsilon)
pruneLen = alpha.shape[0]
sigma2 = Kss - np.dot(alpha, Kstar[:pruneLen, 0])
# Add in the prior preditictive in the first row
alpha = np.concatenate((np.zeros((1, pruneLen)), alpha), axis=0)
sigma2 = np.append(Kss, sigma2)
assert (sigma2 > 0).all()
return (alpha, np.atleast_2d(sigma2).T)
if __name__ == '__main__':
import pyGPs
logtheta = np.log(np.asarray([1., 2.0, 3.0]))
k = pyGPs.cov.RQ(logtheta[0], logtheta[1], logtheta[2])
(alpha, sigma2) = gpr1step(logtheta, k, 10, 1)
print alpha, sigma2