Skip to content

Instantly share code, notes, and snippets.

@bigsnarfdude
Last active April 7, 2023 10:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bigsnarfdude/324a34b199b7dd8de5915311375e5dc2 to your computer and use it in GitHub Desktop.
Save bigsnarfdude/324a34b199b7dd8de5915311375e5dc2 to your computer and use it in GitHub Desktop.
using kalman filter for rolling window instead of average for faster convergence esp 2 sigma
# https://scipy-cookbook.readthedocs.io/items/KalmanFiltering.html
# https://github.com/pykalman/pykalman
import pandas as pd
from pykalman import KalmanFilter
import numpy as np
def rolling_window(a, step):
shape = a.shape[:-1] + (a.shape[-1] - step + 1, step)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
def get_kf_value(y_values):
kf = KalmanFilter()
Kc, Ke = kf.em(y_values, n_iter=1).smooth(0)
return Kc
n = 2000
index = pd.date_range(start='2000-01-01', periods=n)
data = np.random.randn(n, 4)
df = pd.DataFrame(data, columns=list('ABCD'), index=index)
wsize = 3
arr = rolling_window(df.D.values, wsize)
zero_padding = np.zeros(shape=(wsize-1,wsize))
arrst = np.concatenate((zero_padding, arr))
arrkalman = np.zeros(shape=(len(arrst),1))
for i in range(len(arrst)):
arrkalman[i] = get_kf_value(arrst[i])
kalmandf = pd.DataFrame(arrkalman, columns=['D_kalman'], index=index)
df = pd.concat([df,kalmandf], axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment