Determining market efficiency with autoregressive models

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:

Determining market efficiency with autoregressive models

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.

How to do it...

The script is in the autoregressive_test.ipynb file in this book's code bundle:

  1. The imports are as follows:
    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
  2. Fit the models using (7.13) and (7.14) and then calculate the market efficiency using (7.15) for our list of stocks:
    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)
  3. Plot the market efficiency and R-squared values as follows:
    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:

How to do it...

See also

..................Content has been hidden....................

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