BLOG

Behind the numbers.

Code examples, market analysis, and data quality deep-dives.

Is AI Capex Paying Back Fast Enough? Revenue Hurdle Forecasting in Python
Could Shorter AI Asset Lives Hit Earnings? Depreciation Stress Test in Python
How Much AI Capex Risk Can a Portfolio Remove? Constrained Optimization in Python
Is the AI Capex Trade Crowded? Rolling Volatility and Sector Rotation in Python
Did the AI Boom Come From Existing S&P 500 Members? Point-in-Time Momentum Test in Python
Is AI Revenue Circular? Customer-Vendor Capex Loop Analysis in Python
Is the AI Trade Connected to Private Credit? Rolling Correlation Network in Python
Is Apollo More Balance-Sheet Sensitive Than Peers? Leverage Screen in Python
Are AI Earnings Supported by Cash Flow? Accrual and Capex Screen in Python
Can Defensive Stocks Hedge AI Drawdowns? Basket Regime Test in Python
How Fast Does the Market Price In Fed Decisions? FOMC Event Study in Python
How Much Are Options Sellers Overpaid? The Variance Risk Premium in Python
Which Companies Have the Worst Earnings Quality? Sloan Accrual Screen with Geographic Revenue Data in Python
Does the Oil-to-Gold Ratio Signal Recessions? XLE/GLD Backtest in Python
Is AI Spending Crowding Out Free Cash Flow? Capex Sustainability Across the Mag 7 in Python
Does a Long Energy / Short Bonds Portfolio Capture Inflation Surprises? Factor Construction in Python
Can a Hidden Markov Model Detect Oil Market Regimes? HMM Analysis in Python
Do Grain Prices Predict Food Inflation? Granger Causality Test in Python
Does the Corporate Credit Spread Predict Stock Market Crashes? BAA-AAA Spread Analysis in Python
Do Oil Stocks Hedge Inflation? Rolling Beta Analysis in Python
Which Stocks Are Most Rate-Sensitive? Equity Duration via Bond Beta in Python
Which Companies Have the Highest Accrual Ratios? Earnings Quality Screening in Python
Is Alpha Persistent or Decaying? Rolling Sharpe Ratio Analysis in Python
Are Markets Trending or Mean-Reverting? Hurst Exponent Analysis in Python
Is Consumer Discretionary vs Staples a Leading Indicator? XLY/XLP Ratio Analysis in Python
Does Heavy Capex Predict Future Stock Returns? Capital Expenditure Analysis in Python
How to Estimate Cost of Equity Using CAPM in Python
Is Volatility Predictable? Testing for Volatility Clustering in Python
Which Industrials Are Overleveraged? Net Debt to EBITDA Screening in Python
GM Before and After Bankruptcy: Why Entity Resolution Matters for Financial Data
What Is Adjusted Beta? Merrill Lynch Beta Shrinkage in Python
How Good Is a Stock Pick? Information Ratio and Tracking Error in Python
Do Stock Returns Follow a Normal Distribution? Testing for Fat Tails in Python
Which Large Caps Have the Highest Free Cash Flow Yield? FCF Screening in Python
Which Sectors Won Over 5 Years? Sector Rotation Analysis in Python
How to Forecast Stock Volatility with GARCH Models in Python
Are Stock Prices Mean-Reverting? Augmented Dickey-Fuller Test in Python
How to Calculate CAPM Alpha and Beta with Regression in Python
How to Compare Sector Sharpe Ratios and Sortino Ratios in Python
DELL: Why Stitching Historical Price Data Together Is Wrong
How to Analyze Drawdown and Recovery for Bank Stocks in Python
How to Screen SaaS Stocks by Revenue Growth and Cash Flow in Python
How to Screen REITs by Dividend Yield and Valuation in Python
How Correlated Are the Magnificent 7? Intra-Group Correlation in Python
AAPL vs XOM: Do Individual Stocks Have Seasonal Patterns?
How to Rank Large-Cap Stocks by Momentum in Python
How to Build a Multi-Endpoint Financial Dashboard in Python
How to Compare Volatility Across Energy Stocks in Python
How to Screen Healthcare Stocks by Valuation in Python
How to Build a Sector Correlation Matrix for Portfolio Diversification in Python
How to Find Oversold and Overbought Stocks Using Z-Scores in Python
How to Measure Earnings Quality: Cash Flow vs Net Income in Python
How to Build a Multi-Factor Stock Screen in Python (Value + Momentum + Quality)
How to Build a Simple DCF Model for Any Stock in Python
How to Screen Tech Stocks by Revenue Growth in Python
How to Screen Stocks by Balance Sheet Health in Python
Is "Sell in May" Real? SPY Monthly Seasonality Over 10 Years
How to Compare Sector Performance YTD Using Python
How to Track S&P 500 Additions and Removals Over Time in Python
How to Screen Dividend Stocks by Yield and Quality in Python
How to Calculate Max Drawdown and Recovery Time for Any Stock in Python
How to Compare Profitability Across Mega-Cap Tech Stocks in Python
Why Ticker Symbols Are Unreliable: The Recycling Problem Every Quant Should Know
How to Calculate and Compare Stock Volatility in Python
How to Screen Blue-Chip Stocks by P/E Ratio in Python
How to Track Companies Through Ticker Changes, Bankruptcies, and Renames in Python
S&P 500 Turnover: How Much the Index Has Changed Since 2010
How to Calculate Stock Beta and Correlation in Python
← All articles

