Introduction to the Hurst exponent — with code in Python | by Eryk Lewinson | Towards Data Science

In finance, some of the most commonly used trading strategies are based on momentum and mean-reversion. I will try to provide a quick explanation of the two.
In momentum-based strategies, the investors try to capitalize on the continuance of the existing market trend. For example, in the last period of interest, a certain company was doing well and the stock price was steadily rising over time. In such a case, the investors might make a bet that the price will continue to rise and thus enter a long position. Or vice versa for a short position. Naturally, the strategies are not that simple and the entry and exit decisions are most often made based on a series of technical indicators.
Mean-reversion assumes that properties such as stock returns and volatility will revert to their long-term average over time. Mathematically, such a time series is referred to as an Ornstein-Uhlenbeck process. In such strategies, investors try to make money by assuming that after some extreme events (either positive or negative), the stock price will revert to the long-term pattern.
We can quite easily identify those two patterns on the plot depicting the stock price evolution over time. However, when building automatic trading strategies it is not feasible to go over each and every stock manually and decide which pattern is visible on the plot. That is why we need to use some sort of an automated approach to determining which of the two patterns we can observe in the given stock price.
In this article, I will present how to use the Hurst exponent to identify if a given time series (not only financial time series such as stock price or returns) is trending, mean-reverting, or simply a random walk.
We can use the Hurst exponent (H) as a measure for long-term memory of a time series, that is, to measure the amount by which that series deviates from a random walk. The scalar represents the relative tendency of a time series either to regress strongly to the mean (mean-reverting pattern) or to cluster in a certain direction (trending pattern).
The values of the Hurst exponent range between 0 and 1. Based on the value of H, we can classify any time series into one of the three categories:
H < 0.5 — a mean-reverting (anti-persistent) series. The closer the value is to 0, the stronger the mean-reversion process is. In practice, it means that a high value is followed by a low value and vice-versa.
H = 0.5 — a geometric random walk.
H > 0.5 — a trending (persistent) series. The closer the value is to 1, the stronger the trend. In practice, it means that a high value is followed by a higher one.
There are at least a few methods of calculating the Hurst exponent. Unfortunately, they do not always agree on the conclusion (you can see an example of such a case here).
In this article, I will only show the simplest implementation, which is based on estimating the rate of the diffusive behavior based on the variance of log prices.
First, let’s define x as the logarithm of stock prices S.
The variance for an arbitrary lag (denoted by τ) can be expressed as:
If the stock prices follow a geometric Brownian motion (GBM, random walk), the variance would vary linearly with the lag τ:
However, the stock prices do not always follow a GBM. In cases of deviations from a random walk (so when some form of autocorrelation exists), the variance for a given lag is no longer proportional to the lag itself, but it gains an anomalous exponent. The new relationship looks as follows:
Where H again stands for the Hurst exponent.
Another possible approach to calculating the Hurst exponent is based on the rescaled range (R/S) analysis. To keep the article brief, I will not describe all the required steps here, but will refer you to a step-by-step instruction. It was shown in the literature that using the R/S statistic leads to superior results as compared to other methods such as the analysis of autocorrelations, variance ratios, etc. However, it also has a shortcoming of being very sensitive to short-range dependence.
As always, we need to import some libraries first. This time, however, we will not rely entirely on a library, but create a function for calculating the Hurst exponent ourselves.
Then, we prepare the data. As a real-life example, we will use 10 years’ worth of historical prices of S&P 500. We can easily download the prices using the yfinance library. If you are interested in more details on the library, please check out my other article.
We will also generate some artificial data to reflect all three cases which the Hurst exponent can identify: mean-reverting, random walk, and trending. We do so in the following snippet.
I would say the series clearly show the patterns we wanted them to show. Maybe the trending one is a bit of an overkill, but at the very least it should be easy to correctly classify.
The next step is to define a function used for calculating the Hurst exponent.
To calculate the Hurst exponent, we first calculate the standard deviation of the differences between a series and its lagged version, for a range of possible lags. We then estimate the Hurst exponent as the slope of the log-log plot of the number of lags versus the mentioned standard deviations.
The number of lags is somewhat arbitrary. The default value of 20 is based on Matlab’s implementation of the Hurst function. However, it is reported in the literature that if we set the number of lags too high, the results might be inaccurate. We will investigate that soon enough.
Let’s start with the S&P 500 data. In general, we can see on the plot that the series is trending — with some exceptions, the overall trend is positive. We calculate the Hurst exponent using a selection of max lag values to see how it impacts the value of the exponent.
With the default value of 20, the S&P 500 series seems to be lightly mean-reverting. As we increase the number of lags, the mean-reversion is only stronger (the value of the exponent decreases towards 0).
Hurst exponent with 20 lags: 0.4394 Hurst exponent with 100 lags: 0.3834 Hurst exponent with 300 lags: 0.3257 Hurst exponent with 500 lags: 0.3231 Hurst exponent with 1000 lags: 0.1795
Please bear in mind that those values are obtained for the 10-year series. If we narrow it down to a certain time period, the conclusions could be vastly different! This is also something to remember while using the Hurst exponent in practice.
Now it is time to repeat the exercise for the artificially generated series. Let’s see if the conclusions based on the Hurst exponent will match the expectations.
Hurst exponents with 20 lags ---- mean_rev: 0.0064 gbm: 0.4539 trending: 0.8715 Hurst exponents with 100 lags ---- mean_rev: -0.0021 gbm: 0.5401 trending: 0.8442 Hurst exponents with 300 lags ---- mean_rev: 0.0002 gbm: 0.5691 trending: 0.7463 Hurst exponents with 500 lags ---- mean_rev: 0.0015 gbm: 0.4854 trending: 0.6662
The conclusions for the mean-reverting series are most consistent over all the values of the lags. We should just note that the negative value for the maximum lag of 100 is probably due to some approximations and by design, it should stop at 0.
For the random walk series, the value of the exponent oscillates around the intended 0.5.
Lastly, the trending series is correctly identified by the Hurst exponent for all the selected lags, however, as we increase the lag the value of the exponent decreases toward 0.5, which would indicate a random walk.
We can conclude that overall the results are close enough to the initial expectations, however, the maximum value of the lags we consider can impact our conclusions. This analysis shows that a given series can be neither purely mean-reverting nor trending. The state of the regime depends on whether we are looking at shorter or longer-term (by looking at a different range of lags). That is why it’s best to get some feeling for working with the Hurst exponent before actually trying to create trading strategies based on it.
Lastly, as mentioned before, there are quite a few ways to calculate the Hurst exponent. For an implementation of the Hurst exponent based on the rescaled range (R/S) analysis, you can check out the hurst library (repository). I have not used it in this article, as the library is not in active development (the last update was 2 years ago). However, you can definitely give it a go.
the Hurst exponent is a measure of the memory in a time series and is used to classify the series as mean-reverting, trending, or a random walk.
depending on the choice of the maximum lag parameter (i.e., whether we are looking are short or long term), the results can differ significantly.
there are a few ways to calculate the Hurst exponent, the two most popular ones are based on anomalous diffusion and rescaled range analysis.
You can find the code used for this article on my GitHub. Also, any constructive feedback is welcome. You can reach out to me on Twitter or in the comments.
Disclaimer: Affiliate link ahead
I have recently encountered the Hurst exponent while going through a MOOC on momentum-based trading strategies. Quantra is a platform that offers interactive courses with a focus on Python for Quantitative Finance. I wrote a short article showcasing some of the platform’s features and describing my personal experience.
If you liked this article, you might also be interested in one of the following:
towardsdatascience.com
towardsdatascience.com
Kroha, P., & Skoula, M. (2018, March). Hurst Exponent and Trading Signals Derived from Market Time Series. In ICEIS (1) (pp. 371–378).
Hurst, H. E. (1951). Long term storage capacity of reservoirs. ASCE Transactions, 116(776), 770–808.