-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMFDFA.py
357 lines (283 loc) · 17.2 KB
/
MFDFA.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
#MFDFA-Analytics-by-SKDataScience
#multifractal DFA singularity spectra - module A
#Version 3.0 - Modified by R.R.Rosa - Dec 2018 - mfdfa_ss_m1.py
# This code implements a modification of the first-order unifractal analysis algorithm originally described in [1].
# It covers both the detrended fluctuation analysis (DFA) and the Hurst (a.k.a. R/S) analysis methods. For more details
# on the DFA and Hurst analysis methods, please refer to [2, 3].
#
# At the input, 'dx' is a time series of increments of the physical observable 'x(t)', of the length equal to an
# integer power of two greater than two (i.e. 4, 8, 16, 32, etc.), 'normType_p' is any real greater than or
# equal to one specifying the p-norm, 'isDFA' is a boolean value prescribing to use either the DFA-based algorithm or
# the standard Hurst (a.k.a. R/S) analysis, 'normType_q' is any real greater than or equal to one specifying the q-norm.
#
# At the output, 'timeMeasure' is the time measure of the data's support at different scales, 'meanDataMeasure' is
# the data measure at different scales, while 'scales' is the scales at which the data measure is computed.
#
# The conventional way of using the output values is to plot the data measure vs the scales; the time measure,
# being the inverse quantity to the scales, is computed for an alternative representation and may be ignored.
#
# The requirement to have a power-of-two data length is aimed at avoiding inaccuracies when computing the data measure
# on different time scales.
#
# REFERENCES:
# [1] D.M. Filatov, J. Stat. Phys., 165 (2016) 681-692. DOI: 10.1007/s10955-016-1641-6.
# [2] J.W. Kantelhardt, Fractal and Multifractal Time Series, available at http://arxiv.org/abs/0804.0747, 2008.
# [3] J. Feder, Fractals, Plenum Press, New York, 1988.
#
# The end user is granted perpetual permission to reproduce, adapt, and/or distribute this code, provided that
# an appropriate link is given to the original repository it was downloaded from.
#input: read your time series as a 1d vector, with size 2ˆn, named: dx
import numpy as np
def getHurstByUpscaling(dx, normType_p = np.inf, isDFA = 1, normType_q = 1.0):
## Some initialiation
dx_len = len(dx)
# We have to reserve the most major scale for shifts, so we divide the data
# length by two. (As a result, the time measure starts from 2.0, not from
# 1.0, see below.)
dx_len = np.int(dx_len / 2)
dx_shift = np.int(dx_len / 2)
nScales = np.int(np.round(np.log2(dx_len))) # Number of scales involved. P.S. We use 'round()' to prevent possible malcomputing of the logarithms
j = 2 ** (np.arange(1, nScales + 1) - 1) - 1
meanDataMeasure = np.zeros(nScales)
## Computing the data measure
for ji in range(1, nScales + 1):
# At the scale 'j(ji)' we deal with '2 * (j(ji) + 1)' elements of the data 'dx'
dx_k_len = 2 * (j[ji - 1] + 1)
n = np.int(dx_len / dx_k_len)
dx_leftShift = np.int(dx_k_len / 2)
dx_rightShift = np.int(dx_k_len / 2)
for k in range(1, n + 1):
# We get a portion of the data of the length '2 * (j(ji) + 1)' plus the data from the left and right boundaries
dx_k_withShifts = dx[(k - 1) * dx_k_len + 1 + dx_shift - dx_leftShift - 1 : k * dx_k_len + dx_shift + dx_rightShift]
# Then we perform free upscaling and, using the above-selected data (provided at the scale j = 0),
# compute the velocities at the scale 'j(ji)'
j_dx = np.convolve(dx_k_withShifts, np.ones(dx_rightShift), 'valid')
# Then we compute the accelerations at the scale 'j(ji) + 1'
r = (j_dx[1 + dx_rightShift - 1 : ] - j_dx[1 - 1 : -dx_rightShift]) / 2.0
# Finally, we compute the range ...
if (normType_p == 0):
R = np.max(r[2 - 1 : ]) - np.min(r[2 - 1 : ])
elif (np.isinf(normType_p)):
R = np.max(np.abs(r[2 - 1 : ]))
else:
R = (np.sum(r[2 - 1 : ] ** normType_p) / len(r[2 - 1 : ])) ** (1.0 / normType_p)
# ... and the normalisation factor ("standard deviation")
S = np.sqrt(np.sum(np.abs(np.diff(r)) ** 2.0) / (len(r) - 1))
if (isDFA == 1):
S = 1.0
meanDataMeasure[ji - 1] += (R / S) ** normType_q
meanDataMeasure[ji - 1] = (meanDataMeasure[ji - 1] / n) ** (1.0 / normType_q)
# We pass from the scales ('j') to the time measure; the time measure at the scale j(nScales) (the most major one)
# is assumed to be 2.0, while it is growing when the scale is tending to j(1) (the most minor one).
# (The scale j(nScales)'s time measure is NOT equal to 1.0, because we reserved the highest scale for shifts
# in the very beginning of the function.)
timeMeasure = 2.0 * dx_len / (2 * (j + 1))
scales = j + 1
return [timeMeasure, meanDataMeasure, scales]
################################
# MODULO B
################################
#MFDFA-Analytics-by-SKDataScience
#multifractal DFA singularity spectra - module B
#Version 3.0 - Modified by R.R.Rosa - Dec 2018 - mfdfa_ss_m2.py
# This code implements a modification of the first-order multifractal analysis algorithm. It is based on the
# corresponding unifractal analysis technique described in [1]. It computes the Lipschitz-Holder multifractal
# singularity spectrum, as well as the minimum and maximum generalised Hurst exponents [2, 3].
#
# At the input, 'dx' is a time series of increments of the physical observable 'x(t)', of the length equal to an
# integer power of two greater than two (i.e. 4, 8, 16, 32, etc.), 'normType' is any real greater than or
# equal to one specifying the p-norm, 'isDFA' is a boolean value prescribing to use either the DFA-based algorithm or
# the standard Hurst (a.k.a. R/S) analysis, 'isNormalised' is a boolean value prescribing either to normalise the
# intermediate range-to-deviation (R/S) expression or to proceed computing without normalisation.
#
# At the output, 'timeMeasure' is the time measure of the data's support at different scales, 'dataMeasure' is
# the data measure at different scales computed for each value of the variable q-norm, 'scales' is the scales at which
# the data measure is computed, 'stats' is the structure containing MF-DFA statistics, while 'q' is the values of the
# q-norm used.
#
# Similarly to unifractal analysis (see getHurstByUpscaling()), the time measure is computed merely for an alternative
# representation of the dependence 'dataMeasure(q, scales) ~ scales ^ -tau(q)'.
#
# REFERENCES:
# [1] D.M. Filatov, J. Stat. Phys., 165 (2016) 681-692. DOI: 10.1007/s10955-016-1641-6.
# [2] J.W. Kantelhardt, Fractal and Multifractal Time Series, available at http://arxiv.org/abs/0804.0747, 2008.
# [3] J. Feder, Fractals, Plenum Press, New York, 1988.
#
# The end user is granted perpetual permission to reproduce, adapt, and/or distribute this code, provided that
# an appropriate link is given to the original repository it was downloaded from.
import numpy as np
def getMSSByUpscaling(dx, normType = np.inf, isDFA = 1, isNormalised = 1):
## Some initialiation
aux_eps = np.finfo(float).eps
# We prepare an array of values of the variable q-norm
aux = [-16.0, -8.0, -4.0, -2.0, -1.0, -0.5, -0.0001, 0.0, 0.0001, 0.5, 0.9999, 1.0, 1.0001, 2.0, 4.0, 8.0, 16.0, 32.0]
nq = len(aux)
q = np.zeros((nq, 1))
q[:, 1 - 1] = aux
dx_len = len(dx)
# We have to reserve the most major scale for shifts, so we divide the data
# length by two. (As a result, the time measure starts from 2.0, not from
# 1.0, see below.)
dx_len = np.int(dx_len / 2)
dx_shift = np.int(dx_len / 2)
nScales = np.int(np.round(np.log2(dx_len))) # Number of scales involved. P.S. We use 'round()' to prevent possible malcomputing of the logarithms
j = 2 ** (np.arange(1, nScales + 1) - 1) - 1
dataMeasure = np.zeros((nq, nScales))
## Computing the data measures in different q-norms
for ji in range(1, nScales + 1):
# At the scale 'j(ji)' we deal with '2 * (j(ji) + 1)' elements of the data 'dx'
dx_k_len = 2 * (j[ji - 1] + 1)
n = np.int(dx_len / dx_k_len)
dx_leftShift = np.int(dx_k_len / 2)
dx_rightShift = np.int(dx_k_len / 2)
R = np.zeros(n)
S = np.ones(n)
for k in range(1, n + 1):
# We get a portion of the data of the length '2 * (j(ji) + 1)' plus the data from the left and right boundaries
dx_k_withShifts = dx[(k - 1) * dx_k_len + 1 + dx_shift - dx_leftShift - 1 : k * dx_k_len + dx_shift + dx_rightShift]
# Then we perform free upscaling and, using the above-selected data (provided at the scale j = 0),
# compute the velocities at the scale 'j(ji)'
j_dx = np.convolve(dx_k_withShifts, np.ones(dx_rightShift), 'valid')
# Then we compute the accelerations at the scale 'j(ji) + 1'
r = (j_dx[1 + dx_rightShift - 1 : ] - j_dx[1 - 1 : -dx_rightShift]) / 2.0
# Finally we compute the range ...
if (normType == 0):
R[k - 1] = np.max(r[2 - 1 : ]) - np.min(r[2 - 1 : ])
elif (np.isinf(normType)):
R[k - 1] = np.max(np.abs(r[2 - 1 : ]))
else:
R[k - 1] = (np.sum(r[2 - 1 : ] ** normType) / len(r[2 - 1 : ])) ** (1.0 / normType)
# ... and the normalisation factor ("standard deviation")
if (isDFA == 0):
S[k - 1] = np.sqrt(np.sum(np.abs(np.diff(r)) ** 2.0) / (len(r) - 1))
if (isNormalised == 1): # Then we either normalise the R / S values, treating them as probabilities ...
p = np.divide(R, S) / np.sum(np.divide(R, S))
else: # ... or leave them unnormalised ...
p = np.divide(R, S)
# ... and compute the measures in the q-norms
for k in range(1, n + 1):
# This 'if' is needed to prevent measure blow-ups with negative values of 'q' when the probability is close to zero
if (p[k - 1] < 1000.0 * aux_eps):
continue
dataMeasure[:, ji - 1] = dataMeasure[:, ji - 1] + np.power(p[k - 1], q[:, 1 - 1])
# We pass from the scales ('j') to the time measure; the time measure at the scale j(nScales) (the most major one)
# is assumed to be 2.0, while it is growing when the scale is tending to j(1) (the most minor one).
# (The scale j(nScales)'s time measure is NOT equal to 1.0, because we reserved the highest scale for shifts
# in the very beginning of the function.)
timeMeasure = 2.0 * dx_len / (2 * (j + 1))
scales = j + 1
## Determining the exponents 'tau' from 'dataMeasure(q, timeMeasure) ~ timeMeasure ^ tau(q)'
tau = np.zeros((nq, 1))
log10tm = np.log10(timeMeasure)
log10dm = np.log10(dataMeasure)
log10tm_mean = np.mean(log10tm)
# For each value of the q-norm we compute the mean 'tau' over all the scales
for qi in range(1, nq + 1):
tau[qi - 1, 1 - 1] = np.sum(np.multiply(log10tm, (log10dm[qi - 1, :] - np.mean(log10dm[qi - 1, :])))) / np.sum(np.multiply(log10tm, (log10tm - log10tm_mean)))
## Finally, we only have to pass from 'tau(q)' to its conjugate function 'f(alpha)'
# In doing so, first we find the Lipschitz-Holder exponents 'alpha' (represented by the variable 'LH') ...
aux_top = (tau[2 - 1] - tau[1 - 1]) / (q[2 - 1] - q[1 - 1])
aux_middle = np.divide(tau[3 - 1 : , 1 - 1] - tau[1 - 1 : -1 - 1, 1 - 1], q[3 - 1 : , 1 - 1] - q[1 - 1 : -1 - 1, 1 - 1])
aux_bottom = (tau[-1] - tau[-1 - 1]) / (q[-1] - q[-1 - 1])
LH = np.zeros((nq, 1))
LH[:, 1 - 1] = -np.concatenate((aux_top, aux_middle, aux_bottom))
# ... and then compute the conjugate function 'f(alpha)' itself
f = np.multiply(LH, q) + tau
## The last preparations
# We determine the minimum and maximum values of 'alpha' ...
LH_min = LH[-1, 1 - 1]
LH_max = LH[1 - 1, 1 - 1]
# ... and find the minimum and maximum values of another multifractal characteristic, the so-called
# generalised Hurst (or DFA) exponent 'h'. (These parameters are computed according to [2, p. 27].)
h_min = -(1.0 + tau[-1, 1 - 1]) / q[-1, 1 - 1]
h_max = -(1.0 + tau[1 - 1, 1 - 1]) / q[1 - 1, 1 - 1]
stats = {'tau': tau,
'LH': LH,
'f': f,
'LH_min': LH_min,
'LH_max': LH_max,
'h_min': h_min,
'h_max': h_max}
return [timeMeasure, dataMeasure, scales, stats, q]
################################
# MODULO C
################################
#MFDFA-Analytics-by-SKDataScience
#multifractal DFA singularity spectra - module D
#Version 3.0 - Modified by R.R.Rosa - Dec 2018 - mfdfa_ss_m3.py
# This function determines the optimal linear approximations of the data measure using two segments and returns
# the index of the corresponding boundary scale (a.k.a. crossover), the boundary scale itself, as well as the
# unifractal characteristics at the major and minor scales. For examples of using crossovers, see [1, 2].
#
# At the input, 'timeMeasure' is a time measure at different scales, while 'dataMeasure' is a data measure at the same
# scales.
#
# At the output, 'bScale' is the boundary scale, or crossover, separating the major and minor scales, 'bDM' is the
# data measure at the boundary scale, 'bsIndex' is the crossover's index with respect to the time measure, 'HMajor' is
# the unifractal dimension at the major scales, 'HMinor' is the unifractal dimension at the minor scales.
#
# REFERENCES:
# [1] D.M. Filatov, J. Stat. Phys., 165 (2016) 681-692. DOI: 10.1007/s10955-016-1641-6.
# [2] C.-K. Peng, S. Havlin, H.E. Stanley and A.L. Goldberger, Chaos, 5 (1995) 82–87. DOI: 10.1063/1.166141.
#
# The end user is granted perpetual permission to reproduce, adapt, and/or distribute this code, provided that
# an appropriate link is given to the original repository it was downloaded from.
import numpy as np
def getScalingExponents(timeMeasure, dataMeasure):
## Initialisation
nScales = len(timeMeasure)
log10tm = np.log10(timeMeasure)
log10dm = np.log10(dataMeasure)
res = 1.0e+07
bsIndex = nScales
## Computing
# We find linear approximations for major and minor subsets of the data measure and determine the index of the
# boundary scale at which the approximations are optimal in the sense of best fitting to the data measure
for i in range(3, nScales - 2 + 1):
# Major 'i' scales are approximated by the function 'k * x + b' ...
curr_log10tm = log10tm[nScales - i + 1 - 1 : nScales]
curr_log10dm = log10dm[nScales - i + 1 - 1 : nScales]
detA = i * np.sum(curr_log10tm ** 2.0) - np.sum(curr_log10tm) ** 2.0
detK = i * np.sum(np.multiply(curr_log10tm, curr_log10dm)) - np.sum(curr_log10tm) * np.sum(curr_log10dm)
detB = np.sum(curr_log10dm) * np.sum(curr_log10tm ** 2.0) - np.sum(curr_log10tm) * np.sum(np.multiply(curr_log10tm, curr_log10dm))
k = detK / detA
b = detB / detA
# ... and the maximum residual is computed
resMajor = max(np.abs(k * curr_log10tm + b - curr_log10dm))
# Minor 'nScales - i + 1' scales are approximated by the function 'k * x + b' ...
curr_log10tm = log10tm[1 - 1 : nScales - i + 1]
curr_log10dm = log10dm[1 - 1 : nScales - i + 1]
detA = (nScales - i + 1) * np.sum(curr_log10tm ** 2.0) - np.sum(curr_log10tm) ** 2.0
detK = (nScales - i + 1) * np.sum(np.multiply(curr_log10tm, curr_log10dm)) - np.sum(curr_log10tm) * np.sum(curr_log10dm)
detB = np.sum(curr_log10dm) * np.sum(curr_log10tm ** 2.0) - np.sum(curr_log10tm) * np.sum(np.multiply(curr_log10tm, curr_log10dm))
k = detK / detA
b = detB / detA
# ... and the maximum residual is computed
resMinor = max(np.abs(k * curr_log10tm + b - curr_log10dm))
if (resMajor ** 2.0 + resMinor ** 2.0 < res):
res = resMajor ** 2.0 + resMinor ** 2.0
bsIndex = i
# Now we determine the boundary scale and the boundary scale's data measure, ...
bScale = 2.0 * timeMeasure[1 - 1] / timeMeasure[nScales - bsIndex + 1 - 1] / 2.0
bDM = dataMeasure[nScales - bsIndex + 1 - 1]
# ... as well as compute the unifractal dimensions using the boundary scale's index:
# at the major 'bsIndex' scales ...
curr_log10tm = log10tm[nScales - bsIndex + 1 - 1 : nScales]
curr_log10dm = log10dm[nScales - bsIndex + 1 - 1 : nScales]
detA = bsIndex * np.sum(curr_log10tm ** 2.0) - np.sum(curr_log10tm) ** 2.0
detK = bsIndex * np.sum(np.multiply(curr_log10tm, curr_log10dm)) - np.sum(curr_log10tm) * np.sum(curr_log10dm)
DMajor = detK / detA
HMajor = -DMajor
# ... and at the minor 'nScales - bsIndex + 1' scales
curr_log10tm = log10tm[1 - 1 : nScales - bsIndex + 1]
curr_log10dm = log10dm[1 - 1 : nScales - bsIndex + 1]
detA = (nScales - bsIndex + 1) * np.sum(curr_log10tm ** 2.0) - np.sum(curr_log10tm) ** 2.0
detK = (nScales - bsIndex + 1) * np.sum(np.multiply(curr_log10tm, curr_log10dm)) - np.sum(curr_log10tm) * np.sum(curr_log10dm)
DMinor = detK / detA
HMinor = -DMinor
return [bScale, bDM, bsIndex, HMajor, HMinor]
def gamma2(df):
[_, dataMeasure, _, stats, q] = getMSSByUpscaling(df, isNormalised = 1)
dalfa = (stats['LH_max'] - stats['LH_min'])
gamma2 = 2/3*dalfa
return gamma2,dalfa