權重的時間序列
在上一個練習中,你探索了 Return.portfolio() 函式的功能,並用兩種策略建立了投資組合。不過,在 Return.portfolio() 中將引數 verbose = TRUE 設定為真時,除了投資組合的報酬與貢獻外,你還能建立包含期初(BOP)與期末(EOP)權重與價值的清單。
你可以從 Return.portfolio() 所產生的清單物件中存取這些內容。這個清單包含 $returns、$contributions、$BOP.Weight、$EOP.Weight、$BOP.Value 和 $EOP.Value。
在本練習中,你會重作上一題的投資組合,但進一步以 verbose = TRUE 建立一份計算清單。接著你會將 Apple 的期末權重視覺化。
物件 returns 已經預先載入到你的工作環境中。
本練習屬於課程
R 投資組合分析入門
練習說明
- 定義向量
eq_weights,表示兩個資產等權重。 - 使用買入並持有策略建立報酬的投資組合,並設定
verbose = TRUE。將其命名為pf_bh。 - 使用每月再平衡策略建立報酬的投資組合,並設定
verbose = TRUE。將其命名為pf_rebal。 - 使用
pf_bh的期末權重建立一個名為eop_weight_bh的新物件。 - 使用
pf_rebal的期末權重建立一個名為eop_weight_rebal的新物件。 - 使用
plot.zoo()繪製eop_weight_bh中 Apple 的期末權重,並使用plot.zoo()繪製eop_weight_rebal中 Apple 的期末權重。par(mfrow = c(2, 1), mar = c(2, 4, 2, 2))用來排列你建立的圖形。不要更動這段程式碼。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the weights
eq_weights <-
# Create a portfolio using buy and hold
pf_bh <- Return.portfolio(returns, weights = eq_weights, ___ )
# Create a portfolio that rebalances monthly
pf_rebal <- Return.portfolio(returns, weights = eq_weights, rebalance_on = "months", ___ )
# Create eop_weight_bh
# Create eop_weight_rebal
# Plot end of period weights
par(mfrow = c(2, 1), mar=c(2, 4, 2, 2))
plot.zoo(___$AAPL)
plot.zoo(___$AAPL)