mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-07 19:12:40 +02:00
UPD: Added SVD Kalman Filter, EM algorithm for gradient calculation (only for discrete KF)
This commit is contained in:
parent
abdce992ec
commit
b8e21057f5
5 changed files with 786 additions and 67 deletions
|
|
@ -38,11 +38,11 @@ class sde_Matern32(Matern32):
|
||||||
lengthscale = float(self.lengthscale.values)
|
lengthscale = float(self.lengthscale.values)
|
||||||
|
|
||||||
foo = np.sqrt(3.)/lengthscale
|
foo = np.sqrt(3.)/lengthscale
|
||||||
F = np.array(((0, 1), (-foo**2, -2*foo)))
|
F = np.array(((0, 1.0), (-foo**2, -2*foo)))
|
||||||
L = np.array(( (0,), (1,) ))
|
L = np.array(( (0,), (1.0,) ))
|
||||||
Qc = np.array(((12.*np.sqrt(3) / lengthscale**3 * variance,),))
|
Qc = np.array(((12.*np.sqrt(3) / lengthscale**3 * variance,),))
|
||||||
H = np.array(((1, 0),))
|
H = np.array(((1.0, 0),))
|
||||||
Pinf = np.array(((variance, 0), (0, 3.*variance/(lengthscale**2))))
|
Pinf = np.array(((variance, 0.0), (0.0, 3.*variance/(lengthscale**2))))
|
||||||
P0 = Pinf.copy()
|
P0 = Pinf.copy()
|
||||||
|
|
||||||
# Allocate space for the derivatives
|
# Allocate space for the derivatives
|
||||||
|
|
|
||||||
|
|
@ -56,19 +56,20 @@ class sde_StdPeriodic(StdPeriodic):
|
||||||
|
|
||||||
|
|
||||||
w0 = 2*np.pi/self.wavelengths # frequency
|
w0 = 2*np.pi/self.wavelengths # frequency
|
||||||
|
lengthscales = 2*self.lengthscales
|
||||||
|
|
||||||
[q2,dq2l] = seriescoeff(N,2*self.lengthscales,self.variance)
|
[q2,dq2l] = seriescoeff(N,lengthscales,self.variance)
|
||||||
# lengthscale is multiplied by 2 because of slightly different
|
# lengthscale is multiplied by 2 because of slightly different
|
||||||
# formula for periodic covariance function.
|
# formula for periodic covariance function.
|
||||||
# For the same reason:
|
# For the same reason:
|
||||||
|
|
||||||
dq2l = 2*dq2l
|
dq2l = 2*dq2l
|
||||||
|
|
||||||
if np.any( np.isnan(q2)):
|
if np.any( np.isfinite(q2) == False):
|
||||||
raise ValueError("SDE periodic covariance error1")
|
raise ValueError("SDE periodic covariance error 1")
|
||||||
|
|
||||||
if np.any( np.isnan(dq2l)):
|
if np.any( np.isfinite(dq2l) == False):
|
||||||
raise ValueError("SDE periodic covariance error1")
|
raise ValueError("SDE periodic covariance error 2")
|
||||||
|
|
||||||
F = np.kron(np.diag(range(0,N+1)),np.array( ((0, -w0), (w0, 0)) ) )
|
F = np.kron(np.diag(range(0,N+1)),np.array( ((0, -w0), (w0, 0)) ) )
|
||||||
L = np.eye(2*(N+1))
|
L = np.eye(2*(N+1))
|
||||||
|
|
@ -159,8 +160,9 @@ def seriescoeff(m=6,lengthScale=1.0,magnSigma2=1.0, true_covariance=False):
|
||||||
|
|
||||||
else:
|
else:
|
||||||
coeffs = 2*magnSigma2*sp.exp( -lengthScale**(-2) ) * special.iv(range(0,m+1),1.0/lengthScale**(2))
|
coeffs = 2*magnSigma2*sp.exp( -lengthScale**(-2) ) * special.iv(range(0,m+1),1.0/lengthScale**(2))
|
||||||
if np.any( np.isnan(coeffs)):
|
if np.any( np.isfinite(coeffs) == False):
|
||||||
pass
|
raise ValueError("sde_standard_periodic: Coefficients are not finite!")
|
||||||
|
#import pdb; pdb.set_trace()
|
||||||
coeffs[0] = 0.5*coeffs[0]
|
coeffs[0] = 0.5*coeffs[0]
|
||||||
|
|
||||||
# Derivatives wrt (lengthScale)
|
# Derivatives wrt (lengthScale)
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ class sde_RBF(RBF):
|
||||||
|
|
||||||
# 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)
|
||||||
# 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])
|
||||||
|
|
@ -96,13 +96,14 @@ class sde_RBF(RBF):
|
||||||
dPinf[:,:,0] = dPinf_variance
|
dPinf[:,:,0] = dPinf_variance
|
||||||
dPinf[:,:,1] = dPinf_lengthscale
|
dPinf[:,:,1] = dPinf_lengthscale
|
||||||
|
|
||||||
# Benefits of this are unjustified
|
|
||||||
#import GPy.models.state_space_main as ssm
|
|
||||||
#(F, L, Qc, H, Pinf, dF, dQc, dPinf,T) = ssm.balance_ss_model(F, L, Qc, H, Pinf, dF, dQc, dPinf)
|
|
||||||
|
|
||||||
P0 = Pinf.copy()
|
P0 = Pinf.copy()
|
||||||
dP0 = dPinf.copy()
|
dP0 = dPinf.copy()
|
||||||
|
|
||||||
|
# Benefits of this are not very sound. Helps only in one case:
|
||||||
|
# SVD Kalman + RBF kernel
|
||||||
|
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 )
|
||||||
|
|
||||||
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):
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -31,7 +31,7 @@ from .. import likelihoods
|
||||||
from . import state_space_main as ssm
|
from . import state_space_main as ssm
|
||||||
|
|
||||||
class StateSpace(Model):
|
class StateSpace(Model):
|
||||||
def __init__(self, X, Y, kernel=None, sigma2=1.0, name='StateSpace'):
|
def __init__(self, X, Y, kernel=None, noise_var=1.0, kalman_filter_type = 'regular', name='StateSpace'):
|
||||||
super(StateSpace, self).__init__(name=name)
|
super(StateSpace, self).__init__(name=name)
|
||||||
self.num_data, input_dim = X.shape
|
self.num_data, input_dim = X.shape
|
||||||
assert input_dim==1, "State space methods for time only"
|
assert input_dim==1, "State space methods for time only"
|
||||||
|
|
@ -42,13 +42,15 @@ class StateSpace(Model):
|
||||||
assert num_data_Y == self.num_data, "X and Y data don't match"
|
assert num_data_Y == self.num_data, "X and Y data don't match"
|
||||||
assert self.output_dim == 1, "State space methods for single outputs only"
|
assert self.output_dim == 1, "State space methods for single outputs only"
|
||||||
|
|
||||||
|
self.kalman_filter_type = kalman_filter_type
|
||||||
|
|
||||||
# Make sure the observations are ordered in time
|
# Make sure the observations are ordered in time
|
||||||
sort_index = np.argsort(X[:,0])
|
sort_index = np.argsort(X[:,0])
|
||||||
self.X = X[sort_index]
|
self.X = X[sort_index]
|
||||||
self.Y = Y[sort_index]
|
self.Y = Y[sort_index]
|
||||||
|
|
||||||
# Noise variance
|
# Noise variance
|
||||||
self.likelihood = likelihoods.Gaussian()
|
self.likelihood = likelihoods.Gaussian(variance=noise_var)
|
||||||
|
|
||||||
# Default kernel
|
# Default kernel
|
||||||
if kernel is None:
|
if kernel is None:
|
||||||
|
|
@ -68,6 +70,7 @@ class StateSpace(Model):
|
||||||
"""
|
"""
|
||||||
Parameters have now changed
|
Parameters have now changed
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# Get the model matrices from the kernel
|
# Get the model matrices from the kernel
|
||||||
(F,L,Qc,H,P_inf, P0, dFt,dQct,dP_inft, dP0t) = self.kern.sde()
|
(F,L,Qc,H,P_inf, P0, dFt,dQct,dP_inft, dP0t) = self.kern.sde()
|
||||||
|
|
@ -92,9 +95,10 @@ class StateSpace(Model):
|
||||||
dR = np.zeros([measurement_dim,measurement_dim,grad_params_no])
|
dR = np.zeros([measurement_dim,measurement_dim,grad_params_no])
|
||||||
dR[:,:,-1] = np.eye(measurement_dim)
|
dR[:,:,-1] = np.eye(measurement_dim)
|
||||||
|
|
||||||
#(F,L,Qc,H,P_inf,dF,dQc,dP_inf) = ssm.balance_ss_model(F,L,Qc,H,P_inf,dF,dQc,dP_inf)
|
# Balancing
|
||||||
# Use the Kalman filter to evaluate the likelihood
|
#(F,L,Qc,H,P_inf,P0, dF,dQc,dP_inf,dP0) = ssm.balance_ss_model(F,L,Qc,H,P_inf,P0, dF,dQc,dP_inf, dP0)
|
||||||
|
|
||||||
|
# Use the Kalman filter to evaluate the likelihood
|
||||||
grad_calc_params = {}
|
grad_calc_params = {}
|
||||||
grad_calc_params['dP_inf'] = dP_inf
|
grad_calc_params['dP_inf'] = dP_inf
|
||||||
grad_calc_params['dF'] = dF
|
grad_calc_params['dF'] = dF
|
||||||
|
|
@ -102,10 +106,12 @@ class StateSpace(Model):
|
||||||
grad_calc_params['dR'] = dR
|
grad_calc_params['dR'] = dR
|
||||||
grad_calc_params['dP_init'] = dP0
|
grad_calc_params['dP_init'] = dP0
|
||||||
|
|
||||||
|
kalman_filter_type = self.kalman_filter_type
|
||||||
|
|
||||||
(filter_means, filter_covs, log_likelihood,
|
(filter_means, filter_covs, log_likelihood,
|
||||||
grad_log_likelihood,SmootherMatrObject) = ssm.ContDescrStateSpace.cont_discr_kalman_filter(F,L,Qc,H,
|
grad_log_likelihood,SmootherMatrObject) = ssm.ContDescrStateSpace.cont_discr_kalman_filter(F,L,Qc,H,
|
||||||
float(self.Gaussian_noise.variance),P_inf,self.X,self.Y,m_init=None,
|
float(self.Gaussian_noise.variance),P_inf,self.X,self.Y,m_init=None,
|
||||||
P_init=P0, calc_log_likelihood=True,
|
P_init=P0, p_kalman_filter_type = kalman_filter_type, calc_log_likelihood=True,
|
||||||
calc_grad_log_likelihood=True,
|
calc_grad_log_likelihood=True,
|
||||||
grad_params_no=grad_params_no,
|
grad_params_no=grad_params_no,
|
||||||
grad_calc_params=grad_calc_params)
|
grad_calc_params=grad_calc_params)
|
||||||
|
|
@ -120,7 +126,7 @@ class StateSpace(Model):
|
||||||
def log_likelihood(self):
|
def log_likelihood(self):
|
||||||
return self._log_marginal_likelihood
|
return self._log_marginal_likelihood
|
||||||
|
|
||||||
def _raw_predict(self, Xnew, Ynew=None, filteronly=False):
|
def _raw_predict(self, Xnew=None, Ynew=None, filteronly=False):
|
||||||
"""
|
"""
|
||||||
Performs the actual prediction for new X points.
|
Performs the actual prediction for new X points.
|
||||||
Inner function. It is called only from inside this class.
|
Inner function. It is called only from inside this class.
|
||||||
|
|
@ -154,9 +160,15 @@ class StateSpace(Model):
|
||||||
Ynew = self.Y
|
Ynew = self.Y
|
||||||
|
|
||||||
# Make a single matrix containing training and testing points
|
# Make a single matrix containing training and testing points
|
||||||
X = np.vstack((self.X, Xnew))
|
if Xnew is not None:
|
||||||
Y = np.vstack((Ynew, np.nan*np.zeros(Xnew.shape)))
|
X = np.vstack((self.X, Xnew))
|
||||||
|
Y = np.vstack((Ynew, np.nan*np.zeros(Xnew.shape)))
|
||||||
|
predict_only_training = False
|
||||||
|
else:
|
||||||
|
X = self.X
|
||||||
|
Y = Ynew
|
||||||
|
predict_only_training = True
|
||||||
|
|
||||||
# Sort the matrix (save the order)
|
# Sort the matrix (save the order)
|
||||||
_, return_index, return_inverse = np.unique(X,True,True)
|
_, return_index, return_inverse = np.unique(X,True,True)
|
||||||
X = X[return_index] # TODO they are not used
|
X = X[return_index] # TODO they are not used
|
||||||
|
|
@ -170,10 +182,14 @@ class StateSpace(Model):
|
||||||
#Y = self.Y[:, 0,0]
|
#Y = self.Y[:, 0,0]
|
||||||
# Run the Kalman filter
|
# Run the Kalman filter
|
||||||
#import pdb; pdb.set_trace()
|
#import pdb; pdb.set_trace()
|
||||||
|
|
||||||
|
kalman_filter_type = self.kalman_filter_type
|
||||||
|
|
||||||
(M, P, log_likelihood,
|
(M, P, log_likelihood,
|
||||||
grad_log_likelihood,SmootherMatrObject) = ssm.ContDescrStateSpace.cont_discr_kalman_filter(
|
grad_log_likelihood,SmootherMatrObject) = ssm.ContDescrStateSpace.cont_discr_kalman_filter(
|
||||||
F,L,Qc,H,float(self.Gaussian_noise.variance),P_inf,self.X,Y,m_init=None,
|
F,L,Qc,H,float(self.Gaussian_noise.variance),P_inf,self.X,Y,m_init=None,
|
||||||
P_init=P0, calc_log_likelihood=False,
|
P_init=P0, p_kalman_filter_type = kalman_filter_type,
|
||||||
|
calc_log_likelihood=False,
|
||||||
calc_grad_log_likelihood=False)
|
calc_grad_log_likelihood=False)
|
||||||
# Run the Rauch-Tung-Striebel smoother
|
# Run the Rauch-Tung-Striebel smoother
|
||||||
if not filteronly:
|
if not filteronly:
|
||||||
|
|
@ -189,8 +205,9 @@ class StateSpace(Model):
|
||||||
P = P[return_inverse,:,:]
|
P = P[return_inverse,:,:]
|
||||||
|
|
||||||
# Only return the values for Xnew
|
# Only return the values for Xnew
|
||||||
M = M[self.num_data:,:]
|
if not predict_only_training:
|
||||||
P = P[self.num_data:,:,:]
|
M = M[self.num_data:,:]
|
||||||
|
P = P[self.num_data:,:,:]
|
||||||
|
|
||||||
# Calculate the mean and variance
|
# Calculate the mean and variance
|
||||||
m = np.dot(M,H.T)
|
m = np.dot(M,H.T)
|
||||||
|
|
@ -201,7 +218,7 @@ class StateSpace(Model):
|
||||||
# Return the posterior of the state
|
# Return the posterior of the state
|
||||||
return (m, V)
|
return (m, V)
|
||||||
|
|
||||||
def predict(self, Xnew, filteronly=False):
|
def predict(self, Xnew=None, filteronly=False):
|
||||||
|
|
||||||
# Run the Kalman filter to get the state
|
# Run the Kalman filter to get the state
|
||||||
(m, V) = self._raw_predict(Xnew,filteronly=filteronly)
|
(m, V) = self._raw_predict(Xnew,filteronly=filteronly)
|
||||||
|
|
@ -216,7 +233,7 @@ class StateSpace(Model):
|
||||||
# Return mean and variance
|
# Return mean and variance
|
||||||
return (m, V, lower, upper)
|
return (m, V, lower, upper)
|
||||||
|
|
||||||
def predict_quantiles(self, Xnew, quantiles=(2.5, 97.5)):
|
def predict_quantiles(self, Xnew=None, quantiles=(2.5, 97.5)):
|
||||||
mu, var = self._raw_predict(Xnew)
|
mu, var = self._raw_predict(Xnew)
|
||||||
#import pdb; pdb.set_trace()
|
#import pdb; pdb.set_trace()
|
||||||
return [stats.norm.ppf(q/100.)*np.sqrt(var + float(self.Gaussian_noise.variance)) + mu for q in quantiles]
|
return [stats.norm.ppf(q/100.)*np.sqrt(var + float(self.Gaussian_noise.variance)) + mu for q in quantiles]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue