8. 确定股票收益的正态值

  1. # 加载亚马逊的股票数据,使用Data作为行索引
  2. In[57]: amzn = pd.read_csv('data/amzn_stock.csv', index_col='Date', parse_dates=['Date'])
  3. amzn.head()
  4. Out[57]:

8. 确定股票收益的正态值 - 图1

  1. # 选取Close收盘价,用pct_change()计算每日回报率
  2. In[58]: amzn_daily_return = amzn.Close.pct_change()
  3. amzn_daily_return.head()
  4. Out[58]: Date
  5. 2010-01-04 NaN
  6. 2010-01-05 0.005900
  7. 2010-01-06 -0.018116
  8. 2010-01-07 -0.017013
  9. 2010-01-08 0.027077
  10. Name: Close, dtype: float64
  1. # 去掉缺失值,画一张柱状图,查看分布情况
  2. In[59]: amzn_daily_return = amzn_daily_return.dropna()
  3. amzn_daily_return.hist(bins=20)
  4. Out[59]: <matplotlib.axes._subplots.AxesSubplot at 0x1174b3128>

8. 确定股票收益的正态值 - 图2

  1. # 计算平均值和标准差
  2. In[60]: mean = amzn_daily_return.mean()
  3. std = amzn_daily_return.std()
  4. # 计算每个数据的z-score的绝对值:z-score是远离平均值的标准差值得个数
  5. In[61]: abs_z_score = amzn_daily_return.sub(mean).abs().div(std)
  6. # 计算位于1,2,3个标准差之内的收益率的比例
  7. In[62]: pcts = [abs_z_score.lt(i).mean() for i in range(1,4)]
  8. print('{:.3f} fall within 1 standard deviation. '
  9. '{:.3f} within 2 and {:.3f} within 3'.format(*pcts))
  10. 0.787 fall within 1 standard deviation. 0.956 within 2 and 0.985 within 3

更多

  1. # 将上面的方法整合成一个函数
  2. In[63]: def test_return_normality(stock_data):
  3. close = stock_data['Close']
  4. daily_return = close.pct_change().dropna()
  5. daily_return.hist(bins=20)
  6. mean = daily_return.mean()
  7. std = daily_return.std()
  8. abs_z_score = abs(daily_return - mean) / std
  9. pcts = [abs_z_score.lt(i).mean() for i in range(1,4)]
  10. print('{:.3f} fall within 1 standard deviation. '
  11. '{:.3f} within 2 and {:.3f} within 3'.format(*pcts))
  12. In[64]: slb = pd.read_csv('data/slb_stock.csv',
  13. index_col='Date', parse_dates=['Date'])
  14. test_return_normality(slb)
  15. 0.742 fall within 1 standard deviation. 0.946 within 2 and 0.986 within 3

8. 确定股票收益的正态值 - 图3