1. 程式人生 > >多元季節性時間序列模型SARIMAX的應用——預測與異常診斷

多元季節性時間序列模型SARIMAX的應用——預測與異常診斷

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from matplotlib.pylab import rcParams
rcParams["figure.figsize"] = 15,6
#讀取資料
f = open(r"D:\miss_predict.txt")
data = pd.read_table(f)
#時間序列索引
data_bs.index = pd.date_range(start='2018-08-01 00:00:00',periods=744,freq='h',normalize=True)
data = data_bs["指標值"]

#判斷資料的平穩性
%pylab inline
plt.figure(figsize(20,8))
plt.plot(data)

#使用均值填充

#判斷資料的穩定性
from statsmodels.tsa.stattools import adfuller
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

# 移動平均圖
def draw_trend(timeSeries, size):
    f = plt.figure(facecolor='white')
    # 對size個數據進行移動平均
    rol_mean = timeSeries.rolling(window=size).mean()
    # 對size個數據進行加權移動平均
    rol_weighted_mean = pd.ewma(timeSeries, span=size)

    timeSeries.plot(color='blue', label='Original')
    rol_mean.plot(color='red', label='Rolling Mean')
    rol_weighted_mean.plot(color='black', label='Weighted Rolling Mean')
    plt.legend(loc='best')
    plt.title('Rolling Mean')
    plt.show()

def draw_ts(timeSeries):
    f = plt.figure(facecolor='white')
    timeSeries.plot(color='blue')
    plt.show()

'''
  Unit Root Test
   The null hypothesis of the Augmented Dickey-Fuller is that there is a unit
   root, with the alternative that there is no unit root. That is to say the
   bigger the p-value the more reason we assert that there is a unit root
'''
def testStationarity(ts):
    dftest = adfuller(ts)
    # 對上述函式求得的值進行語義描述
    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
    for key,value in dftest[4].items():
        dfoutput['Critical Value (%s)'%key] = value
    return dfoutput

# 自相關和偏相關圖,預設階數為31階
def draw_acf_pacf(ts, lags=50):
    f = plt.figure(facecolor='white')
    ax1 = f.add_subplot(211)
    plot_acf(ts, lags=50, ax=ax1)
    ax2 = f.add_subplot(212)
    plot_pacf(ts, lags=50, ax=ax2)
    plt.show()

p值沒有小於0.05,且ACF/PACF圖不平穩

分解,季節性模型:

建立模型:

預測:

診斷異常:

上述內容,只是個大概的思路,並沒有詳細敘述,後續有機會填補。另外想要詳細內容,請參考下面文章或者評論區討論。

http://www.statsmodels.org/stable/examples/index.html#regression

https://www.cnblogs.com/bradleon/p/6832867.html

https://blog.csdn.net/u014281392/article/details/77585419/

https://www.cnblogs.com/foley/p/5582358.html

http://www.lizenghai.com/archives/595.html

https://www.howtoing.com/a-guide-to-time-series-forecasting-with-arima-in-python-3/

還有相關的論文,如: