统计模型中时间序列的交叉验证

2024-03-28 23:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我查看了统计模型的例子,但是我没有看到很多将交叉验证应用于时间序列的例子。在

就这样说吧

`In [1]: from __future__ import print_function

In [2]: import numpy as np

In [3]: import statsmodels.api as sm
import pandas as pd
from statsmodels.tsa.arima_process import arma_generate_sample
np.random.seed(12345)

In [4]: import pandas as pd

In [5]: from statsmodels.tsa.arima_process import arma_generate_sample

In [6]: np.random.seed(12345)

In [7]: arparams = np.array([.75, -.25])

In [8]: maparams = np.array([.65, .35])

In [9]:

In [9]: arparams = np.r_[1, -arparams]

In [10]: maparam = np.r_[1, maparams]

In [11]: nobs = 250

In [12]: y = arma_generate_sample(arparams, maparams, nobs)

In [13]: dates = sm.tsa.datetools.dates_from_range('1980m1', length=nobs)

In [14]: y = pd.TimeSeries(y, index=dates)
/Users/pcoyle/anaconda3/bin/ipython:1: FutureWarning: TimeSeries is deprecated. Please use Series
  #!/bin/bash /Users/pcoyle/anaconda3/bin/python.app

In [15]: arma_mod = sm.tsa.ARMA(y, order=(2,2))

In [16]: arma_res = arma_mod.fit(trend='nc', disp=-1)

In [17]: print(arma_res.summary())
                              ARMA Model Results
==============================================================================
Dep. Variable:                      y   No. Observations:                  250
Model:                     ARMA(2, 2)   Log Likelihood                -245.887
Method:                       css-mle   S.D. of innovations              0.645
Date:                Mon, 01 Aug 2016   AIC                            501.773
Time:                        17:51:51   BIC                            519.381
Sample:                    01-31-1980   HQIC                           508.860
                         - 10-31-2000
==============================================================================
                 coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
ar.L1.y        0.8411      0.403      2.089      0.038         0.052     1.630
ar.L2.y       -0.2693      0.247     -1.092      0.276        -0.753     0.214
ma.L1.y        0.5352      0.412      1.299      0.195        -0.273     1.343
ma.L2.y        0.0157      0.306      0.051      0.959        -0.585     0.616
                                    Roots
=============================================================================
                 Real           Imaginary           Modulus         Frequency
-----------------------------------------------------------------------------
AR.1            1.5618           -1.1289j            1.9271           -0.0996
AR.2            1.5618           +1.1289j            1.9271            0.0996
MA.1           -1.9835           +0.0000j            1.9835            0.5000
MA.2          -32.1794           +0.0000j           32.1794            0.5000
-----------------------------------------------------------------------------`

我如何从这个例子转到交叉验证。理想的情况是类似于时间依赖的交叉验证。 有什么建议吗?在


Tags: sampleinfromimportasnp交叉generate