Heteroskedasticity and Autocorrelation Estimators for Julia.
Feasible GLS correction methods for linear regression models covering heteroskedasticity, AR(1) serial correlation, and their combination -- with both sequential and joint estimation strategies.
All estimators accept both matrix and @formula interfaces and return typed
result structs that subtype StatsBase.RegressionModel, making them
compatible with the broader Julia statistics ecosystem.
using Pkg
Pkg.add(url = "https://github.com/Trumpingtons/HARE.jl")ols(X, y) and ols(@formula(y ~ x1 + x2), data) fit ordinary least squares with the classical covariance matrix and return OLSResult, with the same interface as the corrected estimators, so the two can be compared side by side.
Four variance functions are available, each on auxiliary regressors Z (without a constant column; a constant is prepended, and Z defaults to X). Each has a two-step FWLS estimator (one auxiliary regression, then WLS), an iterated FWLS estimator that alternates until convergence, and a maximum likelihood estimator.
| Variance function | Two-step FWLS | Iterated FWLS | MLE |
|---|---|---|---|
Exponential (Harvey 1976): sigma_i^2 = sigma_0^2 * exp(z_i' * gamma) |
exponential_fwls |
exponential_ifwls |
exponential_mle |
Quadratic (Glejser 1969): sigma_i = gamma_0 + z_i' * gamma |
quadratic_fwls |
quadratic_ifwls |
quadratic_mle |
Linear: sigma_i^2 = gamma_0 + z_i' * gamma |
linear_fwls |
linear_ifwls |
linear_mle |
Power: sigma_i^2 = sigma^2 * prod_j abs(z_ij)^(2 * delta_j) (Z required) |
power_fwls |
power_ifwls |
power_mle |
The FWLS estimators return FWLSResult, the MLE estimators MLEResult.
Groupwise heteroskedasticity, one variance per group (sigma_i^2 = sigma_g^2), takes a vector of group labels: two_step_groupwise(X, y, groups), iterated_groupwise and groupwise_mle, each returning GroupwiseResult.
Six estimators handle serial correlation under the AR(1) model u_t = rho * u_{t-1} + e_t. Prais-Winsten FGLS preserves the first observation via a GLS transformation; Cochrane-Orcutt drops it. Both come in two-step and iterated forms. Hildreth-Lu is also GLS-based, but searches a grid of rho values and picks the one minimising RSS. Beach-MacKinnon maximises the exact concentrated likelihood over rho using Brent's method, with regression coefficients recovered analytically. All return AR1Result.
| Function | Method |
|---|---|
two_step_prais_winsten |
Prais-Winsten FGLS |
iterated_prais_winsten |
Prais-Winsten FGLS |
two_step_cochrane_orcutt |
Cochrane-Orcutt FGLS |
iterated_cochrane_orcutt |
Cochrane-Orcutt FGLS |
hildreth_lu |
Grid search over rho |
beach_mackinnon |
Exact MLE (Brent) |
Two strategies are available for handling both effects simultaneously.
Sequential HARE -- corrects for autocorrelation first (Prais-Winsten), then
heteroskedasticity (exponential variance on Z), alternating until convergence. The two effects
are estimated separately in each pass.
| Function | Result type |
|---|---|
two_step_sequential |
SequentialResult |
iterated_sequential |
SequentialResult |
Joint HARE -- estimates AR(1) and heteroskedasticity simultaneously via
maximum likelihood. The model is u_t = rho * u_{t-1} + sigma_t * e_t with
log(sigma_t^2) = z_t' * gamma. The two-step variant uses the concentrated
likelihood (beta profiled out analytically); the iterated variant uses
coordinate descent.
| Function | Result type |
|---|---|
two_step_joint |
JointResult |
iterated_joint |
JointResult |
Pass regressors without a constant column -- the intercept is added automatically:
using HARE, Random
Random.seed!(1234)
n = 500
x1 = randn(n); x2 = randn(n)
X = hcat(x1, x2)
# Heteroskedastic errors (exponential variance)
y_het = hcat(ones(n), X) * [1.0, 2.0, -1.0] .+ exp.(0.5 .* x1) .* randn(n)
m = exponential_ifwls(X, y_het)The @formula interface works identically to GLM.jl:
data = (y = y_het, x1 = x1, x2 = x2)
m = exponential_ifwls(@formula(y ~ x1 + x2), data)AR(1) correction with the Prais-Winsten estimator:
u = zeros(n)
for t in 2:n; u[t] = 0.7 * u[t-1] + randn(); end
y_ar = hcat(ones(n), X) * [1.0, 2.0, -1.0] .+ u
m = iterated_prais_winsten(X, y_ar)
m.rho # estimated AR(1) coefficient, approx 0.70Combined AR(1) + heteroskedasticity -- sequential approach:
sigma = exp.(0.3 .* x1)
u = zeros(n)
for t in 2:n; u[t] = 0.7 * u[t-1] + sigma[t] * randn(); end
y_hae = hcat(ones(n), X) * [1.0, 2.0, -1.0] .+ u
m = iterated_sequential(X, y_hae)
m.rho # AR(1) estimateCombined AR(1) + heteroskedasticity -- joint MLE:
m = iterated_joint(X, y_hae)
m.rho # AR(1) estimate
m.gamma # log-variance coefficients gamma_hat
m.loglik # maximised log-likelihoodThe quadratic (Glejser) estimator models sigma_i (not sigma_i^2) linearly in auxiliary
regressors. An alternative Z matrix can be supplied for the variance equation:
Z = hcat(abs.(x1)) # variance depends on |x1| only; the constant is added
m = quadratic_ifwls(X, y_het; Z=Z)With the formula interface, Z is a formula: Z = @formula(0 ~ abs(x1)).
The same Z keyword is available for the sequential and joint estimators.
To fit a model without an intercept, pass intercept=false:
m = exponential_fwls(X, y_het; intercept=false)All result types implement the full StatsBase.RegressionModel interface:
m = iterated_prais_winsten(@formula(y ~ x1 + x2), data)
# Coefficients
coef(m) # coefficient vector beta_hat
stderror(m) # standard errors
vcov(m) # covariance matrix
coefnames(m) # ["(Intercept)", "x1", "x2"]
# Sample information
nobs(m) # number of observations
dof(m) # number of parameters k
dof_residual(m) # n - k
# Fitted values and residuals
fitted(m) # X * beta_hat
residuals(m) # y - X * beta_hat
response(m) # y (recovered from fitted + residuals)
# Inference
tstat(m) # t-statistics
pvalues(m) # two-sided p-values (Student-t by default)
pvalues(m; dist=:normal) # asymptotic normal p-values
confint(m) # 95% confidence intervals
confint(m; level=0.90) # 90% confidence intervals
# Fit diagnostics
rss(m) # residual sum of squares
sigma2(m) # sigma_hat^2 = RSS / (n - k)
r2(m) # R^2
adjr2(m) # adjusted R^2
# Formula metadata (formula-fitted models only)
formula(m) # fitted formula
termnames(m) # ["(Intercept)", "x1", "x2"]
responsename(m) # "y"MLEResult, GroupwiseResult, JointResult and the AR1Result of beach_mackinnon additionally expose loglikelihood(m).
Typing m in the REPL displays a formatted coefficient table:
Coefficients:
---------------------------------------------------------------------------
Coef. Std. Error t Pr(>|t|) Lower 95% Upper 95%
---------------------------------------------------------------------------
(Intercept) 1.023 0.089 11.49 <1e-23 0.848 1.198
x1 2.015 0.092 21.90 <1e-64 1.834 2.196
x2 -0.998 0.091 -10.97 <1e-22 -1.177 -0.819
---------------------------------------------------------------------------
rho: 0.6934 Converged: true Iterations: 8
N: 500 R^2: 0.9421 Adj. R^2: 0.9419
Matrix-fitted models accept a new design matrix, including the constant column when the model has an intercept:
m = exponential_ifwls(X, y_het)
predict(m, hcat(ones(size(Xnew, 1)), Xnew))Formula-fitted models accept a new data table and apply the original encoding automatically:
m = exponential_ifwls(@formula(y ~ x1 + x2), data)
predict(m, newdata) # Tables.jl-compatible: DataFrame, NamedTuple, etc.Tests the linear restriction H0: Rbeta = r using the GLS covariance matrix. The test statistic F = (Rbeta_hat - r)' * (RVR')^(-1) * (R*beta_hat - r) / q is compared to F(q, n-k).
m = iterated_prais_winsten(X, y_ar)
# Joint significance of x1 and x2
R = [0.0 1.0 0.0; 0.0 0.0 1.0]
wald_test(m, R)
# Wald test (F):
# F(2, 497) = 312.45 p-value = 0.0000
# Test beta_2 = 1.0
wald_test(m, [0.0 1.0 0.0], [1.0])Compares two nested Beach-MacKinnon models. Restricted model is passed first.
X1 = X[:, 1:1] # x1 only
m_r = beach_mackinnon(X1, y_ar) # intercept + x1
m_u = beach_mackinnon(X, y_ar) # intercept + x1 + x2
lrtest(m_r, m_u)
# Likelihood ratio test:
# LR = 95.43 df = 1 p-value = 0.0000| Function | Null hypothesis |
|---|---|
harvey_test(X, y; Z) |
no multiplicative (exponential) heteroskedasticity |
glejser_test(X, y; Z) |
no heteroskedasticity with a linear standard deviation |
breusch_pagan_test(X, y) |
homoskedasticity (Breusch-Pagan) |
white_test(X, y) |
homoskedasticity (White) |
durbin_watson_test(X, y) |
no first-order autocorrelation |
breusch_godfrey_test(X, y, lag) |
no autocorrelation up to lag |
Each also accepts (formula, data).
HAREModel <: StatsBase.RegressionModel means HARE results work directly
with packages that support the standard interface.
HARE: errors that change size, errors that remember, its LaTeX source: Pollis compiles it in Help > Documentation > Package Vignettes.
- Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461-465.
- Glejser, H. (1969). A new test for heteroskedasticity. Journal of the American Statistical Association, 64(325), 316-323.
- Prais, S. J., & Winsten, C. B. (1954). Trend estimators and serial correlation. Cowles Commission Discussion Paper, No. 383.
- Cochrane, D., & Orcutt, G. H. (1949). Application of least squares regression to relationships containing auto-correlated error terms. JASA, 44(245), 32-61.
- Hildreth, C., & Lu, J. Y. (1960). Demand relations with autocorrelated disturbances. MSU Agricultural Experiment Station Technical Bulletin, No. 276.
- Oberhofer, W., & Kmenta, J. (1974). A general procedure for obtaining maximum likelihood estimates in generalized regression models. Econometrica, 42(3), 579-590.
- Beach, C. M., & MacKinnon, J. G. (1978). A maximum likelihood procedure for regression with autocorrelated errors. Econometrica, 46(1), 51-58.