Nhập môn Phân tích Danh mục đầu tư với Python
Charlotte Werger
Data Scientist

$$

Biến động giá trị theo thời gian
$ Return_t = \frac{V_t - V_{t-1}}{V_{t-1}} $

$$
df.head(2)
AAPL AMZN TSLA
date
2018-03-25 13.88 114.74 92.48
2018-03-26 13.35 109.95 89.79
# Tính lợi nhuận theo ngày
returns = df.pct_change()
returns.head(2)
AAPL AMZN TSLA
date
2018-03-25 NaN NaN NaN
2018-03-26 -0.013772 0.030838 0.075705
weights = np.array([0, 0.50, 0.25])
# Tính lợi nhuận bình quân cho mỗi cổ phiếu
meanDailyReturns = returns.mean()
# Tính lợi nhuận danh mục
portReturn = np.sum(meanDailyReturns*weights)
print (portReturn)
0.05752375881537723
# Tính lợi nhuận danh mục hằng ngày
returns['Portfolio']= returns.dot(weights)
# Xem kết quả
returns.head(3)
AAPL AMZN TSLA Portfolio
date
2018-03-23 -0.020974 -0.026739 -0.029068 -0.025880
2018-03-26 -0.013772 0.030838 0.075705 0.030902
# Ghép lãi theo thời gian
daily_cum_ret=(1+returns).cumprod()
# Vẽ lợi nhuận lũy kế
daily_cum_ret.Portfolio.plot()

Nhập môn Phân tích Danh mục đầu tư với Python