基于python的时间序列分析_python时间序列分析(ARIMA模型)

1 #Note: The information criteria add 1 to the number of parameters

2 #whenever the model has an AR or MA term since, in principle,

3 #the variance could be treated as a free parameter and restricted

4 #This code does not allow this, but it adds consistency with other

5 #packages such as gretl and X12-ARIMA

6

7 from __future__ importabsolute_import8 from statsmodels.compat.python importstring_types, range9 #for 2to3 with extensions

10

11 from datetime importdatetime12

13 importnumpy as np14 from scipy importoptimize15 from scipy.stats importt, norm16 from scipy.signal importlfilter17 from numpy importdot, log, zeros, pi18 from numpy.linalg importinv19

20 from statsmodels.tools.decorators import(cache_readonly,21 resettable_cache)22 importstatsmodels.tsa.base.tsa_model as tsbase23 importstatsmodels.base.wrapper as wrap24 from statsmodels.regression.linear_model importyule_walker, GLS25 from statsmodels.tsa.tsatools import(lagmat, add_trend,26 _ar_transparams, _ar_invtransparams,27 _ma_transparams, _ma_invtransparams,28 unintegrate, unintegrate_levels)29 from statsmodels.tsa.vector_ar importutil30 from statsmodels.tsa.ar_model importAR31 from statsmodels.tsa.arima_process importarma2ma32 from statsmodels.tools.numdiff importapprox_hess_cs, approx_fprime_cs33 from statsmodels.tsa.base.datetools import_index_date34 from statsmodels.tsa.kalmanf importKalmanFilter35

36 _armax_notes = """

37

38 Notes39 -----40 If exogenous variables are given, then the model that is fit is41

42 .. math::43

44 \\phi(L)(y_t - X_t\\beta) = \\theta(L)\epsilon_t45

46 where :math:`\\phi` and :math:`\\theta` are polynomials in the lag47 operator, :math:`L`. This is the regression model with ARMA errors,48 or ARMAX model. This specification is used, whether or not the model49 is fit using conditional sum of square or maximum-likelihood, using50 the `method` argument in51 :meth:`statsmodels.tsa.arima_model.%(Model)s.fit`. Therefore, for52 now, `css` and `mle` refer to estimation methods only. This may53 change for the case of the `css` model in future versions.54 """

55

56 _arma_params = """\57 endog : array-like58 The endogenous variable.59 order : iterable60 The (p,q) order of the model for the number of AR parameters,61 differences, and MA parameters to use.62 exog : array-like, optional63 An optional arry of exogenous variables. This should *not* include a64 constant or trend. You can specify this in the `fit` method."""

65

66 _arma_model = "Autoregressive Moving Average ARMA(p,q) Model"

67

68 _arima_model = "Autoregressive Integrated Moving Average ARIMA(p,d,q) Model"

69

70 _arima_params = """\71 endog : array-like72 The endogenous variable.73 order : iterable74 The (p,d,q) order of the model for the number of AR parameters,75 differences, and MA parameters to use.76 exog : array-like, optional77 An optional arry of exogenous variables. This should *not* include a78 constant or trend. You can specify this in the `fit` method."""

79

80 _predict_notes = """

81 Notes82 -----83 Use the results predict method instead.84 """

85

86 _results_notes = """

87 Notes88 -----89 It is recommended to use dates with the time-series models, as the90 below will probably make clear. However, if ARIMA is used without91 dates and/or `start` and `end` are given as indices, then these92 indices are in terms of the *original*, undifferenced series. Ie.,93 given some undifferenced observations::94

95 1970Q1, 196 1970Q2, 1.597 1970Q3, 1.2598 1970Q4, 2.2599 1971Q1, 1.2100 1971Q2, 4.1101

102 1970Q1 is observation 0 in the original series. However, if we fit an103 ARIMA(p,1,q) model then we lose this first observation through104 differencing. Therefore, the first observation we can forecast (if105 using exact MLE) is index 1. In the differenced series this is index106 0, but we refer to it as 1 from the original series.107 """

108

109 _predict = """

110 %(Model)s model in-sample and out-of-sample prediction111

112 Parameters113 ----------114 %(params)s115 start : int, str, or datetime116 Zero-indexed observation number at which to start forecasting, ie.,117 the first forecast is start. Can also be a date string to118 parse or a datetime type.119 end : int, str, or datetime120 Zero-indexed observation number at which to end forecasting, ie.,121 the first forecast is start. Can also be a date string to122 parse or a datetime type. However, if the dates index does not123 have a fixed frequency, end must be an integer index if you124 want out of sample prediction.125 exog : array-like, optional126 If the model is an ARMAX and out-of-sample forecasting is127 requested, exog must be given. Note that you'll need to pass128 `k_ar` additional lags for any exogenous variables. E.g., if you129 fit an ARMAX(2, q) model and want to predict 5 steps, you need 7130 observations to do this.131 dynamic : bool, optional132 The `dynamic` keyword affects in-sample prediction. If dynamic133 is False, then the in-sample lagged values are used for134 prediction. If `dynamic` is True, then in-sample forecasts are135 used in place of lagged dependent variables. The first forecasted136 value is `start`.137 %(extra_params)s138

139 Returns140 -------141 %(returns)s142 %(extra_section)s143 """

144

145 _predict_returns = """predict : array146 The predicted values.147

148 """

149

150 _arma_predict = _predict % {"Model" : "ARMA",151 "params" : """

152 params : array-like153 The fitted parameters of the model.""",154 "extra_params" : "",155 "returns": _predict_returns,156 "extra_section": _predict_notes}157

158 _arma_results_predict = _predict % {"Model" : "ARMA", "params" : "",159 "extra_params" : "",160 "returns": _predict_returns,161 "extra_section": _results_notes}162

