Nhập môn Quản trị Rủi ro Danh mục bằng Python
Dakota Wixom
Quantitative Analyst | QuantCourse.com
$$ R_p = R_{a_1} w_{a_1} + R_{a_2} w_{a_2} + ... + R_{a_n} w_{a_1} $$
Giả sử StockReturns là DataFrame pandas chứa lợi nhuận cổ phiếu, bạn có thể tính lợi nhuận danh mục cho một bộ tỷ trọng như sau:
import numpy as np
portfolio_weights = np.array([0.25, 0.35, 0.10, 0.20, 0.10])
port_ret = StockReturns.mul(portfolio_weights, axis=1).sum(axis=1)
port_ret
Date
2017-01-03 0.008082
2017-01-04 0.000161
2017-01-05 0.003448
...
StockReturns["Portfolio"] = port_ret
Giả sử StockReturns là DataFrame pandas chứa lợi nhuận cổ phiếu, bạn có thể tính lợi nhuận danh mục cho danh mục tỷ trọng bằng nhau như sau:
import numpy as np
numstocks = 5
portfolio_weights_ew = np.repeat(1/numstocks, numstocks)
StockReturns.iloc[:,0:numstocks].mul(portfolio_weights_ew, axis=1).sum(axis=1)
Date
2017-01-03 0.008082
2017-01-04 0.000161
2017-01-05 0.003448
...
Để vẽ lợi nhuận ngày trong Python:
StockPrices["Returns"] = StockPrices["Adj Close"].pct_change()
StockReturns = StockPrices["Returns"]
StockReturns.plot()

Để vẽ lợi nhuận lũy kế của nhiều danh mục:
import matplotlib.pyplot as plt
CumulativeReturns = ((1 + StockReturns).cumprod() - 1)
CumulativeReturns[["Portfolio","Portfolio_EW"]].plot()


Vốn hóa thị trường: Giá trị cổ phiếu lưu hành của một công ty.
Cũng gọi là Market cap.
Để tính tỷ trọng theo vốn hóa của một cổ phiếu n:
$$ w_{mcap_n} = \frac{mcap_n}{ \sum_{i=1}^n mcap_i } $$
Để tính tỷ trọng theo vốn hóa trong Python, giả sử bạn có dữ liệu vốn hóa của từng công ty:
import numpy as np
market_capitalizations = np.array([100, 200, 100, 100])
mcap_weights = market_capitalizations/sum(market_capitalizations)
mcap_weights
array([0.2, 0.4, 0.2, 0.2])
Nhập môn Quản trị Rủi ro Danh mục bằng Python