一元线性回归的斜率公式是:
\[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()