mfe.tests_stat¶
mfe.tests_stat ¶
mfe.tests_stat — Statistical tests for financial time series.
Serial correlation ljung_box Ljung-Box Q statistic (not robust to heteroskedasticity) lm_test HAC-robust LM serial correlation test (MFE lmtest1)
Conditional heteroskedasticity arch_lm Engle (1982) ARCH-LM test
Forecast evaluation mincer_zarnowitz MZ regression-based forecast evaluation diebold_mariano DM test for equal predictive accuracy (MSE/MAE/QLIKE)
MZResult
dataclass
¶
MZResult(alpha: float, beta: float, alpha_se: float, beta_se: float, t_stat_alpha: float, t_stat_beta: float, f_stat: float, f_pvalue: float, r_squared: float, n_obs: int)
Mincer-Zarnowitz regression output.
ljung_box ¶
Ljung-Box Q statistic for serial correlation.
Q_k = T(T+2) * sum_{j=1}^{k} rho_hat_j^2 / (T - j)
Under H0 of no autocorrelation, Q_k ~ chi2(k) asymptotically.
NOTE: Not appropriate for heteroskedastic data (use lm_test instead).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
FloatArray
|
|
required |
max_lags
|
number of lags to test; returns one statistic per lag 1..max_lags
|
|
10
|
Source code in src/mfe/tests_stat/serial.py
lm_test ¶
LM test for serial correlation in up to max_lags lags.
The test is an LM-test for testing the null that all of the regression coefficients are zero in the auxiliary regression of y_t on lags 1..Q. The null tested is H0: phi_1 = phi_2 = ... = phi_Q = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
FloatArray
|
|
required |
max_lags
|
maximum lag order to test
|
|
10
|
robust
|
bool
|
|
True
|
Notes
Equivalent to MFE toolbox lmtest1.m.
Source code in src/mfe/tests_stat/serial.py
mincer_zarnowitz ¶
Mincer-Zarnowitz regression: realized = alpha + beta * forecast + eps.
Tests alpha = 0, beta = 1 (unbiased forecast), and the joint H0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
realized
|
(T,) actual realized values (e.g. RV_t)
|
|
required |
forecast
|
(T,) model forecasts (e.g. h_{t|t-1})
|
|
required |
nw_lags
|
int
|
|
0
|
Returns:
| Type | Description |
|---|---|
MZResult
|
|
Source code in src/mfe/tests_stat/forecast_eval.py
diebold_mariano ¶
diebold_mariano(errors1: FloatArray, errors2: FloatArray, loss: str = 'mse', nw_lags: int | None = None, alternative: str = 'two-sided') -> DMResult
Diebold-Mariano test for equal predictive accuracy.
Diebold, F.X. & Mariano, R.S. (1995): "Comparing Predictive Accuracy", JBES.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
errors1
|
(T,) forecast error arrays from two models
|
|
required |
errors2
|
(T,) forecast error arrays from two models
|
|
required |
loss
|
str
|
|
'mse'
|
nw_lags
|
int | None
|
|
None
|
alternative
|
str
|
|
'two-sided'
|
Source code in src/mfe/tests_stat/forecast_eval.py
arch_lm ¶
ARCH-LM test for conditional heteroskedasticity.
Engle, R.F. (1982): "Autoregressive Conditional Heteroscedasticity with Estimates of the Variance of United Kingdom Inflation", Econometrica.
The test regresses squared residuals on their lagged values: eps_t^2 = alpha_0 + alpha_1 * eps_{t-1}^2 + ... + alpha_q * eps_{t-q}^2 + u_t
H0: alpha_1 = ... = alpha_q = 0 (no ARCH effects)
Test statistic: T * R^2 ~ chi2(q) under H0.
Also computes the F-form (more reliable in small samples).
arch_lm ¶
Engle ARCH-LM test for conditional heteroskedasticity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
residuals
|
(T,) return or residual series
|
|
required |
lags
|
int
|
|
5
|
Returns:
| Type | Description |
|---|---|
ARCHLMResult
|
.lm_stat / .lm_pval : LM test (chi2 form, q df) .f_stat / .f_pval : F-test form (more reliable in small T) |
Source code in src/mfe/tests_stat/arch_lm.py
forecast_eval ¶
Forecast evaluation tests for volatility models.
Mincer-Zarnowitz (1969): regression-based evaluation of forecasts. Diebold & Mariano (1995): test for equal predictive accuracy. Hansen (2005): Superior Predictive Ability test.
MZResult
dataclass
¶
MZResult(alpha: float, beta: float, alpha_se: float, beta_se: float, t_stat_alpha: float, t_stat_beta: float, f_stat: float, f_pvalue: float, r_squared: float, n_obs: int)
Mincer-Zarnowitz regression output.
mincer_zarnowitz ¶
Mincer-Zarnowitz regression: realized = alpha + beta * forecast + eps.
Tests alpha = 0, beta = 1 (unbiased forecast), and the joint H0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
realized
|
(T,) actual realized values (e.g. RV_t)
|
|
required |
forecast
|
(T,) model forecasts (e.g. h_{t|t-1})
|
|
required |
nw_lags
|
int
|
|
0
|
Returns:
| Type | Description |
|---|---|
MZResult
|
|
Source code in src/mfe/tests_stat/forecast_eval.py
diebold_mariano ¶
diebold_mariano(errors1: FloatArray, errors2: FloatArray, loss: str = 'mse', nw_lags: int | None = None, alternative: str = 'two-sided') -> DMResult
Diebold-Mariano test for equal predictive accuracy.
Diebold, F.X. & Mariano, R.S. (1995): "Comparing Predictive Accuracy", JBES.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
errors1
|
(T,) forecast error arrays from two models
|
|
required |
errors2
|
(T,) forecast error arrays from two models
|
|
required |
loss
|
str
|
|
'mse'
|
nw_lags
|
int | None
|
|
None
|
alternative
|
str
|
|
'two-sided'
|
Source code in src/mfe/tests_stat/forecast_eval.py
serial ¶
Serial correlation tests for financial time series.
Ljung & Box (1978): Q-statistic for autocorrelation up to lag K. Godfrey (1978) / Breusch (1978): LM test for serial correlation.
Key difference from statsmodels: statsmodels.stats.diagnostic.acorr_ljungbox only provides the standard LB test. lmtest here provides a HAC-robust LM variant (lmtest1 from MFE toolbox) which is appropriate for heteroskedastic series — the standard LB test is not.
The heteroskedasticity-robust LM test is essentially an LR-class test: LM = T * s_hat' * S_hat^{-1} * s_hat where s_hat = T^{-1} X'eps_tilde (gradient under null) and S_hat is estimated under the alternative using the White sandwich.
ljung_box ¶
Ljung-Box Q statistic for serial correlation.
Q_k = T(T+2) * sum_{j=1}^{k} rho_hat_j^2 / (T - j)
Under H0 of no autocorrelation, Q_k ~ chi2(k) asymptotically.
NOTE: Not appropriate for heteroskedastic data (use lm_test instead).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
FloatArray
|
|
required |
max_lags
|
number of lags to test; returns one statistic per lag 1..max_lags
|
|
10
|
Source code in src/mfe/tests_stat/serial.py
lm_test ¶
LM test for serial correlation in up to max_lags lags.
The test is an LM-test for testing the null that all of the regression coefficients are zero in the auxiliary regression of y_t on lags 1..Q. The null tested is H0: phi_1 = phi_2 = ... = phi_Q = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
FloatArray
|
|
required |
max_lags
|
maximum lag order to test
|
|
10
|
robust
|
bool
|
|
True
|
Notes
Equivalent to MFE toolbox lmtest1.m.