How to do it...

  1. Load Amazon stock data and set the date as the index:
>>> amzn = pd.read_csv('data/amzn_stock.csv', index_col='Date',
parse_dates=['Date'])
>>> amzn.head()
  1. Create a Series by selecting only the closing price and then using the pct_change method to get the daily rate of return:
>>> amzn_daily_return = amzn.Close.pct_change()
>>> amzn_daily_return.head()
Date 2010-01-04 NaN 2010-01-05 0.005900 2010-01-06 -0.018116 2010-01-07 -0.017013 2010-01-08 0.027077 Name: Close, dtype: float64
  1. Drop the missing value and plot a histogram of the returns to visually inspect the distribution:
>>> amzn_daily_return = amzn_daily_return.dropna()
>>> amzn_daily_return.hist(bins=20)
  1. Normal distributions approximately follow the 68-95-99.7 rule--meaning that 68% of the data falls between 1 standard deviation of the mean, 95% between 2, and 99.7% between 3. We will now calculate the percentage of daily returns that fall between 1, 2, and 3 standard deviations from the mean. For this, we will need the mean and standard deviation:
>>> mean = amzn_daily_return.mean() 
>>> std = amzn_daily_return.std()
  1. Calculate the absolute value of the z-score for each observation. The z-score is the number of standard deviations away from the mean:
>>> abs_z_score = amzn_daily_return.sub(mean).abs().div(std)
  1. Find the percentage of returns that are within 1, 2, and 3 standard deviations:
>>> pcts = [abs_z_score.lt(i).mean() for i in range(1,4)]
>>> print('{:.3f} fall within 1 standard deviation. '
'{:.3f} within 2 and {:.3f} within 3'.format(*pcts))
0.787 fall within 1 standard deviation. 0.957 within 2 and 0.985 within 3
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset