Does Kalman Filter using pykalman on linear trends give correct answers?

549 views Asked by At

I am trying to use KalmanFilter to estimate the mean value of a series but I was unable to find much information related to it for linear trends, so I was trying to use it to predict the values when the input is just a straight line with positive slope.

from pykalman import KalmanFilter as KF
y=np.arange(0,100,1)
y=pd.DataFrame(y)
x=y.shift(1)
x=pd.DataFrame(x,index=np.arange(0,100,1))
kf = KF(transition_matrices = [1],
    observation_matrices = [1],
    initial_state_mean = 10,
    initial_state_covariance = 1,
    observation_covariance=1,
    transition_covariance=.01)

state_means, _ = kf.filter(x.dropna().values)
d={'a':np.asarray(x),'b':np.asarray(state_means)}
sm = pd.DataFrame(state_means,index=x.index[:-1],columns=['state'])
sma=x.rolling(window=10).mean()
x['kalman']=sm
x['rolling']=sma
x.plot(figsize=(10,8))

I was able to apply it but I am not sure if this is correct or not. I am seeing a gap between the actual values and the kalman state means: graph.

I thought that KalmanFilter will just finish that gap and eventually coincide with the straight line but they seem to be parallel after a while.

Is this correct or am I doing something wrong?

1

There are 1 answers

4
bakaDev On

Here is the code that I've shared in the comments.

from pykalman import KalmanFilter as KF
import numpy as np
import pandas as pd

y=np.arange(0,100,1)
y=pd.DataFrame(y)
x=y.shift(1)
x=pd.DataFrame(x,index=np.arange(0,100,1))
kf = KF(initial_state_mean=0, n_dim_obs=1)
#kf = kf.em(x.dropna().values, n_iter=5)
state_means, _ = kf.filter(x.dropna().values)
d={'a':np.asarray(x),'b':np.asarray(state_means)}
sm = pd.DataFrame(state_means,index=x.index[:-1],columns=['state'])
sma=x.rolling(window=10).mean()
x['kalman']=sm
x['rolling']=sma
x.plot(figsize=(10,8))

enter image description here