December 2018
Beginner to intermediate
682 pages
18h 1m
English
To automate this process, we can write a function that accepts stock data in the and outputs the histogram of daily returns along with the percentages that fall within 1, 2, and 3 standard deviations from the mean. The following function does this and replaces the methods with their symbol counterparts:
>>> def test_return_normality(stock_data): close = stock_data['Close'] daily_return = close.pct_change().dropna() daily_return.hist(bins=20) mean = daily_return.mean() std = daily_return.std() abs_z_score = abs(daily_return - mean) / std 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))
>>> slb = pd.read_csv('data/slb_stock.csv', ...