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


KG = error_est / (error_est+error_measurment)
estimate = estimate_prev + KG ( measurment -  estimate_prev)
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&gt;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,

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?

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 ?

Are there any parameters you can add to tweak it (such as window in MA) ?
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.
The Kalman filter uses the model of the system, from one state it can get a reasonable estimate, for instance if you are tracking a device with GPS data, you may create a model that assumes that the acceleration has limited variance, from the previous measurements of position you can have an estimate of velocity (part of the state), and if from there you can predict where the device will be.
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$.