模擬 MA(1) 時間序列
你將使用 statsmodels 中的 arima_process 模組,模擬並繪製幾條 MA(1) 時間序列,每條都有不同的參數 \(\small \theta\),就像前一章對 AR(1) 模型所做的一樣。你會觀察一個 \(\small \theta\) 很大的正值與很大的負值的 MA(1) 模型。
與前一章相同,輸入係數時必須包含延遲 0 的係數 1。不過與 AR 模型不同的是,MA 係數的符號與我們的直覺一致。舉例來說,若 MA(1) 過程的 \(\small \theta=-0.9\),代表 MA 參數的陣列為 ma = np.array([1, -0.9])。
本練習屬於課程
Python 中的時間序列分析
練習說明
- 匯入
arima_process模組中的ArmaProcess類別。 - 繪製模擬出的 MA(1) 過程:
- 讓
ma1代表 MA 參數陣列 [1, \(\small \theta\)](如上所述)。AR 參數陣列只包含延遲 0 的係數 1。 - 使用參數
ar1與ma1,建立ArmaProcess(ar, ma)類別的實例,命名為MA_object1。 - 從剛建立的物件
MA_object1使用.generate_sample()方法模擬 1000 筆資料。將模擬結果繪製在一個子圖中。
- 讓
- 針對另一個 MA 參數重複上述步驟。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# import the module for simulating data
from statsmodels.tsa.arima_process import ArmaProcess
# Plot 1: MA parameter = -0.9
plt.subplot(2,1,1)
ar1 = np.array([1])
ma1 = np.array([1, ____])
MA_object1 = ArmaProcess(____, ____)
simulated_data_1 = MA_object1.generate_sample(nsample=1000)
plt.plot(simulated_data_1)
# Plot 2: MA parameter = +0.9
plt.subplot(2,1,2)
ar2 = np.array([1])
ma2 = np.array([1, ____])
MA_object2 = ArmaProcess(____, ____)
simulated_data_2 = MA_object2.generate_sample(nsample=1000)
plt.plot(simulated_data_2)
plt.show()