-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasures.py
More file actions
657 lines (563 loc) · 20.1 KB
/
Copy pathmeasures.py
File metadata and controls
657 lines (563 loc) · 20.1 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# region Lyapunov
from __future__ import annotations
from math import factorial
from typing import Dict, List, Optional, Sequence, Tuple
import numpy as np
from numpy.typing import ArrayLike, NDArray
from scipy.spatial import cKDTree
def _norm_parameter(metric: float | str) -> float:
if metric in {"euclidean", 2}:
return 2.0
if metric in {"manhattan", 1}:
return 1.0
if metric in {"chebyshev", "infinity", np.inf}: # type: ignore[unreachable]
return np.inf
if isinstance(metric, (int, float)) and metric > 0:
return float(metric)
raise ValueError("Metrica non supportata")
def autocorrelation(
series: ArrayLike,
max_lag: Optional[int] = None,
*,
demean: bool = True,
normalize: bool = True,
method: str = "fft",
) -> NDArray[np.float64]:
"""Compute the (auto-)correlation function of a real-valued series.
Parameters
----------
series:
Input time series.
max_lag:
Largest lag for which the autocorrelation is computed. If ``None``
the default is ``len(series) // 2``.
demean:
Whether to remove the mean before computing the correlations.
normalize:
If ``True`` the returned values are normalised by the variance so that
``acf[0] == 1``.
method:
``"fft"`` (default) uses the convolution theorem and is ``O(n log n)``.
``"direct"`` performs an explicit summation and is ``O(n * max_lag)``.
Returns
-------
numpy.ndarray
Array of length ``max_lag + 1`` containing the autocorrelation values.
"""
x = np.asarray(series, dtype=float)
n = x.size
if n == 0:
raise ValueError("Serie vuota")
if max_lag is None:
max_lag = max(1, n // 2)
if max_lag >= n:
raise ValueError("'max_lag' deve essere minore della lunghezza della serie")
if demean:
x = x - x.mean()
if method not in {"fft", "direct"}:
raise ValueError(f"Metodo non supportato: {method}")
if method == "fft":
fft_size = 1 << (2 * n - 1).bit_length()
fft = np.fft.rfft(x, n=fft_size)
acf = np.fft.irfft(fft * np.conjugate(fft), n=fft_size)[: max_lag + 1]
acf /= np.arange(n, n - max_lag - 1, -1)
else:
acf = np.array(
[np.dot(x[: n - lag], x[lag:]) / (n - lag) for lag in range(max_lag + 1)]
)
if normalize:
variance = acf[0]
if variance == 0:
return np.zeros_like(acf)
acf = acf / variance
return acf
def integrated_autocorrelation_time(
series: ArrayLike,
max_lag: Optional[int] = None,
*,
window: Optional[int] = None,
method: str = "fft",
) -> float:
"""Estimate the integrated autocorrelation time.
The sum is truncated at ``window`` if provided, otherwise at the first
non-positive correlation or at ``max_lag``.
"""
acf = autocorrelation(series, max_lag=max_lag, method=method)
if window is None:
# heuristic: stop at first negative value of ACF
positive = np.where(acf <= 0)[0]
cutoff = positive[0] if positive.size > 0 else len(acf)
else:
cutoff = min(window + 1, len(acf))
tau = 1 + 2 * np.sum(acf[1:cutoff])
return float(tau)
def time_delay_embedding(series: ArrayLike, emb_dim: int, delay: int) -> NDArray[np.float64]:
series = np.asarray(series, dtype=float)
if emb_dim <= 0 or delay <= 0:
raise ValueError("'emb_dim' e 'delay' devono essere positivi")
n_vectors = len(series) - (emb_dim - 1) * delay
if n_vectors <= 0:
raise ValueError("Serie troppo corta per l'embedding richiesto")
return np.array([
series[i : i + emb_dim * delay : delay] for i in range(n_vectors)
])
def correlation_integral(
embedded: NDArray[np.float64],
radii: Sequence[float],
*,
theiler: int = 0,
metric: float | str = "euclidean",
) -> NDArray[np.float64]:
"""Compute the Grassberger–Procaccia correlation integral.
Parameters
----------
embedded:
Delay vectors in the reconstructed phase space.
radii:
Iterable of radii at which the correlation integral is evaluated.
theiler:
Theiler window: pairs of points closer than this delay are ignored.
metric:
Norm used for distances (passed to :meth:`cKDTree.query_pairs`). Use
``np.inf`` for Chebyshev metric.
"""
points = np.asarray(embedded, dtype=float)
if points.ndim != 2:
raise ValueError("'embedded' deve essere una matrice 2D di vettori ritardati")
if points.shape[0] < 2:
raise ValueError("Servono almeno due vettori per calcolare l'integrale di correlazione")
tree = cKDTree(points)
n = len(points)
radii = np.asarray(radii, dtype=float)
if np.any(radii <= 0):
raise ValueError("I raggi devono essere positivi")
counts = np.empty_like(radii)
p = _norm_parameter(metric)
for idx, r in enumerate(radii):
pairs = tree.query_pairs(r, p=p, output_type="ndarray")
if theiler > 0 and pairs.size:
mask = np.abs(pairs[:, 0] - pairs[:, 1]) > theiler
pairs = pairs[mask]
counts[idx] = pairs.shape[0]
norm = n * (n - 1) / 2
return counts / norm
def correlation_dimension(
series: ArrayLike,
*,
emb_dim: int,
delay: int,
radii: Sequence[float],
theiler: int = 0,
metric: float | str = "euclidean",
max_points: Optional[int] = 5000,
fit_range: Optional[Tuple[int, int]] = None,
) -> Tuple[np.ndarray, np.ndarray, float]:
"""Estimate the correlation dimension via the Grassberger–Procaccia method.
Returns the radii, the correlation integral values and the slope of the
scaling region (log-log fit). ``fit_range`` specifies the slice over which
the linear regression in log-space is computed; if ``None`` the full range
is used.
"""
data = np.asarray(series, dtype=float)
embedded = time_delay_embedding(data, emb_dim, delay)
if max_points is not None and len(embedded) > max_points:
rng = np.random.default_rng(12345)
indices = rng.choice(len(embedded), size=max_points, replace=False)
embedded = embedded[np.sort(indices)]
radii_array = np.asarray(radii, dtype=float)
corr = correlation_integral(embedded, radii_array, theiler=theiler, metric=metric)
positive = corr > 0
if not np.any(positive):
return radii_array, corr, np.nan
log_r = np.log(radii_array[positive])
log_c = np.log(corr[positive])
if fit_range is None:
start, end = 0, len(log_r)
else:
start = max(0, fit_range[0])
end = min(len(log_r), fit_range[1])
if end - start < 2:
return radii_array, corr, np.nan
slope, _ = np.polyfit(log_r[start:end], log_c[start:end], 1)
return radii_array, corr, float(slope)
def largest_lyapunov_rosenstein(series, dt, emb_dim=6, delay=8, theiler=50, fit_range=(5, 25)):
embedded = time_delay_embedding(series, emb_dim, delay)
tree = cKDTree(embedded)
distances, indices = tree.query(embedded, k=emb_dim + 2)
nn_indices = np.full(len(embedded), -1, dtype=int)
for i in range(len(embedded)):
for neighbor in indices[i, 1:]:
if neighbor == i:
continue
if abs(neighbor - i) > theiler:
nn_indices[i] = neighbor
break
if nn_indices[i] == -1:
nn_indices[i] = indices[i, 1]
max_t = min(200, len(embedded))
log_divergence = []
valid_steps = []
for t in range(max_t):
separations = []
for idx, neighbor in enumerate(nn_indices):
if neighbor < 0:
continue
if idx + t >= len(embedded) or neighbor + t >= len(embedded):
continue
dist = np.linalg.norm(embedded[idx + t] - embedded[neighbor + t])
if dist > 0:
separations.append(np.log(dist))
if separations:
log_divergence.append(np.mean(separations))
valid_steps.append(t * dt)
elif log_divergence:
break
if len(valid_steps) < 6:
return np.nan
start, end = fit_range
end = min(end, len(valid_steps))
start = min(start, end - 2)
coeffs = np.polyfit(valid_steps[start:end], log_divergence[start:end], 1)
return coeffs[0]
# endregion
# region IAAFT
def iaaft_surrogate(series, n_iterations=100):
series = np.asarray(series, dtype=float)
sorted_series = np.sort(series)
target_amplitude = np.abs(np.fft.rfft(series))
surrogate = np.random.permutation(series)
for _ in range(n_iterations):
surrogate_fft = np.fft.rfft(surrogate)
surrogate = np.fft.irfft(
target_amplitude * np.exp(1j * np.angle(surrogate_fft)), n=len(series)
)
ranks = np.argsort(np.argsort(surrogate))
surrogate = sorted_series[ranks]
return surrogate
def iaaft_nonlinearity_test(series, dt=1.0, n_surrogates=20):
reduced = series[::2]
statistics = []
for _ in range(n_surrogates):
surrogate = iaaft_surrogate(reduced, n_iterations=50)
statistics.append(largest_lyapunov_rosenstein(surrogate, dt))
statistics = np.array([s for s in statistics if np.isfinite(s)])
observed = largest_lyapunov_rosenstein(reduced, dt)
if statistics.size == 0 or not np.isfinite(observed):
return np.nan, np.nan, np.nan, np.nan
mean = statistics.mean()
std = statistics.std(ddof=1) if statistics.size > 1 else np.nan
z_score = (observed - mean) / std if np.isfinite(std) and std > 0 else np.nan
p_value = (np.sum(statistics >= observed) + 1) / (statistics.size + 1)
return observed, mean, std, z_score, p_value
# endregion
# region HUSRT exponent
def hurst_rs(series, min_window=16, max_window=None, num_windows=20):
series = np.asarray(series, dtype=float)
series = series - np.mean(series)
n = len(series)
if max_window is None:
max_window = n // 6
windows = np.unique(
np.logspace(
np.log10(min_window), np.log10(max_window), num=num_windows, dtype=int
)
)
rs_values = []
valid_windows = []
for w in windows:
if w < min_window or w >= n // 2:
continue
n_segments = n // w
if n_segments < 2:
continue
data = series[: n_segments * w].reshape((n_segments, w))
data = data - data.mean(axis=1, keepdims=True)
cumulative = np.cumsum(data, axis=1)
ranges = cumulative.max(axis=1) - cumulative.min(axis=1)
stds = data.std(axis=1, ddof=1)
valid = stds > 0
if not np.any(valid):
continue
rs = np.mean(ranges[valid] / stds[valid])
rs_values.append(rs)
valid_windows.append(w)
if len(rs_values) < 2:
return np.nan
slope, _ = np.polyfit(np.log(valid_windows), np.log(rs_values), 1)
return slope
def dfa_alpha(series, min_window=16, max_window=None, num_windows=20, order=1):
series = np.asarray(series, dtype=float)
series = series - np.mean(series)
n = len(series)
if max_window is None:
max_window = n // 6
windows = np.unique(
np.logspace(
np.log10(min_window), np.log10(max_window), num=num_windows, dtype=int
)
)
profile = np.cumsum(series)
flucts = []
valid_windows = []
for w in windows:
if w < min_window or w >= n // 2:
continue
n_segments = n // w
if n_segments < 2:
continue
segments = profile[: n_segments * w].reshape((n_segments, w))
x = np.arange(w)
rms = []
for segment in segments:
coeffs = np.polyfit(x, segment, order)
trend = np.polyval(coeffs, x)
rms.append(np.sqrt(np.mean((segment - trend) ** 2)))
rms = np.array(rms)
if np.all(rms == 0):
continue
flucts.append(np.mean(rms))
valid_windows.append(w)
if len(flucts) < 2:
return np.nan
slope, _ = np.polyfit(np.log(valid_windows), np.log(flucts), 1)
return slope
# endregion
# region Recurrence plots and entropies
def recurrence_matrix(
embedded: NDArray[np.float64],
*,
threshold: Optional[float] = None,
metric: float | str = "euclidean",
percentage: Optional[float] = None,
theiler: int = 0,
) -> Tuple[NDArray[np.bool_], float]:
"""Compute the recurrence matrix for a set of embedded vectors.
Parameters
----------
embedded:
Delay vectors (``n × d`` array).
threshold:
Distance threshold ``ε``. If ``None`` it is estimated so that the
recurrence rate matches ``percentage`` if provided, otherwise the 10th
percentile of the distance distribution is used.
percentage:
Desired recurrence rate (between 0 and 1). Ignored if ``threshold`` is
given. The Theiler window is not considered when computing this rate.
metric:
Norm used for distances (``np.inf`` corresponds to the Chebyshev norm).
theiler:
Sets to zero the band of width ``2 * theiler + 1`` around the main
diagonal.
"""
points = np.asarray(embedded, dtype=float)
if points.ndim != 2:
raise ValueError("'embedded' deve essere un array 2D")
n = len(points)
if n == 0:
raise ValueError("L'array embedding è vuoto")
tree = cKDTree(points)
p = _norm_parameter(metric)
if threshold is None:
rng = np.random.default_rng(12345)
sample_size = min(n, 2000)
if sample_size < 2:
eps = 0.0
else:
indices = rng.choice(n, size=sample_size, replace=False)
subset = points[indices]
diffs = subset[:, None, :] - subset[None, :, :]
if np.isinf(p):
distances = np.max(np.abs(diffs), axis=-1)
else:
distances = np.linalg.norm(diffs, ord=p, axis=-1)
tri = distances[np.triu_indices(sample_size, k=1)]
if tri.size == 0:
eps = 0.0
else:
if percentage is not None:
if not (0 < percentage < 1):
raise ValueError("'percentage' deve essere nell'intervallo (0, 1)")
k = int(np.floor(percentage * tri.size))
k = np.clip(k, 0, tri.size - 1)
eps = float(np.partition(tri, k)[k])
else:
eps = float(np.percentile(tri, 10))
else:
eps = float(threshold)
if eps < 0:
raise ValueError("La soglia deve essere non negativa")
# Build recurrence matrix
pairs = tree.query_pairs(eps, p=p, output_type="ndarray")
R = np.zeros((n, n), dtype=bool)
if pairs.size:
i, j = pairs[:, 0], pairs[:, 1]
R[i, j] = True
R[j, i] = True
np.fill_diagonal(R, True)
if theiler > 0:
for offset in range(-theiler, theiler + 1):
if offset >= 0:
rows = np.arange(0, n - offset)
cols = rows + offset
else:
cols = np.arange(0, n + offset)
rows = cols - offset
R[rows, cols] = False
return R, eps
def _run_lengths(binary: NDArray[np.bool_]) -> List[int]:
if binary.ndim != 1:
raise ValueError("L'array deve essere monodimensionale")
lengths: List[int] = []
count = 0
for value in binary:
if value:
count += 1
elif count:
lengths.append(count)
count = 0
if count:
lengths.append(count)
return lengths
def _diagonal_lengths(R: NDArray[np.bool_]) -> List[int]:
n = R.shape[0]
lengths: List[int] = []
for offset in range(-n + 1, n):
diag = np.diagonal(R, offset=offset)
if diag.size:
lengths.extend(_run_lengths(diag))
return lengths
def _vertical_lengths(R: NDArray[np.bool_]) -> List[int]:
lengths: List[int] = []
for col in range(R.shape[1]):
lengths.extend(_run_lengths(R[:, col]))
return lengths
def recurrence_quantification(
R: NDArray[np.bool_],
*,
l_min: int = 2,
v_min: int = 2,
) -> Dict[str, float]:
"""Compute standard RQA (Recurrence Quantification Analysis) measures."""
if R.ndim != 2 or R.shape[0] != R.shape[1]:
raise ValueError("La matrice di ricorrenza deve essere quadrata")
n = R.shape[0]
R = R.astype(bool, copy=True)
np.fill_diagonal(R, False)
total = n * n - n # exclude main diagonal
off_diag = R.sum()
rr = off_diag / total if total > 0 else np.nan
diag_lengths = [L for L in _diagonal_lengths(R) if L >= l_min]
if off_diag == 0:
det = np.nan
l_mean = np.nan
l_max = 0
entr = np.nan
else:
diag_points = sum(L for L in diag_lengths)
det = diag_points / off_diag if off_diag > 0 else np.nan
l_mean = diag_points / len(diag_lengths) if diag_lengths else np.nan
l_max = max(diag_lengths) if diag_lengths else 0
if diag_lengths:
counts = np.bincount(diag_lengths)
probs = counts[counts > 0] / counts.sum()
entr = -np.sum(probs * np.log(probs))
else:
entr = np.nan
vert_lengths = [L for L in _vertical_lengths(R) if L >= v_min]
vert_points = sum(vert_lengths)
lam = vert_points / off_diag if off_diag > 0 else np.nan
tt = vert_points / len(vert_lengths) if vert_lengths else np.nan
return {
"RR": float(rr),
"DET": float(det),
"L_mean": float(l_mean),
"L_max": float(l_max),
"LAM": float(lam),
"TT": float(tt),
"ENTR": float(entr),
}
def permutation_entropy(
series: ArrayLike,
*,
order: int = 3,
delay: int = 1,
normalize: bool = True,
) -> float:
"""Permutation entropy as defined by Bandt & Pompe."""
x = np.asarray(series, dtype=float)
if order < 2:
raise ValueError("L'ordine deve essere almeno 2")
if delay <= 0:
raise ValueError("Il ritardo deve essere positivo")
n = len(x) - (order - 1) * delay
if n <= 0:
raise ValueError("Serie troppo corta per l'ordine richiesto")
patterns: Dict[Tuple[int, ...], int] = {}
for i in range(n):
window = x[i : i + order * delay : delay]
ranks = tuple(np.argsort(np.argsort(window, kind="mergesort")))
patterns[ranks] = patterns.get(ranks, 0) + 1
counts = np.array(list(patterns.values()), dtype=float)
probs = counts / counts.sum()
entropy = -np.sum(probs * np.log(probs))
if normalize:
entropy /= np.log(factorial(order))
return float(entropy)
def sample_entropy(
series: ArrayLike,
*,
m: int = 2,
r: float = 0.2,
theiler: int = 0,
) -> float:
"""Sample entropy (SampEn) following Richman & Moorman (2000)."""
x = np.asarray(series, dtype=float)
if m < 1:
raise ValueError("'m' deve essere almeno 1")
if r < 0:
raise ValueError("'r' deve essere non negativo")
if len(x) <= m + 1:
raise ValueError("Serie troppo corta per i parametri richiesti")
std = np.std(x)
if std == 0:
return 0.0
tol = r * std
emb_m = time_delay_embedding(x, m, 1)
emb_m1 = time_delay_embedding(x, m + 1, 1)
def _count_pairs(embedded: NDArray[np.float64]) -> float:
tree = cKDTree(embedded)
pairs = tree.query_pairs(tol, p=np.inf, output_type="ndarray")
if theiler > 0 and pairs.size:
mask = np.abs(pairs[:, 0] - pairs[:, 1]) > theiler
pairs = pairs[mask]
return float(pairs.shape[0])
count_m = _count_pairs(emb_m)
count_m1 = _count_pairs(emb_m1)
nm = len(emb_m)
nm1 = len(emb_m1)
total_m = nm * (nm - 1) / 2
total_m1 = nm1 * (nm1 - 1) / 2
if total_m == 0 or total_m1 == 0:
return np.nan
B = count_m / total_m
A = count_m1 / total_m1
if A == 0 or B == 0:
return np.inf
return float(-np.log(A / B))
__all__ = [
"autocorrelation",
"integrated_autocorrelation_time",
"time_delay_embedding",
"correlation_integral",
"correlation_dimension",
"largest_lyapunov_rosenstein",
"iaaft_surrogate",
"iaaft_nonlinearity_test",
"hurst_rs",
"dfa_alpha",
"recurrence_matrix",
"recurrence_quantification",
"permutation_entropy",
"sample_entropy",
]
# endregion