開始使用免費開始

解一個簡單的投資組合最佳化問題

在這個第一個練習中,你會學到如何使用 PortfolioAnalytics 解一個簡單的投資組合最佳化問題。你會學會建立投資組合規格物件、加入限制與目標,並解出最佳化結果。此投資組合問題是要在「全額投資」與「僅做多」這兩個限制下,建立一個最小變異投資組合。目標是最小化投資組合變異數。本題有兩個限制:全額投資限制代表權重總和必須為 1;僅做多限制代表所有權重必須大於或等於 0(也就是不允許放空部位)。

本練習屬於課程

R 中級投資組合分析

檢視課程

練習說明

  • 使用 index_returns 資料集中的資產名稱建立一個投資組合規格物件,並將其命名為 port_spec
  • port_spec 物件加入全額投資限制,使權重總和為 1。
  • port_spec 物件加入僅做多限制,使每個資產的權重介於 0 與 1 之間。
  • port_spec 物件加入一個目標,將投資組合的標準差最小化。
  • 使用 optimize_method = "ROI" 解投資組合最佳化問題。將最佳化結果指定給名為 opt 的物件。

動手互動練習

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

# Create the portfolio specification
port_spec <- portfolio.spec(colnames(___))

# Add a full investment constraint such that the weights sum to 1
port_spec <- add.constraint(portfolio = ___, type = "___")

# Add a long only constraint such that the weight of an asset is between 0 and 1
port_spec <- add.constraint(portfolio = ___, type = "___")

# Add an objective to minimize portfolio standard deviation
port_spec <- add.objective(portfolio = ___, type = "___", name = "___")

# Solve the optimization problem
opt <- optimize.portfolio(___, portfolio = ___, optimize_method = "___")
編輯並執行程式碼