開始使用免費開始

尋找平均—變異數有效投資組合

所謂平均—變異數有效(mean-variance efficient)的投資組合,是在投資組合期望報酬等於目標報酬的限制下,使投資組合變異數最小的解。R 套件 tseries 中的函式 portfolio.optim() 很方便用來完成這件事。 其預設作法會在「投資組合報酬等於等權重投資組合報酬」的限制下,找出平均—變異數有效的投資組合權重。 唯一需要的引數是要決定權重之投資組合成分的月報酬資料。

主控台已載入變數 returns,其中包含 DJIA 成分股的月報酬率。

本練習屬於課程

R 投資組合分析入門

檢視課程

練習說明

  • 載入函式庫 tseries
  • 使用 portfolio.optim() 的預設設定(目標為等權重投資組合報酬)建立由月報酬構成的平均—變異數有效投資組合,並將輸出指定給變數 opt
  • 從最佳化後的投資組合建立一個權重向量。投資組合權重可在 opt$pw 中找到。將其命名為 pf_weights
  • 使用提供的程式碼指派資產名稱。
  • pf_weights 中挑選大於或等於 1% 的最佳權重,命名為 opt_weights
  • 使用 barplot() 視覺化 opt_weights 的分布。
  • 列印最佳化投資組合的期望報酬(opt$pm)與波動度(opt$ps)。

動手互動練習

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

# Load tseries


# Create an optimized portfolio of returns
opt <- portfolio.optim(___)

# Create pf_weights
pf_weights <- ___$pw

# Assign asset names
names(pf_weights) <- colnames(returns)

# Select optimum weights opt_weights
opt_weights <- pf_weights[___ >= 0.01]

# Bar plot of opt_weights


# Print expected portfolio return and volatility
___$pm
___$ps
 
編輯並執行程式碼