Serial correlation序列相關

In a time series, this month is rarely independent of last month. If more accidents than expected happened in March, more than expected probably happened in April too. Week 8 dealt with errors whose variances differ; this week deals with errors that are correlated with each other over time. The plan is the same: what goes wrong, how to see it, how to test for it, and how to repair the standard errors.

在時間序列中,這個月的情況很少與上個月無關。如果三月份的交通意外比預期多,四月份很可能也比預期多。第 8 週處理方差各不相同的誤差;本週處理隨時間互相相關的誤差。思路一樣:會出現甚麼問題、怎樣觀察、怎樣檢定,以及怎樣修正標準誤。

Builds on 先修內容
Week 5, Week 8
Reading time 閱讀時間
About 50 minutes
Data 數據
traffic2
Code 程式碼
R, Python

What serial correlation is甚麼是序列相關

Assumption A4 has two parts. Week 8 relaxed the first, equal variances. This week relaxes the second: no serial correlation, Cov⁡(ut,us∣X)=0\operatorname{Cov}(u_t, u_s \mid \mathbf{X}) = 0 for t≠st \neq s. We write tt rather than ii because the observations are periods in time, in order. When the errors are serially correlated (or autocorrelated), the off-diagonal entries of their covariance matrix are no longer zero:

假設 A4 有兩部分。第 8 週放寬了第一部分,即方差相等。本週放寬第二部分:沒有序列相關,即對 t≠st \neq s 有 Cov⁡(ut,us∣X)=0\operatorname{Cov}(u_t, u_s \mid \mathbf{X}) = 0。這裏用 tt 而不是 ii,因為觀測值是按時間順序排列的時期。當誤差序列相關(或自相關)時,其協方差矩陣的非對角元素不再是零:

Var⁡(u∣X)=Ω=[σ12σ12⋯σ1nσ12σ22⋯σ2n⋮⋮⋱⋮σ1nσ2n⋯σn2].\begin{aligned} \operatorname{Var}(\mathbf{u} \mid \mathbf{X}) &= \boldsymbol{\Omega} \\ &= \begin{bmatrix} \sigma_1^2 & \sigma_{12} & \cdots & \sigma_{1n} \\ \sigma_{12} & \sigma_2^2 & \cdots & \sigma_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ \sigma_{1n} & \sigma_{2n} & \cdots & \sigma_n^2 \end{bmatrix}. \end{aligned}

In cross sections drawn at random, there is no natural order and no reason for one person’s error to predict another’s. In time series it is the rule rather than the exception: shocks such as a wet winter, a recession or a new law last longer than one period, and anything the model leaves out that moves slowly over time ends up in several consecutive errors.

在隨機抽取的橫截面數據中,觀測值沒有自然的次序,一個人的誤差也沒有理由能預測另一個人的誤差。但在時間序列中,序列相關是常態而非例外:多雨的冬季、經濟衰退或新法例等衝擊都會持續超過一個時期;模型遺漏的任何緩慢變化的因素,都會同時出現在連續幾期的誤差之中。

A model of it: AR(1) errors一個模型:AR(1) 誤差

The simplest model of serial correlation is the first-order autoregressive process, AR(1): each error carries over a fraction ϕ\phi of the previous one and adds a fresh shock.

最簡單的序列相關模型是一階自回歸過程 AR(1):每期誤差承接上一期誤差的 ϕ\phi 部分,再加上新的衝擊。

yt=β0+β1xt1+⋯+βkxtk+ut,ut=ϕut−1+et,et∼i.i.d.(0,σe2).\begin{aligned} y_t &= \beta_0 + \beta_1 x_{t1} + \dots + \beta_k x_{tk} + u_t, \\ u_t &= \phi u_{t-1} + e_t, \qquad e_t \sim \text{i.i.d.}(0, \sigma_e^2). \end{aligned}

If ∣ϕ∣<1|\phi| < 1 the process is stable: its variance does not change over time and shocks fade away. Then, for every lag j≥1j \geq 1,

如果 ∣ϕ∣<1|\phi| < 1,過程是穩定的:方差不隨時間改變,衝擊會逐漸消失。於是對每個滯後期 j≥1j \geq 1,

Var⁡(ut)=σe21−ϕ2,Cov⁡(ut,ut−j)=ϕjσe21−ϕ2,\operatorname{Var}(u_t) = \frac{\sigma_e^2}{1 - \phi^2}, \qquad \operatorname{Cov}(u_t, u_{t-j}) = \frac{\phi^j \sigma_e^2}{1 - \phi^2},

so the correlation between errors jj periods apart is Corr⁡(ut,ut−j)=ϕj\operatorname{Corr}(u_t, u_{t-j}) = \phi^j. Unless ϕ=0\phi = 0, every pair of errors is correlated, and A4(b) fails. The correlation decays geometrically with distance: with ϕ=0.7\phi = 0.7 it is 0.7 one period apart, 0.49 two periods apart and 0.03 ten periods apart.

所以相隔 jj 期的誤差之間的相關係數是 Corr⁡(ut,ut−j)=ϕj\operatorname{Corr}(u_t, u_{t-j}) = \phi^j。除非 ϕ=0\phi = 0,否則每一對誤差都相關,A4(b) 不成立。相關程度隨距離以幾何級數遞減:當 ϕ=0.7\phi = 0.7 時,相隔一期為 0.7,相隔兩期為 0.49,相隔十期為 0.03。

Show every step 顯示完整推導

Variance. Stability means Var⁡(ut)=Var⁡(ut−1)=σu2\operatorname{Var}(u_t) = \operatorname{Var}(u_{t-1}) = \sigma_u^2. The new shock ete_t is independent of the past, so of ut−1u_{t-1}:

方差。穩定表示 Var⁡(ut)=Var⁡(ut−1)=σu2\operatorname{Var}(u_t) = \operatorname{Var}(u_{t-1}) = \sigma_u^2。新衝擊 ete_t 與過去獨立,所以也與 ut−1u_{t-1} 獨立:

σu2=Var⁡(ϕut−1+et)=ϕ2σu2+σe2⟹σu2=σe21−ϕ2.\begin{aligned} \sigma_u^2 &= \operatorname{Var}(\phi u_{t-1} + e_t) \\ &= \phi^2 \sigma_u^2 + \sigma_e^2 \\ \Longrightarrow \quad \sigma_u^2 &= \frac{\sigma_e^2}{1 - \phi^2}. \end{aligned}

This is positive only if ϕ2<1\phi^2 < 1, which is why stability needs ∣ϕ∣<1|\phi| < 1.

只有當 ϕ2<1\phi^2 < 1 時它才是正數,這正是穩定性需要 ∣ϕ∣<1|\phi| < 1 的原因。

Covariance. Substitute the AR(1) equation into itself jj times:

協方差。把 AR(1) 方程代入自身 jj 次:

ut=ϕut−1+et=ϕ2ut−2+ϕet−1+et=ϕjut−j+∑i=0j−1ϕiet−i.\begin{aligned} u_t &= \phi u_{t-1} + e_t \\ &= \phi^2 u_{t-2} + \phi e_{t-1} + e_t \\ &= \phi^j u_{t-j} + \sum_{i=0}^{j-1} \phi^i e_{t-i}. \end{aligned}

The shocks et,…,et−j+1e_t, \dots, e_{t-j+1} all come after ut−ju_{t-j} and are independent of it, so only the first term contributes:

衝擊 et,…,et−j+1e_t, \dots, e_{t-j+1} 都發生在 ut−ju_{t-j} 之後,與它獨立,所以只有第一項有貢獻:

Cov⁡(ut,ut−j)=ϕjVar⁡(ut−j)=ϕjσe21−ϕ2.\begin{aligned} \operatorname{Cov}(u_t, u_{t-j}) &= \phi^j \operatorname{Var}(u_{t-j}) \\ &= \frac{\phi^j \sigma_e^2}{1 - \phi^2}. \end{aligned}

Dividing by σu2\sigma_u^2 gives the autocorrelation ϕj\phi^j.

除以 σu2\sigma_u^2 便得到自相關係數 ϕj\phi^j。

Move the slider. At φ = 0 the series is white noise that crosses zero constantly. As φ rises it wanders: long runs above zero, then long runs below. At 0.99 it barely returns to zero in 300 periods, and its correlogram stays high far out.

拖動滑桿。φ = 0 時,序列是不斷穿越零的白噪音。φ 上升時,序列開始游走:長時間高於零,然後長時間低於零。到了 0.99,序列在 300 期內幾乎不回到零,相關圖也一直維持在高位。

The series 序列 utu_t

Its correlogram 相關圖

Sample ACF 樣本自相關Theory ϕj\phi^j±1.96/√n

Lecture values:
Lag-1 sample autocorrelation 一階樣本自相關
–
Lags outside the band 超出範圍的滯後期
–
sd of uₜ, theory uₜ 的理論標準差
–
True se of a mean vs usual 均值的真實標準誤相對一般
–

Each series is ut=ϕut−1+etu_t = \phi u_{t-1} + e_t with the same 300 standard normal shocks ete_t, so only ϕ\phi changes between pictures. The last figure is (1+ϕ)/(1−ϕ)\sqrt{(1+\phi)/(1-\phi)}, the factor derived in Section 3: how much the true standard error of a sample mean exceeds the one that assumes no serial correlation.

每個序列都是 ut=ϕut−1+etu_t = \phi u_{t-1} + e_t,並使用同一組 300 個標準正態衝擊 ete_t,所以圖與圖之間只有 ϕ\phi 改變。最後一個數字是第 3 節推導的因子 (1+ϕ)/(1−ϕ)\sqrt{(1+\phi)/(1-\phi)}:樣本均值的真實標準誤比假設沒有序列相關的標準誤大多少倍。

What it does to OLS序列相關對 OLS 的影響

The consequences mirror those of heteroskedasticity:

其後果與異方差的情況相同:

  1. OLS is still unbiased, as long as A1–A3 hold.

    OLS 仍然無偏,只要 A1–A3 成立。

  2. Its variance is not σ²(X′X)⁻¹ but the sandwich (X′X)−1X′ΩX(X′X)−1(\mathbf{X}\T\mathbf{X})^{-1}\mathbf{X}\T\boldsymbol{\Omega}\mathbf{X}(\mathbf{X}\T\mathbf{X})^{-1}, so the usual standard errors, t tests and F tests are wrong.

    它的方差不是 σ²(X′X)⁻¹,而是三文治形式 (X′X)−1X′ΩX(X′X)−1(\mathbf{X}\T\mathbf{X})^{-1}\mathbf{X}\T\boldsymbol{\Omega}\mathbf{X}(\mathbf{X}\T\mathbf{X})^{-1},所以一般的標準誤、t 檢定和 F 檢定都是錯的。

  3. OLS is no longer BLUE.

    OLS 不再是 BLUE。

Which way are the usual standard errors wrong? Take the simplest case, estimating a mean, so the only regressor is the constant. With AR(1) errors, for large n,

一般標準誤錯在哪個方向?考慮最簡單的情況,估計均值,即唯一的解釋變數是常數。在 AR(1) 誤差下,當 n 很大時,

Var⁡(uˉ)≈σu2n⋅1+ϕ1−ϕ.\operatorname{Var}(\bar u) \approx \frac{\sigma_u^2}{n} \cdot \frac{1 + \phi}{1 - \phi}.

The usual formula is σu2/n\sigma_u^2/n. With ϕ=0.64\phi = 0.64, close to the first autocorrelation in the example below, the factor is 1.64/0.36=4.61.64/0.36 = 4.6: the true variance is 4.6 times what the usual formula says, and the true standard error 4.6=2.1\sqrt{4.6} = 2.1 times as large. Positive serial correlation makes consecutive errors repeat each other, so nn correlated observations carry less information than nn independent ones.

一般公式是 σu2/n\sigma_u^2/n。當 ϕ=0.64\phi = 0.64(接近下文例子中的一階自相關)時,因子是 1.64/0.36=4.61.64/0.36 = 4.6:真實方差是一般公式的 4.6 倍,真實標準誤則是 4.6=2.1\sqrt{4.6} = 2.1 倍。正序列相關令相鄰誤差互相重複,所以 nn 個相關觀測值所含的資訊少於 nn 個獨立觀測值。

Show every step: the variance with AR(1) errors 顯示完整推導:AR(1) 誤差下的方差

Simple regression. The OLS slope is β^1=β1+∑tatut\bh_1 = \beta_1 + \sum_t a_t u_t with at=(xt−xˉ)/SSTxa_t = (x_t - \bar x)/\text{SST}_x. The variance of a sum includes every covariance:

簡單回歸。OLS 斜率是 β^1=β1+∑tatut\bh_1 = \beta_1 + \sum_t a_t u_t,其中 at=(xt−xˉ)/SSTxa_t = (x_t - \bar x)/\text{SST}_x。總和的方差包含所有協方差:

Var⁡(β^1∣x) =∑t∑satasCov⁡(ut,us) =σu2SSTx+2σu2SSTx2∑j=1n−1ϕj×∑t=j+1n(xt−xˉ)(xt−j−xˉ).\begin{aligned} &\operatorname{Var}(\bh_1 \mid \mathbf{x}) \\ &\ = \sum_t \sum_s a_t a_s \operatorname{Cov}(u_t, u_s) \\ &\ = \frac{\sigma_u^2}{\text{SST}_x} + \frac{2\sigma_u^2}{\text{SST}_x^2} \sum_{j=1}^{n-1} \phi^j \\ &\qquad \times \sum_{t=j+1}^{n} (x_t - \bar x)(x_{t-j} - \bar x). \end{aligned}

The first term is the usual formula. The second is extra. If the regressor is itself positively autocorrelated, as trends and most economic series are, and ϕ>0\phi > 0, the inner sums are positive, so the true variance is larger and the usual standard error is too small.

第一項就是一般公式,第二項是額外部分。如果解釋變數本身正自相關(趨勢和大部分經濟序列都是如此),而且 ϕ>0\phi > 0,內層的總和便是正數,所以真實方差較大,一般標準誤偏小。

The mean. Put at=1/na_t = 1/n. There are n−jn - j pairs jj periods apart, so

均值。設 at=1/na_t = 1/n。相隔 jj 期的配對共有 n−jn - j 對,所以

Var⁡(uˉ)=1n2[nσu2+2∑j=1n−1(n−j)ϕjσu2]=σu2n[1+2∑j=1n−1(1−jn)ϕj].\begin{aligned} \operatorname{Var}(\bar u) &= \frac{1}{n^2}\Big[n\sigma_u^2 \\ &\qquad + 2\sum_{j=1}^{n-1} (n - j)\phi^j \sigma_u^2\Big] \\ &= \frac{\sigma_u^2}{n}\Big[1 + 2\sum_{j=1}^{n-1} \Big(1 - \frac{j}{n}\Big)\phi^j\Big]. \end{aligned}

As nn grows, the weights 1−j/n1 - j/n approach 1 and the sum approaches ∑j≥1ϕj=ϕ/(1−ϕ)\sum_{j \geq 1} \phi^j = \phi/(1 - \phi), so the bracket becomes

當 nn 增大,權重 1−j/n1 - j/n 趨向 1,總和趨向 ∑j≥1ϕj=ϕ/(1−ϕ)\sum_{j \geq 1} \phi^j = \phi/(1 - \phi),所以括號變成

1+2ϕ1−ϕ=1+ϕ1−ϕ.1 + \frac{2\phi}{1 - \phi} = \frac{1 + \phi}{1 - \phi}.

The declining weights 1 − j/n reappear in Section 7: the Newey–West estimator uses the same shape.

遞減權重 1 − j/n 會在第 7 節再次出現:Newey–West 估計量正是使用相同形狀的權重。

The line graph of the residuals殘差的折線圖

Example. The traffic2 data record the number of traffic accidents in California each month from January 1981 to December 1989, 108 months. As in the lecture’s tourism example, regress the log of the monthly total on a linear time trend and eleven month dummies (January is the base):

例子。traffic2 數據記錄了 1981 年 1 月至 1989 年 12 月加州每月的交通意外數目,共 108 個月。與課堂的旅遊例子一樣,把每月總數的對數對線性時間趨勢和十一個月份虛擬變數(一月為基準)做回歸:

log⁡(totacct)=β0+β1t+∑m=212δmDmt+ut.\log(\text{totacc}_t) = \beta_0 + \beta_1 t + \sum_{m=2}^{12} \delta_m D_{mt} + u_t.

The trend is β^1=0.00275\bh_1 = 0.00275 per month, about 3.4% more accidents a year, and R2=0.797R^2 = 0.797. We cannot see the errors, but the residuals u^t\uh_t stand in for them. Plot them in time order:

趨勢為每月 β^1=0.00275\bh_1 = 0.00275,即每年交通意外約多 3.4%,R2=0.797R^2 = 0.797。我們看不到誤差,但可以用殘差 u^t\uh_t 代替。按時間順序把它們畫出來:

−0.15−0.10−0.050.000.050.100.151981’821983’841985’861987’881989 Month

Above the fitted value 高於擬合值Below 低於擬合值

The residuals move in long waves: positive in 11 of the 12 months of 1981, negative in most months of 1982 to 1984, positive in every month of 1986 and 1987, and negative in every month of 1989. Independent errors would cross zero about every second month. These residuals change sign only 25 times in 107 steps, compared with about 53 expected without serial correlation.

殘差呈長波動:1981 年 12 個月中有 11 個月為正,1982 至 1984 年大部分月份為負,1986 及 1987 年每個月都為正,1989 年每個月都為負。獨立的誤差大約每兩個月便會穿越零一次。這些殘差在 107 步中只改變正負號 25 次,而沒有序列相關時預期約為 53 次。

The correlogram相關圖

A correlogram puts numbers on the picture. The autocorrelation function (ACF) gives the sample correlation between the residuals and their own past values jj periods earlier:

相關圖把圖像量化。自相關函數(ACF)給出殘差與 jj 期前自身數值之間的樣本相關係數:

ρ^j=Corr⁡^(u^t,u^t−j),j=1,2,…\hat\rho_j = \widehat{\operatorname{Corr}}(\uh_t, \uh_{t-j}), \qquad j = 1, 2, \dots

If the errors are not serially correlated, each ρ^j\hat\rho_j is approximately normal with mean zero and standard deviation 1/n1/\sqrt{n}. So ρ^j\hat\rho_j outside ±1.96/n\pm 1.96/\sqrt{n} rejects H0:ρj=0H_0: \rho_j = 0 at the 5% level.

如果誤差沒有序列相關,每個 ρ^j\hat\rho_j 大約服從均值為零、標準差為 1/n1/\sqrt{n} 的正態分佈。所以 ρ^j\hat\rho_j 落在 ±1.96/n\pm 1.96/\sqrt{n} 之外,便在 5% 水平下拒絕 H0:ρj=0H_0: \rho_j = 0。

The partial autocorrelation function (PACF) asks a sharper question: does lag jj help once lags 1 to j−1j - 1 are accounted for? Its value at lag jj is the last coefficient ϕ^j\hat\phi_{j} in the regression of u^t\uh_t on u^t−1,…,u^t−j\uh_{t-1}, \dots, \uh_{t-j}:

偏自相關函數(PACF)問一個更精確的問題:在考慮了滯後 1 至 j−1j - 1 期之後,滯後 jj 期是否仍有幫助?它在滯後 jj 的值,就是把 u^t\uh_t 對 u^t−1,…,u^t−j\uh_{t-1}, \dots, \uh_{t-j} 回歸時最後一個係數 ϕ^j\hat\phi_{j}:

ut=ϕ1ut−1+et,ut=ϕ1ut−1+ϕ2ut−2+et,ut=ϕ1ut−1+ϕ2ut−2+ϕ3ut−3+et, …\begin{aligned} u_t &= \phi_1 u_{t-1} + e_t, \\ u_t &= \phi_1 u_{t-1} + \phi_2 u_{t-2} + e_t, \\ u_t &= \phi_1 u_{t-1} + \phi_2 u_{t-2} + \phi_3 u_{t-3} + e_t, \ \dots \end{aligned}

For an AR(1) process the ACF decays gradually, as φʲ, but the PACF cuts off after lag 1: once last period is known, earlier periods add nothing. In general an AR(p) process has a PACF that cuts off after lag p, which makes the PACF the tool for choosing p.

AR(1) 過程的 ACF 按 φʲ 逐漸遞減,但 PACF 在滯後 1 期之後便截斷:知道上一期之後,更早的時期不再提供額外資訊。一般而言,AR(p) 過程的 PACF 在滯後 p 期之後截斷,所以 PACF 是選擇 p 的工具。

Autocorrelation (ACF)−0.20.00.20.40.60.8123456789101112 Partial autocorrelation (PACF)−0.20.00.20.40.60.8123456789101112 Lag (months)

The shaded band is ±1.96/n=±0.189\pm 1.96/\sqrt{n} = \pm 0.189. Bars that leave it are drawn in a darker colour.

陰影帶為 ±1.96/n=±0.189\pm 1.96/\sqrt{n} = \pm 0.189。超出範圍的柱以較深顏色顯示。

All twelve autocorrelations lie outside the bands, starting at 0.644 and declining slowly, so we reject H0:ρj=0H_0: \rho_j = 0 for every j=1,…,12j = 1, \dots, 12. The partial autocorrelations at lags 1 (0.644) and 2 (0.354) are outside; lag 3 (0.139) and beyond are inside. This suggests the errors follow an AR(2) process,

全部十二個自相關係數都落在範圍之外,由 0.644 開始緩慢下降,所以對每個 j=1,…,12j = 1, \dots, 12 都拒絕 H0:ρj=0H_0: \rho_j = 0。偏自相關係數在滯後 1 期(0.644)和 2 期(0.354)落在範圍之外;滯後 3 期(0.139)及以後都在範圍之內。這表示誤差服從 AR(2) 過程:

ut=ϕ1ut−1+ϕ2ut−2+et.u_t = \phi_1 u_{t-1} + \phi_2 u_{t-2} + e_t.

The Breusch–Godfrey testBreusch–Godfrey 檢定

The Breusch–Godfrey (BG) test checks all lags up to qq at once. Suppose the errors follow an AR(qq) process,

Breusch–Godfrey(BG)檢定一次過檢查直至 qq 期的所有滯後期。假設誤差服從 AR(qq) 過程,

ut=ϕ1ut−1+⋯+ϕqut−q+et,u_t = \phi_1 u_{t-1} + \dots + \phi_q u_{t-q} + e_t,

and test H0:ϕ1=⋯=ϕq=0H_0: \phi_1 = \dots = \phi_q = 0 against the alternative that at least one is not zero. Choose qq to match the frequency of the data: 1 or 2 for annual data, 4 for quarterly, 12 for monthly, so that the test can pick up a relation with the same season last year.

並檢定 H0:ϕ1=⋯=ϕq=0H_0: \phi_1 = \dots = \phi_q = 0,備擇假設是至少有一個不為零。qq 應配合數據頻率:年度數據取 1 或 2,季度取 4,月度取 12,讓檢定能捕捉與去年同一季節的關係。

  1. Fit the model 估計模型

    Run OLS and save the residuals u^t\uh_t.

    進行 OLS 估計並保存殘差 u^t\uh_t。

  2. Auxiliary regression 輔助回歸

    u^t=α0+α1xt1+⋯+αkxtk+ϕ1u^t−1+⋯+ϕqu^t−q+vt\begin{aligned} \uh_t &= \alpha_0 + \alpha_1 x_{t1} + \dots + \alpha_k x_{tk} \\ &\quad + \phi_1 \uh_{t-1} + \dots + \phi_q \uh_{t-q} + v_t \end{aligned}

    Keep its Ru^2R^2_{\uh}. Including the regressors matters: it makes the test valid even when they are not strictly exogenous.

    記下它的 Ru^2R^2_{\uh}。加入解釋變數很重要:即使它們不是嚴格外生的,檢定仍然有效。

  3. Test statistic 檢定統計量

    BG=(n−q)Ru^2∼aχq2 under H0.\text{BG} = (n - q) R^2_{\uh} \overset{a}{\sim} \chi^2_q \text{ under } H_0.
  4. Decide 作出結論

    Reject if BG exceeds the χq2\chi^2_q critical value. Equivalently, use the usual F test of the qq lag coefficients.

    如果 BG 超過 χq2\chi^2_q 的臨界值便拒絕。也可以對 qq 個滯後係數做一般的 F 檢定。

Why n − q? The first qq periods have no complete set of lagged residuals: u^0\uh_0 does not exist, so period 1 has no first lag, period 2 no second lag, and so on. The auxiliary regression uses only periods q+1q + 1 to nn. Some software instead sets the missing lags to zero, keeps all nn periods and reports nRu^2nR^2_{\uh}. That changes both nn and R2R^2, but both versions have the same large-sample distribution.

為甚麼用 n − q?最初 qq 期沒有完整的滯後殘差:u^0\uh_0 並不存在,所以第 1 期沒有一階滯後,第 2 期沒有二階滯後,如此類推。輔助回歸只用第 q+1q + 1 至第 nn 期。有些軟件則把缺失的滯後值設為零,保留全部 nn 期並報告 nRu^2nR^2_{\uh}。這會同時改變 nn 和 R2R^2,但兩個版本在大樣本中的分佈相同。

Example. With monthly data, take q=12q = 12. The auxiliary regression of the residual on the trend, the eleven month dummies and twelve lagged residuals uses 108−12=96108 - 12 = 96 months and has Ru^2=0.580R^2_{\uh} = 0.580:

例子。月度數據取 q=12q = 12。把殘差對趨勢、十一個月份虛擬變數和十二個滯後殘差回歸,使用 108−12=96108 - 12 = 96 個月,Ru^2=0.580R^2_{\uh} = 0.580:

BG=96×0.580=55.66>21.03=χ12, 0.052.\begin{aligned} \text{BG} &= 96 \times 0.580 = 55.66 \\ &> 21.03 = \chi^2_{12,\,0.05}. \end{aligned}

The p-value is 1.4×10−71.4 \times 10^{-7}. The zero-filled version, which Python’s acorr_breusch_godfrey() and R’s lmtest::bgtest() report, gives 108×0.533=57.55108 \times 0.533 = 57.55: same conclusion. The result does not hinge on qq:

p 值為 1.4×10−71.4 \times 10^{-7}。Python 的 acorr_breusch_godfrey() 和 R 的 lmtest::bgtest() 所報告的補零版本得出 108×0.533=57.55108 \times 0.533 = 57.55:結論相同。結果並不取決於 qq:

qqRu^2R^2_{\uh}(n−q)Ru^2(n - q)R^2_{\uh}5% critical value 5% 臨界值
10.48852.173.84
20.56359.705.99
30.57360.127.81
120.58055.6621.03

Every version rejects with a p-value below 0.000001.

所有版本都以低於 0.000001 的 p 值拒絕。

HAC standard errorsHAC 標準誤

As in Week 8, we can keep the OLS estimates and fix only their standard errors. Whitney Newey and Kenneth West (1987) proposed an estimator of the sandwich that is consistent when the errors are serially correlated, heteroskedastic, or both: the HAC (heteroskedasticity and autocorrelation consistent) covariance matrix,

與第 8 週一樣,我們可以保留 OLS 估計值,只修正它們的標準誤。Whitney Newey 和 Kenneth West(1987)提出了一個三文治形式的估計量,在誤差序列相關、異方差或兩者兼有時都具一致性:HAC(異方差及自相關一致)協方差矩陣,

V^HAC=(X′X)−1 S^ (X′X)−1,\hat{\mathbf{V}}_{\text{HAC}} = (\mathbf{X}\T\mathbf{X})^{-1}\,\hat{\mathbf{S}}\,(\mathbf{X}\T\mathbf{X})^{-1},
S^=∑t=1nu^t2xtxt′+∑j=1Lwj∑t=j+1nu^tu^t−j×(xtxt−j′+xt−jxt′),wj=1−jL+1.\begin{aligned} \hat{\mathbf{S}} &= \sum_{t=1}^{n} \uh_t^2 \mathbf{x}_t\mathbf{x}_t\T + \sum_{j=1}^{L} w_j \sum_{t=j+1}^{n} \uh_t \uh_{t-j} \\ &\qquad \times \big(\mathbf{x}_t\mathbf{x}_{t-j}\T + \mathbf{x}_{t-j}\mathbf{x}_t\T\big), \\ w_j &= 1 - \frac{j}{L + 1}. \end{aligned}

The first sum is White’s heteroskedasticity-robust term from Week 8. The second adds the covariances between errors up to LL periods apart, each estimated by products of residuals. The declining Bartlett weights wjw_j give distant lags less say and guarantee that the variance estimate is never negative.

第一個總和是第 8 週 White 的異方差穩健項。第二個總和加入相隔最多 LL 期的誤差之間的協方差,每項都以殘差乘積估計。遞減的 Bartlett 權重 wjw_j 令較遠的滯後期影響較小,並保證方差估計值永不為負。

Choosing L. L must grow with the sample, but slowly. A common rule of thumb is L=⌊4(n/100)2/9⌋L = \lfloor 4(n/100)^{2/9} \rfloor, which gives L=4L = 4 for n=108n = 108. Results should not change much for nearby values.

選擇 L。L 必須隨樣本增大,但要增得慢。常用的經驗法則是 L=⌊4(n/100)2/9⌋L = \lfloor 4(n/100)^{2/9} \rfloor,當 n=108n = 108 時得出 L=4L = 4。在相近的數值之間,結果不應有太大變化。

Dependent variable: log of monthly accidents, n = 108 被解釋變數:每月交通意外數目的對數
EstimateUsualHAC
settsett
t 趨勢0.0027470.00016117.060.0002799.86
feb 二月−0.04270.0244−1.750.0175−2.44
aug 八月0.05400.02452.210.02771.95
dec 十二月0.09620.02453.920.02983.23

Newey–West with L = 4 lags and no small-sample adjustment. The other eight month dummies are in the code output.

Newey–West,L = 4 個滯後期,沒有小樣本調整。其餘八個月份虛擬變數見程式碼輸出。

The trend’s standard error rises by 73%, and its t statistic falls from 17.1 to 9.9. That matches Section 3: a trend is as autocorrelated as a regressor can be, and the errors are positively autocorrelated, so the usual formula understated the uncertainty. The trend is still clearly significant. HAC standard errors are not always larger, though. For February the standard error falls and the effect becomes significant at 5%; for August it rises and the effect is no longer significant at 5%.

趨勢的標準誤增加了 73%,t 統計量由 17.1 降至 9.9。這與第 3 節一致:趨勢是自相關程度最高的解釋變數,而誤差正自相關,所以一般公式低估了不確定性。趨勢仍然明顯顯著。不過 HAC 標準誤並非總是較大。二月份的標準誤下降,效應在 5% 水平下變得顯著;八月份的標準誤上升,效應在 5% 水平下不再顯著。

Two failures of A4 side by side並排比較 A4 的兩種失效

Heteroskedasticity and serial correlation are the two ways A4 can fail. The treatment is parallel:

異方差和序列相關是 A4 失效的兩種方式。處理方法互相對應:

Heteroskedasticity (Week 8) 異方差(第 8 週)Serial correlation (Week 9) 序列相關(第 9 週)
Breaks 違反A4(a): equal variances A4(a):方差相等A4(b): zero covariances A4(b):協方差為零
Typical data 常見數據Cross sections 橫截面數據Time series 時間序列
OLS estimates OLS 估計值Unbiased if A1–A3 hold; no longer BLUE A1–A3 成立時無偏;不再是 BLUE
Look at 觀察Residuals against regressors 殘差對解釋變數Residuals over time; correlogram 殘差隨時間變化;相關圖
Test 檢定Breusch–Pagan, WhiteBreusch–Godfrey
Fix the standard errors 修正標準誤Robust (HC) 穩健(HC)Newey–West (HAC), which also covers heteroskedasticity Newey–West(HAC),同時處理異方差

Reproduce the results自己重現結果

The data come with the free wooldridge package. The R code computes the Breusch–Godfrey test and the Newey–West standard errors by hand; lmtest::bgtest() and sandwich::NeweyWest() give the same results in one line each.

數據收錄在免費的 wooldridge 套件中。R 程式碼親手計算 Breusch–Godfrey 檢定和 Newey–West 標準誤;lmtest::bgtest() 和 sandwich::NeweyWest() 各用一行便可得出相同結果。

R
library(wooldridge)

# Log of monthly traffic accidents in California, 1981-1989
f <- ltotacc ~ t + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec
m <- lm(f, data = traffic2)
summary(m)                                  # trend 0.002747, se 0.000161

# Line graph and correlogram of the residuals
e <- resid(m); n <- length(e)
plot(e, type = "l"); abline(h = 0)
acf(e, lag.max = 12)                        # 0.644, 0.622, 0.558, ...
pacf(e, lag.max = 12)                       # 0.644, 0.354, 0.139, ...

# Breusch-Godfrey with q = 12: lm() drops the 12 rows with missing lags
q <- 12
lags <- sapply(1:q, function(j) c(rep(NA, j), e[1:(n - j)]))
aux <- lm(e ~ model.matrix(m)[, -1] + lags)
bg <- (n - q) * summary(aux)$r.squared
c(bg, qchisq(0.95, q), pchisq(bg, q, lower.tail = FALSE))   # 55.66 > 21.03

# Newey-West (HAC) standard errors with L = 4 lags, by hand
X <- model.matrix(m); L <- 4
S <- crossprod(X * e)
for (j in 1:L) {
  G <- crossprod(X[(j + 1):n, ] * e[(j + 1):n], X[1:(n - j), ] * e[1:(n - j)])
  S <- S + (1 - j / (L + 1)) * (G + t(G))
}
bread <- solve(crossprod(X))
hac <- sqrt(diag(bread %*% S %*% bread))
cbind(usual = coef(summary(m))[, 2], hac, t_hac = coef(m) / hac)
# trend: se 0.000161 -> 0.000279, t 17.06 -> 9.86
# Same as sandwich::NeweyWest(m, lag = 4, prewhite = FALSE)
Python
# pip install wooldridge statsmodels
import numpy as np
import statsmodels.formula.api as smf
from statsmodels.tsa.stattools import acf, pacf
from statsmodels.stats.diagnostic import acorr_breusch_godfrey
import wooldridge as wd

traffic = wd.data("traffic2")                  # monthly, California, 1981-1989
months = " + ".join(["feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"])
m = smf.ols("ltotacc ~ t + " + months, data=traffic).fit()
print(m.params["t"], m.bse["t"])               # 0.002747, se 0.000161

# Correlogram of the residuals
e = m.resid.to_numpy()
n = len(e)
print(acf(e, nlags=12)[1:].round(3))           # 0.644, 0.622, 0.558, ...
print(pacf(e, nlags=12, method="ldb")[1:].round(3))   # 0.644, 0.354, 0.139, ...
print(1.96 / np.sqrt(n))                       # bands at +/- 0.189

# Breusch-Godfrey with q = 12, dropping the first 12 months
q = 12
lags = np.column_stack([np.r_[np.full(j, np.nan), e[:-j]] for j in range(1, q + 1)])
traffic[[f"e_lag{j}" for j in range(1, q + 1)]] = lags
traffic["e"] = e
aux = smf.ols("e ~ t + " + months + " + " + " + ".join(f"e_lag{j}" for j in range(1, q + 1)),
              data=traffic).fit()                # rows with missing lags are dropped
print((n - q) * aux.rsquared)                  # BG = 96 x 0.580 = 55.66
print(acorr_breusch_godfrey(m, nlags=q)[:2])   # zero-filled version: n R^2 = 57.55

# Newey-West (HAC) standard errors with 4 lags
hac = smf.ols("ltotacc ~ t + " + months, data=traffic).fit(cov_type="HAC", cov_kwds={"maxlags": 4})
print(hac.bse["t"], hac.tvalues["t"])          # se 0.000279, t = 9.86

Check your understanding自我測驗

  1. The errors follow an AR(1) process with ϕ=0.5\phi = 0.5 and σe2=3\sigma_e^2 = 3. What are Var⁡(ut)\operatorname{Var}(u_t) and Corr⁡(ut,ut−3)\operatorname{Corr}(u_t, u_{t-3})?

    誤差服從 ϕ=0.5\phi = 0.5、σe2=3\sigma_e^2 = 3 的 AR(1) 過程。Var⁡(ut)\operatorname{Var}(u_t) 和 Corr⁡(ut,ut−3)\operatorname{Corr}(u_t, u_{t-3}) 是多少?

    Answer 答案

    Var⁡(ut)=3/(1−0.25)=4\operatorname{Var}(u_t) = 3/(1 - 0.25) = 4 and Corr⁡(ut,ut−3)=0.53=0.125\operatorname{Corr}(u_t, u_{t-3}) = 0.5^3 = 0.125.

    Var⁡(ut)=3/(1−0.25)=4\operatorname{Var}(u_t) = 3/(1 - 0.25) = 4,Corr⁡(ut,ut−3)=0.53=0.125\operatorname{Corr}(u_t, u_{t-3}) = 0.5^3 = 0.125。

  2. A residual correlogram shows a slowly decaying ACF and a PACF with one large spike at lag 1. What process do the errors seem to follow?

    殘差相關圖顯示 ACF 緩慢遞減,PACF 只在滯後 1 期有一個大尖峰。誤差看來服從甚麼過程?

    Answer 答案

    AR(1): the partial autocorrelations cut off after lag 1, and the autocorrelations decay like φʲ.

    AR(1):偏自相關在滯後 1 期之後截斷,自相關則按 φʲ 遞減。

  3. With quarterly data on n=80n = 80 quarters, a BG test with q=4q = 4 gives an auxiliary R2R^2 of 0.12 on the usable observations. Do you reject at 5%?

    用 n=80n = 80 個季度的季度數據,q=4q = 4 的 BG 檢定在可用觀測值上得出輔助回歸 R2R^2 為 0.12。在 5% 水平下是否拒絕?

    Answer 答案

    BG=(80−4)×0.12=9.12\text{BG} = (80 - 4) \times 0.12 = 9.12, below the χ42\chi^2_4 critical value 9.49. Do not reject at 5%, though it is close (p ≈ 0.058).

    BG=(80−4)×0.12=9.12\text{BG} = (80 - 4) \times 0.12 = 9.12,低於 χ42\chi^2_4 的臨界值 9.49。在 5% 水平下不拒絕,但很接近(p ≈ 0.058)。

  4. A researcher regresses yty_t on yt−1y_{t-1} and finds serially correlated residuals. Why is this worse than serial correlation in a model with only a trend?

    一位研究員把 yty_t 對 yt−1y_{t-1} 回歸,發現殘差序列相關。為甚麼這比只有趨勢的模型出現序列相關更嚴重?

    Answer 答案

    With a trend, OLS is still unbiased and only the standard errors need fixing. With yt−1y_{t-1} as a regressor, a serially correlated error utu_t is correlated with ut−1u_{t-1}, which is part of yt−1y_{t-1}. The regressor is correlated with the error, A3 fails, and the coefficient itself is biased. HAC standard errors cannot fix that.

    在只有趨勢的模型中,OLS 仍然無偏,只需修正標準誤。當 yt−1y_{t-1} 是解釋變數時,序列相關的誤差 utu_t 與 ut−1u_{t-1} 相關,而 ut−1u_{t-1} 是 yt−1y_{t-1} 的一部分。解釋變數與誤差相關,A3 不成立,係數本身便有偏誤。HAC 標準誤無法修正這個問題。

  5. Errors follow an AR(1) process with ϕ=0.8\phi = 0.8. Roughly how many independent observations is a sample of 200 worth when estimating a mean?

    誤差服從 ϕ=0.8\phi = 0.8 的 AR(1) 過程。估計均值時,200 個觀測值大約相當於多少個獨立觀測值?

    Answer 答案

    The variance of the mean is (1+0.8)/(1−0.8)=9(1 + 0.8)/(1 - 0.8) = 9 times the independent-sample formula, so 200 correlated observations are worth about 200/9≈22200/9 \approx 22 independent ones.

    均值的方差是獨立樣本公式的 (1+0.8)/(1−0.8)=9(1 + 0.8)/(1 - 0.8) = 9 倍,所以 200 個相關觀測值大約只相當於 200/9≈22200/9 \approx 22 個獨立觀測值。

Sources and acknowledgement 資料來源與鳴謝

  • Lecture materials for Introductory Econometrics (ETC2410) by Associate Professor Didier Nibbering, Department of Econometrics and Business Statistics, Monash Business School.
  • Jeffrey M. Wooldridge, Introductory Econometrics: A Modern Approach, Chapters 10.5, 12.1–12.3 and 12.5.
  • Whitney K. Newey and Kenneth D. West (1987), “A simple, positive semi-definite, heteroskedasticity and autocorrelation consistent covariance matrix”, Econometrica 55(3), 703–708.
  • Data: traffic2, distributed with the textbook and in the wooldridge R package. All estimates on this page were computed from it.

Any errors are our own.