Are Markets Trending or Mean-Reverting? Hurst Exponent Analysis in Python

What’s the question?

Financial markets are frequently described as either trending (prices move in sustained directions) or mean-reverting (prices oscillate around a stable average). These two regimes imply opposite trading strategies: trending markets reward momentum, while mean-reverting markets reward buying dips and selling rallies. The Hurst exponent (H) is a statistical measure of long-range dependence in a time series, originally developed by hydrologist Harold Hurst in the 1950s to study Nile River flood patterns. When H equals 0.5, the series behaves as a random walk — past price movements provide no information about future movements. When H is greater than 0.5, the series exhibits persistence (trending behavior), meaning that an upward move is more likely to be followed by another upward move. When H is less than 0.5, the series is anti-persistent (mean-reverting), meaning upward moves tend to be followed by downward moves. The question is whether major US equities and ETFs exhibit measurable deviation from the random walk hypothesis at daily frequency.

The approach

Compute the Hurst exponent for 6 tickers using the rescaled range (R/S) method. For each ticker, pull 5 years of daily prices and compute log returns. The R/S method works by dividing the return series into sub-periods of varying lengths, computing the range of cumulative deviations from the mean divided by the standard deviation for each sub-period, and regressing the log of the average R/S statistic against the log of the sub-period length. The slope of this regression is the Hurst exponent. Sub-period lengths range from 20 to 500 days to capture dependence at multiple timescales.

import xfinlink as xfl
import pandas as pd
import numpy as np

xfl.api_key = "YOUR_API_KEY"  # free at https://xfinlink.com/signup

# -- Configuration ----------------------------------------------------------
tickers = ["AAPL", "MSFT", "NVDA", "XOM", "JNJ", "SPY"]

# -- Fetch 5Y daily prices -------------------------------------------------
df = xfl.prices(tickers, period="5y", fields=["close"])


