I am implementing the 1D Kalman Filter in Python on a fundamentally noisy set of measurement data, and I should be observing a large amount of smoothing...but, instead, my Kalman Filter is doing the exact opposite of smoothing, so that my filtered results look like this:
And since my "model" is just the simple trend formula, $$x_{k} = 3*(x_{k-1} - x_{k-2}) + x_{k-3} $$ the model comes up with outrageous predictions when the expected smoothing doesn't happen:
I am working off an incompletely specified 1D Kalman Filter implementation, so I am having to fill in the gaps...but I should be seeing much softer filtered values, with the peaks and valleys being outliers, with my naive model. Also, my predictions should actually be more modest than the actual data (which would follow from a smoothed filtered collection).
I am trying to figure out what I am missing. Here is the pseudo code for what I have implemented so far:
Step 1: Minimize T = -log(Q/R) in order to discover Q
Here I basically run the kalman filter with given T values,
calculating Q from the Residual std
Step 2: Run Kalman Filter with T held constant
xprior = [x1,x2,x3] (calculated from the first three values of data)
#prediction
x_{k|k-1} = model_estimate(xprior)
#add measurement
innovation = data[k] - x_{k|k-1}
K = R / (R + Q)
#filtered value
x_{k|k} = x_{k|k-1} + K*(innovation)
xprior <- update_xprior(x_{k|k})
residuals <- update_residuals(measurement,x_{k|k})
R = variance(residuals)
Q = e^(-T)*R
...
I must be missing something...Also, I am not confident that what I am doing with T and the way I am calculating Q is correct...but I should be seeing smoothing regardless, since that is what they did in the document I am working off of.