有效前沿亦称“有效边界”。理性的投资者一般是厌恶风险而偏好收益的,对于相同的风险水平,会选择能提供最大收益率的组合;对于相同的预期收益率,会选择风险最小的组合。能同时满足这些条件的投资组合就是有效前沿。
“现代投资组合理论”(MPT)是1952年哈里.马克维茨在金融期刊上发表的名为“Portfolio Selection”的文章上提出的投资理论。
虽然在一个投资组合的收益为所有股票收益的平均值/加权平均值,但这个投资组合的风险不等于投资组合里所有股票风险的平均值/加权平均值。风险就是指一个资产的变化情况,如果在你的投资组合里有多只股票,那么你就需要计算一下这些股票之间变化的相关性。多样性的好处在于,你可以通过优化资产配置,使得该投资组合的风险低于投资组合中风险最低的股票的风险。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
tickers = ['601100.SH', '600000.SH', '600031.SH']
def put_some_stock_close_price_into_df():
some_close_price_df = pd.DataFrame
for count, ticker in enumerate(tickers):
df = pd.read_csv('stock_dfs/{}.csv'.format(ticker))
df.set_index('trade_date', inplace=True)
df.drop(['index', 'ts_code', 'open', 'high', 'low',
'pre_close', 'change',
'pct_chg', 'vol', 'amount'], 1, inplace=True)
df.rename(columns={'close':ticker}, inplace=True)
if some_close_price_df.empty:
some_close_price_df = df
else:
some_close_price_df = some_close_price_df.join(df,how='outer')
print('This is {} step'.format(count))
some_close_price_df.to_csv('CSI_selected_stocks_close_price.csv')
#put_some_stock_close_price_into_df()
df = pd.read_csv('CSI_selected_stocks_close_price.csv')
df = df.set_index('trade_date')
df = df.dropna()
daily_returns = df.pct_change()
year_returns = daily_returns.mean()*250
daily_cov = daily_returns.cov()
year_cov = daily_cov * 250
portfolio_return = []
portfolio_volatility = []
stock_weight = []
num_assets = len(tickers)
num_portfolio = 5000
for single_portfolio in range(num_portfolio):
weight = np.random.random(num_assets)
weight /= np.sum(weight)
returns = np.dot(weight, year_returns)
volatility = np.sqrt(np.dot(np.dot(weight, year_cov), weight.T))
portfolio_return.append(returns)
portfolio_volatility.append(volatility)
stock_weight.append(weight)
portfolio = {'Return':portfolio_return,
'Volatility':portfolio_volatility}
for counter , ticker in enumerate(tickers):
portfolio[ticker + ' weight'] = [weight[counter] for weight in stock_weight]
df = pd.DataFrame(portfolio)
df.to_csv('big_data.csv')
plt.style.use('seaborn')
df.plot.scatter(x='Volatility', y='Return', grid=True)
plt.xlabel('Volatility / std Deviation')
plt.ylabel('Expected Return')
plt.title('Efficient Frontier')
plt.show()