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:

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.

ParameterTypeDescriptionExample
periodstrThe data frequency for returns (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
within_periodboolWhether 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
cutoffstrThe 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_benchmarkboolWhether to include “Benchmark” among the assets tested. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None.2
show_columnsstr | list[str] | NoneThe 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
Share