API Reference
HARE — Module
HARE - Heteroskedasticity and Autocorrelation Estimators
Classical feasible GLS correction methods for linear regression models, including multiplicative heteroskedasticity (Harvey 1976), AR(1) serial correlation (Prais-Winsten, Hildreth-Lu, Cochrane-Orcutt), and joint correction via the Sequential HARE estimator (Oberhofer-Kmenta 1974) and the Beach-MacKinnon (1978) exact MLE.
Result Types
HARE.HAREModel — Type
HAREModelAbstract supertype for all HARE estimator result objects. Subtypes support the StatsBase interface: coef, stderror, vcov, residuals, fitted, and predict.
HARE.OLSResult — Type
OLSResult <: HAREModelResult of an ordinary least squares fit, with the classical covariance matrix. Returned by ols.
Fields
coef: coefficient vector beta_hat.coefnames: coefficient names.mf:ModelFramefrom formula fit;nothingfor matrix fit.vcov: estimated covariance matrix of betahat, sigma2hat * inv(X'X).residuals: residuals, y - X*beta_hat.fitted: fitted values X*beta_hat.loglik: Gaussian log-likelihood at the estimates.
HARE.FWLSResult — Type
FWLSResult <: HAREModelResult of a Feasible Weighted Least Squares (FWLS) estimator for heteroskedastic regression. Returned by exponential_fwls, exponential_ifwls, quadratic_fwls, quadratic_ifwls, linear_fwls, and linear_ifwls.
Fields
coef: coefficient vector beta_hat.coefnames: coefficient names.mf:ModelFramefrom formula fit;nothingfor matrix fit.vcov: estimated covariance matrix of beta_hat.residuals: residuals in the original space, y - X*beta_hat.fitted: fitted values X*beta_hat.gamma: estimated variance-equation coefficients gamma_hat.gamma_vcov: estimated covariance matrix of gamma_hat (from the auxiliary OLS regression).iterations: number of FWLS iterations performed.converged:trueif the convergence criterion was satisfied.link: variance model —:exponential,:quadratic, or:linear.
HARE.MLEResult — Type
MLEResult <: HAREModelResult of a heteroskedastic regression MLE estimator (exponential_mle, quadratic_mle, or linear_mle).
Fields
coef: coefficient vector beta_hat.coefnames: coefficient names.mf:ModelFramefrom formula fit;nothingfor matrix fit.vcov: estimated covariance matrix of beta_hat (from observed information).residuals: residuals in the original space, y - X*beta_hat.fitted: fitted values X*beta_hat.gamma: estimated variance equation coefficients gamma_hat (including intercept).gamma_vcov: estimated covariance matrix of gamma_hat (from observed information).loglik: maximised log-likelihood value.link: link function —:exponential,:quadratic, or:linear.converged:trueif the L-BFGS convergence criterion was satisfied.iterations: number of L-BFGS iterations performed.
HARE.AR1Result — Type
AR1Result <: HAREModelResult of an AR(1) serial-correlation estimator. Returned by two_step_prais_winsten, iterated_prais_winsten, two_step_cochrane_orcutt, iterated_cochrane_orcutt, hildreth_lu, and beach_mackinnon.
Fields
coef: coefficient vector beta_hat.coefnames: coefficient names.mf:ModelFramefrom formula fit;nothingfor matrix fit.vcov: estimated covariance matrix of beta_hat.residuals: residuals in the original space, y - X*beta_hat.fitted: fitted values X*beta_hat.rho: estimated AR(1) coefficient rho_hat.loglik: maximised log-likelihood (:beach_mackinnononly;NaNotherwise).rss: minimised transformed RSS (:hildreth_luonly;NaNotherwise).iterations: iterations performed (grid points for:hildreth_lu;0for:beach_mackinnon).converged:trueif the convergence criterion was satisfied.method: estimator used —:prais_winsten,:cochrane_orcutt,:hildreth_lu, or:beach_mackinnon.
HARE.SequentialResult — Type
SequentialResult <: HAREModelResult of the Sequential HARE estimator (two_step_sequential or iterated_sequential). Corrects for AR(1) autocorrelation first (Prais-Winsten), then multiplicative heteroskedasticity (Harvey), sequentially.
Fields
coef: coefficient vector beta_hat.coefnames: coefficient names.mf:ModelFramefrom formula fit;nothingfor matrix fit.vcov: estimated covariance matrix of beta_hat.residuals: residuals in the original space, y - X*beta_hat.fitted: fitted values X*beta_hat.rho: estimated AR(1) coefficient rho_hat.gamma: estimated log-variance coefficients gamma_hat (from the Harvey step).gamma_vcov: estimated covariance matrix of gamma_hat (from the auxiliary OLS regression).iterations: number of sequential iterations performed.converged:trueif the joint convergence criterion was satisfied.
HARE.JointResult — Type
JointResult <: HAREModelResult of the Joint HARE estimator (two_step_joint or iterated_joint). Estimates AR(1) autocorrelation and multiplicative heteroskedasticity simultaneously via maximum likelihood.
Fields
coef: coefficient vector beta_hat.coefnames: coefficient names.mf:ModelFramefrom formula fit;nothingfor matrix fit.vcov: estimated covariance matrix of betahat (conditional on rhohat, gamma_hat).residuals: residuals in the original space, y - X*beta_hat.fitted: fitted values X*beta_hat.rho: estimated AR(1) coefficient rho_hat.gamma: estimated log-variance coefficients gammahat (for `log(sigmat^2) = z_t' * gamma`).gamma_vcov: estimated covariance matrix of gamma_hat (from the observed information matrix).loglik: log-likelihood at the final estimates.iterations: number of iterations performed.converged:trueif the convergence criterion was satisfied.
HARE.GroupwiseResult — Type
GroupwiseResult <: HAREModelResult of a groupwise heteroskedasticity estimator (two_step_groupwise, iterated_groupwise, or groupwise_mle).
Fields
coef: coefficient vector beta_hat.coefnames: coefficient names.mf:ModelFramefrom formula fit;nothingfor matrix fit.vcov: estimated covariance matrix of betahat. For `groupwisemle` this is (X′WX)⁻¹; for FWLS estimators it is the dispersion-scaled WLS covariance.residuals: residuals in the original space, y - X*beta_hat.fitted: fitted values X*beta_hat.group_labels: group identifiers (one per group, in order of first occurrence).group_sizes: number of observations per group.sigma2: estimated group variances σ̂²_g, one per group.loglik: log-likelihood at the final estimates.iterations: number of FWLS iterations performed.converged:trueif the convergence criterion was satisfied.
HARE.HarveyTestResult — Type
Result of harvey_test. Fields: n (sample size), lm (LM statistic), dof (degrees of freedom).
HARE.GlejserTestResult — Type
Result of glejser_test. Fields: n (sample size), lm (LM statistic), dof (degrees of freedom).
HARE.WaldTestResult — Type
Result of a Wald test for linear restrictions R*beta = r.
Fields: stat (F statistic), df (numerator), df_residual (denominator), pvalue.
HARE.LRTestResult — Type
Result of a likelihood ratio test comparing two nested Beach-MacKinnon models.
Fields: stat (LR statistic), df, pvalue.
Ordinary Least Squares
HARE.ols — Function
ols(X, y; intercept=true) -> OLSResult
ols(formula, data) -> OLSResultOrdinary least squares estimator with the classical covariance matrix sigma2hat * inv(X'X), where sigma2hat = RSS / (n - k). The coefficients come from a Householder QR decomposition of X, and a rank-deficient X is an error. The estimates, standard errors, residuals and log-likelihood agree with GLM.jl's lm to machine precision, which the test suite checks.
Arguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).
Returns
OLSResult. Use stderror(result) to obtain standard errors.
Examples
julia> using HARE, Random
julia> Random.seed!(42); n = 50; X = randn(n, 1); y = hcat(ones(n), X) * [1.0, 2.0] .+ randn(n);
julia> m = ols(X, y);
julia> length(coef(m))
2
julia> coefnames(m)
2-element Vector{String}:
"(Intercept)"
"x1"Heteroskedasticity FWLS Estimators
HARE.exponential_fwls — Function
exponential_fwls(X, y; Z=nothing, intercept=true) -> FWLSResult
exponential_fwls(formula, data; Z=nothing) -> FWLSResultTwo-step Feasible Weighted Least Squares for multiplicative heteroskedasticity (Harvey 1976). Models the log-variance as a linear function of auxiliary regressors:
\[\begin{aligned} \sigma_i^2 &= \exp(\gamma_0 + \mathbf{z}_i^\top\boldsymbol{\gamma}) \end{aligned}\]
Step 1. OLS → residuals û. Step 2. Auxiliary regression log(û²ᵢ) - c = Zᵢγ + vᵢ → weights wᵢ = 1/exp(Zᵢγ̂). Step 3. WLS with weights w.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.
Returns
References
Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461–465.
HARE.exponential_ifwls — Function
exponential_ifwls(X, y; Z=nothing, intercept=true, tol=1e-8, maxiter=100) -> FWLSResult
exponential_ifwls(formula, data; Z=nothing, tol=1e-8, maxiter=100) -> FWLSResultIterated FWLS for multiplicative heteroskedasticity (Harvey 1976). Repeats the weight-update step until max|β̂(i) - β̂(i-1)| < tol.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.tol: convergence tolerance (default1e-8).maxiter: maximum iterations (default100).
Returns
References
Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461–465.
HARE.quadratic_fwls — Function
quadratic_fwls(X, y; Z=nothing, intercept=true) -> FWLSResult
quadratic_fwls(formula, data; Z=nothing) -> FWLSResultTwo-step Feasible Weighted Least Squares for heteroskedasticity with a linear standard deviation (Glejser 1969):
\[\begin{aligned} \sigma_i &= \gamma_0 + \mathbf{z}_i^\top\boldsymbol{\gamma} \end{aligned}\]
Step 1. OLS → residuals û. Step 2. Auxiliary regression |ûᵢ| = Zᵢγ + vᵢ → weights wᵢ = 1/σ̂²ᵢ. Step 3. WLS with weights w.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.
Returns
FWLSResult. Throws an ArgumentError if a fitted standard deviation is zero or negative.
References
Glejser, H. (1969). A new test for heteroskedasticity. Journal of the American Statistical Association, 64(325), 316–323.
HARE.quadratic_ifwls — Function
quadratic_ifwls(X, y; Z=nothing, intercept=true, tol=1e-8, maxiter=100) -> FWLSResult
quadratic_ifwls(formula, data; Z=nothing, tol=1e-8, maxiter=100) -> FWLSResultIterated FWLS for heteroskedasticity with a linear standard deviation (Glejser 1969). Repeats the weight-update step until max|β̂(i) - β̂(i-1)| < tol.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.tol: convergence tolerance (default1e-8).maxiter: maximum iterations (default100).
Returns
FWLSResult. Throws an ArgumentError if a fitted standard deviation is zero or negative at any iteration.
References
Glejser, H. (1969). A new test for heteroskedasticity. Journal of the American Statistical Association, 64(325), 316–323.
HARE.linear_fwls — Function
linear_fwls(X, y; Z=nothing, intercept=true) -> FWLSResult
linear_fwls(formula, data; Z=nothing) -> FWLSResultTwo-step Feasible Weighted Least Squares for heteroskedasticity with a linear variance function:
\[\begin{aligned} \sigma_i^2 &= \gamma_0 + \mathbf{z}_i^\top\boldsymbol{\gamma} \end{aligned}\]
Step 1. OLS → residuals û. Step 2. Auxiliary OLS of û²ᵢ on Zᵢ → weights wᵢ = 1/σ̂²ᵢ. Step 3. WLS with weights w.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.
Returns
FWLSResult. Throws an ArgumentError if a fitted variance is zero or negative.
HARE.linear_ifwls — Function
linear_ifwls(X, y; Z=nothing, intercept=true, tol=1e-8, maxiter=100) -> FWLSResult
linear_ifwls(formula, data; Z=nothing, tol=1e-8, maxiter=100) -> FWLSResultIterated FWLS for heteroskedasticity with a linear variance function. Repeats the weight-update step until max|β̂(i) - β̂(i-1)| < tol.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.tol: convergence tolerance (default1e-8).maxiter: maximum iterations (default100).
Returns
FWLSResult. Throws an ArgumentError if a fitted variance is zero or negative at any iteration.
HARE.power_fwls — Function
power_fwls(X, y; Z, intercept=true) -> FWLSResult
power_fwls(formula, data; Z) -> FWLSResultTwo-step FWLS for heteroskedasticity with a power variance function. The variance is modelled as:
\[\sigma_i^2 = \sigma^2 \prod_j |z_{ij}|^{2\delta_j}\]
Equivalently, $\log\sigma_i^2 = \log\sigma^2 + 2\delta_1\log|z_{i1}| + \cdots$, which is the exponential (Harvey) model on $\log|Z|$. The reported $\boldsymbol{\gamma} = (\gamma_0, \gamma_1, \ldots)$ satisfies $\gamma_0 = \log\sigma^2$ and $\gamma_j = 2\delta_j$. Standard errors are exact — no delta-method approximation.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix without a constant column. A constant is prepended internally. Required for the power model.
Returns
FWLSResult with link = :power.
HARE.power_ifwls — Function
power_ifwls(X, y; Z, intercept=true, tol=1e-8, maxiter=100) -> FWLSResult
power_ifwls(formula, data; Z, tol=1e-8, maxiter=100) -> FWLSResultIterated FWLS for heteroskedasticity with a power variance function. Repeats the weight-update step until max|β̂(i) - β̂(i-1)| < tol.
See power_fwls for the variance model definition.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix without a constant column.tol: convergence tolerance (default1e-8).maxiter: maximum iterations (default100).
Returns
FWLSResult with link = :power.
Heteroskedasticity MLE Estimators
HARE.exponential_mle — Function
exponential_mle(X, y; Z=nothing, intercept=true, tol=1e-10, maxiter=1000) -> MLEResult
exponential_mle(formula, data; Z=nothing, tol=1e-10, maxiter=1000) -> MLEResultMaximum likelihood estimation of the heteroskedastic regression model with an exponential variance function (Harvey 1976):
\[\begin{aligned} y_i &= \mathbf{x}_i^\top\boldsymbol{\beta} + \varepsilon_i, \qquad \varepsilon_i \sim \mathcal{N}(0,\,\sigma_i^2) \\[4pt] \sigma_i^2 &= \exp(\gamma_0 + \mathbf{z}_i^\top\boldsymbol{\gamma}) \end{aligned}\]
The log-likelihood is maximised jointly over $(\boldsymbol{\beta}, \boldsymbol{\gamma})$ using L-BFGS with automatic differentiation. Two-step Harvey FWLS estimates serve as warm starts. Standard errors come from the inverse observed information matrix.
Corresponds to SAS PROC AUTOREG HETERO LINK=EXP.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.Z: n × p auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: gradient norm tolerance for L-BFGS convergence, per observation (default1e-10).maxiter: maximum number of L-BFGS iterations (default1000).
Returns
MLEResult with link = :exponential.
References
Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461–465.
HARE.quadratic_mle — Function
quadratic_mle(X, y; Z=nothing, intercept=true, tol=1e-10, maxiter=1000) -> MLEResult
quadratic_mle(formula, data; Z=nothing, tol=1e-10, maxiter=1000) -> MLEResultMaximum likelihood estimation of the heteroskedastic regression model with a quadratic (linear standard deviation) variance function (Glejser 1969):
\[\begin{aligned} y_i &= \mathbf{x}_i^\top\boldsymbol{\beta} + \varepsilon_i, \qquad \varepsilon_i \sim \mathcal{N}(0,\,\sigma_i^2) \\[4pt] \sigma_i &= \gamma_0 + \mathbf{z}_i^\top\boldsymbol{\gamma} \end{aligned}\]
The log-likelihood is maximised jointly over $(\boldsymbol{\beta}, \boldsymbol{\gamma})$ using L-BFGS with automatic differentiation. Two-step Glejser FWLS estimates serve as warm starts. Standard errors come from the inverse observed information matrix.
Corresponds to SAS PROC AUTOREG HETERO LINK=SQUARE.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.Z: n × p auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: gradient norm tolerance for L-BFGS convergence, per observation (default1e-10).maxiter: maximum number of L-BFGS iterations (default1000).
Returns
MLEResult with link = :quadratic. Throws an ArgumentError if a fitted standard deviation is zero or negative at the optimum.
References
Glejser, H. (1969). A new test for heteroskedasticity. Journal of the American Statistical Association, 64(325), 316–323.
HARE.linear_mle — Function
linear_mle(X, y; Z=nothing, intercept=true, tol=1e-8, maxiter=1000) -> MLEResult
linear_mle(formula, data; Z=nothing, tol=1e-8, maxiter=1000) -> MLEResultMaximum likelihood estimation of the heteroskedastic regression model with a linear variance function:
\[\begin{aligned} y_i &= \mathbf{x}_i^\top\boldsymbol{\beta} + \varepsilon_i, \qquad \varepsilon_i \sim \mathcal{N}(0,\,\sigma_i^2) \\[4pt] \sigma_i^2 &= \gamma_0 + \mathbf{z}_i^\top\boldsymbol{\gamma} \end{aligned}\]
The log-likelihood is maximised jointly over $(\boldsymbol{\beta}, \boldsymbol{\gamma})$ using L-BFGS with automatic differentiation. OLS of squared OLS residuals on Z serves as a warm start. Standard errors come from the inverse observed information matrix.
Corresponds to SAS PROC AUTOREG HETERO LINK=LINEAR.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.Z: n × p auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: gradient norm tolerance for L-BFGS convergence, per observation (default1e-8).maxiter: maximum number of L-BFGS iterations (default1000).
Returns
MLEResult with link = :linear. Throws an ArgumentError if a fitted variance is zero or negative at the optimum.
HARE.power_mle — Function
power_mle(X, y; Z, intercept=true, tol=1e-10, maxiter=1000) -> MLEResult
power_mle(formula, data; Z, tol=1e-10, maxiter=1000) -> MLEResultMaximum likelihood estimation of the heteroskedastic regression model with a power variance function:
\[\begin{aligned} y_i &= \mathbf{x}_i^\top\boldsymbol{\beta} + \varepsilon_i, \qquad \varepsilon_i \sim \mathcal{N}(0,\,\sigma_i^2) \\[4pt] \sigma_i^2 &= \sigma^2 \prod_j |z_{ij}|^{2\delta_j} \end{aligned}\]
Equivalent to Harvey's exponential model on $\log|Z|$: the variance equation is $\log\sigma_i^2 = \gamma_0 + \gamma_1\log|z_{i1}| + \cdots$ so the reported $\boldsymbol{\gamma} = (\gamma_0, \gamma_1, \ldots)$ satisfies $\gamma_0 = \log\sigma^2$ and $\gamma_j = 2\delta_j$. Standard errors are exact (from the observed information matrix), with no delta-method approximation.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.Z: n × p auxiliary regressor matrix without a constant column. A constant is prepended internally. Required for the power model.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: gradient norm tolerance for L-BFGS convergence, per observation (default1e-10).maxiter: maximum number of L-BFGS iterations (default1000).
Returns
MLEResult with link = :power. The gamma field contains $(\log\sigma^2, 2\delta_1, \ldots)$.
Groupwise Heteroscedasticity
HARE.two_step_groupwise — Function
two_step_groupwise(X, y, groups; intercept=true) -> GroupwiseResult
two_step_groupwise(formula, data, groups) -> GroupwiseResultTwo-step Feasible GLS for groupwise heteroskedasticity (Greene §9.7.2).
Step 1. OLS → estimate σ̂²g = (1/ng) Σ{i∈g} ûi² for each group g. Step 2. WLS with weights wi = 1/σ̂²g.
Standard errors are conditional on σ̂²_g (WLS sandwich).
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.groups: length-n vector of group labels (any type: integers, strings, …).intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).
Returns
References
Greene, W. H. (2018). Econometric Analysis (8th ed.). Pearson, §9.7.2.
HARE.iterated_groupwise — Function
iterated_groupwise(X, y, groups; intercept=true, tol=1e-8, maxiter=100) -> GroupwiseResult
iterated_groupwise(formula, data, groups; tol=1e-8, maxiter=100) -> GroupwiseResultIterated Feasible GLS for groupwise heteroskedasticity (Greene §9.7.2).
Each iteration:
- Update σ̂²g = (1/ng) Σ{i∈g} ûi² from current residuals.
- Update β by WLS with weights wi = 1/σ̂²g.
Convergence criterion: max|β(i) − β(i−1)| < tol.
At convergence this is equivalent to the joint MLE of (β, σ²1, …, σ²G), since the MLE first-order conditions for σ²_g reduce to the within-group sample variance.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.groups: length-n vector of group labels.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: convergence tolerance (default1e-8).maxiter: maximum number of iterations (default100).
Returns
References
Greene, W. H. (2018). Econometric Analysis (8th ed.). Pearson, §9.7.2.
HARE.groupwise_mle — Function
groupwise_mle(X, y, groups; intercept=true, tol=1e-8, maxiter=100) -> GroupwiseResult
groupwise_mle(formula, data, groups; tol=1e-8, maxiter=100) -> GroupwiseResultMaximum likelihood estimation for groupwise heteroskedasticity.
The model is:
\[y_i = \mathbf{x}_i^\top\boldsymbol{\beta} + \varepsilon_i, \qquad \varepsilon_i \sim \mathcal{N}(0,\,\sigma_g^2), \quad i \in g\]
The MLE first-order conditions are:
- $\hat{\boldsymbol{\beta}}$: WLS with weights $1/\sigma_g^2$
- $\hat{\sigma}_g^2 = \frac{1}{n_g}\sum_{i \in g}\hat{u}_i^2$
These are solved by coordinate descent (alternating WLS and variance updates) until $\max|\hat{\boldsymbol{\beta}}^{(t)} - \hat{\boldsymbol{\beta}}^{(t-1)}| < \texttt{tol}$.
Standard errors come from the exact observed information matrix $(\mathbf{X}^\top\mathbf{W}\mathbf{X})^{-1}$, not from the dispersion-scaled WLS covariance. This is the key difference from iterated_groupwise, which gives identical point estimates but slightly different standard errors in finite samples.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.groups: length-n vector of group labels (any type: integers, strings, …).intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: convergence tolerance (default1e-8).maxiter: maximum number of iterations (default100).
Returns
References
Greene, W. H. (2018). Econometric Analysis (8th ed.). Pearson, §9.7.2.
AR(1) Estimators
HARE.two_step_prais_winsten — Function
two_step_prais_winsten(X, y; intercept=true) -> AR1Result
two_step_prais_winsten(formula, data) -> AR1ResultTwo-step Prais-Winsten GLS estimator correcting for AR(1) autocorrelation.
Step 1. Fit OLS, estimate rho via the Cochrane-Orcutt moment estimator. Step 2. Apply the Prais-Winsten transformation (retaining the first observation) and fit OLS on the transformed system.
Arguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).
Returns
AR1Result. Use stderror(result) to obtain standard errors.
Examples
julia> using HARE, Random, StatsBase
julia> Random.seed!(42); n = 50; u = zeros(n); for t in 2:n; u[t] = 0.7*u[t-1] + randn(); end;
julia> X = randn(n, 1); y = hcat(ones(n), X) * [1.0, 2.0] .+ u;
julia> m = two_step_prais_winsten(X, y);
julia> length(coef(m))
2
julia> m.converged
trueReferences
Prais, S. J., & Winsten, C. B. (1954). Trend estimators and serial correlation. Cowles Commission Discussion Paper, No. 383.
HARE.iterated_prais_winsten — Function
iterated_prais_winsten(X, y; intercept=true, tol=1e-8, maxiter=100) -> AR1Result
iterated_prais_winsten(formula, data; tol=1e-8, maxiter=100) -> AR1ResultIterated Prais-Winsten (Cochrane-Orcutt) GLS estimator for AR(1) errors. Alternates between estimating rho and re-fitting by Prais-Winsten GLS until |rho(i) - rho(i-1)| < tol.
Arguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: convergence tolerance on |rho(i) - rho(i-1)| (default1e-8).maxiter: maximum number of iterations (default100).
Returns
AR1Result. Use stderror(result) to obtain standard errors.
References
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. Journal of the American Statistical Association, 44(245), 32-61.
HARE.two_step_cochrane_orcutt — Function
two_step_cochrane_orcutt(X, y; intercept=true) -> AR1Result
two_step_cochrane_orcutt(formula, data) -> AR1ResultTwo-step Cochrane-Orcutt GLS estimator for AR(1) autocorrelation.
Step 1. Fit OLS, estimate rho via the Cochrane-Orcutt moment estimator. Step 2. Apply the Cochrane-Orcutt quasi-differencing transformation (dropping the first observation) and fit OLS on the (n−1) × k transformed system.
Prais-Winsten strictly dominates Cochrane-Orcutt: it retains the first observation via the scaling sqrt(1 − ρ²) at zero additional cost, yielding coefficient estimates with smaller variance. two_step_cochrane_orcutt is provided for compatibility with software that uses the CO convention (e.g. Python statsmodels.GLSAR, Stata prais, corc).
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).
Returns
Examples
julia> using HARE, Random, StatsBase
julia> Random.seed!(42); n = 50; u = zeros(n); for t in 2:n; u[t] = 0.7*u[t-1] + randn(); end;
julia> X = randn(n, 1); y = hcat(ones(n), X) * [1.0, 2.0] .+ u;
julia> m = two_step_cochrane_orcutt(X, y);
julia> length(coef(m))
2
julia> m.converged
trueReferences
Cochrane, D., & Orcutt, G. H. (1949). Application of least squares regression to relationships containing auto-correlated error terms. Journal of the American Statistical Association, 44(245), 32–61.
HARE.iterated_cochrane_orcutt — Function
iterated_cochrane_orcutt(X, y; intercept=true, tol=1e-8, maxiter=100) -> AR1Result
iterated_cochrane_orcutt(formula, data; tol=1e-8, maxiter=100) -> AR1ResultIterated Cochrane-Orcutt GLS estimator for AR(1) errors. Alternates between estimating rho and re-fitting by CO quasi-differencing until |rho(i) - rho(i-1)| < tol.
Prais-Winsten strictly dominates Cochrane-Orcutt: it retains the first observation via the scaling sqrt(1 − ρ²) at zero additional cost, yielding coefficient estimates with smaller variance. iterated_cochrane_orcutt is provided for compatibility with software that uses the CO convention (e.g. Python statsmodels.GLSAR, Stata prais, corc).
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: convergence tolerance on |rho(i) - rho(i-1)| (default1e-8).maxiter: maximum number of iterations (default100).
Returns
References
Cochrane, D., & Orcutt, G. H. (1949). Application of least squares regression to relationships containing auto-correlated error terms. Journal of the American Statistical Association, 44(245), 32–61.
HARE.hildreth_lu — Function
hildreth_lu(X, y; intercept=true, nrho=200) -> AR1Result
hildreth_lu(formula, data; nrho=200) -> AR1ResultHildreth-Lu grid-search estimator for AR(1) errors. Evaluates the GLS transformation over a uniform grid of nrho candidate values of rho on (-0.99, 0.99) and selects the value that minimises the residual sum of squares.
Unlike iterative methods, Hildreth-Lu is guaranteed to find the global minimum over the search grid and is particularly robust when the likelihood surface is multimodal. Note: the first observation is dropped for each candidate rho (Cochrane-Orcutt convention), so the effective sample size is n-1.
Arguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.nrho: number of grid points for rho in (-0.99, 0.99) (default200).
Returns
AR1Result. Use stderror(result) to obtain standard errors.
References
Hildreth, C., & Lu, J. Y. (1960). Demand relations with autocorrelated disturbances. Michigan State University Agricultural Experiment Station Technical Bulletin, No. 276.
HARE.beach_mackinnon — Function
beach_mackinnon(X, y; intercept=true, tol=1e-10) -> AR1Result
beach_mackinnon(formula, data; tol=1e-10) -> AR1ResultBeach-MacKinnon exact maximum likelihood estimator for the linear regression model with AR(1) errors. Maximises the exact concentrated log-likelihood
L(rho) = -n/2 * log(S(rho)/n) + 1/2 * log(1 - rho^2)where the Jacobian term 1/2 * log(1 - rho^2) distinguishes this from the Cochrane-Orcutt/Prais-Winsten approximation. rho is found by Brent's method; betahat and sigmahat are then recovered analytically. Standard errors are conditional on rho_hat.
Arguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).tol: absolute tolerance for Brent's method (default1e-10).
Returns
AR1Result. Use stderror(result) to obtain standard errors.
References
Beach, C. M., & MacKinnon, J. G. (1978). A maximum likelihood procedure for regression with autocorrelated errors. Econometrica, 46(1), 51-58.
Sequential AR(1) + Heteroskedasticity Estimators
HARE.two_step_sequential — Function
two_step_sequential(X, y; intercept=true, Z=nothing) -> SequentialResult
two_step_sequential(formula, data; Z=nothing) -> SequentialResultTwo-step Feasible GLS correcting sequentially for AR(1) autocorrelation and multiplicative heteroskedasticity (Sequential HARE).
Step 1. OLS -> estimate rho via Cochrane-Orcutt. Step 2. Prais-Winsten transformation -> (Xstar, ystar). Step 3. Harvey log-variance auxiliary regression on the Prais-Winsten residuals against Z (default: augmented X), since the innovations upwt = sigmat * epst have variance exp(Z_t' * gamma) in the original space. Step 4. WLS on (Xstar, ystar) with weights w.
Arguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: n x p auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.
Returns
SequentialResult. Use stderror(result) to obtain standard errors.
References
Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461-465.
Oberhofer, W., & Kmenta, J. (1974). A general procedure for obtaining maximum likelihood estimates in generalized regression models. Econometrica, 42(3), 579-590.
HARE.iterated_sequential — Function
iterated_sequential(X, y; intercept=true, Z=nothing, tol=1e-8, maxiter=100) -> SequentialResult
iterated_sequential(formula, data; Z=nothing, tol=1e-8, maxiter=100) -> SequentialResultIterated Feasible GLS correcting sequentially for AR(1) autocorrelation and multiplicative heteroskedasticity. Each iteration estimates rho, applies the Prais-Winsten transformation, computes Harvey weights, and fits WLS. Convergence criterion:
max(max|beta_hat(i) - beta_hat(i-1)|, |rho(i) - rho(i-1)|) < tolArguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: n x p auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.tol: convergence tolerance (default1e-8).maxiter: maximum number of iterations (default100).
Returns
SequentialResult. Use stderror(result) to obtain standard errors.
References
Oberhofer, W., & Kmenta, J. (1974). A general procedure for obtaining maximum likelihood estimates in generalized regression models. Econometrica, 42(3), 579-590.
Joint AR(1) + Heteroskedasticity Estimators
HARE.two_step_joint — Function
two_step_joint(X, y; intercept=true, Z=nothing) -> JointResult
two_step_joint(formula, data; Z=nothing) -> JointResultTwo-step Joint HARE estimator. Jointly estimates AR(1) autocorrelation and multiplicative heteroskedasticity via the concentrated maximum likelihood:
Step 1. Maximize the concentrated log-likelihood over (rho, gamma), profiling out beta analytically at each evaluation. Step 2. One GLS step on the doubly-transformed system to recover beta_hat.
The variance model is log(sigma_t^2) = z_t' * gamma with auxiliary regressors Z (default: augmented X). Standard errors are conditional on (rhohat, gammahat).
Arguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: n x p auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.
Returns
Examples
julia> using HARE, Random, StatsBase
julia> Random.seed!(42); n = 100; X = randn(n, 1); y = hcat(ones(n), X) * [1.0, 2.0] .+ 0.5 .* randn(n);
julia> m = two_step_joint(X, y);
julia> length(coef(m))
2
julia> m.converged
trueReferences
Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461-465.
Oberhofer, W., & Kmenta, J. (1974). A general procedure for obtaining maximum likelihood estimates in generalized regression models. Econometrica, 42(3), 579-590.
HARE.iterated_joint — Function
iterated_joint(X, y; intercept=true, Z=nothing, tol=1e-8, maxiter=100) -> JointResult
iterated_joint(formula, data; Z=nothing, tol=1e-8, maxiter=100) -> JointResultIterated Joint HARE estimator. Uses coordinate descent to jointly estimate AR(1) autocorrelation and multiplicative heteroskedasticity:
Each iteration:
- Optimize (rho, gamma) via maximum likelihood with beta fixed.
- Update beta via one GLS step on the doubly-transformed system.
Convergence criterion:
max(max|beta_hat(i) - beta_hat(i-1)|, |rho(i) - rho(i-1)|) < tolArguments
X: n x k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant column is prepended toXautomatically.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: n x p auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.tol: convergence tolerance (default1e-8).maxiter: maximum number of iterations (default100).
Returns
References
Oberhofer, W., & Kmenta, J. (1974). A general procedure for obtaining maximum likelihood estimates in generalized regression models. Econometrica, 42(3), 579-590.
Heteroskedasticity Tests
HARE.harvey_test — Function
harvey_test(X, y; intercept=true, Z=nothing) -> HarveyTestResult
harvey_test(formula, data; Z=nothing) -> HarveyTestResultLagrange multiplier test for multiplicative heteroskedasticity (Harvey 1976).
Tests H₀: γ = 0 in the exponential variance model log(σᵢ²) = γ₀ + zᵢ'γ.
The test statistic is
LM = ESS / trigamma(1/2)where ESS is the explained sum of squares from the auxiliary OLS regression of log(ûᵢ²) − c on Z (c = E[log(χ²(1))] = −1.2703628454614782), and trigamma(1/2) = π²/2 is the variance of log(χ²(1)). The statistic is distributed as χ²(p) under H₀, where p = dim(γ) (columns of Z excluding the intercept).
This is Harvey's (1976) exact score-test formula. It differs from the generic LM form n·R² but is asymptotically equivalent.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.
References
Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461–465.
HARE.glejser_test — Function
glejser_test(X, y; intercept=true, Z=nothing) -> GlejserTestResult
glejser_test(formula, data; Z=nothing) -> GlejserTestResultLagrange multiplier test for heteroskedasticity with a linear standard deviation (Glejser 1969).
Tests H₀: γ = 0 in the quadratic variance model σᵢ = γ₀ + zᵢ'γ.
The test statistic is
LM = ESS_|û| / (σ̂² · (1 − 2/π))where ESS_|û| is the explained sum of squares from the auxiliary OLS regression of |ûᵢ| on Z, σ̂² = (1/n)Σûᵢ² is the OLS variance estimate from the main regression, and (1 − 2/π) = Var(|N(0,1)|) is the variance of the half-normal. The statistic is distributed as χ²(p) under H₀, where p = dim(γ) (columns of Z excluding the intercept).
This is Glejser's (1969) exact score-test formula. It differs from the generic LM form n·R² but is asymptotically equivalent.
Arguments
X: n × k regressor matrix without a constant column.y: response vector of length n.intercept: iftrue(default), a constant is prepended toX.formula:@formulaexpression (formula method).data: Tables.jl-compatible data source (formula method).Z: auxiliary regressor matrix for the variance equation, without a constant column (default:X). A constant is prepended internally.
References
Glejser, H. (1969). A new test for heteroskedasticity. Journal of the American Statistical Association, 64(325), 316–323.
HARE.breusch_pagan_test — Function
breusch_pagan_test(X, y; intercept=true) -> WhiteTest
breusch_pagan_test(formula, data) -> WhiteTestBreusch-Pagan / Koenker LM test for heteroskedasticity (linear variance link).
Tests H₀: σᵢ² is constant against σᵢ² = f(Xβ). The LM statistic n·R² from the regression of ê² on X is distributed as χ²(k−1) under H₀, where k is the number of columns in X (including the intercept).
Returns a HypothesisTests.WhiteTest with type = :linear; call pvalue on the result as usual.
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.
HARE.white_test — Function
white_test(X, y; intercept=true, type=:White) -> WhiteTest
white_test(formula, data; type=:White) -> WhiteTestWhite's general test for heteroskedasticity.
The type keyword selects which auxiliary regressors are used:
:White(default) — linear terms, squares, and cross-products.:linear_and_squares— linear terms and squares only (no cross-products).:linear— linear terms only (equivalent to Breusch-Pagan/Koenker).
The LM statistic n·R² from the regression of ê² on the selected terms is distributed as χ²(dof) under H₀ of homoskedasticity.
Returns a HypothesisTests.WhiteTest; call pvalue on the result as usual.
References
White, H. (1980). A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity. Econometrica, 48(4), 817–838.
HARE.durbin_watson_test — Function
durbin_watson_test(X, y; intercept=true) -> DurbinWatsonTest
durbin_watson_test(formula, data) -> DurbinWatsonTestDurbin-Watson test for first-order serial correlation in OLS residuals.
The test statistic DW = Σ(eₜ − eₜ₋₁)² / Σeₜ² lies in [0, 4]; values near 2 indicate no autocorrelation, near 0 positive, near 4 negative.
P-values use Pan's exact algorithm for n < 100 and a normal approximation otherwise. One-sided p-values can be obtained with pvalue(t; tail = :right) (positive autocorrelation) or pvalue(t; tail = :left) (negative autocorrelation).
Returns a HypothesisTests.DurbinWatsonTest; call pvalue on the result as usual.
Note: the test is invalid if X contains a lagged dependent variable.
References
Durbin, J., & Watson, G. S. (1951). Testing for serial correlation in least squares regression, II. Biometrika, 38(1–2), 159–177.
HARE.breusch_godfrey_test — Function
breusch_godfrey_test(X, y, lag; intercept=true) -> BreuschGodfreyTest
breusch_godfrey_test(formula, data, lag) -> BreuschGodfreyTestBreusch-Godfrey LM test for serial correlation up to order lag.
Unlike the Durbin-Watson test, this test remains valid when X contains lagged dependent variables. The LM statistic is distributed as χ²(lag) under H₀ of no serial correlation.
Returns a HypothesisTests.BreuschGodfreyTest; call pvalue on the result as usual.
References
Breusch, T. S. (1978). Testing for autocorrelation in dynamic linear models. Australian Economic Papers, 17(31), 334–355.
Godfrey, L. G. (1978). Testing against general autoregressive and moving average error models when the regressors include lagged dependent variables. Econometrica, 46(6), 1293–1301.
Model Comparison Tests
HARE.wald_test — Function
wald_test(m, R, r = zeros(size(R, 1))) -> WaldTestResultWald test for the linear restriction H0: R*beta = r.
R is a q x k restriction matrix; r is a q-vector (default: zero vector). The test statistic F = (Rbeta_hat - r)' * (RVR')^(-1) * (Rbeta_hat - r) / q is compared to F(q, n-k).
StatsModels.lrtest — Function
lrtest(m_r, m_u) -> LRTestResultLikelihood ratio test comparing two nested Beach-MacKinnon models.
m_r is the restricted (fewer parameters) model; m_u is the unrestricted model. LR = 2*(Lu - Lr) ~ chi^2(q) where q = dof(mu) - dof(mr).