Python金融学基础——夏普比率(Sharpe( 二 )


大概可以了,然后我们把对应的经过权重计算的归一日回报率全部都整合到一张表当中 。
total_stock=pd.concat([aapl['weighted daily return'], cisco['weighted daily return'], ibm['weighted daily return'], amzn['weighted daily return']],axis=1)total_stock.columns=['aapl', 'cisco', 'ibm', 'amzn']total_stock.head()
2.2 投资
然后假设我们投资10000元,那么就在上面回报率的基础上乘以10000 。
total_invest=total_stock*10000total_invest.head()
total_invest['Total Pos']=total_invest.sum(axis=1)total_invest.head()
然后我们绘制下每天的总收益.
plt.style.use('ggplot')total_invest['Total Pos'].plot(label='Total Pos')plt.legend(loc='best')plt.title('Total Portfolio Value')
我们绘制一下除了总资产以外的其他单只股票的收益情况
total_invest.drop('Total Pos',axis=1).plot(figsize=(8,4))
三、资产的统计学值 3.1 日回报率
total_invest['daily return']=total_invest['Total Pos'].pct_change(1)total_invest['daily return'].head()
3.2 累积回报率
计算方法是最后一天与一开始第一天的变化百分比,相当于是增加了多少百分比 。
cumulative_return=total_invest['Total Pos'].iloc[-1]/total_invest['Total Pos'].iloc[0]-1print cumulative_return
3.3 平均日回报率
也就是对日回报率做平均计算
total_invest['daily return'].mean()
3.4 日回报率的标准差
total_invest['daily return'].std()
total_invest['daily return'].plot(kind='kde')
四、夏普比率
接下来我们计算一下总资产的夏普比率,也就是拿总资产日回报率的均值除以日回报率的标准差 。之后由于我们这里的粒度是以天算的,所以要乘以sqrt(252),252代表252天
SR=total_invest['daily return'].mean()/total_invest['daily return'].std()SR
import numpy as npASR=np.sqrt(252)*SRASR
最后我们绘制一下各个股票的收盘价分布情况
for stock in [aapl, cisco, ibm, amzn]:stock['Adj. Close'].pct_change(1).plot(kind='kde')