計算 EWMA
現在我們要開始建立一些特徵,來預測理想的投資組合。暫時先以價格變動作為特徵。為此,我們會先計算每日的指數加權移動平均(EWMA),再將其重採樣到每月的時間框架。最後,把每月價格移動平均往未來平移 1 個月,這樣就能把它當作特徵,用來預測未來的投資組合。
本練習屬於課程
Python 金融 Machine Learning
練習說明
- 使用
span為 30 計算每日的指數加權移動平均(ewma_daily)。 - 以 Business Monthly Start(BMS)頻率將每日 EWMA 重採樣為每月,並取每個月的第 1 天(
.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])