Hausman Test
Calculate the Hausman specification test comparing a Fixed Effects and a Random Effects fit of dependent_tickers (the panel of entities) on a regressor x.
Also known as: Hausman specification test, fixed versus random effects test.
Random Effects is more efficient than Fixed Effects but relies on entity effects being uncorrelated with the regressor(s) – if that assumption is violated, Random Effects is inconsistent while Fixed Effects remains consistent regardless. See panel_data_model.get_hausman_test for the full formula and references, and get_fixed_effects/get_random_effects for how the two models being compared are estimated (including how independent_tickers/independent_column shape the panel, and why independent_column is generally the more reliable choice here).
The formula is as follows:
\[H = (\text{betâ\_FE} - \text{betâ\_RE}) \text{'} \cdot \left[\operatorname{Var}(\text{betâ\_FE}) - \operatorname{Var}(\text{betâ\_RE})\right] \text{⁻¹} \cdot (\text{betâ\_FE} - \text{betâ\_RE}),\;\; \text{over the k slope coefficients the two models share (a pseudo-inverse when the difference is singular, and H floored at 0)}\]-
H is chi-squared with k degrees of freedom; Random Effects is rejected when the p-value is below 0.05
-
β̂_FE comes from
get_fixed_effectswith entity effects only and β̂_RE fromget_random_effects
Hausman Test in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_hausman_test as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
["AAPL", "MSFT", "AMZN"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.econometrics.get_hausman_test(independent_column="Volume", period="weekly")
Or, after setting up the Finance Toolkit MCP, ask:
Should a weekly panel of Apple, Microsoft and Amazon returns on trading volume from 2021 to 2025 use fixed or random effects, according to the Hausman test?
Which returns:
| Value | |
|---|---|
| Hausman Statistic | 0.5866 |
| Degrees of Freedom | 1 |
| P-Value | 0.4438 |
| Prefer Fixed Effects (5%) | 0 |
Using Your Own Data
get_hausman_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_hausman_test with independent_tickers or independent_column, using the column names of the data as tickers.
Parameters
get_hausman_test accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
independent_tickers | str | list[str] | None | The factor ticker(s), whose column values are broadcast identically to every entity at each date. Mutually exclusive with independent_column. | "Benchmark" |
independent_column | str | None | A different historical data column, taken per-entity from each of dependent_tickers’ own data, to use as the regressor. Mutually exclusive with independent_tickers. | "Volume" |
dependent_tickers | str | list[str] | None | The panel of entity tickers to explain. Defaults to None, meaning every ticker in the Toolkit instance (other than independent_tickers, if given). | ["AAPL", "MSFT", "GOOGL"] |
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The dependent variable’s historical data column. Defaults to “Return”. | "Return" |
include_benchmark | bool | Whether to include “Benchmark” among the entities. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |