# Kalman Filter implementation is too slow

I have implemented with a simple code a Kalman Filter for time domain, based on these:

1. KG = error_est / (error_est+error_measurment)
2. estimate = estimate_prev + KG ( measurment - estimate_prev)
3. error_est = (1 - KG) * error_est_prev

The simple code to implement is :

error_est = 1.0
error_measurment = 1.0
estimates = []
start = 50.0

# loop over data array of numbers such as [1,2,3,4,..]

for index in range(len(data)):

measurment = data[index]
prev_estimate = estimates[index-1] if index>0 else start

KG = error_est / (error_est+error_measurment)
estimate = prev_estimate + KG * ( measurment -  prev_estimate)
estimates.append(estimate)
error_est = (1.0 - KG) * error_est


Now I have 3 questions,

1. The filter is extremely slow, which mean for array of [1...50] , I will get a result of [1...25], and generally it is much slower than moving average, but i read that it's advantage is that it is fast. What am i doing wrong?

2. I read that Kalman is used to estimate next result, but how is it possible that a filter ( based on the past) can be used to estimate the next result ?

3. Are there any parameters you can add to tweak it (such as window in MA) ?

• Please edit your question for details and clarity. What are your initial values of "error_est" and "measurement_error" ($p$ and $r$, in the scalar case). Do you mean it settles slower, or that it takes more processor resources? What do you mean by "for an array of [1..50], I will get a result of [1..25]" -- your vector size changes??? Your pseudo-code, as given, with an initial variance of $p_1^- = \infty$, results in a Kalman gain of $k_n = 1/n$, which makes your filter an infinitely long moving average. May 23 at 18:30

1. Maybe what is extremely slow is python, if you are comparing with a rolling average function implemented in some library.Specially if the dimension of the observations large, each step it will invert a matrix, $$O(n^3)$$, it seems you have only one observation in your example. Even for similar implementations Kalman filter will be slower than moving average.
3. The things you can tweak in Kalman filter is the covariances of the process and the covariances of the observation, this will tell how much the filter should rely in the estimate vs. the observation. For instance if you don't trust your model you can set the model covariance $$Q$$ large, if your observations are not that accurate you acn increase the observation covariance $$R$$.