Random Walk
A random walk is a series whose next step is unpredictable from its past: each value is the previous value plus an independent random shock. Applied to prices, the random walk hypothesis says that tomorrow’s price change cannot be forecast from today’s and earlier prices, so charts of past prices hold no information about the direction of the next move. Bachelier (1900) first modelled prices this way in his thesis on the Paris bond market, and Fama (1965) brought the idea to the stock market.
The formula for a random walk is as follows:
\[P_{t} = \mu + P_{t-1} + \epsilon_{t}\]Where \(P_{t}\) is the price, or more often its logarithm, mu a drift and \(\epsilon_{t}\) an independent shock with mean zero. A shock moves the level for good, so the series has a unit root and is not stationary, and its variance grows in proportion to time.
That proportionality gives a test. If prices follow a random walk, the variance of a return over q periods is q times the variance of a one-period return. Lo and MacKinlay (1988) used this to test weekly US stock returns and rejected the random walk, as the returns of portfolios of small stocks were positively correlated. A variance ratio above 1 points to trending, persistent returns, one below 1 to mean reversion.
The Finance Toolkit tests the random walk with the variance ratio test, with the runs test, which checks whether up and down moves follow each other at random, and with the autocorrelation and the Ljung-Box test of the returns. The unit root tests ask the related question of whether a price level has a unit root, and the Hurst exponent is 0.5 for a random walk.
Example · SPY, EURUSD, BTCUSD · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then:
from financetoolkit import Toolkit
toolkit = Toolkit(
["SPY", "EURUSD", "BTCUSD"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
benchmark_ticker=None,
)
toolkit.econometrics.get_variance_ratio_test(q=5)
Which returns:
| Item | SPY | EURUSD | BTCUSD |
|---|---|---|---|
| Variance Ratio | 0.8806 | 0.9381 | 0.9659 |
| Variance Ratio Statistic | -1.9304 | -1.0332 | -0.6647 |
| P-Value | 0.0536 | 0.3015 | 0.5062 |
| Robust Variance Ratio Statistic | -1.1755 | -0.8609 | -0.5431 |
| Robust P-Value | 0.2398 | 0.3893 | 0.5871 |
References
- Paper Bachelier, L. (1900). “Théorie de la spéculation.” Annales scientifiques de l’École normale supérieure, 17, 21-86.
- Paper Fama, E.F. (1965). “The Behavior of Stock-Market Prices.” The Journal of Business, 38(1), 34-105.
- Paper Lo, A.W. & MacKinlay, A.C. (1988). “Stock Market Prices Do Not Follow Random Walks: Evidence from a Simple Specification Test.” The Review of Financial Studies, 1(1), 41-66.