# Unscented Kalman Filter with Gaussian Process regression for time series prediction [closed]

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)
)
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)
q= np.diag(np.array([1,0.,0.,0.,0.]))
q = 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 = GP.predict_GP_regression(temp_ukf.x.reshape(1,5),mr)
temp_ukf.predict()  #2step forecast
print "two step ahead x : ", temp_ukf.x
uxs.append(temp_ukf.x.copy())
ukf.update(z)
print "updated : ", ukf.x
ukf.Q = GP.predict_GP_regression(ukf.x.reshape(1,5),mr)

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)

• Are you using FilterPy? May 24, 2015 at 7:29
• This sounds like a question about how to use the python package - I suppose there's an underlying conceptual issue but it should be explained more clearly Jul 1, 2017 at 19:10

I assume you are using FilterPy (since you're following his book). You are looking at UKF.x, which represents the computed filter values (mean), and by definition changes on calling UKF.update(). What it seems like you are wanting are the actual state predictions, which are in UKF.xp. Calling UKF.update() basically says to create a new filter given a new state based on the new measurements (z in your example).

• Thanks. but there are a few problems: 1) xp is only for scaledUKF. I added \alpha and \beta to get SUKF object, but now it's giving me this error 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)
– Alex
May 24, 2015 at 15:52
• xp should be a readable for all UKFs, and the docs seem to say as much. Why do you say it is only for scaledUKF? May 24, 2015 at 16:09
• Well, maybe I'm missing sth. it does say it on the docs you mentioned. but in the source code on github, as well as my ukf object there are no xps. github.com/rlabbe/filterpy/blob/master/filterpy/kalman/UKF.py
– Alex
May 24, 2015 at 16:20
• I see, the version on github is much older than the one on the docs
– Alex
May 24, 2015 at 16:22
• so let's say we are working with the latest version. we call predict() once and when the new observation comes, we call update(). isn't this still going to completely change my x (not just the last one), which will be used the next time predict() is called?
– Alex
May 24, 2015 at 16:25