I've trained a gaussian process which will take X (x1:5) and predict Y (x6). I'm trying to do 1step ahead prediction with Unscented Kalman filter with this GP as my state transition funtion. The current model is from this paper:
with f being GP function. Here is the main part of my code in Python:
def f_2(x, dt):
""" state transition function """
F = np.append(
np.dot( np.array([[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1]]) , x)
,
GP.predict_GP_regression(x.reshape(1,5),mr)[0]
)
return F
def h_2(x):
return np.dot(np.array([[0,0,0,0,1]]), x)
ukf = UKF(dim_x=5, dim_z=1, fx=f_2, hx=h_2, dt=dt, kappa=0)
ukf.x = xpr[:5]
ukf.R = .0001 #(np.random.randn())
#ukf.P = np.diag(np.array([1.001,1.001,1.001,1.001,1.001]))
v = GP.predict_GP_regression(ukf.x.reshape(1,5),mr)[1]
q= np.diag(np.array([1,0.,0.,0.,0.]))
q[0][0] = np.sqrt(v) # or np.sqrt?
ukf.Q = q #
uxs = []
for i in range(1,len(xpr)-5):
z = (xpr[i:i+5])
ukf.predict() # 1step forecast
print "one step ahead x : ", ukf.x
temp_ukf = deepcopy(ukf)
temp_ukf.Q[0][0] = GP.predict_GP_regression(temp_ukf.x.reshape(1,5),mr)[1]
temp_ukf.predict() #2step forecast
print "two step ahead x : ", temp_ukf.x
uxs.append(temp_ukf.x.copy()[4])
ukf.update(z[4])
print "updated : ", ukf.x
ukf.Q[0][0] = GP.predict_GP_regression(ukf.x.reshape(1,5),mr)[1]
uxs = np.asarray(uxs)
predictions = uxs + avg[6:]
expected = data[6:]
The problem that I have is that after the update, my state vector is completely changed, while what I want is to only update the last element of it:
one step ahead x : [ -5.52173913 8.82608696 32.2173913 -16.39130435 -7.89397634]
two step ahead x : [ 8.82608696 32.2173913 -16.39130435 -7.89397634 -9.53768887]
updated : [ -8.25530324 -0.19687363 37.25430811 25.76093524 16.42698078]
what I want is that upon observing the new value, the state changes to :
[ -5.52173913 8.82608696 32.2173913 -16.39130435 updated]
I've studied kalman filter (mostly from this book), yetI don't understand how to change my model to keep it from altering all elements of my state vector.
EDIT Here is the scaled unscented kf code, which gives an error when applying the predict function:
ukf1 = sUKF(dim_x=5, dim_z=1, fx=f_, hx=h_, dt=dt, beta=2,alpha=1e-3 ,kappa=0)
now in the previous code, if we use this new object, it throws this error on predict:
File "C:\Continuum\Anaconda\lib\site-packages\filterpy\kalman\SUKF.py", line 181, in predict
sigmas = self.sigma_points(self.x, self.P, self.kappa)
TypeError: sigma_points() takes exactly 4 arguments (3 given)