I wrote a function for Hurst exponent’s calculation. I decided to test my function with “random walk” series.
difs = np.random.normal(0, 1, 100000)
series = np.cumsum(difs)
According to several sources Hurst exponent for random walk series should be approximately equal to 0.5. However all that I got there is H = 1. Is there something wrong with my function or I just misunderstood something? Would appreciate any explanations.
Function for Hurst exponent calculation:
def rs(series: np.ndarray):
n, m = 0, 0
shape = series.shape
if len(shape) == 1:
m = 1
n = shape[0]
series = series.reshape(-1, 1)
else:
n, m = shape
result = []
Hs = []
for col in range(m):
scales = utils.get_scales(n)
rs_values = []
for scale in scales:
current_rs_values = []
for i in range(n//scale):
segment = series[i*scale:(i + 1)*scale, col]
mean = segment.mean()
corr_segment = segment - mean
cum_dev = np.cumsum(corr_segment)
R = np.max(cum_dev) - np.min(cum_dev)
S = np.std(corr_segment)
current_rs_values.append(R/S)
rs = np.mean(current_rs_values)
rs_values.append((log(scale), log(rs)))
result.append(np.array(rs_values))
H, _ = np.polyfit(result[-1][:,0], result[-1][:,1], 1)
Hs.append(H)
return Hs, np.array(result)
There I use get_scales
that I wrote:
def get_scales(n: int):
scales = np.logspace(np.log10(4), np.log10(n//2), 30, dtype='int')
scales = np.unique(scales)
return scales
nyekitka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.