1+ import numpy as np
2+
3+ from StatTools .experimental .analysis .tools import get_extra_h_dfa
4+
5+ def adjust_hurst_to_range (signal : np .ndarray ) -> tuple [np .ndarray , int ]:
6+ """
7+ Adjusts the Hurst exponent of a signal to fall within the range [0.5, 1.5]
8+ using cumulative sums and differences.
9+
10+ The function uses mathematical transformations:
11+ - Cumulative sum (np.cumsum) increases the Hurst exponent by ~1.
12+ - Differencing (np.diff) decreases the Hurst exponent by ~1.
13+
14+ Parameters:
15+ signal (array_like): Input 1D signal whose Hurst exponent needs adjustment
16+
17+ Returns:
18+ adjusted_signal (ndarray): Signal with Hurst exponent adjusted to the target range
19+ applied_steps (int): Number of transformation steps applied:
20+ - Positive: number of cumulative sums applied (Hurst increased)
21+ - Negative: number of differences applied (Hurst decreased)
22+ - Zero: no transformation needed
23+
24+ Notes:
25+ - For H > 1.5: applies differencing to reduce Hurst exponent.
26+ - For H < 0.5: applies cumulative sums to increase Hurst exponent.
27+ - The transformation follows approximate relationship:
28+ H_transformed ≈ H_original + applied_steps.
29+ """
30+ h = get_extra_h_dfa (signal )
31+ adjusted_signal = signal
32+ applied_steps = 0
33+ effective_h = h
34+ if h > 1.5 :
35+ while effective_h > 1.5 :
36+ applied_steps -= 1
37+ effective_h -= 1
38+ elif h < 0.5 :
39+ while effective_h < 0.5 :
40+ applied_steps += 1
41+ effective_h += 1
42+
43+ if applied_steps < 0 :
44+ for _ in range (abs (applied_steps )):
45+ adjusted_signal = np .diff (adjusted_signal )
46+
47+ elif applied_steps > 0 :
48+ for _ in range (applied_steps ):
49+ adjusted_signal = np .cumsum (adjusted_signal )
50+
51+ return adjusted_signal , applied_steps
52+
53+
54+ def reverse_hurst_adjustment (adjusted_signal : np .ndarray , applied_steps : int ) -> np .ndarray :
55+ """
56+ Reverses the Hurst exponent adjustment applied by adjust_hurst_to_range().
57+
58+ Applies the inverse transformations to recover the original signal
59+ with its initial Hurst exponent.
60+
61+ Parameters:
62+ adjusted_signal (array_like): Signal that was previously processed by adjust_hurst_to_range().
63+ applied_steps (int): Number of transformation steps that were originally applied.
64+
65+ Returns:
66+ original_signal (ndarray): Signal with the original Hurst exponent restored.
67+
68+ Notes:
69+ - If applied_steps was negative (differencing applied originally),
70+ applies cumulative sums to reconstruct the signal.
71+ - If applied_steps was positive (cumulative sums applied originally),
72+ applies differencing to reconstruct the signal.
73+ - The length of the returned signal may differ from the original if
74+ multiple differencing operations were applied.
75+ """
76+ if applied_steps < 0 :
77+ for _ in range (abs (applied_steps )):
78+ adjusted_signal = np .cumsum (adjusted_signal )
79+
80+ elif applied_steps > 0 :
81+ for _ in range (applied_steps ):
82+ adjusted_signal = np .diff (adjusted_signal )
83+
84+ return adjusted_signal
0 commit comments