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: .
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?
Here is the code that I've shared in the comments.