分割樣本評估
在第 2 章,你使用過 window() 函式來為繪圖擷取報酬的子集。這個練習中,你會使用 window() 來建立兩個樣本:一個用來估計(estimation)的樣本,以及一個用來評估(evaluation)的樣本。本練習將示範當估計視窗改變時,投資組合權重如何不同。
提醒你,window() 的引數為 x、start 和 end。其中 start 與 end 的格式為 "YYYY-MM-DD"。
物件 returns 已載入到你的工作空間。
本練習屬於課程
R 投資組合分析入門
練習說明
- 建立樣本
returns_estim,以returns為基礎擷取子集,樣本起始為 1991-01-01,結束為 2003-12-31。 - 建立樣本
returns_eval,以returns為基礎擷取子集,樣本起始為 2004 年第一天,結束為 2015 年最後一天。 - 建立一個最大權重向量,元素皆為 10%,長度等於
returns的欄數,命名為max_weights。 - 以估計樣本建立投資組合
pf_estim,並將最大權重(reshigh)設為max_weights。 - 以評估樣本建立投資組合
pf_eval,並將最大權重(reshigh)設為max_weights。 - 繪製評估投資組合權重與估計投資組合權重的散佈圖(你可以使用
$pw)。如果投資組合權重相同,點位應落在 45 度線上。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create returns_estim
returns_estim <- window(___, start = "YYYY-MM-DD", end = "YYYY-MM-DD")
# Create returns_eval
# Create vector of max weights
max_weights <- rep(___, ncol(___))
# Create portfolio with estimation sample
pf_estim <- portfolio.optim(___, reshigh = ___)
# Create portfolio with evaluation sample
# Create a scatter plot with evaluation portfolio weights on the vertical axis
plot(___, ___)
abline(a = 0, b = 1, lty = 3)