def hurst_rs(prices: pd.Series, min_window: int = 20, max_window: int = 500) -> float:
    """Compute Hurst exponent using the rescaled range (R/S) method."""
    log_returns = np.log(prices / prices.shift(1)).dropna().values
    n = len(log_returns)

    # Generate window sizes (powers of 2, plus intermediate values)
    window_sizes = []
    size = min_window
    while size <= min(max_window, n // 2):
        window_sizes.append(size)
        size = int(size * 1.4)

    if len(window_sizes) < 3:
        return np.nan

    log_rs = []
    log_n = []

    for w in window_sizes:
        num_blocks = n // w
        if num_blocks < 1:
            continue

        rs_values = []
        for i in range(num_blocks):
            block = log_returns[i * w : (i + 1) * w]
            mean_block = block.mean()
            deviations = block - mean_block
            cumulative = np.cumsum(deviations)
            r = cumulative.max() - cumulative.min()
            s = block.std(ddof=1)
            if s > 0:
                rs_values.append(r / s)

        if rs_values:
            log_rs.append(np.log(np.mean(rs_values)))
            log_n.append(np.log(w))

    if len(log_rs) < 3:
        return np.nan

    # Linear regression: log(R/S) = H * log(n) + c
    coeffs = np.polyfit(log_n, log_rs, 1)
    return coeffs[0]


# -- Compute Hurst exponent per ticker -------------------------------------
results = []
for ticker in tickers:
    prices = df[df["ticker"] == ticker].sort_values("date")["close"]
    h = hurst_rs(prices)
    results.append({"ticker": ticker, "hurst": h})

results_df = pd.DataFrame(results).sort_values("hurst", ascending=False)

print("=== Hurst Exponent: Trending or Mean-Reverting? (5Y Daily) ===")
print(f"{'Ticker':8s} {'Hurst H':>8s}   Interpretation")
print("-" * 43)

for _, r in results_df.iterrows():
    h = r["hurst"]
    if h > 0.6:
        interp = "Moderate persistence (trending)"
    elif h > 0.5:
        interp = "Weak persistence (slightly trending)"
    elif h > 0.4:
        interp = "Weak anti-persistence (slightly mean-reverting)"
    else:
        interp = "Strong anti-persistence (mean-reverting)"
    print(f"{r['ticker']:8s} {h:>8.3f}   {interp}")

# -- Summary statistics -----------------------------------------------------
print("\n=== Statistical Context ===")
print(f"Mean H across tickers:   {results_df['hurst'].mean():.3f}")
print(f"Std dev of H:            {results_df['hurst'].std():.3f}")
print(
    f"All tickers fall in range [{results_df['hurst'].min():.3f}, "
    f"{results_df['hurst'].max():.3f}]"
)

print("""
H = 0.5  -> random walk (no predictability)
H > 0.5  -> persistent / trending
H < 0.5  -> anti-persistent / mean-reverting

=== Comparison with ADF Test ===
The ADF test (augmented Dickey-Fuller) tests whether price LEVELS are
stationary -- i.e., whether prices revert to a fixed mean. Hurst measures
whether RETURNS exhibit long-range dependence -- i.e., whether the direction
of returns persists across timescales. A stock can fail the ADF test
(non-stationary levels, as expected for prices) while showing H = 0.5
(no return persistence). These are complementary, not redundant, tests.""")

Output:

Hurst exponent bar chart showing all stocks slightly above 0.5 random walk threshold
=== Hurst Exponent: Trending or Mean-Reverting? (5Y Daily) ===
Ticker   Hurst H   Interpretation
-------------------------------------------
MSFT       0.597   Weak persistence (slightly trending)
AAPL       0.580   Weak persistence (slightly trending)
SPY        0.577   Weak persistence (slightly trending)
JNJ        0.575   Weak persistence (slightly trending)
XOM        0.537   Weak persistence (slightly trending)
NVDA       0.533   Weak persistence (slightly trending)

=== Statistical Context ===
Mean H across tickers:   0.567
Std dev of H:            0.025
All tickers fall in range [0.533, 0.597]

H = 0.5  -> random walk (no predictability)
H > 0.5  -> persistent / trending
H < 0.5  -> anti-persistent / mean-reverting

=== Comparison with ADF Test ===
The ADF test (augmented Dickey-Fuller) tests whether price LEVELS are
stationary -- i.e., whether prices revert to a fixed mean. Hurst measures
whether RETURNS exhibit long-range dependence -- i.e., whether the direction
of returns persists across timescales. A stock can fail the ADF test
(non-stationary levels, as expected for prices) while showing H = 0.5
(no return persistence). These are complementary, not redundant, tests.

What this tells us

All six tickers produce Hurst exponents between 0.533 and 0.597 — slightly above the 0.5 random walk threshold but not by a large margin. MSFT at 0.597 shows the strongest persistence, meaning its daily returns exhibit a mild tendency to continue in the same direction across multiple timescales. NVDA at 0.533 is the closest to a pure random walk despite being the highest-volatility stock in the group, which suggests that NVDA’s large daily moves do not predict the direction of subsequent moves. SPY at 0.577 is consistent with the well-documented finding that broad market indices exhibit slightly more persistence than individual stocks, likely because index-level momentum is driven by macroeconomic factors that unfold gradually. The standard deviation of H across all six tickers is only 0.025, indicating that the persistence structure is remarkably uniform across sectors and market capitalizations.

So what?

A Hurst exponent of 0.55-0.60 is theoretically interesting but practically marginal. The deviation from 0.5 is small enough that transaction costs, bid-ask spreads, and execution slippage would likely consume any edge from a persistence-based trading strategy at daily frequency. The more productive application is regime detection: computing rolling Hurst exponents over 1-year windows to identify periods when H spikes above 0.65 (strong trending, favoring momentum strategies) or drops below 0.40 (strong mean reversion, favoring contrarian strategies). At the current snapshot, none of these tickers exhibit strong enough persistence or anti-persistence to justify a directional strategy based on the Hurst exponent alone.

Built with xfinlink — free financial data API for Python. pip install xfinlink
← All articles