给模型可比较的输入
250 这个价格对模型毫无意义:它取决于股票,又会随时间上升,所以 2020 年的数值永远不像 2010 年的。特征工程把原始数据变成跨时间、跨资产可比较的输入:用收益率代替价格,用波动率代替区间,用标准化分数代替绝对水平。好的特征对结果的影响比模型选择更大,而且每个特征只能描述当天已知的信息。
import numpy as np
prices = np.array([100.0, 101.0, 99.0, 102.0, 104.0, 103.0, 106.0])
window = 3
momentum = np.full(len(prices), np.nan)
momentum[window:] = prices[window:] / prices[:-window] - 1 # the return over the last 3 days
print(np.round(momentum, 4)) # nan for the first 3 days: no price 3 days back向后看的窗口
大多数特征是向后看的窗口的统计量:包含今天在内的最近 n 个值。动量比较今天与 n 天前;滚动波动率是最近 n 个收益率的标准差;滚动 z 分数说明今天距窗口均值有多少个窗口标准差。用 sliding_window_view 可以把所有窗口当作矩阵的行,沿某个轴计算统计量,不需要循环。前 n - 1 天没有完整窗口,保持为 nan。
import numpy as np
prices = np.array([100.0, 101.0, 99.0, 102.0, 104.0, 103.0, 106.0])
returns = np.diff(prices) / prices[:-1]
window = 3
windows = np.lib.stride_tricks.sliding_window_view(returns, window) # every 3-day window as a row
vol = np.full(len(returns), np.nan)
vol[window - 1:] = windows.std(axis=1, ddof=1)
print(np.round(vol, 4)) # nan nan then one value per complete window
p_windows = np.lib.stride_tricks.sliding_window_view(prices, window)
z = np.full(len(prices), np.nan)
z[window - 1:] = (prices[window - 1:] - p_windows.mean(axis=1)) / p_windows.std(axis=1, ddof=1)
print(np.round(z, 3)) # how unusual each price is for its last 3 days# Features put prices of different stocks on the same footing
import numpy as np
cheap = np.array([5.0, 5.2, 5.1, 5.4, 5.5])
dear = np.array([500.0, 520.0, 510.0, 540.0, 550.0]) # ten times the price, the same movements
print(cheap[-1] / cheap[-3] - 1, dear[-1] / dear[-3] - 1) # identical momentum, about 0.0784 for both
print(dear[-1] - dear[-3], cheap[-1] - cheap[-3]) # price changes (40.0 and 0.4) would not be comparable目标向前看
模型要预测的是目标,通常是未来 h 天的收益率:prices[t + h] / prices[t] - 1。它是唯一允许向前看的一列,最后 h 天因为未来尚未发生而为 nan,训练前要去掉这些行。常见的错误是特征意外包含了目标,例如居中的窗口、用全部数据算出的统计量,或把未来收益率当作输入。检验很简单:加入后面的日期时,第 t 天的特征不应该改变。
import numpy as np
def forward_return(prices, horizon):
out = np.full(len(prices), np.nan)
out[:-horizon] = prices[horizon:] / prices[:-horizon] - 1
return out
def momentum(prices, window):
out = np.full(len(prices), np.nan)
out[window:] = prices[window:] / prices[:-window] - 1
return out
prices = np.array([100.0, 101.0, 99.0, 102.0, 104.0, 103.0, 106.0])
print(np.round(momentum(prices, 2), 4)) # looks back: known on each day
print(np.round(forward_return(prices, 2), 4)) # looks forward: the target, nan for the last 2 days
# The prefix test: a feature computed on the first 5 days equals the first 5 values of the full computation
print(np.allclose(momentum(prices[:5], 2), momentum(prices, 2)[:5], equal_nan=True)) # True当心暗中就是目标的特征预测得太好的特征是危险信号。用居中窗口算的动量、包含明天收益率的波动率、用全样本标准化的 z 分数:每一种都会让回测光彩夺目,而实盘失败。每个特征都应当能在第
t 天早晨的生产环境中算出,并用上面的前缀检验来验证。- 把价格变成可比较的特征:动量
prices[t] / prices[t - n] - 1、滚动波动率、滚动 z 分数,均基于包含今天的向后窗口。 sliding_window_view把每个窗口当作一行,统计量无需循环;前n - 1天保持为nan。- 未来收益率
prices[t + h] / prices[t] - 1是目标而非特征;用前缀检验检查每个特征。
下一节:滚动前向验证,按时间真实流逝的顺序检验模型。
实验:评分内容
- 用
momentum度量趋势25 分 - 用
rolling_vol跟踪近期风险25 分 - 用
rolling_zscore表示今天有多反常25 分 - 用
forward_return定义预测目标25 分