F-Test
Calculate a nested-model F-test for the joint significance of the regressors in unrestricted_independent_tickers that are not already in restricted_independent_tickers.
Two OLS regressions of dependent_ticker are fitted internally: a “restricted” one on restricted_independent_tickers and an “unrestricted” one that adds the extra tickers. The test checks the null hypothesis that the q extra tickers are jointly zero, i.e. that adding them does not explain the dependent ticker any better, by comparing how much the Residual Sum of Squares (RSS) drops when they are added.
The formula is as follows:
\[F = ((\text{RSS\_restricted} - \text{RSS\_unrestricted}) / q) / (\text{RSS\_unrestricted} / (n - k_{\text{unrestricted}}))\]- Both models are OLS regressions of the dependent ticker’s
column(Return by default), with a constant when add_constant=True
Where q = k_unrestricted - k_restricted is the number of added regressors, n the number of observations and k_unrestricted the number of parameters of the larger model. Under the null, F follows an F(q, n - k_unrestricted) distribution. A low p-value means the extra tickers meaningfully improve the fit as a group, even when none of them is significant individually. get_likelihood_ratio_test is the asymptotic Maximum Likelihood counterpart.
Also known as: nested F-test, restricted vs. unrestricted F-test, partial F-test.
F-Test in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_f_test as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
["AAPL", "MSFT"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.econometrics.get_f_test(
"AAPL", "MSFT", ["MSFT", "Benchmark"], period="weekly"
)
Or, after setting up the Finance Toolkit MCP, ask:
Did adding the S&P 500 to a regression of Apple's weekly returns on Microsoft's improve the fit from 2021 to 2025, according to an F-test?
Which returns:
| 0 | |
|---|---|
| F-Statistic | 104.748 |
| Df Numerator | 1 |
| Df Denominator | 257 |
| P-Value | 0 |
| Reject Restrictions (5%) | 1 |
Using Your Own Data
get_f_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, where each country is a column:
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")
Then call econometrics.get_f_test with dependent_ticker, restricted_independent_tickers and unrestricted_independent_tickers, using the column names of the data as tickers.
Parameters
get_f_test accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
dependent_ticker | str | The dependent (predicted) asset. | "AAPL" |
restricted_independent_tickers | str | list[str] | The independent asset(s) in the restricted (smaller) model. | "MSFT" |
unrestricted_independent_tickers | str | list[str] | The independent asset(s) in the unrestricted (larger) model – must be a superset of restricted_independent_tickers. | ["MSFT", "Benchmark"] |
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The historical data column to regress on. Defaults to “Return”. | "Return" |
add_constant | bool | Whether to include an intercept in both models. Defaults to True. | False |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |