Data Snooping Test
Calculate a data-snooping test of the tickers against the benchmark: whether the best of them beats the benchmark once the search for the best is taken into account, with White’s Reality Check, Hansen’s test for Superior Predictive Ability (SPA) or the stepwise test (StepM) of Romano and Wolf.
Picking the best of many strategies, funds or trading rules and testing it on its own overstates its significance: among enough of them without any edge, one beats the benchmark by chance. These tests treat the tickers as the strategies searched and judge the best one against the bootstrapped distribution of the best of all of them. The null hypothesis is that no ticker beats the benchmark on average, so a small p-value says that the best one still does after the search. The StepM test names every ticker that beats the benchmark, holding the probability of naming one that does not at alpha.
The formula is as follows (with the default SPA test):
\[d_{k,t} = \text{Return}_{k,t} - \text{Benchmark Return}_{t}\] \[\bar{d}_{k} = \frac{\sum_{t=1}^{n} d_{k,t}}{n}\] \[T = \max\left(\max\left(\frac{\sqrt{n} \cdot \bar{d}_{k}}{\hat{\omega}_{k}}\right), 0\right)\] \[T_{b} = \max\left(\max\left(\frac{\sqrt{n} \cdot (\bar{d}_{k,b} - \hat{\mu}_{k})}{\hat{\omega}_{k}}\right), 0\right)\] \[I_{b} = 1 \quad \text{if } T_{b} > T\] \[I_{b} = 0 \quad \text{if } T_{b} \leq T\] \[\text{P Value} = \frac{\sum_{b=1}^{B} I_{b}}{B}\] \[\hat{\mu}_{k} = \max\left(\bar{d}_{k}, 0\right)\] \[\hat{\mu}_{k} = \bar{d}_{k} \quad \text{if } \bar{d}_{k} \geq -\sqrt{\frac{\hat{\omega}_{k}^{2}}{n} \cdot 2 \cdot \ln\left(\ln\left(n\right)\right)}\] \[\hat{\mu}_{k} = 0 \quad \text{if } \bar{d}_{k} < -\sqrt{\frac{\hat{\omega}_{k}^{2}}{n} \cdot 2 \cdot \ln\left(\ln\left(n\right)\right)}\] \[\hat{\mu}_{k} = \bar{d}_{k}\]with hat(omega_k)^2 the stationary bootstrap variance of sqrt(n) * bar(d_k), bar(d_{k,b}) the average of the b-th bootstrap resample and the inner maximum over the tickers k. hat(mu_k) is \(\max (\text{bar} (d_{k}) , 0)\) for the Lower test, the two piecewise lines for the Consistent test and bar(d_k) for the Upper test.
The formula for the Reality Check is as follows:
\[V = \max\left(\sqrt{n} \cdot \bar{d}_{k}\right)\] \[V_{b} = \max\left(\sqrt{n} \cdot (\bar{d}_{k,b} - \bar{d}_{k})\right)\] \[\text{P Value} = \frac{\sum_{b=1}^{B} I_{b}}{B}\]with \(I_{b} = 1\) when \(V_{b} > V\) and 0 otherwise.
The formula for the StepM test is as follows:
\[c = Q_{1-\alpha}\left(\max\left(\frac{\sqrt{n} \cdot (\bar{d}_{k,b} - \bar{d}_{k})}{\hat{\omega}_{k}}\right)\right)\]with \(Q_{1 - \alpha}\) the 1 - alpha quantile over the B resamples and the maximum over the tickers not yet found. Every ticker not yet found with sqrt(n) * bar(d_k) / hat(omega_k) above c outperforms the benchmark; the step is repeated until it finds no further ticker.
The B resamples of the periods are drawn with the stationary bootstrap of Politis and Romano (1994), blocks of a random length with an average of block_size periods, which keeps the dependence of the returns over time. \(\hat{\omega}_{k}^{2} = \gamma_{0} + 2 \sum_{i} \kappa (n , i) \cdot \gamma_{i}\) weighs the autocovariances \(\gamma_{i}\) of \(d_{k}\) as that bootstrap does (Hansen 2005), with \(\kappa (n , i)\) = (1 - i / n)·(1 - q)^i + (i / n)·(1 - q)^(n - i) and \(q = 1\) / block_size (see forecast_evaluation_model.get_stationary_bootstrap_variance).
The Reality Check recenters every ticker at its average, as if each were as good as the benchmark, so one far worse than the benchmark still widens the distribution of the best one and makes the test conservative. The SPA test divides every average by its standard deviation, so a volatile ticker does not dominate the maximum, and leaves out the tickers that are clearly worse: the consistent p-value is the one to use, and the lower and upper p-values bound it.
Also known as: data snooping, Reality Check, White’s Reality Check, Superior Predictive Ability, SPA test, Hansen’s SPA, StepM, Romano-Wolf, multiple testing of strategies, best strategy test.
Example · XLK, XLF, XLE, XLV, 5 more · 2016 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_data_snooping_test:
from financetoolkit import Toolkit
toolkit = Toolkit(
["XLK", "XLF", "XLE", "XLV", "XLY", "XLP", "XLI", "XLU", "XLB"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2016-01-01",
end_date="2025-12-31",
)
toolkit.econometrics.get_data_snooping_test()
Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:
Did the best of the nine Select Sector SPDR ETFs (technology, financials, energy, health care, consumer discretionary, consumer staples, industrials, utilities and materials) beat the S&P 500 ETF from 2016 to 2025 once the search for the best is taken into account, by Hansen's SPA test?
Which returns:
| Item | SPA |
|---|---|
| Best Strategy | XLK |
| Average Excess Return | 0.0003 |
| Statistic | 3.0024 |
| Lower P-Value | 0.0030 |
| Consistent P-Value | 0.0050 |
| Upper P-Value | 0.0050 |
| Strategies | 9.0000 |
| Observations | 2,513.0000 |
8 rows
Using Your Own Data
get_data_snooping_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_data_snooping_test()
Parameters
get_data_snooping_test accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency for returns. Options are: “daily”, “weekly”, “monthly”, “quarterly”, “yearly”. Defaults to “daily”. | "weekly" |
method | str | The test. Options are: “spa” (Hansen’s Superior Predictive Ability), “reality_check” (White’s Reality Check), “stepm” (Romano and Wolf’s StepM). Defaults to “spa”. | "spa" |
alpha | float | The familywise error rate of the StepM test: the probability of naming a ticker that does not beat the benchmark. Defaults to 0.05. | 0.01 |
n_bootstrap | int | The number of bootstrap resamples B. Defaults to 1000. | 1000 |
block_size | int | None | The average block length of the stationary bootstrap, in periods. Defaults to None, which uses the cube root of the number of periods, rounded. | None, which uses the cube root of the number of periods, rounded |
studentize | bool | Whether the SPA and StepM tests divide every average by \(\hat{\omega}_{k}\), as Hansen and Romano and Wolf recommend. The Reality Check is never studentized. Defaults to True. | True |
seed | int | The seed of the bootstrap. Defaults to 0. | 42 |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise). | 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 White, H. (2000). “A Reality Check for Data Snooping.” Econometrica, 68(5), 1097-1126.
- Paper Hansen, P.R. (2005). “A Test for Superior Predictive Ability.” Journal of Business & Economic Statistics, 23(4), 365-380.
- Paper Romano, J.P. and Wolf, M. (2005). “Stepwise Multiple Testing as Formalized Data Snooping.” Econometrica, 73(4), 1237-1282.
- Paper Politis, D.N. and Romano, J.P. (1994). “The Stationary Bootstrap.” Journal of the American Statistical Association, 89(428), 1303-1313.
- Library Referenced against arch’s SPA
- Library Referenced against arch’s StepM