開始使用免費開始

使用自訂矩函式進行最佳化

現在要使用自訂的矩函式來執行最佳化。請回想,在 optimize.portfolio() 中會於評估矩函式時設定投資組合的矩。我們透過在 optimize.portfolio()momentFUN 參數傳入自訂矩函式的名稱來使用它。請注意,你可以利用 PortfolioAnalytics 輕鬆以不同的矩估計方法來執行最佳化,這能讓你比較各種矩估計技巧,並透過分析最佳化結果來微調這些估計。

本練習屬於課程

R 中級投資組合分析

檢視課程

練習說明

已為本練習建立投資組合規格物件 port_spec,以及自訂矩函式 moments_robust()

  • 以自訂的矩估計執行最佳化,並將結果指定給變數 opt_custom
  • 印出 opt_custom 的輸出。
  • 以樣本矩估計執行最佳化,並將結果指定給變數 opt_sample
  • 印出 opt_sample 的輸出。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Run the optimization with custom moment estimates
opt_custom <- optimize.portfolio(R = ___, portfolio = ___, optimize_method = "random", rp = rp, momentFUN = ___)

# Print the results of the optimization with custom moment estimates


# Run the optimization with sample moment estimates
opt_sample <- optimize.portfolio(R = ___, portfolio = ___, optimize_method = "random", rp = rp)

# Print the results of the optimization with sample moment estimates
編輯並執行程式碼