Runs Test
Calculate the Wald-Wolfowitz runs test of every asset’s returns: whether returns above and below their mean (or median, or zero) follow each other at random.
A run is a stretch of consecutive returns on the same side of the cutoff. Fewer runs than expected mean that returns stay on one side for a while (momentum, trends), more runs that they alternate (mean reversion). Unlike the Ljung-Box test it only looks at the side of each return, not its size, so a few large returns do not drive it.
The formula is as follows:
\[n = n_{1} + n_{2}\] \[\mathbb{E}\left[R\right] = \frac{2 \cdot n_{1} \cdot n_{2}}{n} + 1\] \[\operatorname{var}\left(R\right) = \frac{2 \cdot n_{1} \cdot n_{2} \cdot (2 \cdot n_{1} \cdot n_{2} - n)}{n^{2} \cdot (n - 1)}\] \[Z = \frac{R - \mathbb{E}\left[R\right]}{\sqrt{\operatorname{var}\left(R\right)}}\] \[\text{P Value} = 2 \cdot \left(1 - \Phi\left(\left\lvert Z\right\rvert\right)\right)\]with \(n_{1}\) and \(n_{2}\) the returns of period above and below the cutoff (returns equal to it are left out) and R the number of runs. Z is standard normal, with Phi its distribution function.
Also known as: Wald-Wolfowitz test, runs test for randomness, sign runs test.
Example · ^GSPC, BTCUSD, TLT · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_runs_test:
from financetoolkit import Toolkit
toolkit = Toolkit(
["^GSPC", "BTCUSD", "TLT"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.econometrics.get_runs_test(cutoff="zero")
Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:
Did the daily returns of the S&P 500, Bitcoin and the iShares 20+ Year Treasury Bond ETF switch between gains and losses more or less often than chance from 2021 to 2025, by a runs test?
Which returns:
| Item | ^GSPC | BTCUSD | TLT |
|---|---|---|---|
| Runs | 632.0000 | 974.0000 | 647.000 |
| Expected Runs | 624.3250 | 913.4870 | 626.324 |
| Z-Statistic | 0.4362 | 2.8338 | 1.170 |
| P-Value | 0.6627 | 0.0046 | 0.242 |
| Observations Above | 675.0000 | 909.0000 | 615.000 |
| Observations Below | 579.0000 | 916.0000 | 636.000 |
6 rows
Using Your Own Data
get_runs_test runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:
from financetoolkit import Economics, Econometrics
economics = Economics(start_date="2000-01-01")
unemployment = economics.get_unemployment_rate(
countries=["United States", "Germany"], period="monthly"
)
econometrics = Econometrics(data=unemployment, changes="difference")
econometrics.get_runs_test()
Parameters
get_runs_test accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency for returns (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
within_period | bool | Whether to calculate the test within the specified period or for the entire period. Thus whether to look at the test within a specific year (if period = ‘yearly’) or look at the entirety of all years. Defaults to False. Note that this requires intraday data when period = ‘daily’, since a single day only nests observations when intraday data was fetched. | True |
cutoff | str | The value that splits the returns. Options are: “mean” (the mean return), “median” (the median return), “zero” (up and down moves). Defaults to “mean”. | 0.01 |
include_benchmark | bool | Whether to include “Benchmark” among the assets tested. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |
show_columns | str | list[str] | None | The columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column. | None, which keeps every column |
References
- Paper Wald, A. and Wolfowitz, J. (1940). “On a Test Whether Two Samples are from the Same Population.” The Annals of Mathematical Statistics, 11(2), 147-162.
- Library Referenced against statsmodels’ runstest_1samp