Phân tích danh mục đầu tư với Python – Dữ liệu, hiệu suất, phân bổ
· 5 min read
Phân tích danh mục đầu tư là một phần quan trọng trong quản lý tài chính. Với Python, chúng ta có thể thực hiện các phân tích phức tạp một cách hiệu quả. Bài viết này sẽ hướng dẫn bạn cách sử dụng Python để phân tích danh mục đầu tư từ việc thu thập dữ liệu đến đánh giá hiệu suất và tối ưu hóa phân bổ.
1. Thu thập dữ liệu
Cài đặt thư viện cần thiết
pip install yfinance pandas numpy matplotlib seaborn scipy
Sử dụng yfinance để lấy dữ liệu chứng khoán
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.optimize import minimize
# Định nghĩa danh mục đầu tư
portfolio = {
'AAPL': 0.3, # Apple
'MSFT': 0.3, # Microsoft
'GOOGL': 0.2, # Google
'AMZN': 0.2 # Amazon
}
# Lấy dữ liệu lịch sử
data = pd.DataFrame()
for ticker in portfolio.keys():
stock = yf.Ticker(ticker)
hist = stock.history(period='1y')
data[ticker] = hist['Close']
# Tính toán lợi nhuận hàng ngày
returns = data.pct_change()
# Hiển thị dữ liệu
print("Dữ liệu giá đóng cửa:")
print(data.head())
print("\nLợi nhuận hàng ngày:")
print(returns.head())
2. Phân tích hiệu suất
Tính toán các chỉ số quan trọng
# Lợi nhuận trung bình hàng năm
annual_returns = returns.mean() * 252
# Độ lệch chuẩn (rủi ro)
volatility = returns.std() * np.sqrt(252)
# Tỷ lệ Sharpe (giả sử lãi suất phi rủi ro là 0.02)
risk_free_rate = 0.02
sharpe_ratio = (annual_returns - risk_free_rate) / volatility
# Tạo bảng tổng hợp
performance = pd.DataFrame({
'Lợi nhuận hàng năm': annual_returns,
'Độ biến động': volatility,
'Tỷ lệ Sharpe': sharpe_ratio
})
# Hiển thị kết quả
print("\nPhân tích hiệu suất:")
print(performance)
# Vẽ biểu đồ so sánh
plt.figure(figsize=(12, 6))
performance['Lợi nhuận hàng năm'].plot(kind='bar')
plt.title('Lợi nhuận hàng năm của các tài sản')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
3. Phân tích tương quan
Xem xét mối quan hệ giữa các tài sản
# Ma trận tương quan
correlation_matrix = returns.corr()
# Vẽ biểu đồ nhiệt
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', center=0)
plt.title('Ma trận tương quan giữa các tài sản')
plt.tight_layout()
plt.show()
# Vẽ biểu đồ phân tán
sns.pairplot(returns)
plt.suptitle('Phân tích phân tán giữa các tài sản', y=1.02)
plt.show()
4. Tối ưu hóa danh mục đầu tư
Sử dụng Modern Portfolio Theory (MPT)
def portfolio_volatility(weights, returns):
return np.sqrt(np.dot(weights.T, np.dot(returns.cov() * 252, weights)))
def negative_sharpe(weights, returns, risk_free_rate):
returns_array = returns.mean() * 252
volatility = portfolio_volatility(weights, returns)
return -(returns_array.dot(weights) - risk_free_rate) / volatility
# Tối ưu hóa danh mục
n_assets = len(portfolio)
constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
bounds = tuple((0, 1) for _ in range(n_assets))
initial_weights = np.array([1/n_assets] * n_assets)
optimal_weights = minimize(
negative_sharpe,
initial_weights,
args=(returns, risk_free_rate),
method='SLSQP',
bounds=bounds,
constraints=constraints
)
# Hiển thị kết quả tối ưu
print("\nPhân bổ tài sản tối ưu:")
for ticker, weight in zip(portfolio.keys(), optimal_weights.x):
print(f"{ticker}: {weight:.2%}")
# Vẽ biểu đồ phân bổ
plt.figure(figsize=(10, 6))
plt.pie(optimal_weights.x, labels=portfolio.keys(), autopct='%1.1f%%')
plt.title('Phân bổ tài sản tối ưu')
plt.show()
5. Phân tích rủi ro
Đánh giá rủi ro danh mục
# Value at Risk (VaR)
def calculate_var(returns, weights, confidence_level=0.95):
portfolio_returns = returns.dot(weights)
return np.percentile(portfolio_returns, (1 - confidence_level) * 100)
# Expected Shortfall (ES)
def calculate_es(returns, weights, confidence_level=0.95):
portfolio_returns = returns.dot(weights)
var = calculate_var(returns, weights, confidence_level)
return portfolio_returns[portfolio_returns <= var].mean()
# Tính toán các chỉ số rủi ro
var_95 = calculate_var(returns, optimal_weights.x)
es_95 = calculate_es(returns, optimal_weights.x)
print(f"\nValue at Risk (95%): {var_95:.2%}")
print(f"Expected Shortfall (95%): {es_95:.2%}")
# Vẽ biểu đồ phân phối lợi nhuận
portfolio_returns = returns.dot(optimal_weights.x)
plt.figure(figsize=(10, 6))
sns.histplot(portfolio_returns, kde=True)
plt.axvline(var_95, color='r', linestyle='--', label=f'VaR (95%): {var_95:.2%}')
plt.axvline(es_95, color='g', linestyle='--', label=f'ES (95%): {es_95:.2%}')
plt.title('Phân phối lợi nhuận danh mục')
plt.xlabel('Lợi nhuận')
plt.ylabel('Tần suất')
plt.legend()
plt.show()
6. Trực quan hóa kết quả
Tạo biểu đồ hiệu suất và phân bổ
# Biểu đồ hiệu suất tích lũy
cumulative_returns = (1 + returns).cumprod()
plt.figure(figsize=(12, 6))
for column in cumulative_returns.columns:
plt.plot(cumulative_returns.index, cumulative_returns[column], label=column)
plt.title('Hiệu suất tích lũy của danh mục')
plt.xlabel('Ngày')
plt.ylabel('Hiệu suất tích lũy')
plt.legend()
plt.grid(True)
plt.show()
# Biểu đồ phân bổ tài sản
plt.figure(figsize=(10, 6))
plt.pie(optimal_weights.x, labels=portfolio.keys(), autopct='%1.1f%%')
plt.title('Phân bổ tài sản tối ưu')
plt.show()
# Biểu đồ so sánh hiệu suất
plt.figure(figsize=(12, 6))
performance['Lợi nhuận hàng năm'].plot(kind='bar')
plt.title('So sánh lợi nhuận hàng năm')
plt.xlabel('Tài sản')
plt.ylabel('Lợi nhuận hàng năm')
plt.xticks(rotation=45)
plt.grid(True)
plt.show()
Kết luận
Phân tích danh mục đầu tư với Python cung cấp cho chúng ta các công cụ mạnh mẽ để:
- Thu thập và xử lý dữ liệu thị trường
- Đánh giá hiệu suất và rủi ro
- Tối ưu hóa phân bổ tài sản
- Trực quan hóa kết quả
Việc kết hợp các thư viện như pandas, numpy, yfinance và matplotlib cho phép chúng ta thực hiện các phân tích phức tạp một cách hiệu quả và dễ hiểu.