Non-Nested Test

Calculate a test between two non-nested regressions of the dependent ticker: whether the regression on the independent tickers (model A) or the one on the alternative tickers (model B) explains it, when neither is a special case of the other.

Two explanations of the same returns, such as a sector ETF against the market, or one factor set against another, cannot be compared with an F-test because neither model nests the other. Each test is run both ways: model A against B and model B against A. Rejecting one and not the other favours the one not rejected; rejecting both says each misses something the other has, and rejecting neither says the data cannot tell them apart.

The formula for the J test is as follows:

\[y = X_{A} \cdot a + \delta \cdot \hat{y}_{B} + u\] \[t = \frac{\hat{\delta}}{\text{SE}(\hat{\delta})}\]

fitted by OLS, with hat(y_B) the fitted values of model B: model A is rejected when model B’s fit adds to it.

The formula for the encompassing test is as follows:

\[y = X_{A} \cdot a + Z_{B} \cdot b + u\] \[F = \frac{\frac{\text{SSR}_{A} - \text{SSR}}{q}}{\frac{\text{SSR}}{n - k}}\]

fitted by OLS, with \(Z_{B}\) the regressors of model B that model A does not have and q the columns of \(Z_{B}\).

The formula for the Cox test is as follows:

\[\hat{\sigma}_{A}^{2} = \frac{\text{SSR}_{A}}{n}\] \[\hat{\sigma}_{B}^{2} = \frac{\text{SSR}_{B}}{n}\] \[M_{B} = I - X_{B} \cdot (X'_{B} \cdot X_{B})^{-1} \cdot X'_{B}\] \[\hat{\sigma}_{\text{BA}}^{2} = \hat{\sigma}_{A}^{2} + \frac{\hat{\beta}_{A}' \cdot X'_{A} \cdot M_{B} \cdot X_{A} \cdot \hat{\beta}_{A}}{n}\] \[c = \left(\frac{n}{2}\right) \cdot \ln\left(\frac{\hat{\sigma}_{B}^{2}}{\hat{\sigma}_{\text{BA}}^{2}}\right)\] \[v = \left(\frac{\hat{\sigma}_{A}^{2}}{\hat{\sigma}_{\text{BA}}^{4}}\right) \cdot \hat{\beta}_{A}' \cdot X'_{A} \cdot M_{B} \cdot M_{A} \cdot M_{B} \cdot X_{A} \cdot \hat{\beta}_{A}\] \[q = \frac{c}{\sqrt{v}}\]

with q standard normal.

y is the dependent ticker’s column (Return by default); every model has a constant.

Also known as: J test, Davidson-MacKinnon J test, Cox test, Cox-Pesaran test, encompassing test, non-nested model test, model comparison test.

Example · XOM, XLE · 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_non_nested_test:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["XOM", "XLE"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2025-01-01",
    end_date="2025-12-31",
)

# XOM explained by XLE (model A) or by the benchmark SPY (model B)
toolkit.econometrics.get_non_nested_test()

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

Were ExxonMobil's daily returns in 2025 better explained by the Energy Select Sector SPDR ETF or by the S&P 500 ETF, by the J test?

Which returns:

ItemJ t-StatisticP-ValueReject Tested Model (5%)
Model A against Model B-7.305801
Model B against Model A33.215001

2 rows

Using Your Own Data

get_non_nested_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_non_nested_test()

Parameters

get_non_nested_test accepts the following parameters.

ParameterTypeDescriptionExample
dependent_tickerstr | NoneThe dependent (predicted) asset. Defaults to None, meaning the Toolkit instance’s first ticker."AAPL"
independent_tickersstr | list[str] | NoneThe regressors of model A. Defaults to None, meaning every other ticker in the Toolkit instance besides dependent_ticker and the other independent tickers.["MSFT", "Benchmark"]
other_independent_tickersstr | list[str] | NoneThe regressors of model B. Defaults to None, meaning “Benchmark”."GOOGL"
include_benchmarkboolWhether to include “Benchmark” in the default independent ticker(s) of model A. Defaults to False.True
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column to regress on. Defaults to “Return”."Return"
methodstrThe test. Options are: “\(j_{\text{test}}\)” (J test), “encompassing” (encompassing test), “cox” (Cox test). Defaults to “\(j_{\text{test}}\)”."$$j_{\text{test}}$$"
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 Davidson, R. and MacKinnon, J.G. (1981). “Several Tests for Model Specification in the Presence of Alternative Hypotheses.” Econometrica, 49(3), 781-793.
  • Paper Cox, D.R. (1961). “Tests of Separate Families of Hypotheses.” Proceedings of the Fourth Berkeley Symposium on Mathematical Statistics and Probability, 1, 105-123.
  • Paper Pesaran, M.H. (1974). “On the General Problem of Model Selection.” Review of Economic Studies, 41(2), 153-171.
  • Paper Mizon, G.E. and Richard, J.-F. (1986). “The Encompassing Principle and its Application to Testing Non-Nested Hypotheses.” Econometrica, 54(3), 657-678.
  • Library Referenced against statsmodels’ compare_j (J test)
  • Library Referenced against statsmodels’ compare_encompassing (encompassing test)
  • Library Referenced against statsmodels’ compare_cox (Cox test)
Share