According to the efficient-market hypothesis (refer to the Examining the market with the non-parametric runs test recipe), all information about an asset is immediately reflected in the price of the asset. This means that previous prices don't influence the current price. The following equations specify an autoregressive model (7. 13) and a restricted model (7. 14) with all the coefficients set to zero:
If we believe the market to be efficient, we would expect the unrestricted model to have nothing to add over the restricted model, and, therefore, the ratio (7. 15) of the respective R-squared coefficients should be close to one.
The script is in the autoregressive_test.ipynb
file in this book's code bundle:
import dautil as dl import ch7util import numpy as np import matplotlib.pyplot as plt import statsmodels.api as sm from IPython.display import HTML
ohlc = dl.data.OHLC() efficiencies = [] restricted_r2 = [] unrestricted_r2 = [] for stock in ch7util.STOCKS: rets = ch7util.log_rets(ohlc.get(stock)['Adj Close']) restricted = sm.OLS(rets, rets.mean() * np.ones_like(rets)).fit() rets_1 = rets[3:-1] rets_2 = rets[2:-2] rets_3 = rets[1:-3] rets_4 = rets[:-4] x = np.vstack((rets_1, rets_2, rets_3, rets_4)).T x = sm.add_constant(x) y = rets[4:] unrestricted = sm.OLS(y, x).fit() restricted_r2.append(restricted.rsquared) unrestricted_r2.append(unrestricted.rsquared) efficiencies.append(1 - restricted.rsquared/unrestricted.rsquared)
sp = dl.plotting.Subplotter(2, 1, context) dl.plotting.bar(sp.ax, ch7util.STOCKS, efficiencies) sp.label() dl.plotting.plot_text(sp.next_ax(), unrestricted_r2, np.array(restricted_r2)/10 ** -16, ch7util.STOCKS, add_scatter=True) sp.label() HTML(sp.exit())
Refer to the following screenshot for the end result: