Time Series: Stationarity and Autocorrelation
Asset prices are not independent over time; today's return is related to yesterday's. Understanding stationarity and autocorrelation is essential before doing any time series analysis.
What Makes a Time Series Different
Most statistics courses deal with data where each observation is independent: the height of one person does not affect the height of the next person in the dataset. Time series data breaks this assumption. A stock return today is not independent of yesterday’s return. Prices build on previous prices. Economies cycle. Seasons repeat.
A time series is simply data collected in chronological order, where the sequence itself carries information.
When you work with time series, the questions change. Instead of asking “what is the average return?” you ask “is the average return stable over time? Does this year’s return tell you anything about next year’s?” These questions lead directly to the concepts of stationarity and autocorrelation.
Stationarity: When Statistics Stay the Same
A time series is (weakly) stationary when three conditions hold:
- The mean of the series is constant over time. It does not drift up or down.
- The variance of the series is constant over time. Fluctuations do not grow or shrink systematically.
- The autocovariance between two observations depends only on the gap between them (the lag), not on where you are in time.
Formally, for a weakly stationary series :
The value depends only on the lag , not on .
If you take any stationary segment of the series and plot it, the shape looks similar regardless of when you sampled. The level, spread, and pattern are roughly the same.
Many statistical tests and forecasting methods (including most of the classical tools in econometrics) assume your data is stationary. If you apply them to non-stationary data, you get misleading results, like finding relationships that do not really exist or missing relationships that do.
Why Stationarity Matters in Finance
Asset prices are almost never stationary. A stock price that starts at $50 and trends upward to $200 over ten years has a changing mean. A series with a unit root (the financial default) does not revert to a constant level. Log prices behave similarly.
Returns, on the other hand, often appear closer to stationary. A return of +2% in one period does not systematically predict that the next return will be positive or negative (at least in efficient markets). This is why quant analysts work with returns instead of prices.
First-differencing is the simplest way to transform a non-stationary price series into a stationary one. If is the price, then is just a dollar price change, not a return. The simple return and log return are defined as:
The log return is additive across time, which makes it algebraically convenient. Most financial time series analysis uses log returns.
Autocorrelation: Measuring Memory in Data
If stationarity asks “is the level stable?”, autocorrelation asks “do values at different points in time relate to each other?”
The autocorrelation function (ACF) measures the correlation between and , where is the lag:
At lag 1, you compare each value with the one immediately before it. At lag 5, you compare each value with the one five periods back.
A series with positive autocorrelation (like temperature readings through a day) tends to persist: high values follow high values, low follow low. In finance, a momentum effect creates positive autocorrelation in returns: assets that have been rising continue to rise, at least for a while.
A series with negative autocorrelation (like a mean-reverting spread) tends to oscillate: high values follow low values and vice versa.
Visualizing Autocorrelation: The Correlogram
A correlogram is a bar chart that displays ACF values at each lag. Each bar’s height represents , and bars that extend beyond the dashed significance lines suggest meaningful autocorrelation.
A correlogram lets you read off at a glance how many periods of “memory” a series has.
The Ljung-Box test provides a formal statistical test for autocorrelation. Its null hypothesis is that all autocorrelation coefficients up to lag are zero; in other words, that the series looks like white noise. A small p-value rejects the null and tells you the series has significant autocorrelation.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.stats.diagnostic import acorr_ljungbox
np.random.seed(42)
n = 252
# Pure white noise returns
white_noise = np.random.normal(0, 1, n)
# Returns with momentum: positive autocorrelation
returns_momentum = np.zeros(n)
returns_momentum[0] = np.random.normal(0, 1)
for t in range(1, n):
returns_momentum[t] = 0.4 * returns_momentum[t-1] + np.random.normal(0, 1)
# Plot correlograms
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes[0, 0].plot(white_noise)
axes[0, 0].set_title("White Noise Returns")
axes[0, 0].set_xlabel("Day")
axes[0, 1].plot(returns_momentum)
axes[0, 1].set_title("Momentum Returns (Autocorrelated)")
axes[0, 1].set_xlabel("Day")
plot_acf(white_noise, lags=20, ax=axes[1, 0], title="ACF: White Noise")
plot_acf(returns_momentum, lags=20, ax=axes[1, 1], title="ACF: Momentum")
plt.tight_layout()
plt.show()
# Ljung-Box test on both series
print("White noise Ljung-Box p-values:")
print(acorr_ljungbox(white_noise, lags=[5, 10], return_df=True))
print("\nMomentum returns Ljung-Box p-values:")
print(acorr_ljungbox(returns_momentum, lags=[5, 10], return_df=True))
PROMPT
Use numpy, matplotlib, and statsmodels to simulate white-noise and AR(1) return series, plot the series and their ACFs, and run the Ljung-Box test for autocorrelation.
Running this code, you will see the white noise series produces a flat correlogram with no significant bars, while the momentum series shows slowly decaying ACF values, a hallmark of persistent autocorrelation.
Applications in Quant Finance
Momentum strategies deliberately exploit positive autocorrelation in returns. If recent winners tend to keep winning, buying recent up-movers can be profitable, at least until the pattern self-destructs from overuse.
Mean reversion strategies do the opposite, exploiting negative autocorrelation. If short-term deviations from a fair price tend to correct, you can sell assets that have risen too far and buy assets that have fallen too far.
Both approaches require measuring autocorrelation rigorously rather than assuming it exists. The tools in this article (ACF, correlograms, Ljung-Box) are the starting point for doing that properly.
References
Brockwell & Davis, Introduction to Time Series and Forecasting, 3rd edition, Springer, Chapters 1–2. ISBN: 978-3319298528.
Tsay, Analysis of Financial Time Series, 3rd edition, Wiley, Chapter 1. ISBN: 978-0470414354.