import numpy as np
import pandas as pd
import statsmodels
from statsmodels.tsa.stattools import coint
import statsmodels.api as stat
import statsmodels.tsa.stattools as ts
import matplotlib.pyplot as plt
from nsepy import get_history
from datetime import date
S1 = get_history(symbol = "NIFTY",
start = date(2010,1,1),
end = date(2020,9,3), index=True)
S1[['Close']].plot()
A time series can then be characterised in the following manner:
from numpy import cumsum, log, polyfit, sqrt, std, subtract
from numpy.random import randn
def hurst(ts):
"""Returns the Hurst Exponent of the time series vector ts"""
# Create the range of lag values
lags = range(2, 100)
# Calculate the array of the variances of the lagged differences
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
# Use a linear fit to estimate the Hurst Exponent
poly = polyfit(log(lags), log(tau), 1)
# Return the Hurst exponent from the polyfit output
return poly[0]*2.0
# Create a Gometric Brownian Motion
gbm = log(cumsum(randn(100000))+1000)
plt.plot(gbm)
plt.show()
#Calculate Hurst Exponent for GBM
print("Hurst(GBM): %s" % hurst(gbm))
# Create a Mean-Reverting Series
mr = log(randn(100000)+1000)
plt.plot(mr)
plt.show()
#Calculate Hurst Exponent for MR
print("Hurst(MR): %s" % hurst(mr))
# Create a Trending Series
tr = log(cumsum(randn(100000)+1)+1000)
plt.plot(tr)
plt.show()
print("Hurst(TR): %s" % hurst(tr))
print("Hurst(Nifty): %s" % hurst(S1[['Close']]))