Python 投资组合分析入门
Charlotte Werger
Data Scientist

认识 Harry Markowitz

$$

换句话说:

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
# 计算预期年化收益和样本协方差
mu = expected_returns.mean_historical_return(df)
Sigma = risk_models.sample_cov(df)
# 计算预期年化收益和风险
mu = expected_returns.mean_historical_return(df)
Sigma = risk_models.sample_cov(df)
# 获取有效前沿
ef = EfficientFrontier(mu, Sigma)
# 选择一个最优组合
ef.max_sharpe()
# 选择最大夏普比组合
ef.max_sharpe()
# 针对目标风险选择最优收益
ef.efficient_risk(2.3)
# 针对目标收益选择最小风险
ef.efficient_return(1.5)
# 获取绩效指标
ef.portfolio_performance(verbose=True, risk_free_rate = 0.01)
预期年化收益:21.3%
年化波动率:19.5%
夏普比率:0.98
Python 投资组合分析入门