decay_linear(x,d) = weighted moving average over the past d days with linearly decaying weights d,d-1,…,1 (rescaled up to 1)
x中时间从最远到最近的值,分别乘权重d,d-1,…,1(权重要进行标准化,使和为1)
def linear_decay(data, window):
result = pd.DataFrame(np.nan, index=data.index, columns=data.columns)
weight = np.arange(window) + 1.
weight = weight / weight.sum()
for i in range(window, data.shape[0]):
t = data.index[i]
result.ix[t, :] = data[i-window:i].T.dot(weight)
return result
decay_linear(x,d) = weighted moving average over the past d days with linearly decaying weights d,d-1,…,1 (rescaled up to 1)
x中时间从最远到最近的值,分别乘权重d,d-1,…,1(权重要进行标准化,使和为1)
def linear_decay(data, window):
result = pd.DataFrame(np.nan, index=data.index, columns=data.columns)
weight = np.arange(window) + 1.
weight = weight / weight.sum()
for i in range(window, data.shape[0]):
t = data.index[i]
result.ix[t, :] = data[i-window:i].T.dot(weight)
return result