163 _arima_predict = _predict % {"Model" : "ARIMA",164 "params" : """params : array-like165 The fitted parameters of the model.""",166 "extra_params" : """typ : str {'linear', 'levels'}167

168 - 'linear' : Linear prediction in terms of the differenced169 endogenous variables.170 - 'levels' : Predict the levels of the original endogenous171 variables.\n""", "returns": _predict_returns,172 "extra_section": _predict_notes}173

174 _arima_results_predict = _predict % {"Model" : "ARIMA",175 "params" : "",176 "extra_params":177 """typ : str {'linear', 'levels'}178

179 - 'linear' : Linear prediction in terms of the differenced180 endogenous variables.181 - 'levels' : Predict the levels of the original endogenous182 variables.\n""",183 "returns": _predict_returns,184 "extra_section": _results_notes}185

186 _arima_plot_predict_example = """Examples187 --------188 >>> import statsmodels.api as sm189 >>> import matplotlib.pyplot as plt190 >>> import pandas as pd191 >>>192 >>> dta = sm.datasets.sunspots.load_pandas().data[['SUNACTIVITY']]193 >>> dta.index = pd.DatetimeIndex(start='1700', end='2009', freq='A')194 >>> res = sm.tsa.ARMA(dta, (3, 0)).fit()195 >>> fig, ax = plt.subplots()196 >>> ax = dta.ix['1950':].plot(ax=ax)197 >>> fig = res.plot_predict('1990', '2012', dynamic=True, ax=ax,198 ... plot_insample=False)199 >>> plt.show()200

201 .. plot:: plots/arma_predict_plot.py202 """

203

204 _plot_predict = ("""

205 Plot forecasts206 """ + '\n'.join(_predict.split('\n')[2:])) %{207 "params" : "",208 "extra_params" : """alpha : float, optional209 The confidence intervals for the forecasts are (1 - alpha)%210 plot_insample : bool, optional211 Whether to plot the in-sample series. Default is True.212 ax : matplotlib.Axes, optional213 Existing axes to plot with.""",214 "returns" : """fig : matplotlib.Figure215 The plotted Figure instance""",216 "extra_section" : ('\n' + _arima_plot_predict_example +

217 '\n' +_results_notes)218 }219

220 _arima_plot_predict = ("""

221 Plot forecasts222 """ + '\n'.join(_predict.split('\n')[2:])) %{223 "params" : "",224 "extra_params" : """alpha : float, optional225 The confidence intervals for the forecasts are (1 - alpha)%226 plot_insample : bool, optional227 Whether to plot the in-sample series. Default is True.228 ax : matplotlib.Axes, optional229 Existing axes to plot with.""",230 "returns" : """fig : matplotlib.Figure231 The plotted Figure instance""",232 "extra_section" : ('\n' + _arima_plot_predict_example +

233 '\n' +

234 '\n'.join(_results_notes.split('\n')[:3]) +

235 ("""

236 This is hard-coded to only allow plotting of the forecasts in levels.237 """) +

238 '\n'.join(_results_notes.split('\n')[3:]))239 }240

241

242 defcumsum_n(x, n):243 ifn:244 n -= 1

245 x =np.cumsum(x)246 returncumsum_n(x, n)247 else:248 returnx249

250

251 def_check_arima_start(start, k_ar, k_diff, method, dynamic):252 if start <0:253 raise ValueError("The start index %d of the original series"

254 "has been differenced away" %start)255 elif (dynamic or 'mle' not in method) and start

<

<

<

<

<

<

<

<<

Statsmodels是Python中用于统计建模和计量经济学的库,它提供了各种统计模型,包括线性回归、时间序列分析等。在时间序列分析中,ARIMA模型是一种常用的模型ARIMA模型是自回归移动平均模型的缩写,它是一种广义的线性模型,常用于描述时间序列数据的自相关结构和随机性。ARIMA模型可以分为AR(自回归)、MA(移动平均)和差分(I)三部分,其中AR是指用当前值的前几个值来预测当前值,MA是指用当前误差的前几个值来预测当前误差,差分是指对时间序列进行差分处理,使其变得平稳。 在Python中,使用Statsmodels中的ARIMA模型进行时间序列分析可以分为以下几个步骤: 1. 导入相关库 ```python import pandas as pd import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt ``` 2. 读取数据 ```python data = pd.read_csv("data.csv", index_col=0, parse_dates=True) ``` 3. 绘制时间序列图 ```python plt.plot(data) plt.show() ``` 4. 确定模型阶数 可以使用ACF和PACF图来确定ARIMA模型的阶数。ACF图展示了时间序列与其滞后版本之间的自相关性,PACF图展示了当前时间序列与其滞后版本之间的部分自相关性。根据ACF和PACF图的信息,可以确定ARIMA模型的p、d和q参数。 ```python fig, ax = plt.subplots(2,1) sm.graphics.tsa.plot_acf(data, lags=30, ax=ax[0]) sm.graphics.tsa.plot_pacf(data, lags=30, ax=ax[1]) plt.show() ``` 5. 拟合模型 根据确定的ARIMA模型阶数,使用ARIMA()函数拟合时间序列数据。 ```python model = sm.tsa.ARIMA(data, order=(p,d,q)) results = model.fit() ``` 6. 模型诊断 使用plot_diagnostics()函数进行模型诊断,检查残差是否符合白噪声假设。 ```python results.plot_diagnostics(figsize=(15, 12)) plt.show() ``` 7. 预测 使用forecast()函数进行预测。 ```python forecast = results.forecast(steps=10) ``` 以上就是使用Python中Statsmodels包进行时间序列分析ARIMA模型的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值