mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-04-28 14:26:23 +02:00
[sde stationary] whitespaces
This commit is contained in:
parent
d6ccccc7e4
commit
e9bc9393b1
1 changed files with 75 additions and 75 deletions
|
|
@ -14,10 +14,10 @@ import scipy as sp
|
||||||
|
|
||||||
class sde_RBF(RBF):
|
class sde_RBF(RBF):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Class provide extra functionality to transfer this covariance function into
|
Class provide extra functionality to transfer this covariance function into
|
||||||
SDE form.
|
SDE form.
|
||||||
|
|
||||||
Radial Basis Function kernel:
|
Radial Basis Function kernel:
|
||||||
|
|
||||||
.. math::
|
.. math::
|
||||||
|
|
@ -30,90 +30,90 @@ class sde_RBF(RBF):
|
||||||
Update gradient in the order in which parameters are represented in the
|
Update gradient in the order in which parameters are represented in the
|
||||||
kernel
|
kernel
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.variance.gradient = gradients[0]
|
self.variance.gradient = gradients[0]
|
||||||
self.lengthscale.gradient = gradients[1]
|
self.lengthscale.gradient = gradients[1]
|
||||||
|
|
||||||
def sde(self):
|
def sde(self):
|
||||||
"""
|
"""
|
||||||
Return the state space representation of the covariance.
|
Return the state space representation of the covariance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
N = 10# approximation order ( number of terms in exponent series expansion)
|
N = 10# approximation order ( number of terms in exponent series expansion)
|
||||||
roots_rounding_decimals = 6
|
roots_rounding_decimals = 6
|
||||||
|
|
||||||
fn = np.math.factorial(N)
|
fn = np.math.factorial(N)
|
||||||
|
|
||||||
kappa = 1.0/2.0/self.lengthscale**2
|
kappa = 1.0/2.0/self.lengthscale**2
|
||||||
|
|
||||||
Qc = np.array((self.variance*np.sqrt(np.pi/kappa)*fn*(4*kappa)**N,),)
|
Qc = np.array((self.variance*np.sqrt(np.pi/kappa)*fn*(4*kappa)**N,),)
|
||||||
|
|
||||||
pp = np.zeros((2*N+1,)) # array of polynomial coefficients from higher power to lower
|
pp = np.zeros((2*N+1,)) # array of polynomial coefficients from higher power to lower
|
||||||
|
|
||||||
for n in range(0, N+1): # (2N+1) - number of polynomial coefficients
|
for n in range(0, N+1): # (2N+1) - number of polynomial coefficients
|
||||||
pp[2*(N-n)] = fn*(4.0*kappa)**(N-n)/np.math.factorial(n)*(-1)**n
|
pp[2*(N-n)] = fn*(4.0*kappa)**(N-n)/np.math.factorial(n)*(-1)**n
|
||||||
|
|
||||||
pp = sp.poly1d(pp)
|
pp = sp.poly1d(pp)
|
||||||
roots = sp.roots(pp)
|
roots = sp.roots(pp)
|
||||||
|
|
||||||
neg_real_part_roots = roots[np.round(np.real(roots) ,roots_rounding_decimals) < 0]
|
neg_real_part_roots = roots[np.round(np.real(roots) ,roots_rounding_decimals) < 0]
|
||||||
aa = sp.poly1d(neg_real_part_roots, r=True).coeffs
|
aa = sp.poly1d(neg_real_part_roots, r=True).coeffs
|
||||||
|
|
||||||
F = np.diag(np.ones((N-1,)),1)
|
F = np.diag(np.ones((N-1,)),1)
|
||||||
F[-1,:] = -aa[-1:0:-1]
|
F[-1,:] = -aa[-1:0:-1]
|
||||||
|
|
||||||
L= np.zeros((N,1))
|
L= np.zeros((N,1))
|
||||||
L[N-1,0] = 1
|
L[N-1,0] = 1
|
||||||
|
|
||||||
H = np.zeros((1,N))
|
H = np.zeros((1,N))
|
||||||
H[0,0] = 1
|
H[0,0] = 1
|
||||||
|
|
||||||
# Infinite covariance:
|
# Infinite covariance:
|
||||||
Pinf = sp.linalg.solve_lyapunov(F, -np.dot(L,np.dot( Qc[0,0],L.T)))
|
Pinf = sp.linalg.solve_lyapunov(F, -np.dot(L,np.dot( Qc[0,0],L.T)))
|
||||||
Pinf = 0.5*(Pinf + Pinf.T)
|
Pinf = 0.5*(Pinf + Pinf.T)
|
||||||
# Allocating space for derivatives
|
# Allocating space for derivatives
|
||||||
dF = np.empty([F.shape[0],F.shape[1],2])
|
dF = np.empty([F.shape[0],F.shape[1],2])
|
||||||
dQc = np.empty([Qc.shape[0],Qc.shape[1],2])
|
dQc = np.empty([Qc.shape[0],Qc.shape[1],2])
|
||||||
dPinf = np.empty([Pinf.shape[0],Pinf.shape[1],2])
|
dPinf = np.empty([Pinf.shape[0],Pinf.shape[1],2])
|
||||||
|
|
||||||
# Derivatives:
|
# Derivatives:
|
||||||
dFvariance = np.zeros(F.shape)
|
dFvariance = np.zeros(F.shape)
|
||||||
dFlengthscale = np.zeros(F.shape)
|
dFlengthscale = np.zeros(F.shape)
|
||||||
dFlengthscale[-1,:] = -aa[-1:0:-1]/self.lengthscale * np.arange(-N,0,1)
|
dFlengthscale[-1,:] = -aa[-1:0:-1]/self.lengthscale * np.arange(-N,0,1)
|
||||||
|
|
||||||
dQcvariance = Qc/self.variance
|
dQcvariance = Qc/self.variance
|
||||||
dQclengthscale = np.array(((self.variance*np.sqrt(2*np.pi)*fn*2**N*self.lengthscale**(-2*N)*(1-2*N,),)))
|
dQclengthscale = np.array(((self.variance*np.sqrt(2*np.pi)*fn*2**N*self.lengthscale**(-2*N)*(1-2*N,),)))
|
||||||
|
|
||||||
dPinf_variance = Pinf/self.variance
|
dPinf_variance = Pinf/self.variance
|
||||||
|
|
||||||
lp = Pinf.shape[0]
|
lp = Pinf.shape[0]
|
||||||
coeff = np.arange(1,lp+1).reshape(lp,1) + np.arange(1,lp+1).reshape(1,lp) - 2
|
coeff = np.arange(1,lp+1).reshape(lp,1) + np.arange(1,lp+1).reshape(1,lp) - 2
|
||||||
coeff[np.mod(coeff,2) != 0] = 0
|
coeff[np.mod(coeff,2) != 0] = 0
|
||||||
dPinf_lengthscale = -1/self.lengthscale*Pinf*coeff
|
dPinf_lengthscale = -1/self.lengthscale*Pinf*coeff
|
||||||
|
|
||||||
dF[:,:,0] = dFvariance
|
dF[:,:,0] = dFvariance
|
||||||
dF[:,:,1] = dFlengthscale
|
dF[:,:,1] = dFlengthscale
|
||||||
dQc[:,:,0] = dQcvariance
|
dQc[:,:,0] = dQcvariance
|
||||||
dQc[:,:,1] = dQclengthscale
|
dQc[:,:,1] = dQclengthscale
|
||||||
dPinf[:,:,0] = dPinf_variance
|
dPinf[:,:,0] = dPinf_variance
|
||||||
dPinf[:,:,1] = dPinf_lengthscale
|
dPinf[:,:,1] = dPinf_lengthscale
|
||||||
|
|
||||||
P0 = Pinf.copy()
|
P0 = Pinf.copy()
|
||||||
dP0 = dPinf.copy()
|
dP0 = dPinf.copy()
|
||||||
|
|
||||||
# Benefits of this are not very sound. Helps only in one case:
|
# Benefits of this are not very sound. Helps only in one case:
|
||||||
# SVD Kalman + RBF kernel
|
# SVD Kalman + RBF kernel
|
||||||
import GPy.models.state_space_main as ssm
|
import GPy.models.state_space_main as ssm
|
||||||
(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf,dP0, T) = ssm.balance_ss_model(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0 )
|
(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf,dP0, T) = ssm.balance_ss_model(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0 )
|
||||||
|
|
||||||
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
||||||
|
|
||||||
class sde_Exponential(Exponential):
|
class sde_Exponential(Exponential):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Class provide extra functionality to transfer this covariance function into
|
Class provide extra functionality to transfer this covariance function into
|
||||||
SDE form.
|
SDE form.
|
||||||
|
|
||||||
Exponential kernel:
|
Exponential kernel:
|
||||||
|
|
||||||
.. math::
|
.. math::
|
||||||
|
|
@ -121,53 +121,53 @@ class sde_Exponential(Exponential):
|
||||||
k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r \\bigg) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} }
|
k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r \\bigg) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} }
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def sde_update_gradient_full(self, gradients):
|
def sde_update_gradient_full(self, gradients):
|
||||||
"""
|
"""
|
||||||
Update gradient in the order in which parameters are represented in the
|
Update gradient in the order in which parameters are represented in the
|
||||||
kernel
|
kernel
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.variance.gradient = gradients[0]
|
self.variance.gradient = gradients[0]
|
||||||
self.lengthscale.gradient = gradients[1]
|
self.lengthscale.gradient = gradients[1]
|
||||||
|
|
||||||
def sde(self):
|
def sde(self):
|
||||||
"""
|
"""
|
||||||
Return the state space representation of the covariance.
|
Return the state space representation of the covariance.
|
||||||
"""
|
"""
|
||||||
variance = float(self.variance.values)
|
variance = float(self.variance.values)
|
||||||
lengthscale = float(self.lengthscale)
|
lengthscale = float(self.lengthscale)
|
||||||
|
|
||||||
F = np.array(((-1.0/lengthscale,),))
|
F = np.array(((-1.0/lengthscale,),))
|
||||||
L = np.array(((1.0,),))
|
L = np.array(((1.0,),))
|
||||||
Qc = np.array( ((2.0*variance/lengthscale,),) )
|
Qc = np.array( ((2.0*variance/lengthscale,),) )
|
||||||
H = np.array(((1.0,),))
|
H = np.array(((1.0,),))
|
||||||
Pinf = np.array(((variance,),))
|
Pinf = np.array(((variance,),))
|
||||||
P0 = Pinf.copy()
|
P0 = Pinf.copy()
|
||||||
|
|
||||||
dF = np.zeros((1,1,2));
|
dF = np.zeros((1,1,2));
|
||||||
dQc = np.zeros((1,1,2));
|
dQc = np.zeros((1,1,2));
|
||||||
dPinf = np.zeros((1,1,2));
|
dPinf = np.zeros((1,1,2));
|
||||||
|
|
||||||
dF[:,:,0] = 0.0
|
dF[:,:,0] = 0.0
|
||||||
dF[:,:,1] = 1.0/lengthscale**2
|
dF[:,:,1] = 1.0/lengthscale**2
|
||||||
|
|
||||||
dQc[:,:,0] = 2.0/lengthscale
|
dQc[:,:,0] = 2.0/lengthscale
|
||||||
dQc[:,:,1] = -2.0*variance/lengthscale**2
|
dQc[:,:,1] = -2.0*variance/lengthscale**2
|
||||||
|
|
||||||
dPinf[:,:,0] = 1.0
|
dPinf[:,:,0] = 1.0
|
||||||
dPinf[:,:,1] = 0.0
|
dPinf[:,:,1] = 0.0
|
||||||
|
|
||||||
dP0 = dPinf.copy()
|
dP0 = dPinf.copy()
|
||||||
|
|
||||||
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
||||||
|
|
||||||
class sde_RatQuad(RatQuad):
|
class sde_RatQuad(RatQuad):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Class provide extra functionality to transfer this covariance function into
|
Class provide extra functionality to transfer this covariance function into
|
||||||
SDE form.
|
SDE form.
|
||||||
|
|
||||||
Rational Quadratic kernel:
|
Rational Quadratic kernel:
|
||||||
|
|
||||||
.. math::
|
.. math::
|
||||||
|
|
@ -177,16 +177,16 @@ class sde_RatQuad(RatQuad):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def sde(self):
|
def sde(self):
|
||||||
"""
|
"""
|
||||||
Return the state space representation of the covariance.
|
Return the state space representation of the covariance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert False, 'Not Implemented'
|
assert False, 'Not Implemented'
|
||||||
|
|
||||||
# Params to use:
|
# Params to use:
|
||||||
|
|
||||||
# self.lengthscale
|
# self.lengthscale
|
||||||
# self.variance
|
# self.variance
|
||||||
#self.power
|
#self.power
|
||||||
|
|
||||||
#return (F, L, Qc, H, Pinf, dF, dQc, dPinf)
|
#return (F, L, Qc, H, Pinf, dF, dQc, dPinf)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue