--- title: 【時間序列分析】Ch4:落後運算元與移動平均模型 image: https://ppt.cc/flVaux@.jpg --- # 【時間序列分析】Ch4:落後運算元與移動平均模型 # 4.1 落後運算子 落後運算子 (Lag operator) 是一個描述時間序列模型很常被使用的一個線性運算子,因為在時間序列分析中很常會要討論資料間「跨期」的關係,因而衍生出落後運算子。 我們定義: $$L^k y_t = y_{t-k}$$ 如此便連接起 $y_t$ 與「落後 $k$ 期」資料間的關係。 ## 4.1.1 落後運算子的基本性質 以下是落後運算子的基本運算: 1. $(L^k+L^j)y_t = L^k y_t + L^j y_t = y_{t-k} + y_{t-j}$ 2. $L^k L^j y_t = L^{k+j}y_t = y_{t-k-j}$ 3. $L^0 y_t=y_t$ 4. $L^{-k}y_t = y_{t+k}$ 5. 若 $c$ 為常數,則 $Lc = c$ 在有這些基本的認識後,舉例來說,差分就可以用落後運算子來表示: - 一階差分: $$\Delta Y_t = Y_t -Y_{t-1} = Y_t - L Y_t=(1-L)Y_t$$ - 與落後 $k$ 期資料的差: $$\Delta_k Y_t = Y_t -Y_{t-k} = Y_t - L^k Y_t=(1-L^k)Y_t$$ ## 4.1.2 落後運算多項式 從上面的例子來看,對於一個時間序列模型,我們可以使用落後運算元將各期資料與第 $t$ 期的資料做連接。 我們可以依著相同的概念定義**落後運算多項式 (lag polynomial)**: $$\phi(L) = 1-\phi_1L-\phi_2L^2-\cdots-\phi_kL^k = 1-\sum_{j=1}^{k}\phi_jL^j$$ 當 $p \to \infty$,此即**無窮期落後運算多項式**: $$\phi(L) = 1-\phi_1L-\phi_2L^2-\cdots = 1-\sum_{j=1}^{\infty}\phi_jL^j$$ :::success 💡 對於無窮期落後運算多項式,若我們說常數 $|\phi| <1$,透過無窮等比級數公式,可得: $$1+\phi L+\phi^2 L^2 + \phi^3 L^3 + \cdots = \frac{1}{1-\phi L}$$ ::: # 4.2 移動平均模型 在介紹**移動平均模型 (moving average model)** 之前,我們先來講講何為[**移動平均**](https://zh.wikipedia.org/zh-tw/%E7%A7%BB%E5%8B%95%E5%B9%B3%E5%9D%87)。 <img style="display: block; margin: auto;" src="https://ppt.cc/flQk7x@.png" width="100%"> <p style="text-align: center;"> <a href="https://www.cmoney.tw/forum/stock/2330?s=technical-analysis ">CMoney</a> 上的台積電股價 K 線圖 </p> 在討論股票價格時,對於每日成交價的視覺化,常用[ k 線圖](https://startingedu.com/how-to-understand-candlestick-charts/)來表示,在各大股票網站與券商 APP 皆會輔以 5 日線、20 日線以及 60 日線來表示股價的趨勢,而所謂「5 日線」即是「5 日簡單移動平均線」的意思。 ## 4.2.1 簡單移動平均 讓我們以 **20 日線**為例來介紹**簡單移動平均 (simple moving average,SMA)**,就是蒐集從今天開始前 20 個交易日的收盤價 $\{ p_t,p_{t-1},p_{t-2},\dots,p_{t- \color{Red}{19} } \}$ 一共 20 筆資料: $$\text{SMA}_{20} = \frac{ p_t+p_{t-1}+p_{t-2}+\dots+p_{t- \color{Red}{19} } }{20}$$ 當時間又過到明天 (新的一個交易日),移動平均就會進行更新,將最舊的一筆資料 ($p_{t-19}$) 移除,並且加入新的一筆資料 ($p_{t+1}$): $$\text{SMA}_{20,\color{Red}{\text{next}}} = \frac{ p_{t \color{Red}{+1} } + p_t+p_{t-1}+p_{t-2}+\dots+p_{t- \color{Red}{18} } }{20}$$ 我們可以將 20 日線的例子一般化成 $n$ 日線: $$\text{SMA}_{n} = \frac{ p_t+p_{t-1}+p_{t-2}+\dots+p_{t- \color{Red}{n+1} } }{n}$$ $n$ 日移動平均的更新過程為: $$\text{SMA}_{n,\color{Red}{\text{next}}} = \text{SMA}_{n} + \frac{1}{n}(p_{t+1}-p_{t-k+1})$$ ## 4.2.2 移動平均模型 移動平均模型 (moving average model,MA model) 是認為當期時間序列的值由「當期隨機衝擊」與「過去所面對之隨機衝擊的加權平均」所組成,描述隨機變數某種誤差修正的能力。移動平均模型也是一個「**定態**」的時間序列模型。 MA 模型可以是 AR 模型的一種拓展,但我們暫時在此打住不提。 **1. 一階移動平均模型 (MA(1) model)** $$Y_t = \mu + e_t + \theta_1 e_{t-1},\ e_t \overset{\mathrm{i.i.d.}}{\sim} WN(0,\sigma^2) $$ 此為 MA(1) 模型,其中 $e_t$ 為嚴格定態並具有遍歷性之白噪音。 接著,我們可以討論 MA(1) 模型 的動差性質與自我共變異數: - 均數 $$\begin{align} E[Y_t] &= E[ \mu + e_t + \theta_1 e_{t-1}]\\ &= \mu +E[e_t] + \theta_1 E[e_{t-1}] \\ &= \mu + 0 + 0 \\ &=\mu \end{align} $$ - 變異數 $$\begin{align} Var[Y_t] &= Var[ \mu + e_t + \theta_1 e_{t-1}]\\ &= E[e_t^2] - 2\theta E[e_t e_{t-1}] + \theta_1^2 E[e_{t-1}^2] \\ &= \sigma^2 - 0 +\theta_1^2 \sigma^2\\ &= (1+\theta_1^2) \sigma^2 \end{align} $$ - 1 階自我共變異數 (Lag-1 autocovariance) $$\begin{align} \gamma(1) &= E[(e_t + \theta_1 e_{t-1})(e_{t-1} + \theta_1 e_{t-2})]\\ &= E[e_t e_{t-1}] +\theta_1E[e_t e_{t-2}] +\theta_1 E[e_{t-1}^2] + \theta_1^2 E[e_{t-1}e_{t-2}]\\ &= \theta_1 \sigma^2 \end{align} $$ 故 $\theta_1$ 的正負號決定了 1 階自我共變異數為正相關或負相關。 - - 1 階自我共相關係數 (Lag-1 autocorrelation) $$\rho(1) = \frac{\gamma{1}}{\gamma{0}} = \frac{\gamma{1}}{Var[Y_t]} = \frac{\theta_1 \sigma^2}{(1+\theta_1^2 )\sigma^2} =\frac{\theta_1 }{1+\theta_1^2 } $$ :::info 📚 $\gamma(k) = \rho(k) =0, \ k \geq 2$ 。 ::: **2. q 階移動平均模型 (MA(q) model)** 在認識 MA(1) 模型後,我們跟著相同的概念,來建立一個 q 階 (有限階) 的移動平均模型: $$Y_t = \mu + \color{red}{\theta_0} e_t + \theta_1 e_{t-1} + \theta_2 e_{t-2}+ \cdots + \theta_q e_{t-q}$$ 通常 $\theta_0 =1$。 MA(q) 模型 的動差性質: - 均數 $$E[Y_t] = \mu$$ 因為 $\{e_t\} \overset{\mathrm{i.i.d.}}{\sim} WN(0,\sigma^2)$。 - 變異數 $$Var[Y_t] = (\sum_{j=0}^q \theta_j^2)\sigma^2$$ 因為「白噪音」是序列無關,任兩不同期的一階交叉動差階為0。 - k 階自我共變異數 (Lag-k autocovariance) $$\gamma(k) = (\sum_{j=0}^{q-k} \theta_{j+k}\theta_j)\sigma^2,\ k \leq q$$ 此式透過「白噪音」是序列無關的性質一樣可以簡單地推出。 - k 階自我共相關係數 (Lag-k autocorrelation) $$\rho(k)=\frac{\gamma(k)}{Var[Y_t]}=\frac{(\sum_{j=0}^{q-k} \theta_{j+k}\theta_j)\sigma^2}{ (\sum_{j=0}^q \theta_j^2)\sigma^2}=\frac{\sum_{j=0}^{q-k} \theta_{j+k}\theta_j}{ \sum_{j=0}^q \theta_j^2 }, \ k \leq q$$ $\rho(k) =0, \text{if}\ k>q$。 :::success 💡 用**落後運算子**描述 MA(q) 模型: $$\begin{align} Y_t &= \mu +{\theta_0} e_t + \theta_1 e_{t-1} + \theta_2 e_{t-2}+ \cdots + \theta_q e_{t-q} \\ &=\mu + \color{red}{(\theta_0+\theta_1 L+\theta_2 L^2 + \cdots + \theta_q L^q)} e_t\\ &=\mu + \color{red}{\theta(L)} e_t \end{align} $$ 💡 MA(q) 模型的**可逆性 (invertibility)**: 承上,我們用落後運算子去描述了 MA(q) 模型,我們可以對其進行簡單的代數運算得: $$\begin{align} e_t &= \frac{1}{\color{red}{\theta(L)}} (Y_t -\mu) \end{align} $$ 若此式成立,則表示該 MA(q) 模型可逆,其必要條件為 $\theta(z) =0$ 之根要落在單位圓之外。 (此性質牽涉到 MA 與 AR 模型的關聯性以及[**單根**](https://www.ptt.cc/bbs/CCU-GIE96/M.1219732052.A.268.html)問題,我們會於後續部分補充) ::: :::success 💡 MA(1) 模型的可逆性 (invertibility): MA(1): $$Y_t = \mu + e_t + \theta_1 e_{t-1},\ e_t \overset{\mathrm{i.i.d.}}{\sim} WN(0,\sigma^2)$$ 當 $|\theta_1| < 1$,表示 MA(1) 是可逆的。 :::spoiler 說明 我們先將 MA(1) 移項為: $$e_t = -\mu + Y_t - \theta_1 e_{t-1}$$ 因此,$e_{t-1} = -\mu + Y_{t-1} - \theta_1 e_{t-2}$ 將其代入 $e_t$,並且反覆帶入 n 次: \begin{align*} e_t &= -\mu + Y_t - \theta_1 e_{t-1} \\ &= -\mu + Y_t - \theta_1 (-\mu + Y_{t-1}-\theta_1 e_{t-1}) \\ &= (-1+\theta_1)\mu + Y_t - \theta_1 Y_{t-1} + \theta_1^2 e_{t-2} \\ &= (-1+\theta_1 -\theta_1^2)\mu + \sum_{j=0}^2(-\theta_1)^j Y_{t-j} - \theta_1^3 e_{t-3}\\ & \cdots \\ &= \sum_{j=0}^n(-\theta_1)^j(-\mu) + \sum_{j=0}^n(-\theta_1)^j Y_{t-j} + (-\theta_1)^{n+1} e_{t-n-1} \end{align*} 此時當 $n \to \infty$, \begin{align*} e_t &= \sum_{j=0}^\infty (-\theta_1)^j(-\mu) + \sum_{j=0}^\infty (-\theta_1)^j Y_{t-j} \end{align*} 若上式要成立,則必須要求 $|\theta_1|<1$,此為 MA(1) 可逆的充要條件。 ::: **3. 無窮階移動平均模型 (MA(∞) model)** 近一步地拓展 MA(q) 模型,當 $q \to \infty$,即為 MA(∞) 模型。 $$Y_t = \mu +\sum_{j=0}^{\infty}\theta_j e_{t-j} $$ MA(∞) 模型 的動差性質與 MA(q) 相近: - 均數 $$E[Y_t] = \mu$$ 因為 $\{e_t\} \overset{\mathrm{i.i.d.}}{\sim} WN(0,\sigma^2)$。 - 變異數 $$Var[Y_t] = (\sum_{j=0}^\infty \theta_j^2)\sigma^2$$ - k 階自我共變異數 (Lag-k autocovariance) $$\gamma(k) = (\sum_{j=0}^{\infty} \theta_{j+k}\theta_j)\sigma^2 $$ - k 階自我共相關係數 (Lag-k autocorrelation) $$\rho(k)=\frac{\gamma(k)}{Var[Y_t]}=\frac{\sum_{j=0}^{\infty} \theta_{j+k}\theta_j}{ \sum_{j=0}^\infty \theta_j^2 } $$ MA(∞) 模型是一個類似無窮級數的模型,因此我們不會希望該模型發散,如此會使得模型沒有意義。 我們說 MA(∞) 模型的定態條件為: 1. 絕對可加 (absolutely summable):$\sum_{j=0}^{\infty} |\theta_j|<\infty$。 2. 且 $\{e_t\}$ 為白噪音序列。 :::info 📚 對於 MA(q) 模型來說,可以將其當成 MA(∞) 模型的一個特例,當 $j>q$ 時,$\theta_q=0$。 ::: # 4.3 用 R 語言模擬 MA 模型 本部分例題取自賓夕法尼亞州立大學:[應用時間序列課程](https://online.stat.psu.edu/stat510/lesson/2/2.1)開放式網頁。 在 R 語言中,我們可以使用內建 `stats` 套件中的 `arima.sim` 函數去模擬 MA 模型。 ## 4.3.1 模擬 MA(1) 模型 若有一 MA(1) 模型為 $$Y_t=10+e_t+0.7e_{t-1}$$ 其中,$e_t \overset{i.i.d.}{\sim} N(0,1)$。 讓我們來計算其理論上的動差性質並與模擬的結果做對比。 在理論上: $$\begin{align} E[Y_t] &=10 \\ Var[Y_t] &= (1+0.7^2)\times 1^2 = 1.49 \\ \gamma(1) &= 0.7 \times 1^2 =0.7 \\ \rho(1) &= \frac{0.7}{1+0.7^2} = 0.4697987 \end{align} $$ 此外,當 $k \geq 2$ 時,$\gamma(k)=\rho(k)=0$。 讓我們用 `R` 去生成此 MA(1) 模型的 150 個資料點: ``` Input: set.seed(9527) # 生成 MA(1) 的資料, theta1 = 0.7 的模型,預設的白噪音 error 是標準常態 Yt <- 10 + arima.sim(n=150, list(ma=c(0.7))) # 匯出生成點們 print(Yt) ``` ``` Output: Time Series: Start = 1 End = 150 Frequency = 1 [1] 10.260616 10.760471 9.620448 10.139701 10.486962 11.741849 10.905336 [8] 10.234828 11.200450 11.095247 9.817264 9.364172 9.932086 11.139503 [15] 10.950566 8.761292 8.486231 9.762064 10.999911 9.871114 8.919616 [22] 9.075657 10.288620 11.120364 12.034646 10.346365 9.783456 9.652560 [29] 9.101388 8.503054 9.137114 9.646895 8.497241 8.106173 7.869598 [36] 8.499379 9.158426 10.579906 10.298106 8.409116 8.593474 11.026008 [43] 10.671602 10.029862 9.868653 10.170467 10.058508 11.352379 10.655500 [50] 8.830543 8.526831 7.834692 7.088917 9.854170 9.197635 6.964819 [57] 9.516030 10.457591 9.971116 11.633726 11.517887 9.859329 8.845964 [64] 9.197528 9.276873 9.434904 9.755742 9.384180 9.507221 11.108597 [71] 12.721168 11.324514 8.902170 7.488018 9.422065 10.805050 8.964212 [78] 9.233536 9.582402 9.410107 11.801972 11.304690 9.664591 9.652011 [85] 9.938911 9.455853 11.068088 10.940955 9.713682 9.178607 9.398902 [92] 12.021376 12.211921 9.240717 8.798734 8.846317 8.893808 9.721076 [99] 11.623914 10.311029 9.365301 9.584360 8.584382 7.594562 9.280879 [106] 8.844267 9.959752 10.360098 10.755162 10.749810 11.480572 11.725094 [113] 11.410181 9.443613 11.522834 12.612947 12.417776 9.709338 9.425732 [120] 9.162034 10.216360 11.969497 10.379672 8.367551 8.069453 8.701267 [127] 9.357364 12.095766 13.097186 11.571759 9.473030 8.650896 9.162105 [134] 8.532963 9.739305 11.004739 11.495611 10.360584 9.565874 9.374666 [141] 10.309911 11.534165 10.409513 9.140342 8.814228 9.989354 11.013241 [148] 11.048444 9.238198 11.124185 ``` ``` Input: # 繪圖看看走勢 plot(Yt,main = "MA(1) theta_1 = 0.7") abline(h=10) ``` ``` Output: ``` ![ma1](https://i.imgur.com/MHUplyr.png) **計算樣本動差**: ``` Input: #平均數: mean(Yt) #變異數 var(Yt) ``` ``` Output: #平均數: [1] 9.961232 #變異數 [1] 1.423271 ``` ``` Input: #一階自我共變異數 ACVF ACVF <- acf(Yt,type = "covariance", xlim=c(1,10), main="ACVF for simulated MA(1)",plot = T) text(x = 1.5, y =ACVF$acf[2], labels = as.character(round(ACVF$acf[2],4))) ``` ``` Output: ``` ![ACV](https://i.imgur.com/xA145G6.png) ``` Input: # 一階自我相關係數 ACF ACF <- acf(Yt,type = "correlation", xlim=c(1,10), main="ACF for simulated MA(1)",plot = T) text(x = 1.5, y =ACF$acf[2], labels = as.character(round(ACF$acf[2],4))) ``` ``` Output: ``` ![ACV](https://i.imgur.com/enZNa2y.png) - **理論值與模擬值 (n=150) 的比較** | 動差 | 理論值 | 模擬值 | | ---- | ------ | ------ | | 均數 | 10 | 9.9612 | | 變異數 | 1.49 | 1.4232 | 一階自我共變數 | 0.7000 | 0.7129 | |一階自我相關係數 | 0.4698 | 0.5043 | ## 4.3.2 模擬 MA(2) 模型 現考慮一 MA(2) 模型: $$Y_t=10+e_t+0.5e_{t-1}+0.3e_{t-2}$$ 其中,$e_t \overset{i.i.d.}{\sim} N(0,1)$。 我們同樣來計算其理論上的動差性質並與模擬的結果做對比。 在理論上: $$\begin{align} E[Y_t] &=10 \\ Var[Y_t] &= (1+0.5^2+0.3^2)\times 1^2 = 1.34 \\ \rho(1) &= \dfrac{0.5+0.5 \times 0.3}{1+0.5^2 +0.3^2} = 0.4851 \\ \rho(2) &= \dfrac{0.3}{1+0.5^2 +0.3^2}= 0.2239 \end{align} $$ ``` Input: set.seed(9527) # 生成 MA(1) 的資料, theta1 = 0.7 的模型,預設的白噪音 error 是標準常態 Yt2 <- 10 + arima.sim(n=150, list(ma=c(0.5, 0.3))) # 匯出生成點們 print(Yt2) ``` ``` Output: Time Series: Start = 1 End = 150 Frequency = 1 [1] 10.698487 9.589927 10.465117 10.117365 11.945085 10.560790 10.817497 11.017217 [9] 11.053869 9.987314 9.610258 9.832170 11.032279 10.777348 9.034282 8.828049 [17] 9.453933 10.809060 9.733347 9.341804 8.957537 10.232058 10.824931 12.103963 [25] 10.227023 10.408116 9.362516 9.308944 8.433255 9.215778 9.315324 8.541096 [33] 8.270095 7.682791 8.488076 8.827342 10.529794 9.964732 8.756829 8.757684 [41] 10.715102 10.262077 10.490012 9.742056 10.294312 9.898318 11.503950 10.296477 [49] 9.356473 8.589222 7.734815 7.149941 9.744076 8.430541 7.618509 9.424774 [57] 9.707710 10.259324 11.575034 11.223561 10.251897 9.054667 9.240042 9.061397 [65] 9.489621 9.613521 9.363058 9.571893 10.977136 12.443637 11.307131 9.465786 [73] 7.710408 9.439440 10.154880 9.084941 9.597698 9.170044 9.552337 11.695110 [81] 10.842131 10.268035 9.688088 9.882632 9.403069 11.195540 10.474877 10.172172 [89] 9.197214 9.464260 11.849427 11.747680 9.729714 9.271869 8.527591 8.987274 [97] 9.530784 11.481046 10.002577 10.006186 9.355989 8.636960 7.716189 9.252142 [105] 8.286576 10.365546 9.737372 11.106976 10.460537 11.759650 11.458568 11.695902 [113] 9.479100 12.032324 11.784821 12.931725 9.649903 10.250802 8.612140 10.596598 [121] 11.408668 10.443260 8.837954 8.180563 8.519865 9.164928 11.969378 12.573714 [129] 11.947482 9.824828 8.981559 9.042371 8.379625 9.888681 10.512204 11.561230 [137] 10.316951 9.972984 9.284690 10.367723 11.244114 10.398689 9.526266 8.838867 [145] 9.951364 10.686232 11.071509 9.316336 11.536382 11.207942 ``` ``` Input: # 繪圖看看走勢 plot(Yt2,main = "MA(2) theta_1 = 0.5 theta_2 = 0.3") abline(h=10) ``` ``` Output: ``` ![ma2_line](https://i.imgur.com/5E3qziL.png) **計算樣本動差**: ``` Input: #平均數: mean(Yt2) #變異數 var(Yt2) ``` ``` Output: #平均數: [1] 9.966816 #變異數 [1] 1.296597 ``` ``` Input: # 一階自我相關係數 ACF ACF2 <- acf(Yt2,type = "correlation", xlim=c(1,10), main="ACF for simulated MA(2)",plot = T) text(x = c(1.5,2.5), y =ACF2$acf[2:3]+0.05, labels = as.character(round(ACF2$acf[2:3],4))) #圖上加字串 ``` ![ma2](https://i.imgur.com/P4ntsSB.png) - **理論值與模擬值 (n=150) 的比較** | 動差 | 理論值 | 模擬值 | | ---- | ------ | ------ | | 均數 | 10 | 9.9668 | | 變異數 | 1.34 | 1.2966 | 一階自我相關係數 | 0.4851 | 0.4997 | |二階自我相關係數 | 0.2239 | 0.1575 | # 參考資料 1. 陳旭昇(2022)。《時間序列分析: 總體經濟與財務金融之應用》(3版)。 2. 陳旭昇(2015)。《統計學: 應用與進階》(3版)。 3. 楊奕農(2017)。《時間序列分析: 經濟與財務上之應用》(3版)。 4. 葉小蓁(1998)。《時間序列分析與應用》。 5. [顏國勇(2011)。《機率論》,電子書。](https://library.math.ncku.edu.tw/documents/1/Probability21A.pdf) 6. [北京大學「金融時間序列分析」講義](https://www.math.pku.edu.cn/teachers/lidf/course/fts/ftsnotes/html/_ftsnotes/ftsnotes.pdf)。 7. [北京大學「金融中的随机数学」講義](https://www.math.pku.edu.cn/teachers/lidf/course/stochproc/stochprocnotes/html/_book/index.html)。 8. Klaus Neusser (2015), Time Series Analysis in Economics, Springer. 9. Fumio Hayashi (2000), Econometrics, Princeton University Press. 10. Jonathan D. Cryer and Kung-Sik Chan (2008), Time Series Analysis with Applications in R, Springer. 11. Paul S.P. Cowpertwait and Andrew V. Metcalfe (2009), Introductory Time Series with R, Springer. 12. Walter Enders (2015), Applied Econometric Time Series, Wiley. 13. [Christoph Hanck, Martin Arnold, Alexander Gerber, and Martin Schmelzer(2024), Introduction to Econometrics with R.](https://www.econometrics-with-r.org/index.html) 14. Bruce E. Hansen (2021), Econometrics, Princeton University Press. 15. Steven Shreve (2003), Stochastic Calculus for Finance”, Vol 2, Springer. 16. [Rao, S. S. (2008). A course in time series analysis. In Technical Report. Texas A&M University.](https://web.stat.tamu.edu/~suhasini/teaching673/time_series.pdf)