-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyticsModule.py
More file actions
1836 lines (1495 loc) · 85.3 KB
/
analyticsModule.py
File metadata and controls
1836 lines (1495 loc) · 85.3 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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# analytics module, used for statistical analysis of time price data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# from numpy import cumsum, log, polyfit, sqrt, std, subtract, var, log10
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.tsa.stattools import acf, adfuller, kpss
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.seasonal import seasonal_decompose
class TimeSeriesAnalyticsModule:
"""
exploring examples shared in the book
"""
def seasonaldecompose(self, series: pd.Series) -> dict:
results = seasonal_decompose(series, model='additive', filt=None, period=1, two_sided=True,
extrapolate_trend=0)
results_dict = {
'observed': results.observed.to_frame(name='observed'),
'trend': results.trend.to_frame(name='trend'),
'seasonal': results.seasonal.to_frame(name='seasonal'),
'residuals': results.resid.to_frame(name='residuals'),
}
print("successfully deseasonalized given series")
return results_dict
# --- Module 2.5: Analytics module: statistical analysis ---
class AnalyticsModule:
"""
Calculates returns, log returns, and their rolling statistics.
Returns new DataFrames with calculated analytics.
"""
def __init__(self):
pass
def calculate_returns_and_volatility(self, data: pd.DataFrame, period1: int = 10, period2: int = 20, period3: int = 30) -> pd.DataFrame:
"""
Calculates daily returns, daily log returns, and their rolling means and standard deviations.
Args:
data (pd.DataFrame): The input DataFrame with a 'close' column and 'date' as index.
period1: used for rolling windows
period2: used for rolling windows
period3: used for rolling windows
Returns:
pd.DataFrame: A new DataFrame with calculated return mean and volatility metrics.
"""
if 'close' not in data.columns:
print("Error: 'close' column not found in data for returns calculation.")
return pd.DataFrame()
analytics_df = pd.DataFrame(index=data.index)
# daily returns, rolling mean, rolling vol (std dev)
analytics_df['daily_returns'] = data['close'].pct_change()
analytics_df['daily_returns_squared'] = data['close'].pct_change() ** 2
analytics_df['rolling_mean_returns_1'] = analytics_df['daily_returns'].rolling(window=period1).mean()
analytics_df['rolling_mean_returns_2'] = analytics_df['daily_returns'].rolling(window=period2).mean()
analytics_df['rolling_mean_returns_3'] = analytics_df['daily_returns'].rolling(window=period3).mean()
analytics_df['vol_1'] = analytics_df['daily_returns'].rolling(window=period1).std()
analytics_df['vol_2'] = analytics_df['daily_returns'].rolling(window=period2).std()
analytics_df['vol_3'] = analytics_df['daily_returns'].rolling(window=period3).std()
# Daily Log Returns and std dev (log vol)
analytics_df['daily_log_returns'] = np.log(data['close'] / data['close'].shift(1))
analytics_df['daily_log_returns_squared'] = np.log(data['close'] / data['close'].shift(1)) ** 2
analytics_df['rolling_mean_log_returns_1'] = analytics_df['daily_log_returns'].rolling(window=period1).mean()
analytics_df['rolling_mean_log_returns_2'] = analytics_df['daily_log_returns'].rolling(window=period2).mean()
analytics_df['rolling_mean_log_returns_3'] = analytics_df['daily_log_returns'].rolling(window=period3).mean()
analytics_df['vol_log_1'] = analytics_df['daily_log_returns'].rolling(window=period1).std()
analytics_df['vol_log_2'] = analytics_df['daily_log_returns'].rolling(window=period2).std()
analytics_df['vol_log_3'] = analytics_df['daily_log_returns'].rolling(window=period3).std()
# Drop any NaN rows that result from rolling calculations at the beginning
return analytics_df.dropna()
def rolling_coefficient_pearson (self, series_1: pd.Series, series_2: pd.Series, symbol1: str = "", symbol2: str ="", window: int = 7) -> dict:
"""
Calculates and plots the rolling Pearson correlation coefficient between two assets' log returns.
Args:
series_1 (pd.Series): first data series (can be any series, doesn't have to be log returns)
series_2 (pd.Series): second data series (to compare to)
symbol1 (str): The ticker symbol of the first asset (for documentation, actual data depends on input series)
symbol2 (str): The ticker symbol of the second asset (for documentation, actual data depends on input series)
window (int): The rolling window size (number of periods) for correlation calculation.
returns:
dict containing:
df of all rolling correlation values up to the most current date
statistics regarding rolling coefficient:
series.mean(): Calculates the arithmetic mean.
series.std(): Calculates the standard deviation.
series.median(): Calculates the median (50th percentile).
series.min(): Returns the minimum value.
series.max(): Returns the maximum value.
series.quantile(q): Returns the value at the specified quantile q (e.g., 0.25 for the 25th percentile).
series.count(): Returns the number of non-null observations.
series.var(): Calculates the variance.
series.skew(): Calculates the skewness. A positive skew means the tail on the right side is longer or fatter. A negative skew means the tail on the left side is longer or fatter.
series.kurt(): Calculates the kurtosis. Positive kurtosis (leptokurtic) means more extreme outliers than a normal distribution. Negative kurtosis (platykurtic) means fewer extreme outliers.
"""
mydict = {} # empty dict to return when any error is encountered
# Calculate rolling coefficient based on given window
rolling_corr_pair = series_1.rolling(window=window).corr(series_2).dropna()
if rolling_corr_pair.empty:
print(
f"No sufficient data to calculate {window}-day rolling correlation for {symbol1} and {symbol2}. Check window size or data range.")
return mydict
if not rolling_corr_pair.empty: # stats regarding rolling correlation
print(f"\n--- Statistics for Rolling {window}-Day Correlation ({symbol1} vs {symbol2}) ---")
# Mean
print(f"Mean: {rolling_corr_pair.mean():.4f}")
# Standard Deviation
print(f"Standard Deviation: {rolling_corr_pair.std():.4f}")
# Median
print(f"Median: {rolling_corr_pair.median():.4f}")
# Minimum
print(f"Minimum: {rolling_corr_pair.min():.4f}")
# Maximum
print(f"Maximum: {rolling_corr_pair.max():.4f}")
# Quantiles (e.g., 25th, 50th, 75th percentiles)
print(f"25th Percentile (Q1): {rolling_corr_pair.quantile(0.25):.4f}")
print(f"75th Percentile (Q3): {rolling_corr_pair.quantile(0.75):.4f}")
# Count of non-NA/null values
print(f"Count: {rolling_corr_pair.count()}")
# Variance
print(f"Variance: {rolling_corr_pair.var():.4f}")
# Skewness (measure of asymmetry of the distribution)
print(f"Skewness: {rolling_corr_pair.skew():.4f}")
# Kurtosis (measure of 'tailedness' of the distribution)
print(f"Kurtosis: {rolling_corr_pair.kurt():.4f}")
# --- The describe() method for a quick summary ---
print(f"\n--- Full Statistics Summary (.describe()) for Rolling {window}-Day Correlation ---")
print(rolling_corr_pair.describe())
rolling_coefficient_results = {
'rolling_corr_df': rolling_corr_pair,
'mean': rolling_corr_pair.mean(),
'std': rolling_corr_pair.std(),
'median': rolling_corr_pair.median(),
'min': rolling_corr_pair.min(),
'max': rolling_corr_pair.max(),
'quantile_25': rolling_corr_pair.quantile(0.25),
'quantile_75': rolling_corr_pair.quantile(0.75),
'count': rolling_corr_pair.count(),
'variance': rolling_corr_pair.var(),
'skewness': rolling_corr_pair.skew(),
'kurtosis': rolling_corr_pair.kurt()
}
# Plotting the correlation values
plt.figure(figsize=(12, 6))
rolling_corr_pair.plot(title=f'Rolling {window}-Day Pearson Correlation: {symbol1} vs {symbol2}')
plt.xlabel('Date')
plt.ylabel('Correlation Coefficient')
plt.grid(True, linestyle='--', alpha=0.7)
plt.show()
return rolling_coefficient_results
def get_autocorrelation_values(self, data_series: pd.Series, nlags: int = None, qstat: bool = False):
"""
Calculates the Autocorrelation Function (ACF) values and optionally Q-statistic.
Args:
data_series (pd.Series): The time series data. Must not contain NaNs.
nlags (int, optional): The number of lags to return. If None, statsmodels chooses automatically.
qstat (bool, optional): If True, returns the Ljung-Box Q-statistic and p-value.
Returns:
np.ndarray: Array of ACF values.
tuple (optional): Q-statistic and p-value if qstat is True.
"""
if data_series.isnull().any():
print("Warning: Input data_series contains NaNs. ACF calculation might be affected or fail. Dropping NaNs.")
data_series = data_series.dropna()
if data_series.empty:
print("Error: data_series became empty after dropping NaNs. Cannot calculate ACF values.")
return None if not qstat else (None, None, None)
if qstat:
# Returns acf_values, q_statistic, p_values
return acf(x=data_series, nlags=nlags, qstat=True)
else:
return acf(x=data_series, nlags=nlags)
def perform_adf_test(self, series: pd.Series, significance_level: float = 0.05, regression: str = 'c', ):
"""
Performs the Augmented Dickey-Fuller (ADF) test on a time series to check for stationarity.
The null hypothesis of the test is that the series has a unit root (it is non-stationary).
If the p-value is below the significance level, we reject the null hypothesis.
Args:
series (pd.Series): The time series data to test (e.g., a column of closing prices).
significance_level (float): The threshold for the p-value to reject the null hypothesis.
"""
if not isinstance(series, pd.Series):
print("Error: Input must be a pandas Series.")
return
# Drop NaN values which can cause errors in the test
series_cleaned = series.dropna()
print(f"\n--- Augmented Dickey-Fuller Test Results for '{series_cleaned.name}' ---")
# Perform the ADF test
# The autolag='AIC' parameter automatically selects the optimal number of lags
adf_result = adfuller(series_cleaned, maxlag=None, regression='c', autolag='AIC', store=False, regresults=False)
# Extract and print the results in a readable format
print(f'ADF Statistic : {adf_result[0]:.20f}')
print(f'p-value : {adf_result[1]:.20f}')
print(f'# Lags Used : {adf_result[2]}')
print(f'# Observations : {adf_result[3]}')
print('\nCritical Values:')
for key, value in adf_result[4].items():
print(f'\t{key}: {value:.4f}')
# --- Interpretation of the results ---
print("\n--- Interpretation ---")
if adf_result[1] <= significance_level:
print(f"Conclusion: p-value ({adf_result[1]:.4f}) is less than or equal to {significance_level}.")
print(">> We reject the null hypothesis.")
print(">> The series is likely STATIONARY (does not have a unit root).")
else:
print(f"Conclusion: p-value ({adf_result[1]:.4f}) is greater than {significance_level}.")
print(">> We fail to reject the null hypothesis.")
print(">> The series is likely NON-STATIONARY (has a unit root).")
print("------------------------")
def perform_comprehensive_stationarity_test(self, series: pd.Series, significance_level: float = 0.05,
adf_regression: str = 'c', kpss_regression: str = "c",
kpss_nlags: str = "auto"):
"""
Performs both ADF and KPSS tests to provide a comprehensive stationarity analysis.
This method combines two complementary tests:
- ADF Test: H0 = non-stationary, H1 = stationary
- KPSS Test: H0 = stationary, H1 = non-stationary
By using both tests together, we can classify results into four categories:
1. Both reject H0 → Conflicting results (further investigation needed)
2. ADF rejects, KPSS fails to reject → Strong evidence for stationarity
3. ADF fails to reject, KPSS rejects → Strong evidence for non-stationarity
4. Both fail to reject H0 → Inconclusive results
Args:
series (pd.Series): The time series data to test
significance_level (float): Significance level for both tests
adf_regression (str): Regression type for ADF test ('c', 'ct', 'ctt', 'nc')
kpss_regression (str): Regression type for KPSS test ('c' or 'ct')
kpss_nlags (str or int): Lag selection for KPSS test
Returns:
dict: Comprehensive results from both tests with combined interpretation
"""
if not isinstance(series, pd.Series):
print("Error: Input must be a pandas Series.")
return {}
# Ensure we have sufficient data
series_cleaned = series.dropna()
if len(series_cleaned) < 10:
print("Error: Insufficient data points for stationarity tests. Need at least 10 observations.")
return {}
print(f"\n{'=' * 80}")
print(f"COMPREHENSIVE STATIONARITY ANALYSIS FOR '{series_cleaned.name}'")
print(f"{'=' * 80}")
print(f"Sample size: {len(series_cleaned)} observations")
print(f"Significance level: {significance_level}")
# Initialize results dictionary
comprehensive_results = {
'series_name': series_cleaned.name,
'sample_size': len(series_cleaned),
'significance_level': significance_level,
'adf': {},
'kpss': {}
}
# Perform ADF Test
print(f"\n{'-' * 40} ADF TEST {'-' * 40}")
try:
adf_result = adfuller(series_cleaned, maxlag=None, regression=adf_regression,
autolag='AIC', store=False, regresults=False)
adf_statistic = adf_result[0]
adf_pvalue = adf_result[1]
adf_lags = adf_result[2]
adf_nobs = adf_result[3]
adf_critical_values = adf_result[4]
# ADF Test results display
print(f'ADF Statistic : {adf_statistic:.6f}')
print(f'ADF p-value : {adf_pvalue:.6f}')
print(f'Lags Used : {adf_lags}')
print(f'Observations : {adf_nobs}')
adf_rejects_h0 = adf_pvalue <= significance_level
adf_conclusion = "STATIONARY" if adf_rejects_h0 else "NON-STATIONARY"
print(f'ADF Conclusion : {adf_conclusion} (H0: non-stationary)')
comprehensive_results['adf'] = {
'statistic': adf_statistic,
'p_value': adf_pvalue,
'lags_used': adf_lags,
'critical_values': adf_critical_values,
'rejects_h0': adf_rejects_h0,
'conclusion': adf_conclusion
}
except Exception as e:
print(f"ADF Test failed: {str(e)}")
comprehensive_results['adf'] = {'error': str(e)}
return comprehensive_results
# Perform KPSS Test
print(f"\n{'-' * 40} KPSS TEST {'-' * 39}")
try:
kpss_result = kpss(series_cleaned, regression= "c", nlags= "auto")
kpss_statistic = kpss_result[0]
kpss_pvalue = kpss_result[1]
kpss_lags = kpss_result[2]
kpss_critical_values = kpss_result[3]
# KPSS Test results display
print(f'KPSS Statistic : {kpss_statistic:.6f}')
print(f'KPSS p-value : {kpss_pvalue:.6f}')
print(f'Lags Used : {kpss_lags}')
print(f'Observations : {len(series_cleaned)}')
kpss_rejects_h0 = kpss_pvalue <= significance_level
kpss_conclusion = "NON-STATIONARY" if kpss_rejects_h0 else "STATIONARY"
print(f'KPSS Conclusion : {kpss_conclusion} (H0: stationary)')
comprehensive_results['kpss'] = {
'statistic': kpss_statistic,
'p_value': kpss_pvalue,
'lags_used': kpss_lags,
'critical_values': kpss_critical_values,
'rejects_h0': kpss_rejects_h0,
'conclusion': kpss_conclusion
}
except Exception as e:
print(f"KPSS Test failed: {str(e)}")
comprehensive_results['kpss'] = {'error': str(e)}
return comprehensive_results
# Combined Analysis and Interpretation
print(f"\n{'-' * 35} COMBINED ANALYSIS {'-' * 35}")
print("Test Summary:")
print(f" ADF Test: {adf_conclusion} (p-value: {adf_pvalue:.6f})")
print(f" KPSS Test: {kpss_conclusion} (p-value: {kpss_pvalue:.6f})")
# Determine combined conclusion based on test results
if adf_rejects_h0 and not kpss_rejects_h0:
# ADF says stationary, KPSS says stationary → Strong evidence for stationarity
final_conclusion = "STATIONARY"
confidence_level = "HIGH"
interpretation = ("Both tests agree: Strong evidence that the series is STATIONARY.\n"
"ADF rejects non-stationarity, KPSS accepts stationarity.")
elif not adf_rejects_h0 and kpss_rejects_h0:
# ADF says non-stationary, KPSS says non-stationary → Strong evidence for non-stationarity
final_conclusion = "NON-STATIONARY"
confidence_level = "HIGH"
interpretation = ("Both tests agree: Strong evidence that the series is NON-STATIONARY.\n"
"ADF accepts non-stationarity, KPSS rejects stationarity.")
elif adf_rejects_h0 and kpss_rejects_h0:
# ADF says stationary, KPSS says non-stationary → Conflicting results
final_conclusion = "CONFLICTING"
confidence_level = "LOW"
interpretation = ("Conflicting results: ADF suggests stationarity while KPSS suggests non-stationarity.\n"
"This may indicate the presence of structural breaks or other complications.\n"
"Consider: (1) Different lag selections, (2) Structural break tests, (3) Visual inspection.")
else:
# Both fail to reject their respective null hypotheses → Inconclusive
final_conclusion = "INCONCLUSIVE"
confidence_level = "LOW"
interpretation = ("Inconclusive results: Neither test provides strong evidence.\n"
"ADF cannot reject non-stationarity, KPSS cannot reject stationarity.\n"
"Consider: (1) Larger sample size, (2) Different significance levels, (3) Visual analysis.")
print(f"\nFINAL ASSESSMENT:")
print(f" Conclusion: {final_conclusion}")
print(f" Confidence: {confidence_level}")
print(f"\nInterpretation:")
for line in interpretation.split('\n'):
if line.strip():
print(f" {line}")
# Add combined results to return dictionary
comprehensive_results['combined_analysis'] = {
'final_conclusion': final_conclusion,
'confidence_level': confidence_level,
'interpretation': interpretation,
'adf_rejects_h0': adf_rejects_h0,
'kpss_rejects_h0': kpss_rejects_h0
}
print(f"\n{'=' * 80}")
return comprehensive_results
def calculate_hurst_exponent(self, series: pd.Series, max_lag: int = 100):
"""
Calculates the Hurst Exponent of a time series using Rescaled Range (R/S) analysis.
Args:
series (pd.Series): The time series data to analyze.
max_lag (int): The maximum number of lags to use for the calculation.
"""
if not isinstance(series, pd.Series):
print("Error: Input must be a pandas Series.")
return
series_cleaned = series.dropna()
if len(series_cleaned) < max_lag:
print(
f"Error: Series length ({len(series_cleaned)}) is shorter than max_lag ({max_lag}). Cannot calculate Hurst Exponent.")
return np.nan
lags = range(2, max_lag)
print("Index diagnostics:")
print(f"Index type: {type(series_cleaned.index)}")
print(f"Index min: {series_cleaned.index.min()}")
print(f"Index max: {series_cleaned.index.max()}")
print(f"Index is continuous: {series_cleaned.index.is_monotonic_increasing}")
print(f"First 10 index values: {series_cleaned.index[:10].tolist()}")
print(f"Last 10 index values: {series_cleaned.index[-10:].tolist()}")
print(f"Any duplicate indices: {series_cleaned.index.duplicated().any()}")
# Let's also check what happens when we manually align the arrays
lag = 2
array1 = series_cleaned.iloc[lag:] # Use iloc for position-based indexing
array2 = series_cleaned.iloc[:-lag]
manual_diff = array1.values - array2.values # Convert to numpy arrays to avoid index alignment
print(f"\nManual difference calculation (first 20 values): {manual_diff[:20]}")
print(f"Manual diff std: {np.std(manual_diff)}")
# Calculate the array of the variances of the lagged differences
tau = [np.sqrt(np.std(series_cleaned.iloc[lag:].values - series_cleaned.iloc[:-lag].values)) for lag in lags]
print("Calculated Tau values:", tau)
# Use a polyfit to plot the log of lags vs the log of tau
poly = np.polyfit(np.log(lags), np.log(tau), 1)
# The Hurst Exponent is the slope of the line
hurst_exponent = poly[0] * 2.0
print(f"\n--- Hurst Exponent Calculation for '{series_cleaned.name}' ---")
print(f"Hurst Exponent : {hurst_exponent:.4f}")
# --- Interpretation of the results ---
print("\n--- Interpretation ---")
if hurst_exponent < 0.5:
print(">> H < 0.5: The series is likely MEAN-REVERTING (anti-persistent).")
print(" A high value is likely to be followed by a low value and vice-versa.")
elif hurst_exponent > 0.5:
print(">> H > 0.5: The series is likely TRENDING (persistent).")
print(" A high value is likely to be followed by another high value.")
else:
print(">> H = 0.5: The series is likely a RANDOM WALK.")
print(" The movements are unpredictable.")
print("------------------------")
return hurst_exponent
def calculate_hurst_exponent_corrected(self, series: pd.Series,
min_window: int = 10,
max_window: int = None,
n_windows: int = 20) -> tuple[float, dict]:
"""
Calculates the Hurst Exponent using the proper Rescaled Range (R/S) methodology.
This implementation fixes the fundamental issues with the previous approach by:
1. Using cumulative deviations from the mean (not direct differences)
2. Calculating true rescaled range statistics (R/S)
3. Ensuring mathematically consistent results between 0 and 1
Args:
series: Time series data to analyze
min_window: Minimum window size for analysis
max_window: Maximum window size (defaults to series_length // 4)
n_windows: Number of different window sizes to test
Returns:
Tuple of (hurst_exponent, diagnostic_info)
"""
# Step 1: Clean and validate the data (at least 50 data points)
clean_series = series.dropna()
if len(clean_series) < 50:
raise ValueError(f"Need at least 50 data points, got {len(clean_series)}")
# Step 2: Set reasonable window size range
if max_window is None:
max_window = len(clean_series) // 4
print(f'max window: {max_window}, min window: {min_window}, length of series: {len(clean_series)}')
if max_window <= min_window:
raise ValueError(f'max_window ({max_window}) must be greater than min_window {min_window}')
# Step 3: Generate window sizes to test
# Using logarithmic spacing gives better coverage across scales
window_sizes = np.unique(
np.logspace(np.log10(min_window), np.log10(max_window), n_windows).astype(int)
)
print(f"Analyzing {len(window_sizes)} window sizes from {window_sizes[0]} to {window_sizes[-1]}")
# Step 4: Calculate R/S statistics for each window size
rs_statistics = []
for window_size in window_sizes:
print(f'calculating RS for window size {window_size}...')
# Calculate how many non-overlapping windows we can fit
n_segments = len(clean_series) // window_size
if n_segments < 2: # Need at least 2 segments for meaningful analysis
continue
segment_rs_values = []
# Step 5: Analyze each segment separately
for segment_idx in range(n_segments):
# Extract the segment data
start_idx = segment_idx * window_size
end_idx = start_idx + window_size
segment_data = clean_series.iloc[start_idx:end_idx].values
# Step 6: Calculate mean and deviations for this segment
segment_mean = np.mean(segment_data)
deviations = segment_data - segment_mean
# Step 7: Calculate cumulative sum of deviations
# This is the key step that captures long-term memory effects
cumulative_deviations = np.cumsum(deviations)
# Step 8: Calculate the Range (R)
# Range is the difference between max and min cumulative deviation
R = np.max(cumulative_deviations) - np.min(cumulative_deviations)
# Step 9: Calculate the Standard deviation (S)
# Use sample standard deviation (ddof=1)
S = np.std(segment_data, ddof=1)
# Step 10: Calculate R/S ratio (the rescaled range)
# This normalizes the range by the variability in the original data
if S > 0: # Avoid division by zero
segment_rs_values.append(R / S)
# Step 11: Average R/S across all segments for this window size
if segment_rs_values:
mean_rs = np.mean(segment_rs_values)
rs_statistics.append(mean_rs)
else:
rs_statistics.append(np.nan)
# Step 12: Remove any invalid values
valid_data = [(ws, rs) for ws, rs in zip(window_sizes, rs_statistics)
if not np.isnan(rs) and rs > 0]
if len(valid_data) < 5:
raise ValueError("Not enough valid data points for reliable Hurst estimation")
valid_windows, valid_rs = zip(*valid_data)
valid_windows = np.array(valid_windows)
valid_rs = np.array(valid_rs)
# Step 13: Fit the power law relationship
# The Hurst exponent is the slope of log(window_size) vs log(R/S)
log_windows = np.log10(valid_windows)
log_rs = np.log10(valid_rs)
# Linear regression to find the slope
coefficients = np.polyfit(log_windows, log_rs, 1)
hurst_exponent = coefficients[0] # The slope IS the Hurst exponent
intercept = coefficients[1]
# Step 14: Calculate goodness-of-fit measure
predicted_log_rs = hurst_exponent * log_windows + intercept
ss_residual = np.sum((log_rs - predicted_log_rs) ** 2)
ss_total = np.sum((log_rs - np.mean(log_rs)) ** 2)
r_squared = 1 - (ss_residual / ss_total) if ss_total > 0 else 0
# Step 15: Create diagnostic information
diagnostics = {
'window_sizes': valid_windows,
'rs_values': valid_rs,
'r_squared': r_squared,
'intercept': intercept,
'n_data_points': len(clean_series),
'n_window_sizes': len(valid_windows),
'log_windows': log_windows,
'log_rs': log_rs
}
# Step 16: Validate the result
if hurst_exponent < 0 or hurst_exponent > 1:
print(f"WARNING: Hurst exponent {hurst_exponent:.4f} is outside valid range [0,1]")
print("This suggests either data quality issues or computational problems")
# Step 17: Display results with interpretation
print(f"\n" + "=" * 60)
print(f"RESCALED RANGE ANALYSIS RESULTS")
print(f"=" * 60)
print(f"Series: {series.name if series.name else 'Unnamed'}")
print(f"Data points analyzed: {len(clean_series)}")
print(f"Window sizes tested: {len(valid_windows)}")
print(f"Hurst Exponent: {hurst_exponent:.4f}")
print(f"R-squared (fit quality): {r_squared:.4f}")
print(f"\n" + "-" * 40 + " INTERPRETATION " + "-" * 40)
if 0 <= hurst_exponent < 0.5:
strength = "Strong" if hurst_exponent < 0.4 else "Moderate" if hurst_exponent < 0.45 else "Weak"
print(f">> {strength} MEAN-REVERTING behavior (H = {hurst_exponent:.3f})")
print(f" • The series tends to return toward its mean over time")
print(f" • High values are more likely to be followed by lower values")
print(f" • This suggests potential opportunities in mean-reversion strategies")
elif hurst_exponent > 0.5:
strength = "Strong" if hurst_exponent > 0.6 else "Moderate" if hurst_exponent > 0.55 else "Weak"
print(f">> {strength} TRENDING behavior (H = {hurst_exponent:.3f})")
print(f" • The series shows persistence - trends tend to continue")
print(f" • High values are more likely to be followed by higher values")
print(f" • This suggests potential opportunities in momentum strategies")
else: # Very close to 0.5
print(f">> RANDOM WALK behavior (H ≈ 0.5)")
print(f" • The series shows no significant long-term memory")
print(f" • Future movements are largely independent of past movements")
print(f" • This is consistent with efficient market hypothesis")
# Step 18: Quality warnings
if r_squared < 0.8:
print(f"\n⚠️ WARNING: Low R-squared ({r_squared:.3f}) indicates poor fit")
print(f" Consider using more data or checking for non-stationarity")
if len(valid_windows) < 10:
print(f"\n⚠️ WARNING: Only {len(valid_windows)} window sizes analyzed")
print(f" Results may be less reliable with fewer data points")
print("=" * 60)
return float(hurst_exponent), diagnostics
def plot_hurst_analysis(self, hurst_exponent: float, diagnostics: dict,
series_name: str = "Time Series"):
"""
Creates a visualization of the Hurst exponent analysis to help understand the results.
This plot shows the log-log relationship that defines the Hurst exponent,
helping you see how well the power law fits your data.
"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
# Plot 1: The fundamental R/S relationship
log_windows = diagnostics['log_windows']
log_rs = diagnostics['log_rs']
# Scatter plot of actual data points
ax1.scatter(log_windows, log_rs, alpha=0.7, s=50, color='blue',
label='Calculated R/S values')
# Fitted line
fitted_line = hurst_exponent * log_windows + diagnostics['intercept']
ax1.plot(log_windows, fitted_line, 'r-', linewidth=2,
label=f'Fitted line (H = {hurst_exponent:.3f})')
# Reference lines for comparison
theoretical_05 = 0.5 * log_windows + diagnostics['intercept']
ax1.plot(log_windows, theoretical_05, 'g--', alpha=0.5,
label='H = 0.5 (Random Walk)')
ax1.set_xlabel('log₁₀(Window Size)', fontsize=12)
ax1.set_ylabel('log₁₀(R/S)', fontsize=12)
ax1.set_title(f'Rescaled Range Analysis\nR² = {diagnostics["r_squared"]:.3f}',
fontsize=14)
ax1.legend()
ax1.grid(True, alpha=0.3)
# Plot 2: R/S values vs window size (linear scale)
windows = diagnostics['window_sizes']
rs_values = diagnostics['rs_values']
ax2.loglog(windows, rs_values, 'bo-', alpha=0.7, markersize=6,
label='R/S Statistics')
# Theoretical lines for comparison
theoretical_rs_05 = (windows / windows[0]) ** 0.5 * rs_values[0]
theoretical_rs_h = (windows / windows[0]) ** hurst_exponent * rs_values[0]
ax2.loglog(windows, theoretical_rs_05, 'g--', alpha=0.5,
label='H = 0.5 Expected')
ax2.loglog(windows, theoretical_rs_h, 'r-', linewidth=2,
label=f'H = {hurst_exponent:.3f} Fitted')
ax2.set_xlabel('Window Size', fontsize=12)
ax2.set_ylabel('R/S Statistic', fontsize=12)
ax2.set_title(f'R/S Scaling Behavior\n{series_name}', fontsize=14)
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# Example usage with your data:
def analyze_your_returns(self, daily_returns_series):
"""
Analyzes your daily returns using the corrected Hurst exponent method.
This function demonstrates how to use the corrected implementation
and interpret the results for financial time series data.
"""
try:
# Calculate the Hurst exponent using the proper method
hurst, diagnostics = self.calculate_hurst_exponent_corrected(
daily_returns_series,
min_window=10, # Start with 10-day windows
max_window=None, # Let the function choose based on data length
n_windows=25 # Test 25 different window sizes
)
# Create visualization to help understand the results
self.plot_hurst_analysis(hurst, diagnostics, daily_returns_series.name or "Daily Returns")
return hurst, diagnostics
except Exception as e:
print(f"Error in Hurst analysis: {e}")
return None, None
def plot_autocorrelation(self, data_series: pd.Series, title: str = "Autocorrelation Function", lags: int = None,
alpha: float = 0.05):
"""
Plots the Autocorrelation Function (ACF) for a given time series.
Args:
data_series (pd.Series): The time series data to analyze (e.g., daily log returns, rolling correlation).
Must not contain NaNs.
title (str): Title for the plot.
lags (int, optional): The number of lags to plot. If None, statsmodels chooses automatically.
alpha (float, optional): Significance level for the confidence intervals (e.g., 0.05 for 95%).
"""
if data_series.isnull().any():
print(
"Warning: Input data_series contains NaNs. ACF calculation might be affected or fail. Dropping NaNs for plot.")
data_series = data_series.dropna()
if data_series.empty:
print("Error: data_series became empty after dropping NaNs. Cannot plot ACF.")
return
fig, ax = plt.subplots(figsize=(12, 6))
plot_acf(x=data_series, lags=lags, alpha=alpha, ax=ax, title=(title + f" acf plot"),
fft=True) # Use fft=True for potentially faster computation
plt.xlabel('Lag')
plt.ylabel('Autocorrelation')
plt.grid(True, linestyle='--', alpha=0.7)
plt.show()
def plot_autocorrelation_with_interpretation(self, data_series: pd.Series, title: str = "Autocorrelation Function",
lags: int = None, alpha: float = 0.05, interpret_results: bool = True):
"""
Plots the Autocorrelation Function (ACF) for a given time series with optional interpretation.
The confidence intervals represent the bounds within which autocorrelations would fall
if the series were pure white noise (random). Values outside these bounds suggest
genuine patterns or structure in the data.
Args:
data_series (pd.Series): The time series data to analyze
title (str): Title for the plot
lags (int, optional): Number of lags to plot (default: min(10*log10(len(series)), len(series)-1))
alpha (float): Significance level for confidence intervals (0.05 = 95% confidence)
interpret_results (bool): Whether to print interpretation of results
"""
if data_series.isnull().any():
print("Warning: Input data_series contains NaNs. Dropping NaNs for analysis.")
data_series = data_series.dropna()
if data_series.empty:
print("Error: data_series became empty after dropping NaNs. Cannot plot ACF.")
return
# Calculate ACF values for interpretation
n_obs = len(data_series)
max_lags = min(lags if lags else int(10 * np.log10(n_obs)), n_obs - 1)
# Get ACF values for numerical analysis
acf_values = acf(data_series, nlags=max_lags)
# Calculate confidence bounds (approximate for large samples)
confidence_bound = 1.96 / np.sqrt(n_obs) # For 95% confidence
# Create the plot
fig, ax = plt.subplots(figsize=(12, 6))
plot_acf(x=data_series, lags=max_lags, alpha=alpha, ax=ax,
title=f"{title} (n={n_obs}, {100 * (1 - alpha):.0f}% Confidence)", fft=True)
plt.xlabel('Lag')
plt.ylabel('Autocorrelation')
plt.grid(True, linestyle='--', alpha=0.7)
plt.show()
significant_lags = []
if interpret_results:
# Provide interpretation
print(f"\n--- Autocorrelation Analysis for '{data_series.name}' ---")
print(f"Sample size: {n_obs} observations")
print(f"Confidence level: {100 * (1 - alpha):.0f}%")
print(f"Confidence bounds: ±{confidence_bound:.4f}")
# Count significant autocorrelations (excluding lag 0 which is always 1)
for i in range(1, len(acf_values)):
if abs(acf_values[i]) > confidence_bound:
significant_lags.append((i, acf_values[i]))
if significant_lags:
print(f"\nSignificant autocorrelations found at {len(significant_lags)} lags:")
for lag, value in significant_lags[:5]: # Show first 5
print(f" Lag {lag}: {value:.4f}")
if len(significant_lags) > 5:
print(f" ... and {len(significant_lags) - 5} more")
# Provide context based on data type
if True:
print("\nInterpretation:")
print("- Significant autocorrelations suggest the series has memory")
print("- Past values provide information about future values")
print("- Consider this when building forecasting models")
else:
print(f"\nNo significant autocorrelations detected.")
print("The series appears consistent with white noise (random process).")
print("This suggests past values don't help predict future values.")
# Warning about multiple testing
expected_false_positives = max_lags * alpha
if expected_false_positives >= 1:
print(f"\nNote: With {max_lags} lags tested at {alpha:.0%} significance,")
print(f"expect ~{expected_false_positives:.1f} false positives due to multiple testing.")
print("---" + "-" * 50)
return {
'acf_values': acf_values,
'confidence_bound': confidence_bound,
'significant_lags': significant_lags if interpret_results else None,
'n_observations': n_obs
}
class HurstAnalyzer: # Wrapped in a class for context
def calculate_rolling_hurst_exponent(self, series: pd.Series, window_size: int = 252,
min_periods: int = None, method: str = 'rs',
plot_results: bool = True, interpret_results: bool = True):
"""
Calculates the rolling Hurst exponent to identify time-varying mean reversion and persistence patterns.
The Hurst exponent helps identify different market regimes:
- H < 0.5: Mean-reverting behavior (anti-persistent)
- H = 0.5: Random walk behavior (no memory)
- H > 0.5: Trending/persistent behavior (momentum)
This method reveals how these characteristics change over time, which is crucial for
understanding when mean reversion strategies might be most effective.
Args:
series (pd.Series): Time series data (typically log returns)
window_size (int): Rolling window size (default: 252 for ~1 year of daily data)
min_periods (int): Minimum observations required (default: window_size // 2)
method (str): Method for Hurst calculation ('rs' for R/S analysis, 'dfa' for detrended fluctuation)
plot_results (bool): Whether to create visualization
interpret_results (bool): Whether to provide detailed interpretation
Returns:
pd.Series: Rolling Hurst exponent values with same index as input series
"""
# Step 1: data cleaning:
if not isinstance(series, pd.Series):
print("Error: Input must be a pandas Series.")
return pd.Series()
# Clean the data
series_clean = series.dropna()
if len(series_clean) < window_size:
raise ValueError(f"Error: Series length ({len(series_clean)}) is less than window size ({window_size})")
# Set minimum periods if not specified
if min_periods is None:
min_periods = max(50, window_size // 2) # Ensure sufficient data for stable estimation
print(f"Calculating rolling Hurst exponent for '{series_clean.name}'")
print(f"Window size: {window_size}, Method: {method.upper()}")
print(f"This may take a moment for large datasets...")
# RS method for hurst exponent calculation:
def hurst_rs_method(data):
"""
Calculate Hurst exponent using Rescaled Range (R/S) analysis.
This is the classic method that examines how the range of cumulative deviations
scales with different time horizons.
"""
try:
# Remove any remaining NaNs within the window
clean_data = np.array(data.dropna())
if len(clean_data) < 10: # Need minimum data for meaningful calculation
return np.nan
N = len(clean_data)
if N < 2:
return np.nan
# Calculate mean
mean_val = np.mean(clean_data)
# Calculate cumulative deviations from mean
cumulative_deviations = np.cumsum(clean_data - mean_val)
# Calculate range of cumulative deviations
R = np.max(cumulative_deviations) - np.min(cumulative_deviations)
# Calculate standard deviation
S = np.std(clean_data, ddof=1)
# Avoid division by zero
if S == 0 or R == 0:
return np.nan
# For a single window, we approximate Hurst by comparing R/S to expected value
# This is a simplified approach; full R/S analysis uses multiple time scales
rs_ratio = float(R) / float(S)
# Theoretical expectation for random walk: R/S ≈ sqrt(π*N/2)
expected_rs = np.sqrt(np.pi * N / 2)
# Hurst exponent approximation
# If actual R/S > expected, H > 0.5 (persistent)
# If actual R/S < expected, H < 0.5 (anti-persistent)
hurst_approx = 0.5 + 0.5 * np.log(rs_ratio / expected_rs) / np.log(2)
# Bound the result to reasonable range (remove any values that doesn't make sense
return np.clip(hurst_approx, 0.0, 1.0)
except Exception as e:
return np.nan
# DFA method for hurst exponent calculation:
def hurst_dfa_method(data):
"""
Calculate Hurst exponent using Detrended Fluctuation Analysis (DFA).
This method is often more robust and handles non-stationarities better.
"""
try:
clean_data = np.array(data.dropna())
if len(clean_data) < 20: # DFA needs more data points
return np.nan
N = len(clean_data)
# Step 1: Create integrated series (cumulative sum)
integrated_series = np.cumsum(clean_data - np.mean(clean_data))
# Step 2: Divide into boxes of different sizes
# Use a smaller range of box sizes for rolling window
max_box_size = min(N // 4, 50) # Limit for computational efficiency
min_box_size = max(4, N // 20)
if max_box_size <= min_box_size:
return np.nan
box_sizes = np.logspace(np.log10(min_box_size), np.log10(max_box_size),
num=min(10, max_box_size - min_box_size + 1), dtype=int)