相關係數與共變異數

Python 投資組合風險管理入門

Dakota Wixom

Quantitative Analyst | QuantCourse.com

皮爾森相關

兩個隨機變數的不同相關性範例:

Python 投資組合風險管理入門

皮爾森相關

相關矩陣的熱度圖:

Python 投資組合風險管理入門

以 Python 計算相關矩陣

假設 StockReturns 是股票報酬的 pandas DataFrame,可如下計算相關矩陣:

correlation_matrix = StockReturns.corr()
print(correlation_matrix)

Python 投資組合風險管理入門

投資組合標準差

雙資產投資組合的報酬標準差:

$$ \sigma_p = \sqrt{ w_1^2 \sigma_1^2 + w_2^2 \sigma_2^2 + 2 w_1 w_2 \rho_{1, 2} \sigma_1 \sigma_2 }$$

  • $\sigma_p$:投資組合標準差
  • w:資產權重
  • $\sigma$:資產波動度
  • $\rho_{1, 2}$:資產 1 與 2 的相關係數
Python 投資組合風險管理入門

共變異數矩陣

要計算報酬 X 的共變異數矩陣($ \Sigma $):

Python 投資組合風險管理入門

以 Python 計算共變異數矩陣

假設 StockReturns 是股票報酬的 pandas DataFrame,可如下計算共變異數矩陣:

cov_mat = StockReturns.cov()
cov_mat

Python 投資組合風險管理入門

共變異數矩陣年化

將共變異數矩陣年化:

cov_mat_annual = cov_mat * 252
Python 投資組合風險管理入門

用共變異數計算投組標準差

投資組合波動度的公式:

$$ \sigma_{Portfolio} = \sqrt{ w_T \cdot \Sigma \cdot w } $$

  • $ \sigma_{Portfolio} $:投資組合波動度
  • $ \Sigma $:報酬的共變異數矩陣
  • w:投資組合權重($ w_T $ 為轉置的權重向量)
  • $ \cdot $:點乘運算子
Python 投資組合風險管理入門

矩陣轉置

「矩陣轉置」的範例:

Python 投資組合風險管理入門

點積

兩個向量 ab 的「點積」運算:

Python 投資組合風險管理入門

用 Python 計算投組標準差

計算投資組合波動度,假設已知權重陣列與共變異數矩陣:

import numpy as np
port_vol = np.sqrt(np.dot(weights.T, np.dot(cov_mat, weights)))
port_vol
0.035
Python 投資組合風險管理入門

一起來練習吧!

Python 投資組合風險管理入門

Preparing Video For Download...