时间序列分析 - python实现

python的statsmodels模块(http://www.statsmodels.org/dev/tsa.html)提供了时间序列分析相关的内容:

acf() 计算自相关                           statsmodels.tsa.stattools.acf

plt_acf() 画自相关系数                  statsmodels.graphics.tsaplots.plot_acf

pacf() 计算偏自相关                      statsmodels.tsa.stattools.pacf

plot_pacf() 画偏相关系数               statsmodels.graphics.tsaplots.plot_pacf

adfuller()  ADF单位根检验平稳性   statsmodels.tsa.stattools.adfuller

ARMA() 创建ARMA序列模型         statsmodels.tsa.arima_model.ARMA

              其中order参数可以指定p,q阶数,比如:ARMA(order=(1, 2)),其中p=1, q=2

ARMA.fit() ARMA模型拟合             statsmodels.tsa.arima_model.ARMA.fit

ARMA.predict() ARMA模型预测     statsmodels.tsa.arima_model.ARMA.predict

ARMAResults() ARMA模型结果     statsmodels.tsa.arima_model.ARMAResults

               结果提供了模型判别AIC, BIC,以及残差resid等指标结果。

ARIMA() 创建ARIMA序列模型        statsmodels.tsa.arima_model.ARIMA

               其中order参数可以指定p,d,q阶数,比如:ARIMA(order=(3, 1,2)),其中p=3, d=1, q=2

ARIMA.fit() ARIMA模型拟合            statsmodels.tsa.arima_model.ARIMA.fit

ARIMA.predict() ARIAM模型预测    statsmodels.tsa.arima_model.ARIMA.predict

ARIMAResults() ARMA模型结果     statsmodels.tsa.arima_model.ARIMAResults

               结果提供了模型判别AIC, BIC,以及残差resid等指标结果。

q_stat() Ljung-Box Q统计量             statsmodels.tsa.stattools.q_stat

 

[平稳性检验 - ADF单位根检验]

>>> from statsmodels.tsa.stattools import adfuller

返回值:

 

 

  • adf (float) – Test statistic
  • pvalue (float) – MacKinnon’s approximate p-value based on MacKinnon (1994, 2010)
  • usedlag (int) – Number of lags used
  • nobs (int) – Number of observations used for the ADF regression and calculation of the critical values
  • critical values (dict) – Critical values for the test statistic at the 1 %, 5 %, and 10 % levels. Based on MacKinnon (2010)
  • icbest (float) – The maximized information criterion if autolag is not None.
  • resstore (ResultStore, optional) – A dummy class with results attached as attributes

举例:

print (adfuller(data))

(-9.1916312162314355, 2.1156279593784273e-15, 12, 338, {'5%': -2.8701292813761641, '1%': -3.449846029628477, '10%': -2.5713460670144603}, 4542.1540700410897)

如何确定该序列能否平稳呢?主要看:

1) 1%、%5、%10不同程度拒绝原假设的统计值和返回值中的第一项adf的比较,adf同时小于1%、5%、10%即说明非常好地拒绝该假设,本数据中,adf结果为-9, 小于三个level的统计值。

2) 返回值的第二项pvalue是否非常接近0.本数据中pvalue 为 2e-15,接近0.

ADF检验的原假设是存在单位根,只要这个统计值是小于1%水平下的数字就可以极显著的拒绝原假设,认为数据平稳。注意,一般只有在adf小于1%的水平下才能认为是极其显著的拒绝原假设。

 

【差分】

pandas.DataFrame.diff

pandas.Series.diff

参数periods为差分时间间隔,默认为1.

注意:diff(2)与diff(1).diff(1)不相同,前者是相隔时间差为2的差分,后者的意思是先做一次时间差为1的差分,在这个结果之上再做一次时间差为1的差分,即差分的差分,也就是2阶差分。

>>> import pandas
>>> import pandas as pd
>>> s = pd.Series([1, 1, 2, 3, 5, 8])
>>> s
0    1
1    1
2    2
3    3
4    5
5    8
dtype: int64
>>> s.diff()
0    NaN
1    0.0
2    1.0
3    1.0
4    2.0
5    3.0
dtype: float64
>>> s.diff(1)
0    NaN
1    0.0
2    1.0
3    1.0
4    2.0
5    3.0
dtype: float64
>>> s.diff(1).diff(1)
0    NaN
1    NaN
2    1.0
3    0.0
4    1.0
5    1.0
dtype: float64
>>> s.diff(2)
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
5    5.0
dtype: float64
>>> s.diff(periods=2)
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
5    5.0
dtype: float64
>>> 

具体的建模实例可以参考:

https://machinelearningmastery.com/arima-for-time-series-forecasting-with-python/

http://www.seanabu.com/2016/03/22/time-series-seasonal-ARIMA-model-in-python/

 

参考:

http://www.statsmodels.org/devel/generated/statsmodels.tsa.stattools.adfuller.html?highlight=adfuller#statsmodels.tsa.stattools.adfuller

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

https://www.jianshu.com/p/cced6617b423
 

你可能感兴趣的:(机器学习&人工智能,python)