-
Notifications
You must be signed in to change notification settings - Fork 0
/
hurst.py
61 lines (55 loc) · 2.56 KB
/
hurst.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
##
## Rescaled Range related functions
##
def __MeanRange(series, segmentSize, /, *, wrap=True):
# Step 1: Splitting series into segments
_nSegments = len(series) // segmentSize
_segs = series[:_nSegments*segmentSize].reshape(_nSegments, segmentSize)
if wrap:
# wrap might be needed to account for the edge points too
_segs = np.vstack((_segs, series[-_nSegments*segmentSize:].reshape(_nSegments, segmentSize)))
# Step 2: Obtain standard deviation for each segment
_stds = np.std(_segs, axis=1)
# Step 3: Obtain profile (cumulative sum of values in the segment)
_prof = np.cumsum(_segs, axis=1)
del _segs
# Step 5: Establish range
_low = np.min(_prof, axis=1)
_high = np.max(_prof, axis=1)
_rng = _high - _low
# Step 6: Calculate mean range / standard deviation
return np.mean(_rng / _stds)
def __MeanRanges(series, segmentSizes, /, *, wrap=True):
_ranges = np.zeros(len(segmentSizes))
for _idx, _segmentSize in enumerate(segmentSizes):
_ranges[_idx] = __MeanRange(series, _segmentSize, wrap=wrap)
return _ranges
def RescaledRange(series, lowSegmentSize, highSegmentSize, /, *, wrap=True, points=100):
# NOTE: We will work only if series is stationary (equivalent to fractional Gaussian noise)
_lss = np.log10(lowSegmentSize)
_hss = np.log10(highSegmentSize)
_segmentSizes = np.unique(np.floor(np.logspace(_lss, _hss, num = points)).astype(int))
_segmentSizes = _segmentSizes[ _segmentSizes > 1 ]
_ranges = __MeanRanges(series, _segmentSizes, wrap=wrap)
return np.polyfit(np.log10(_segmentSizes), np.log10(_ranges), 1)[0]
##
## Box Counting method
##
def __BoxCount1D(series, nSegments, /, *, wrap=True):
# NOTE: this implementation will work only for one dimensional series (e.g. Cantor set)
_segmentSize = len(series) // nSegments
_segs = series[:nSegments*_segmentSize].reshape(nSegments, _segmentSize)
if wrap:
# wrap might be needed to account for the edge points too
_segs = np.vstack((_segs, series[-nSegments*_segmentSize:].reshape(nSegments, _segmentSize)))
return np.sum(np.sum(_segs,axis=1)>0) // 2
def BoxCount1D(series, lowN, highN, /, *, wrap=True, points=100):
_ln = np.log10(lowN)
_hn = np.log10(highN)
_nBoxes = np.unique(np.floor(np.logspace(_ln, _hn, num = points)).astype(int))
_nBoxes = _nBoxes[ _nBoxes > 1 ]
_counts = np.array([__BoxCount1D(series, nb, wrap=wrap) for nb in _nBoxes])
return np.polyfit(np.log10(_nBoxes), np.log10(_counts), 1)[0]