最小化 CVaR
這個練習會讓你實作以 CVaR 最小化為風險管理目標的 PyPortfolioOpt 工具。
你會載入 pypfopt.efficient_frontier 模組並取得 EfficientCVaR 類別,然後使用 2005–2010 年期間的投資銀行資產來建立該類別的實例。
接著,你會使用此實例的 min_cvar() 方法,找出能將 CVaR 降到最低的最適投資組合權重。
投資組合資產報酬位於向量 returns 中——本練習也會使用字典 names,將投資組合權重對應到銀行名稱。
本練習屬於課程
Python 量化風險管理
練習說明
- 從
pypfopt.efficient_frontier匯入EfficientCVaR類別。 - 使用
returns建立EfficientCVaR類別實例ec;注意你不需要expected_returns,因為此目標函式不同於均值–變異數最佳化。 - 使用
ec的.min_cvar()方法尋找並顯示最適投資組合。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the EfficientCVaR class
from pypfopt.____ import EfficientCVaR
# Create the efficient frontier for CVaR minimization
ec = ____(None, ____)
# Find the cVaR-minimizing portfolio weights at the default 95% confidence level
optimal_weights = ec.____()
# Map the values in optimal_weights to the bank names
optimal_weights = { names[i] : optimal_weights[i] for i in optimal_weights}
# Display the optimal weights
print(optimal_weights)