開始使用免費開始

加入約束條件

使用 add.constraint() 函式將約束條件加入投資組合規格物件。每個加入的約束都是一個獨立物件,並儲存在投資組合物件的 constraints 欄位。如此一來,約束是模組化的,你可以輕鬆在投資組合物件中新增、移除或修改約束。add.constraint() 的必要引數包括要加入約束的 portfolio、約束的 type,以及透過 ... 傳入對應約束建構子的具名引數。

基本約束類型:

  • 指定權重總和的約束
    • weight_sumweightleverage
    • full_investment 是特例,設定 min_sum = max_sum = 1
    • dollar_neutral 是特例,設定 min_sum = max_sum = 0
  • 指定個別資產權重的約束
    • box
    • long_only 是特例,設定 min = 0max = 1
  • 依群組(產業、地區、資產類別等)指定資產權重總和的約束
    • group
  • 指定目標平均報酬的約束
    • return

在本練習中,你會加入幾種較常見的約束。除了上面列出的基本約束類型之外,PortfolioAnalytics 也支援部位上限、周轉率、分散度、因子曝險與槓桿曝險等約束。如果你想了解其他約束類型,請查看各約束建構子的說明文件。說明文件包含約束類型的描述與範例程式碼。

本練習屬於課程

R 中級投資組合分析

檢視課程

練習說明

  • 加入 weight_sum 約束,使權重總和的最小值為 1、最大值為 1。
  • 加入 box 約束,使前 5 檔資產的最小權重為 10%,其餘資產的最小權重為 5%。所有資產的最大權重皆為 40%。
  • 加入 group 約束,使資產 1、5、7、9、10 與 11 為第 1 組,資產 2、3、4、6、8 與 12 為第 2 組。為每一組設定最小權重 40%、最大權重 60%。

動手互動練習

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

# Add the weight sum constraint
port_spec <- add.constraint(portfolio = ___, type = ___, min_sum = ___, max_sum = ___)

# Add the box constraint
port_spec <- add.constraint(portfolio = ___, type = ___, min = c(___), max = ___)

# Add the group constraint
port_spec <- add.constraint(portfolio = ___, type = ___, groups = list(c(___), c(___)), group_min = ___, group_max = ___)


# Print the portfolio specification object

編輯並執行程式碼