現代投資組合理論

Python 投資組合分析入門

Charlotte Werger

Data Scientist

建立最適投資組合

投資策略圖示

Python 投資組合分析入門

什麼是投資組合最佳化?

認識 Harry Markowitz

Harry Markowitz

Python 投資組合分析入門

最佳化問題:找出最適權重

$$ Markowitz 的最佳化問題

口語解釋:

  • 在以下條件下,最小化投資組合變異數:
  • 期望報酬率至少達到目標
  • 權重總和為 100%
  • 至少部分權重為正
Python 投資組合分析入門

改變目標報酬會得到效率前緣

效率前緣圖

Python 投資組合分析入門

用 PyPortfolioOpt 進行投資組合最佳化

from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns
df=pd.read_csv('portfolio.csv')
df.head(2)
                XOM        RRC        BBY         MA        PFE       
date
2010-01-04  54.068794  51.300568  32.524055  22.062426  13.940202 
2010-01-05  54.279907  51.993038  33.349487  21.997149  13.741367
# Calculate expected annualized returns and sample covariance
mu = expected_returns.mean_historical_return(df)
Sigma = risk_models.sample_cov(df)
Python 投資組合分析入門

取得效率前緣與投組權重

# Calculate expected annualized returns and risk
mu = expected_returns.mean_historical_return(df)
Sigma = risk_models.sample_cov(df)
# Obtain the EfficientFrontier
ef = EfficientFrontier(mu, Sigma)
# Select a chosen optimal portfolio
ef.max_sharpe()
Python 投資組合分析入門

不同的最佳化目標

# Select the maximum Sharpe portfolio
ef.max_sharpe()
# Select an optimal return for a target risk 
ef.efficient_risk(2.3)
# Select a minimal risk for a target return
ef.efficient_return(1.5)
Python 投資組合分析入門

計算投組風險與績效

# Obtain the performance numbers
ef.portfolio_performance(verbose=True, risk_free_rate = 0.01)
Expected annual return: 21.3%
Annual volatility: 19.5%
Sharpe Ratio: 0.98
Python 投資組合分析入門

來最佳化一個投資組合吧!

Python 投資組合分析入門

Preparing Video For Download...