一元線性迴歸的斜率公式是:
\[k = \frac{(x - \bar{x})^T (y - \bar{y})}{\|x - \bar{x}\|^2}
\]
由於斜率具有平移不變性,x
通常取 0 到視窗大小減一。
def slope(df, close_col='close', slope_col='slope', window=5, inplace=True):
if not inplace: df = df.copy()
x = np.arange(window, dtype='f')
x -= x.mean()
x_sq_sum = (x ** 2).sum()
df[slope_col] = df[close_col].rolling(window) \
.apply(lambda y: ((y - y.mean()) * x).sum() / x_sq_sum)
return df
測試:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
df = pd.DataFrame({'close': np.random.randint(-1000, 1000, [100])})
slope(df)
df.slope = df.slope.shift(-2)
df.plot()
plt.show()