Timeseries Methods: Kalman Filter from scratch in Python — Part 1

Edwina Gu
2 min readFeb 15, 2023

These posts will be focused on application of various method in analyzing timeseries dataset (stock market dataset).

This first post is about Kalman Filter. History and details can be found in the wiki. There will be several posts on Kalman Filter, starting with the basic linear Kalman Filter with 1D dataset to multidimension dataset in nonlinear space.

Using the equation from the wiki we could easily create a function in python using numpy.


def kalman_filter(x_init, F, Q, R, H, data, B=None, u=None, sd=0, num_state=1):
X_post = x_init
P_post = np.eye(num_state) * sd
num_steps = data.shape[1]
mean = []
covar = []
if B is None:
B = np.array([[0]])
u = np.array([[0]])

for i in range(num_steps):
print(i)
z_k = data[:,i]
#predict
X_prior = np.dot(F, X_post) + np.dot(B, u)
P_prior = np.dot(np.dot(F, P_post) , F.T) + Q
#update
resid = z_k - np.dot(H ,X_prior)
S_k = np.dot(np.dot(H, P_prior), H.T) + R
K_k = np.dot(np.dot(P_prior,H.T), np.linalg.inv(S_k))
X_post = X_prior + np.dot(K_k , resid)
P_post = np.dot(np.eye(num_state) - np.dot(K_k , H), P_prior)
mean.append(X_post)
covar.append(P_prior)
return mean, covar

To validate the result let’s assume that the true model is in the form of y = 3 with random white noise. initializing conditions are as follow:

--

--