├── README.md ├── LICENSE └── Hurst.py /README.md: -------------------------------------------------------------------------------- 1 | # Hurst Exponent 2 | Calculates the Hurst exponent of a time series based on Rescaled range (R/S) analysis. 3 | Reference: https://en.wikipedia.org/wiki/Hurst_exponent 4 | # Environment 5 | Python 3.6.2 AMD64 6 | numpy (1.13.3+mkl) 7 | pandas (0.20.3) 8 | # User Guide 9 | import Hurst 10 | ts = list(range(50)) 11 | hurst = Hurst.hurst(ts) 12 | # Tips 13 | The input ts has to be object list(n_samples,) or np.array(n_samples,). 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 RyanWangZf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Hurst.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Reference: https://en.wikipedia.org/wiki/Hurst_exponent 3 | # python 3.6.2 AMD64 4 | # 2018/4/19 5 | # Calculate Hurst exponent based on Rescaled range (R/S) analysis 6 | # How to use (example): 7 | # import Hurst 8 | # ts = list(range(50)) 9 | # hurst = Hurst.hurst(ts) 10 | # Tip: ts has to be object list(n_samples,) or np.array(n_samples,) 11 | 12 | __Author__ = "Ryan Wang" 13 | 14 | import numpy as np 15 | import pandas as pd 16 | 17 | def hurst(ts): 18 | ts = list(ts) 19 | N = len(ts) 20 | if N < 20: 21 | raise ValueError("Time series is too short! input series ought to have at least 20 samples!") 22 | 23 | max_k = int(np.floor(N/2)) 24 | R_S_dict = [] 25 | for k in range(10,max_k+1): 26 | R,S = 0,0 27 | # split ts into subsets 28 | subset_list = [ts[i:i+k] for i in range(0,N,k)] 29 | if np.mod(N,k)>0: 30 | subset_list.pop() 31 | #tail = subset_list.pop() 32 | #subset_list[-1].extend(tail) 33 | # calc mean of every subset 34 | mean_list=[np.mean(x) for x in subset_list] 35 | for i in range(len(subset_list)): 36 | cumsum_list = pd.Series(subset_list[i]-mean_list[i]).cumsum() 37 | R += max(cumsum_list)-min(cumsum_list) 38 | S += np.std(subset_list[i]) 39 | R_S_dict.append({"R":R/len(subset_list),"S":S/len(subset_list),"n":k}) 40 | 41 | log_R_S = [] 42 | log_n = [] 43 | print(R_S_dict) 44 | for i in range(len(R_S_dict)): 45 | R_S = (R_S_dict[i]["R"]+np.spacing(1)) / (R_S_dict[i]["S"]+np.spacing(1)) 46 | log_R_S.append(np.log(R_S)) 47 | log_n.append(np.log(R_S_dict[i]["n"])) 48 | 49 | Hurst_exponent = np.polyfit(log_n,log_R_S,1)[0] 50 | return Hurst_exponent 51 | 52 | 53 | 54 | --------------------------------------------------------------------------------