樣本動差估計
估計投資組合動差的預設方法是樣本法。optimize.portfolio() 會呼叫傳入 momentFUN 參數的函式來計算動差。momentFUN 的預設為 set.portfolio.moments(),其預設行為是計算樣本動差。接著會將這些動差作為目標函式的輸入。需要估計哪些動差取決於目標。例如,將投資組合的標準差最小化的目標只需要第二動差的估計;相較之下,要最大化 Sharpe Ratio 的目標則需要同時估計第一與第二動差。樣本動差估計的缺點包括估計誤差與維度詛咒。隨著資產維度與需估計的參數增加,估計誤差的風險也會上升。
本練習屬於課程
R 中級投資組合分析
練習說明
- 新增一個報酬目標,目標名稱使用
"mean"。 - 使用
set.portfolio.moments計算樣本動差。指定給名為moments的變數。 - 檢查第一動差是否等於平均報酬的樣本估計值。
- 新增一個風險目標,目標名稱使用
"StdDev"。 - 使用
set.portfolio.moments計算樣本動差。指定給名為moments的變數。 - 檢查第二動差是否等於變異數—共變異數矩陣的樣本估計值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Add a return objective with "mean" as the objective name
port_spec <- add.objective(portfolio = port_spec, type = ___, name = ___)
# Calculate the sample moments
moments <- set.portfolio.moments(R = ___, portfolio = ___)
# Check if moments$mu is equal to the sample estimate of mean returns
moments$mu == colMeans(asset_returns)
# Add a risk objective with "StdDev" as the objective name
port_spec <- add.objective(portfolio = port_spec, type = ___, name = ___)
# Calculate the sample moments using set.portfolio.moments. Assign to a variable named moments.
moments <- set.portfolio.moments(R = ___, portfolio = ___)
# Check if moments$sigma is equal to the sample estimate of the variance-covariance matrix
moments$sigma == cov(asset_returns)