def hurst(prices, plot=False): """ Hurst exponent test """ prices = np.log(prices) lags = range(1, 100) # Variance of differences for each lag tau variances = [((prices.shift(-lag) - prices)**2).mean() for lag in lags] loglags = np.log(lags) logvars = np.log(variances) # Fit a straight line b, a = np.polyfit(loglags, logvars, 1) if plot: fig, ax = plt.subplots() plt.scatter(loglags, logvars) plt.plot(loglags, a + b * loglags, lw=2, color='orange') plotting.style_default(ax, fig, xlabel='log(Lag)', ylabel='log(Variance)') plt.show() # Gradient is 2H hurst = b / 2 return hurst
def plot_scatter(prices): assert len(prices.columns) == 2 xprices = prices.iloc[:, 0] yprices = prices.iloc[:, 1] fig, ax = plt.subplots() ax.scatter(xprices, yprices, label=None) b, a = ols(prices).beta #print sm.ols(formula='{} ~ {}'.format(*reversed(prices.columns)), # data=prices).fit().summary() xmin, xmax = xprices.min(), xprices.max() axrange = np.arange(xmin, xmax, 0.01 * (xmax - xmin)) ax.plot(axrange, a + b * axrange, lw=4, color='orange', label='y = {:.2f}x + {:.2f}'.format(b, a)) axlabel = 'Price of {}' plotting.style_default(ax, fig, xlabel=axlabel.format(prices.columns[0]), ylabel=axlabel.format(prices.columns[1])) plt.show()
def plot_time_series(prices, *names): fig, ax = plt.subplots() prices.plot(ax=ax) plotting.style_default(ax, fig, ylabel='Price') plt.show()
def plot_residuals(prices): fig, ax = plt.subplots() #residuals(prices).plot() ax.plot(residuals(prices)) plotting.style_default(ax, fig, ylabel='Residual') plt.show()