-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhurst_exponent.py
433 lines (373 loc) · 14.3 KB
/
hurst_exponent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Hurst exponent and RS-analysis
https://en.wikipedia.org/wiki/Hurst_exponent
https://en.wikipedia.org/wiki/Rescaled_range
"""
'''
def __to_inc(x):
incs = x[1:] - x[:-1]
return incs
def __to_pct(x):
pcts = x[1:] / x[:-1] - 1.
return pcts
def __get_simplified_RS(series, kind):
"""
Simplified version of rescaled range
Parameters
----------
series : array-like
(Time-)series
kind : str
The kind of series (refer to compute_Hc docstring)
"""
if kind == 'random_walk':
incs = __to_inc(series)
R = max(series) - min(series) # range in absolute values
S = np.std(incs, ddof=1)
elif kind == 'price':
pcts = __to_pct(series)
R = max(series) / min(series) - 1. # range in percent
S = np.std(pcts, ddof=1)
elif kind == 'change':
incs = series
_series = np.hstack([[0.],np.cumsum(incs)])
R = max(_series) - min(_series) # range in absolute values
S = np.std(incs, ddof=1)
if R == 0 or S == 0:
return 0 # return 0 to skip this interval due the undefined R/S ratio
return R / S
def __get_RS(series, kind):
"""
Get rescaled range (using the range of cumulative sum
of deviations instead of the range of a series as in the simplified version
of R/S) from a time-series of values.
Parameters
----------
series : array-like
(Time-)series
kind : str
The kind of series (refer to compute_Hc docstring)
"""
if kind == 'random_walk':
incs = __to_inc(series)
mean_inc = (series[-1] - series[0]) / len(incs)
deviations = incs - mean_inc
Z = np.cumsum(deviations)
R = max(Z) - min(Z)
S = np.std(incs, ddof=1)
elif kind == 'price':
incs = __to_pct(series)
mean_inc = np.sum(incs) / len(incs)
deviations = incs - mean_inc
Z = np.cumsum(deviations)
R = max(Z) - min(Z)
S = np.std(incs, ddof=1)
elif kind == 'change':
incs = series
mean_inc = np.sum(incs) / len(incs)
deviations = incs - mean_inc
Z = np.cumsum(deviations)
R = max(Z) - min(Z)
S = np.std(incs, ddof=1)
if R == 0 or S == 0:
return 0 # return 0 to skip this interval due undefined R/S
return R / S
def compute_Hc(series, kind="random_walk", min_window=10, max_window=None, simplified=True):
"""
Compute H (Hurst exponent) and C according to Hurst equation:
E(R/S) = c * T^H
Refer to:
https://en.wikipedia.org/wiki/Hurst_exponent
https://en.wikipedia.org/wiki/Rescaled_range
https://en.wikipedia.org/wiki/Random_walk
Parameters
----------
series : array-like
(Time-)series
kind : str
Kind of series
possible values are 'random_walk', 'change' and 'price':
- 'random_walk' means that a series is a random walk with random increments;
- 'price' means that a series is a random walk with random multipliers;
- 'change' means that a series consists of random increments
(thus produced random walk is a cumulative sum of increments);
min_window : int, default 10
the minimal window size for R/S calculation
max_window : int, default is the length of series minus 1
the maximal window size for R/S calculation
simplified : bool, default True
whether to use the simplified or the original version of R/S calculation
Returns tuple of
H, c and data
where H and c — parameters or Hurst equation
and data is a list of 2 lists: time intervals and R/S-values for correspoding time interval
for further plotting log(data[0]) on X and log(data[1]) on Y
"""
if len(series)<100:
raise ValueError("Series length must be greater or equal to 100")
ndarray_likes = [np.ndarray]
if "pandas.core.series" in sys.modules.keys():
ndarray_likes.append(pd.core.series.Series)
# convert series to numpy array if series is not numpy array or pandas Series
if type(series) not in ndarray_likes:
series = np.array(series)
if "pandas.core.series" in sys.modules.keys() and type(series) == pd.core.series.Series:
if series.isnull().values.any():
raise ValueError("Series contains NaNs")
series = series.values # convert pandas Series to numpy array
elif np.isnan(np.min(series)):
raise ValueError("Series contains NaNs")
if simplified:
RS_func = __get_simplified_RS
else:
RS_func = __get_RS
err = np.geterr()
np.seterr(all='raise')
max_window = max_window or len(series)-1
window_sizes = list(map(
lambda x: int(10**x),
np.arange(math.log10(min_window), math.log10(max_window), 0.25)))
window_sizes.append(len(series))
RS = []
for w in window_sizes:
rs = []
for start in range(0, len(series), w):
if (start+w)>len(series):
break
_ = RS_func(series[start:start+w], kind)
if _ != 0:
rs.append(_)
RS.append(np.mean(rs))
A = np.vstack([np.log10(window_sizes), np.ones(len(RS))]).T
H, c = np.linalg.lstsq(A, np.log10(RS), rcond=-1)[0]
np.seterr(**err)
c = 10**c
return H, c, [window_sizes, RS]
def random_walk(length, proba=0.5, min_lookback=1, max_lookback=100, cumprod=False):
"""
Generates a random walk series
Parameters
----------
proba : float, default 0.5
the probability that the next increment will follow the trend.
Set proba > 0.5 for the persistent random walk,
set proba < 0.5 for the antipersistent one
min_lookback: int, default 1
max_lookback: int, default 100
minimum and maximum window sizes to calculate trend direction
cumprod : bool, default False
generate a random walk as a cumulative product instead of cumulative sum
"""
assert(min_lookback>=1)
assert(max_lookback>=min_lookback)
if max_lookback > length:
max_lookback = length
warnings.warn("max_lookback parameter has been set to the length of the random walk series.")
if not cumprod: # ordinary increments
series = [0.] * length # array of prices
for i in range(1, length):
if i < min_lookback + 1:
direction = np.sign(np.random.randn())
else:
lookback = np.random.randint(min_lookback, min(i-1, max_lookback)+1)
direction = np.sign(series[i-1] - series[i-1-lookback]) * np.sign(proba - np.random.uniform())
series[i] = series[i-1] + np.fabs(np.random.randn()) * direction
else: # percent changes
series = [1.] * length # array of prices
for i in range(1, length):
if i < min_lookback + 1:
direction = np.sign(np.random.randn())
else:
lookback = np.random.randint(min_lookback, min(i-1, max_lookback)+1)
direction = np.sign(series[i-1] / series[i-1-lookback] - 1.) * np.sign(proba - np.random.uniform())
series[i] = series[i-1] * np.fabs(1 + np.random.randn()/1000. * direction)
return series
'''#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Hurst exponent and RS-analysis
https://en.wikipedia.org/wiki/Hurst_exponent
https://en.wikipedia.org/wiki/Rescaled_range
"""
'''
def __to_inc(x):
incs = x[1:] - x[:-1]
return incs
def __to_pct(x):
pcts = x[1:] / x[:-1] - 1.
return pcts
def __get_simplified_RS(series, kind):
"""
Simplified version of rescaled range
Parameters
----------
series : array-like
(Time-)series
kind : str
The kind of series (refer to compute_Hc docstring)
"""
if kind == 'random_walk':
incs = __to_inc(series)
R = max(series) - min(series) # range in absolute values
S = np.std(incs, ddof=1)
elif kind == 'price':
pcts = __to_pct(series)
R = max(series) / min(series) - 1. # range in percent
S = np.std(pcts, ddof=1)
elif kind == 'change':
incs = series
_series = np.hstack([[0.],np.cumsum(incs)])
R = max(_series) - min(_series) # range in absolute values
S = np.std(incs, ddof=1)
if R == 0 or S == 0:
return 0 # return 0 to skip this interval due the undefined R/S ratio
return R / S
def __get_RS(series, kind):
"""
Get rescaled range (using the range of cumulative sum
of deviations instead of the range of a series as in the simplified version
of R/S) from a time-series of values.
Parameters
----------
series : array-like
(Time-)series
kind : str
The kind of series (refer to compute_Hc docstring)
"""
if kind == 'random_walk':
incs = __to_inc(series)
mean_inc = (series[-1] - series[0]) / len(incs)
deviations = incs - mean_inc
Z = np.cumsum(deviations)
R = max(Z) - min(Z)
S = np.std(incs, ddof=1)
elif kind == 'price':
incs = __to_pct(series)
mean_inc = np.sum(incs) / len(incs)
deviations = incs - mean_inc
Z = np.cumsum(deviations)
R = max(Z) - min(Z)
S = np.std(incs, ddof=1)
elif kind == 'change':
incs = series
mean_inc = np.sum(incs) / len(incs)
deviations = incs - mean_inc
Z = np.cumsum(deviations)
R = max(Z) - min(Z)
S = np.std(incs, ddof=1)
if R == 0 or S == 0:
return 0 # return 0 to skip this interval due undefined R/S
return R / S
def compute_Hc(series, kind="random_walk", min_window=10, max_window=None, simplified=True):
"""
Compute H (Hurst exponent) and C according to Hurst equation:
E(R/S) = c * T^H
Refer to:
https://en.wikipedia.org/wiki/Hurst_exponent
https://en.wikipedia.org/wiki/Rescaled_range
https://en.wikipedia.org/wiki/Random_walk
Parameters
----------
series : array-like
(Time-)series
kind : str
Kind of series
possible values are 'random_walk', 'change' and 'price':
- 'random_walk' means that a series is a random walk with random increments;
- 'price' means that a series is a random walk with random multipliers;
- 'change' means that a series consists of random increments
(thus produced random walk is a cumulative sum of increments);
min_window : int, default 10
the minimal window size for R/S calculation
max_window : int, default is the length of series minus 1
the maximal window size for R/S calculation
simplified : bool, default True
whether to use the simplified or the original version of R/S calculation
Returns tuple of
H, c and data
where H and c — parameters or Hurst equation
and data is a list of 2 lists: time intervals and R/S-values for correspoding time interval
for further plotting log(data[0]) on X and log(data[1]) on Y
"""
if len(series)<100:
raise ValueError("Series length must be greater or equal to 100")
ndarray_likes = [np.ndarray]
if "pandas.core.series" in sys.modules.keys():
ndarray_likes.append(pd.core.series.Series)
# convert series to numpy array if series is not numpy array or pandas Series
if type(series) not in ndarray_likes:
series = np.array(series)
if "pandas.core.series" in sys.modules.keys() and type(series) == pd.core.series.Series:
if series.isnull().values.any():
raise ValueError("Series contains NaNs")
series = series.values # convert pandas Series to numpy array
elif np.isnan(np.min(series)):
raise ValueError("Series contains NaNs")
if simplified:
RS_func = __get_simplified_RS
else:
RS_func = __get_RS
err = np.geterr()
np.seterr(all='raise')
max_window = max_window or len(series)-1
window_sizes = list(map(
lambda x: int(10**x),
np.arange(math.log10(min_window), math.log10(max_window), 0.25)))
window_sizes.append(len(series))
RS = []
for w in window_sizes:
rs = []
for start in range(0, len(series), w):
if (start+w)>len(series):
break
_ = RS_func(series[start:start+w], kind)
if _ != 0:
rs.append(_)
RS.append(np.mean(rs))
A = np.vstack([np.log10(window_sizes), np.ones(len(RS))]).T
H, c = np.linalg.lstsq(A, np.log10(RS), rcond=-1)[0]
np.seterr(**err)
c = 10**c
return H, c, [window_sizes, RS]
def random_walk(length, proba=0.5, min_lookback=1, max_lookback=100, cumprod=False):
"""
Generates a random walk series
Parameters
----------
proba : float, default 0.5
the probability that the next increment will follow the trend.
Set proba > 0.5 for the persistent random walk,
set proba < 0.5 for the antipersistent one
min_lookback: int, default 1
max_lookback: int, default 100
minimum and maximum window sizes to calculate trend direction
cumprod : bool, default False
generate a random walk as a cumulative product instead of cumulative sum
"""
assert(min_lookback>=1)
assert(max_lookback>=min_lookback)
if max_lookback > length:
max_lookback = length
warnings.warn("max_lookback parameter has been set to the length of the random walk series.")
if not cumprod: # ordinary increments
series = [0.] * length # array of prices
for i in range(1, length):
if i < min_lookback + 1:
direction = np.sign(np.random.randn())
else:
lookback = np.random.randint(min_lookback, min(i-1, max_lookback)+1)
direction = np.sign(series[i-1] - series[i-1-lookback]) * np.sign(proba - np.random.uniform())
series[i] = series[i-1] + np.fabs(np.random.randn()) * direction
else: # percent changes
series = [1.] * length # array of prices
for i in range(1, length):
if i < min_lookback + 1:
direction = np.sign(np.random.randn())
else:
lookback = np.random.randint(min_lookback, min(i-1, max_lookback)+1)
direction = np.sign(series[i-1] / series[i-1-lookback] - 1.) * np.sign(proba - np.random.uniform())
series[i] = series[i-1] * np.fabs(1 + np.random.randn()/1000. * direction)
return series
'''