计算 EWMA
现在我们将开始构建一些特征,用来预测理想的投资组合。当前我们先把价格变动作为特征。为此,我们将先计算按日的指数加权移动平均(EWMA),再将其重采样到按月频率。最后,把价格的月度移动平均向未来平移 1 个月,这样就能把它作为特征来预测下个月的投资组合。
本练习是课程的一部分
Python 金融机器学习
练习说明
- 使用
span为 30 来计算日度的指数加权移动平均(ewma_daily)。 - 将日度 EWMA 重采样为月度,使用 Business Monthly Start 频率(BMS)并取每月的第一天(
.first())。 - 将
ewma_monthly向前平移 1 个月,从而使用上个月的 EWMA 作为特征来预测下个月的理想投资组合。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Calculate exponentially-weighted moving average of daily returns
ewma_daily = returns_daily.ewm(span=____).mean()
# Resample daily returns to first business day of the month with the first day for that month
ewma_monthly = ewma_daily.resample(____).____
# Shift ewma for the month by 1 month forward so we can use it as a feature for future predictions
ewma_monthly = ewma_monthly.____.dropna()
print(ewma_monthly.iloc[-1])