Test the heteroscedasticity of a given Time-Series Dataset🔗
Introduction🔗
Summary
From Wikipedia:
In statistics, a sequence of random variables is heteroscedastic if its random variables have different variances. The term means "differing variance" and comes from the Greek "hetero" (different) and "skedasis" (dispersion). In the context of regression analysis, heteroscedasticity refers to the case where the variability of a variable is unequal across the range of values of a second variable that predicts it.
For more info, see: Heteroscedasticity on Wikipedia.
Info
The library currently wraps four widely used heteroscedasticity tests from statsmodels, providing a unified interface for diagnostic checking:
| library | category | algorithm | short | import script | url |
|---|---|---|---|---|---|
| statsmodels | Heteroscedasticity | ARCH Test | ARCH | from statsmodels.stats.diagnostic import het_arch |
https://www.statsmodels.org/stable/generated/statsmodels.stats.diagnostic.het_arch.html |
| Heteroscedasticity | Breusch-Pagan Test | BP | from statsmodels.stats.diagnostic import het_breuschpagan |
https://www.statsmodels.org/stable/generated/statsmodels.stats.diagnostic.het_breuschpagan.html | |
| Heteroscedasticity | Goldfeld-Quandt Test | GQ | from statsmodels.stats.diagnostic import het_goldfeldquandt |
https://www.statsmodels.org/stable/generated/statsmodels.stats.diagnostic.het_goldfeldquandt.html | |
| Heteroscedasticity | White's Test | WHITE | from statsmodels.stats.diagnostic import het_white |
https://www.statsmodels.org/stable/generated/statsmodels.stats.diagnostic.het_white.html |
For more info, see: Statsmodels Diagnostics.
Source Library
statsmodels was chosen as the primary engine for these tests because it provides comprehensive, well-documented, and numerically stable implementations of the most common heteroscedasticity diagnostics. These tests allow for robust verification of OLS assumptions across various scenarios, including autoregressive clusters (ARCH) and general non-constant variance (White's).
Source Module
All of the source code can be found within these modules:
Modules🔗
ts_stat_tests.heteroscedasticity.tests
🔗
Summary
This module provides wrapper functions for various heteroscedasticity tests including: - ARCH Test - Breusch-Pagan Test - Goldfeld-Quandt Test - White's Test
heteroscedasticity
🔗
heteroscedasticity(
res: Union[RegressionResults, RegressionResultsWrapper],
algorithm: str = "bp",
**kwargs: Union[float, int, str, bool, ArrayLike, None]
) -> tuple[Any, ...]
Summary
Perform a heteroscedasticity test on a fitted regression model.
Details
This function is a convenience wrapper around four underlying algorithms:
- arch()
- bp()
- gq()
- white()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
|
Union[RegressionResults, RegressionResultsWrapper]
|
The fitted regression model to be checked. |
required |
algorithm
|
str
|
Which heteroscedasticity algorithm to use. |
'bp'
|
kwargs
|
Union[float, int, str, bool, ArrayLike, None]
|
Additional keyword arguments passed to the underlying test function. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When the given value for |
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float, float, float], tuple[float, float, str], ResultsStore]
|
The results of the heteroscedasticity test. The return type depends on the chosen algorithm and |
Credit
Calculations are performed by statsmodels.
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 | |
| Example 1: Breusch-Pagan test | |
|---|---|
1 2 3 | |
| Example 2: ARCH test | |
|---|---|
1 2 3 | |
Source code in src/ts_stat_tests/heteroscedasticity/tests.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
is_heteroscedastic
🔗
is_heteroscedastic(
res: Union[RegressionResults, RegressionResultsWrapper],
algorithm: str = "bp",
alpha: float = 0.05,
**kwargs: Union[float, int, str, bool, ArrayLike, None]
) -> dict[str, Union[str, float, bool, None]]
Summary
Test whether a given model's residuals exhibit heteroscedasticity or not.
Details
This function checks the results of a heteroscedasticity test against a significance level alpha. The null hypothesis (\(H_0\)) for all supported tests is homoscedasticity (constant variance). If the p-value is less than alpha, the null hypothesis is rejected in favor of heteroscedasticity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
|
Union[RegressionResults, RegressionResultsWrapper]
|
The fitted regression model to be checked. |
required |
algorithm
|
str
|
Which heteroscedasticity algorithm to use. See |
'bp'
|
alpha
|
float
|
The significance level for the test.
Default: |
0.05
|
kwargs
|
Union[float, int, str, bool, ArrayLike, None]
|
Additional keyword arguments passed to the underlying test function. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Union[str, float, bool, None]]
|
A dictionary containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 | |
| Example 1: Check heteroscedasticity with Breusch-Pagan | |
|---|---|
1 2 3 | |
Source code in src/ts_stat_tests/heteroscedasticity/tests.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
ts_stat_tests.heteroscedasticity.algorithms
🔗
Summary
This module implements various heteroscedasticity tests including: - ARCH Test - Breusch-Pagan Test - Goldfeld-Quandt Test - White's Test
VALID_GQ_ALTERNATIVES_OPTIONS
module-attribute
🔗
VALID_GQ_ALTERNATIVES_OPTIONS = Literal[
"two-sided", "increasing", "decreasing"
]
arch
🔗
arch(
resid: ArrayLike,
nlags: Optional[int] = None,
ddof: int = 0,
*,
store: Literal[False] = False
) -> tuple[float, float, float, float]
arch(
resid: ArrayLike,
nlags: Optional[int] = None,
ddof: int = 0,
*,
store: Literal[True]
) -> tuple[float, float, float, float, ResultsStore]
arch(
resid: ArrayLike,
nlags: Optional[int] = None,
ddof: int = 0,
*,
store: bool = False
) -> Union[
tuple[float, float, float, float],
tuple[float, float, float, float, ResultsStore],
]
Summary
Engle's Test for Autoregressive Conditional Heteroscedasticity (ARCH).
Details
This test is used to determine whether the residuals of a time-series model exhibit ARCH effects. ARCH effects are characterized by clusters of volatility, where periods of high volatility are followed by periods of high volatility, and vice versa. The test is essentially a Lagrange Multiplier (LM) test for autocorrelation in the squared residuals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resid
|
ArrayLike
|
The residuals from a linear regression model. |
required |
nlags
|
Optional[int]
|
The number of lags to include in the test regression. If |
None
|
ddof
|
int
|
Degrees of freedom to adjust for in the calculation of the F-statistic.
Default: |
0
|
store
|
bool
|
Whether to return a |
False
|
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float, float, float], tuple[float, float, float, float, ResultsStore]]
|
A tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 7 | |
| Example 1: Basic ARCH test | |
|---|---|
1 2 3 | |
Calculation
The test is performed by regressing the squared residuals \(e_t^2\) on a constant and \(q\) lags of the squared residuals:
The null hypothesis of no ARCH effects is:
The LM statistic is calculated as \(T \times R^2\) from this regression, where \(T\) is the number of observations and \(R^2\) is the coefficient of determination.
Credit
Calculations are performed by statsmodels.
References
- Engle, R. F. (1982). Autoregressive Conditional Heteroscedasticity with Estimates of the Variance of United Kingdom Inflation. Econometrica, 50(4), 987-1007.
Source code in src/ts_stat_tests/heteroscedasticity/algorithms.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
bpl
🔗
bpl(
resid: ArrayLike,
exog_het: ArrayLike,
robust: bool = True,
) -> tuple[float, float, float, float]
Summary
Breusch-Pagan Lagrange Multiplier Test for Heteroscedasticity.
Details
This test checks whether the variance of the errors in a regression model depends on the values of the independent variables. If it does, the errors are heteroscedastic. The null hypothesis assumes homoscedasticity (constant variance).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resid
|
ArrayLike
|
The residuals from a linear regression model. |
required |
exog_het
|
ArrayLike
|
The explanatory variables for the variance (heteroscedasticity). Usually, these are the same as the original regression's exogenous variables. |
required |
robust
|
bool
|
Whether to use a robust version of the test that does not assume the errors are normally distributed (Koenker's version).
Default: |
True
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float]
|
A tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 7 | |
| Example 1: Basic Breusch-Pagan test | |
|---|---|
1 2 3 | |
Calculation
The test first fits a regression of squared residuals (or standardized version) on the specified exogenous variables:
The null hypothesis is:
Koenker's robust version uses the scores of the likelihood function and does not require the normality assumption.
Credit
Calculations are performed by statsmodels.
References
- Breusch, T. S., & Pagan, A. R. (1979). A Simple Test for Heteroscedasticity and Random Coefficient Variation. Econometrica, 47(5), 1287-1294.
- Koenker, R. (1981). A Note on Studentizing a Test for Heteroscedasticity. Journal of Econometrics, 17(1), 107-112.
Source code in src/ts_stat_tests/heteroscedasticity/algorithms.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
gq
🔗
gq(
y: ArrayLike,
x: ArrayLike,
idx: Optional[int] = None,
split: Optional[Union[int, float]] = None,
drop: Optional[Union[int, float]] = None,
alternative: VALID_GQ_ALTERNATIVES_OPTIONS = "increasing",
*,
store: Literal[False] = False
) -> tuple[float, float, str]
gq(
y: ArrayLike,
x: ArrayLike,
idx: Optional[int] = None,
split: Optional[Union[int, float]] = None,
drop: Optional[Union[int, float]] = None,
alternative: VALID_GQ_ALTERNATIVES_OPTIONS = "increasing",
*,
store: Literal[True]
) -> tuple[float, float, str, ResultsStore]
gq(
y: ArrayLike,
x: ArrayLike,
idx: Optional[int] = None,
split: Optional[Union[int, float]] = None,
drop: Optional[Union[int, float]] = None,
alternative: VALID_GQ_ALTERNATIVES_OPTIONS = "increasing",
*,
store: bool = False
) -> Union[
tuple[float, float, str],
tuple[float, float, str, ResultsStore],
]
Summary
Goldfeld-Quandt Test for Heteroscedasticity.
Details
The Goldfeld-Quandt test checks for heteroscedasticity by dividing the dataset into two subsets (usually at the beginning and end of the sample) and comparing the variance of the residuals in each subset using an F-test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ArrayLike
|
The dependent variable (endogenous). |
required |
x
|
ArrayLike
|
The independent variables (exogenous). |
required |
idx
|
Optional[int]
|
The column index of the variable to sort by. If |
None
|
split
|
Optional[Union[int, float]]
|
The index at which to split the sample. If a float between 0 and 1, it represents the fraction of observations.
Default: |
None
|
drop
|
Optional[Union[int, float]]
|
The number of observations to drop in the middle. If a float between 0 and 1, it represents the fraction of observations.
Default: |
None
|
alternative
|
VALID_GQ_ALTERNATIVES_OPTIONS
|
The alternative hypothesis. Options are |
'increasing'
|
store
|
bool
|
Whether to return a |
False
|
Returns:
| Type | Description |
|---|---|
Union[tuple[float, float, str], tuple[float, float, str, ResultsStore]]
|
A tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 | |
| Example 1: Basic Goldfeld-Quandt test | |
|---|---|
1 2 3 | |
Calculation
The dataset is split into two samples after sorting by an independent variable (or using the natural order). Separate regressions are run on each sample:
The test statistic is the ratio of variances:
where \(RSS_i\) are the residual sum of squares and \(df_i\) are the degrees of freedom.
Credit
Calculations are performed by statsmodels.
References
- Goldfeld, S. M., & Quandt, R. E. (1965). Some Tests for Homoscedasticity. Journal of the American Statistical Association, 60(310), 539-547.
Source code in src/ts_stat_tests/heteroscedasticity/algorithms.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
wlm
🔗
wlm(
resid: ArrayLike, exog_het: ArrayLike
) -> tuple[float, float, float, float]
Summary
White's Test for Heteroscedasticity.
Details
White's test is a general test for heteroscedasticity that does not require a specific functional form for the variance of the error terms. It is essentially a test of whether the squared residuals can be explained by the levels, squares, and cross-products of the independent variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resid
|
ArrayLike
|
The residuals from a linear regression model. |
required |
exog_het
|
ArrayLike
|
The explanatory variables for the variance. Usually, these are the original exogenous variables; the test internally handles adding their squares and cross-products. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float]
|
A tuple containing:
- |
Examples
| Setup | |
|---|---|
1 2 3 4 5 6 7 | |
| Example 1: Basic White's test | |
|---|---|
1 2 3 | |
Calculation
Squared residuals are regressed on all distinct variables in the cross-product of the original exogenous variables (including constant, linear terms, squares, and interactions):
The LM statistic is \(T \times R^2\) from this auxiliary regression, where \(T\) is the number of observations.
Credit
Calculations are performed by statsmodels.
References
- White, H. (1980). A Heteroskedasticity-Consistent Covariance Matrix Estimator and a Direct Test for Heteroskedasticity. Econometrica, 48(4), 817-838.
Source code in src/ts_stat_tests/heteroscedasticity/algorithms.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |