Econometrics · Week 12
Nonstationary time series非平穩時間序列
Week 11 showed that OLS and its tests work in large samples, provided the series are stationary. Many economic series are not: GDP, prices and stock indices drift upwards and never settle. This week distinguishes two kinds of nonstationarity, a deterministic trend and a unit root, shows how to tell them apart, and how to transform each so the tools of the course apply again.
第 11 週說明了只要序列是平穩的,OLS 及其檢定在大樣本中便有效。但很多經濟序列並不平穩:國內生產總值、物價和股市指數都向上漂移,永不穩定下來。本週區分兩種非平穩性:確定性趨勢和單位根;說明怎樣分辨兩者,以及怎樣轉換各自的序列,令本課程的工具再次適用。
- Reading time 閱讀時間
- About 40 minutes
- Data 數據
nyse,volat- Code 程式碼
- R, Python
Nonstationary time series非平穩時間序列
Recall from Week 10 that a series is (weakly, or covariance) stationary if its mean , its variance and its autocovariances are finite and do not change over time. Any series with a trend breaks the first condition. There are two kinds of trend:
回顧第 10 週,如果序列的均值 、方差 和自協方差 都有限而且不隨時間改變,它便是(弱或協方差)平穩的。任何有趨勢的序列都違反第一個條件。趨勢有兩種:
They can look alike in a chart, but they behave differently and need different treatment. Getting it wrong matters: regressing one unit-root series on another unrelated one can produce a large t statistic and a high R² from nothing, a spurious regression.
兩者在圖上可能相似,但性質不同,需要不同的處理。處理錯誤的後果很嚴重:把一個單位根序列對另一個毫不相關的單位根序列回歸,可能憑空得出很大的 t 統計量和很高的 R²,即虛假回歸。
Deterministic trends and detrending確定性趨勢與去趨勢
In the deterministic trend model, with stationary,
在確定性趨勢模型中,若 是平穩的,
so the mean changes with and is nonstationary. But the deviations from the trend, , are stationary. Such a series is called trend stationary: after a shock it returns to its trend line. It is handled as in Week 10, by including as a regressor; OLS and the usual tests then remain valid.
所以均值隨 改變, 是非平穩的。但偏離趨勢的部分 是平穩的。這樣的序列稱為趨勢平穩:受衝擊後會回到趨勢線上。處理方法與第 10 週相同,把 作為解釋變數;OLS 和一般的檢定仍然有效。
Detrending makes the check concrete. Regress on a constant and and keep the residuals,
去趨勢令檢查變得具體。把 對常數項和 回歸,保留殘差:
If the detrended series looks stationary, returning to zero with autocorrelations that die out quickly, is trend stationary. If still wanders, with a first autocorrelation near 1 and a correlogram that decays slowly, has a unit root, whether or not it also has a deterministic trend.
如果去趨勢後的序列 看來是平穩的,即會回到零而且自相關很快消失, 便是趨勢平穩的。如果 仍然游走,一階自相關接近 1,相關圖緩慢遞減,那麼無論 是否同時有確定性趨勢,它都有單位根。
Unit roots單位根
The random walk , with , is the AR(1) of Week 10 with . It is said to have a unit root: the coefficients on the lagged dependent variable sum to one. Substituting backwards to a starting value ,
隨機漫步 (其中 )就是第 10 週 的 AR(1)。它被稱為有單位根:滯後被解釋變數的係數之和等於一。向後代入至起始值 :
Every shock stays in the series forever. Given , but , which grows without limit, so the random walk is nonstationary even though its mean is constant. Adding a drift gives both kinds of trend at once:
每個衝擊都永遠留在序列之中。給定 ,,但 會無限增大,所以即使均值不變,隨機漫步也是非平穩的。加入漂移項 ,便同時有兩種趨勢:
with and . Removing the deterministic part still leaves the random walk : detrending cannot make it stationary.
其中 ,。去除確定性部分 後,仍然剩下隨機漫步 :去趨勢無法令它平穩。
Why does a unit root show up as a correlogram stuck near 1? Between two points periods apart, the random walk shares all the shocks up to the earlier one:
為甚麼單位根會表現為一直接近 1 的相關圖?相隔 期的兩點,共享直至較早一點為止的所有衝擊:
which is close to 1 when is large relative to . So the sample autocorrelations of a unit-root series start near 1 and fall very slowly, and the partial autocorrelation has a single spike near 1 at lag 1.
當 遠大於 時,它接近 1。所以單位根序列的樣本自相關由接近 1 開始,下降得非常緩慢,而偏自相關只在滯後 1 期有一個接近 1 的尖峰。
Show every step: the autocorrelation of a random walk 顯示完整推導:隨機漫步的自相關
Take as given and . Write . The bracket contains shocks after , independent of , so
把 視為給定,並設 。寫成 。括號內是 之後的衝擊,與 獨立,所以
The correlation depends on , not only on the gap : one more way the random walk breaks stationarity.
相關係數取決於 ,而不只取決於間隔 :這是隨機漫步違反平穩性的另一方面。
Differencing差分
A unit root is removed by differencing. For the random walk with drift,
單位根可以透過差分去除。對帶漂移的隨機漫步:
so , and : the differenced series is stationary. Differencing removes the deterministic trend too, which becomes the constant mean . A series that is stationary after differencing once is called integrated of order one, I(1); a stationary series is I(0).
所以 、,而 :差分後的序列是平穩的。差分同時去除了確定性趨勢,它變成常數均值 。差分一次後便平穩的序列稱為一階整合,記作 I(1);平穩序列則是 I(0)。
For a series in logs, the difference is the growth rate: is approximately the percentage change from to . This is usually the quantity of interest anyway: returns rather than the level of a stock index, growth rather than the level of GDP.
對於取了對數的序列,差分就是增長率: 大約是由 至 的百分比變化。這通常本來就是我們關心的量:關心股市指數的回報而非水平,關心國內生產總值的增長而非水平。
Detrend or difference?去趨勢還是差分?
In practice, look at the series, detrend it, and look at the correlograms:
實際操作時,觀察序列,去趨勢,再看相關圖:
Plot the series. Does it return to a fixed level or a straight trend line, or does it wander off and stay away?
畫出序列。它會回到固定水平或一條直線趨勢上,還是游走開去並一直遠離?
Detrend it if it trends, and look at the residuals and their correlogram.
如有趨勢便去趨勢,然後觀察殘差及其相關圖。
A first autocorrelation close to 1, decaying very slowly, points to a unit root: difference the series. Autocorrelations that die out quickly point to trend stationarity: keep the levels and include a trend.
一階自相關接近 1 並且非常緩慢地遞減,表示有單位根:應做差分。自相關很快消失,則表示趨勢平穩:保留水平值並加入趨勢。
Correlogram of what is plotted 圖中序列的相關圖
- The series is 序列屬於
- –
- –
- –
The series is with , 200 periods and the same shocks throughout. For it is trend stationary: detrending leaves a stationary series. At it is a random walk with drift: the detrended series still wanders and its correlogram stays high, while the differenced series is stationary. Difference a trend-stationary series with and the first autocorrelation drops to about −0.5.
序列為 ,其中 ,共 200 期,衝擊保持不變。當 時,序列是趨勢平穩的:去趨勢後得到平穩序列。當 時,它是帶漂移的隨機漫步:去趨勢後的序列仍然游走,相關圖一直維持在高位,而差分後的序列則是平穩的。如果把 的趨勢平穩序列差分,一階自相關會降至約 −0.5。
Example: the stock market index例子:股市指數
The nyse data from Week 11 also record the level of the New York Stock Exchange index each week from 1976 to 1989, 691 weeks. The index climbs from about 50 to a peak near 187 with long rises and falls, never returning to any fixed level, and not along a straight trend either:
第 11 週的 nyse 數據同時記錄了 1976 至 1989 年每週的紐約證券交易所指數水平,共 691 週。指數由約 50 升至接近 187 的高峰,期間有長時間的升跌,從不回到任何固定水平,也不是沿著一條直線趨勢:
The October 1987 crash is the deep spike in the returns panel.
回報圖中最深的尖峰是 1987 年 10 月的股災。
| Lag 滯後期 | 1 | 2 | 3 | 6 | 12 |
|---|---|---|---|---|---|
| NYSE index 紐約證券交易所指數 | 0.995 | 0.989 | 0.983 | 0.967 | 0.934 |
| NYSE return 紐約證券交易所回報 | 0.066 | −0.033 | 0.026 | 0.020 | −0.015 |
| IP, detrended 工業生產(去趨勢) | 0.988 | 0.970 | 0.948 | 0.870 | 0.705 |
| IP growth 工業生產增長 | 0.395 | 0.236 | 0.178 | −0.002 | −0.197 |
Sample autocorrelations at selected lags . The 5% bands are about for the weekly series and for the monthly ones.
選定滯後期 的樣本自相關 。5% 範圍約為週度序列 、月度序列 。
The correlogram of the level confirms a unit root: the first autocorrelation is 0.995, and after 12 weeks it is still 0.934. The partial autocorrelation is 0.995 at lag 1 and within after that, the signature of a random walk.
指數水平的相關圖證實了單位根:一階自相關是 0.995,12 週後仍有 0.934。偏自相關在滯後 1 期為 0.995,之後都在 之內,正是隨機漫步的特徵。
That does not stop the analysis. Investors care about returns, and the weekly log return has autocorrelations close to zero (0.066 at lag 1, the largest in absolute value 0.067 at lag 10). The returns are stationary, and Week 11’s efficient-markets regression was run on exactly this kind of series.
這並不妨礙分析。投資者關心的是回報,而每週對數回報 的自相關接近零(滯後 1 期為 0.066,絕對值最大的是滯後 10 期的 0.067)。回報是平穩的,第 11 週的有效市場回歸正是以這類序列進行。
Example: industrial production例子:工業生產
The volat data include the US industrial production index, monthly from January 1947 to June 1993: 558 months. Its log rises steadily, so it might be trend stationary. Regress on a constant and :
volat 數據包括美國工業生產指數,由 1947 年 1 月至 1993 年 6 月的月度數據,共 558 個月。它的對數穩定上升,所以可能是趨勢平穩的。把 對常數項和 回歸:
Production grew about 0.30% a month, 3.6% a year. But the residuals do not behave like a stationary series. They make long swings: below the trend for most of 1947–1964, above it for almost all of 1965–1979, below again for most of the 1980s. Their autocorrelations are 0.988, 0.970 and 0.948 at lags 1 to 3, still 0.705 after a year. Log industrial production has a unit root as well as a deterministic trend.
生產每月增長約 0.30%,即每年 3.6%。但殘差的表現並不像平穩序列。它們出現長幅度的擺動:1947–1964 年大部分時間低於趨勢,1965–1979 年幾乎一直高於趨勢,1980 年代大部分時間又低於趨勢。滯後 1 至 3 期的自相關為 0.988、0.970 和 0.948,一年後仍有 0.705。工業生產的對數除了確定性趨勢之外,還有單位根。
US industrial production index, January 1947 to June 1993.
美國工業生產指數,1947 年 1 月至 1993 年 6 月。
So model the growth rate, , which removes both. It averages 0.29% a month and its autocorrelations die out within a few months (0.395, 0.236, 0.178, then near zero), apart from a negative value at lag 12. To find an AR model for it, follow Week 10’s recipe. With monthly data, start from an AR(12); drop the least significant lag one at a time until every remaining lag is significant at 5%; then check the residuals for serial correlation. The search drops lags 9, 10, 7, 6, 4, 8, 5 and 2, in that order, and ends at
所以應改為對增長率 建模,這同時去除兩種趨勢。它平均每月 0.29%,自相關在幾個月內便消失(0.395、0.236、0.178,然後接近零),只有滯後 12 期出現一個負值。要為它找一個 AR 模型,可依照第 10 週的方法。月度數據由 AR(12) 開始;每次剔除最不顯著的滯後項,直至餘下的滯後項全部在 5% 水平下顯著;然後檢查殘差是否有序列相關。這個搜索依次剔除滯後 9、10、7、6、4、8、5 和 2 期,最後得到
The Breusch–Godfrey test with 12 lags gives 20.22 against a 5% critical value of 21.03 (p = 0.063): no significant serial correlation remains. The implied mean growth rate is a month, close to the sample average. The strong negative coefficient at lag 12 is probably a by-product of seasonal adjustment, which can leave a trace one year apart.
12 個滯後期的 Breusch–Godfrey 檢定得出 20.22,而 5% 臨界值為 21.03(p = 0.063):已沒有顯著的序列相關。隱含的平均增長率為每月 ,接近樣本平均。滯後 12 期的顯著負係數,很可能是季節調整的副產品,季節調整可能在相隔一年處留下痕跡。
Reproduce the results自己重現結果
Both datasets come with the free wooldridge package. Both snippets reproduce every figure on this page.
兩個數據集都收錄在免費的 wooldridge 套件中。兩段程式碼都會重現本頁的所有數字。
library(wooldridge)
# Example 1: the NYSE index, weekly, 1976-1989
price <- nyse$price
acf(price, lag.max = 12) # 0.995, 0.989, 0.983, ... decaying slowly
pacf(price, lag.max = 12) # 0.995, then close to zero
ret <- 100 * diff(log(price)) # weekly log returns, %
acf(ret, lag.max = 12) # 0.066, -0.033, ...: no persistence
# Example 2: US industrial production, monthly, 1947-1993
lip <- log(volat$ip); t <- seq_along(lip)
trend <- lm(lip ~ t) # 0.298% a month
acf(resid(trend), lag.max = 12) # 0.988, 0.970, 0.948, ...: unit root
g <- 100 * diff(lip) # monthly growth, %
acf(g, lag.max = 12) # 0.395, 0.236, 0.178, ...
# General to specific: start from an AR(12), drop the least significant lag
D <- embed(g, 13) # column 1 is g_t, column j + 1 is g_{t-j}
D <- setNames(as.data.frame(D), c("g", paste0("g", 1:12)))
keep <- paste0("g", 1:12)
repeat {
m <- lm(reformulate(keep, "g"), data = D)
p <- coef(summary(m))[-1, 4]
if (max(p) < 0.05) break
keep <- setdiff(keep, names(which.max(p)))
}
summary(m) # lags 1, 3, 11, 12
bg <- function(m, q) { # Breusch-Godfrey, (n - q) R^2
e <- resid(m); n <- length(e)
lags <- sapply(1:q, function(j) c(rep(NA, j), e[1:(n - j)]))
aux <- lm(e ~ model.matrix(m)[, -1] + lags)
s <- (n - q) * summary(aux)$r.squared
c(BG = s, p = pchisq(s, q, lower.tail = FALSE))
}
bg(m, 12) # 20.22, p = 0.063
# pip install wooldridge statsmodels
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
from statsmodels.tsa.stattools import acf, pacf
import wooldridge as wd
# Example 1: the NYSE index, weekly, 1976-1989
price = wd.data("nyse")["price"].to_numpy()
print(acf(price, nlags=12)[1:].round(3)) # 0.995, 0.989, 0.983, ... decaying slowly
print(pacf(price, nlags=12, method="ldb")[1:].round(3)) # 0.995, then close to zero
ret = 100 * np.diff(np.log(price)) # weekly log returns, %
print(acf(ret, nlags=12)[1:].round(3)) # 0.066, -0.033, ...: no persistence
# Example 2: US industrial production, monthly, 1947-1993
lip = np.log(wd.data("volat")["ip"].to_numpy())
t = np.arange(1, len(lip) + 1)
trend = smf.ols("lip ~ t", data={"lip": lip, "t": t}).fit() # 0.298% a month
print(acf(trend.resid, nlags=12)[1:].round(3)) # 0.988, 0.970, 0.948, ...: unit root
g = 100 * np.diff(lip) # monthly growth, %
print(acf(g, nlags=12)[1:].round(3)) # 0.395, 0.236, 0.178, ...
# General to specific: start from an AR(12), drop the least significant lag
D = pd.DataFrame({"g": g})
for j in range(1, 13):
D[f"g{j}"] = D["g"].shift(j)
D = D.dropna()
keep = [f"g{j}" for j in range(1, 13)]
while True:
m = smf.ols("g ~ " + " + ".join(keep), data=D).fit()
p = m.pvalues.drop("Intercept")
if p.max() < 0.05:
break
keep.remove(p.idxmax())
print(m.params.round(4)) # lags 1, 3, 11, 12
def bg(m, q): # Breusch-Godfrey, (n - q) R^2
e = m.resid.to_numpy()
lags = np.column_stack([np.r_[np.full(j, np.nan), e[:-j]] for j in range(1, q + 1)])
ok = ~np.isnan(lags).any(axis=1)
Z = np.column_stack([m.model.exog, lags])[ok]
r = e[ok] - Z @ np.linalg.lstsq(Z, e[ok], rcond=None)[0]
return ok.sum() * (1 - (r @ r) / ((e[ok] - e[ok].mean()) ** 2).sum())
print(bg(m, 12)) # 20.22, p = 0.063
Check your understanding自我測驗
-
For a random walk with and , find and .
對於隨機漫步 ,、,求 和 。
Answer 答案
. .
。。
-
A series trends upwards. After detrending, its first autocorrelation is 0.45 and the correlogram is inside the bands from lag 3 on. Detrend or difference?
一個序列向上趨勢。去趨勢後,一階自相關為 0.45,相關圖由滯後 3 期起都在範圍之內。應去趨勢還是差分?
Answer 答案
Detrend: the deviations from trend are stationary, so the series is trend stationary. Model it in levels with a time trend. Differencing it would introduce negative serial correlation.
去趨勢:偏離趨勢的部分是平穩的,所以序列是趨勢平穩的。應以水平值加上時間趨勢建模。差分反而會引入負序列相關。
-
If with white-noise , what are the mean and the first autocorrelation of ?
如果 , 是白噪音, 的均值和一階自相關是多少?
Answer 答案
: mean 0.2, first autocorrelation . Differencing kept the growth rate but created serial correlation.
:均值 0.2,一階自相關 。差分保留了增長率,卻製造了序列相關。
-
Why can Week 11’s large-sample results not be used for a regression of the NYSE index level on its own lag?
為甚麼第 11 週的大樣本結果,不能用於把紐約證券交易所指數水平對其自身滯後項回歸?
Answer 答案
They require stationary, weakly dependent data. The index has a unit root: its variance grows over time and its autocorrelations stay near 1, so neither condition holds and the usual t statistics do not follow their usual distributions. Returns, its first difference, are fine.
那些結果要求數據平穩且弱相依。指數有單位根:方差隨時間增大,自相關一直接近 1,兩個條件都不成立,一般的 t 統計量也不服從一般的分佈。回報(即其一階差分)則沒有問題。
-
The lecture’s quarterly model is 100Δlog(GDP)ₜ = 0.49 + 0.36 · 100Δlog(GDP)ₜ₋₁. What is the implied average annual growth rate?
課堂的季度模型為 100Δlog(GDP)ₜ = 0.49 + 0.36 · 100Δlog(GDP)ₜ₋₁。隱含的平均年增長率是多少?
Answer 答案
The mean quarterly growth rate is , so about a year.
平均季度增長率為 ,所以每年約為 。
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, 11.3 and 18.2.
- Data: nyse and volat, distributed with the textbook and in the wooldridge R package. All estimates on this page were computed from them. The demo is simulated.
Any errors are our own.