简体   繁体   English

ARMA预测,趋势消除问题

[英]ARMA forecasting, problems with trend elimination

I want to do a rough ARMA forecasting for it, ie to get knowledge more about how to use stats model library and see how it works. 我想对此进行一次粗略的ARMA预测,即获得更多有关如何使用统计模型库并了解其工作原理的知识。 So firstly I launched the example that is somewhere in the web, but ARMA fitting and prediction does not work, as MLE does not converge. 因此,首先,我启动了位于网络中某个位置的示例,但是ARMA拟合和预测不起作用,因为MLE无法收敛。 I decided that series is not stationary, so firstly, I want to eliminate trend, and this is a challenge to me. 我认为系列不是固定的,所以首先,我想消除趋势,这对我来说是一个挑战。 Here is the code: 这是代码:

   import pandas.io.data as web
import statsmodels.api as sm
import statsmodels.tsa.api as tsa
import datetime
import statsmodels.formula.api as smf


start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2010, 1, 27)
f = web.DataReader("F", 'yahoo', start, end)
print f

#+++++++++++++++TREND+++++++++++++++
atrend = tsa.add_trend(f['Close'].values,trend='ctt')
print atrend
#+++++++++++++++fitting ARMA++++++++

arma =tsa.ARMA(f['Close'].values, order =(2,2))
results= arma.fit()

So, printing the 'atrend' gives me: 因此,打印“冒险”给我:

[[  10.28    1.      1.      1.  ]
 [  10.96    1.      2.      4.  ]
 [  11.37    1.      3.      9.  ]
 [  11.66    1.      4.     16.  ]
 [  11.69    1.      5.     25.  ]
 [  12.11    1.      6.     36.  ]
 [  11.87    1.      7.     49.  ]
 [  11.68    1.      8.     64.  ]
 [  11.76    1.      9.     81.  ]
 [  11.6     1.     10.    100.  ]
 [  11.75    1.     11.    121.  ]
 [  11.51    1.     12.    144.  ]
 [  11.18    1.     13.    169.  ]
 [  10.52    1.     14.    196.  ]
 [  11.03    1.     15.    225.  ]
 [  11.19    1.     16.    256.  ]
 [  11.55    1.     17.    289.  ]]

Which I do not understand completely. 我不完全了解。 I asked to have a ctt trend which is : at^2+bt+c I calculated, for comparison, both at^2+bt+c, at^3+bt^2+ct+d in excel, and got the following values. 我要求有一个ctt趋势,它是:at ^ 2 + bt + c我为比较计算了Excel中的at ^ 2 + bt + c,at ^ 3 + bt ^ 2 + ct + d,得到了以下结果价值观。

10,494523
10,780752
11,031687
11,247328
11,427675
11,572728
11,682487
11,756952
11,796123
11,8
11,768583
11,701872
11,599867
11,462568
11,289975
11,082088
10,838907

for parabola, y = -1.7647x2 + 33.917x + 1017.3 ,and: 对于抛物线,y = -1.7647x2 + 33.917x + 1017.3,并且:

10,00432
10,65848
11,1547
11,5105
11,7434
11,87092
11,91058
11,8799
11,7964
11,6776
11,54102
11,40418
11,2846
11,1998
11,1673
11,20462
11,32928

for cubic equation y = 0.292x3 - 9.649x2 + 92.319x + 917.47. 对于三次方程y = 0.292x3-9.649x2 + 92.319x + 917.47。 Even now, I do not have an idea how to insert these values to the arma =tsa.ARMA( f['Close'].values , order =(2,2)) in order to check if I can do the next task. 即使是现在,我仍然不知道如何将这些值插入arma = tsa.ARMA( f ['Close']。values ,order =(2,2))以检查是否可以执行下一个任务。

All in all my idea is to make a brief presentation how easily ARMA forecasting can be used in python, however for me it is not the case. 总而言之,我的想法是简要介绍一下ARMA预测如何在python中使用,但是对我而言并非如此。

It's supposed to be easy but handling of explanatory variables, exog, and trend still have some tricky parts. 它本来应该很容易,但是处理解释变量,exog和趋势仍然有一些棘手的部分。 ( exog handling was a late addition to ARMA.) exog处理是ARMA的新增功能。)

ARMA has an exog keyword for additional explanatory variables for the mean prediction. ARMA使用exog关键字表示平均值预测的其他解释变量。 The constant is included via the trend keyword of the fit method and currently cannot be included in exog . 该常数通过fit方法的trend关键字包含,当前不能包含在exog Unfortunately, the trend keyword doesn't allow for arbitrary order trends. 不幸的是,趋势关键字不允许任意定单趋势。

So in your case something like this should work: 所以在您的情况下,这样的事情应该起作用:

arma = tsa.ARMA(f['Close'].values, exog=f[['trend1', 'trend2']] order=(2,2))

You will need the same kind of trend exog for out of sample prediction in predict . 您将需要同样的趋势exog对样品进行预测的predict

Here https://gist.github.com/josef-pkt/1ea164439b239b228557 is a secret gist of mine that I used to figure out how to use exog in ARMA. 这里https://gist.github.com/josef-pkt/1ea164439b239b228557是我的秘密要点,我曾经弄清楚了如何在ARMA中使用exog。

Current master and upcoming statsmodels 0.7 fix some timing problems when we have exog for out of sample prediction. 当我们对样本外预测有所了解时,当前的主模型和即将推出的statsmodels 0.7解决了一些计时问题。

In general, it is possible to regress the time series on the explanatory variables with OLS or another method, and then use the residuals to estimate an ARMA model. 通常,可以使用OLS或其他方法对解释变量上的时间序列进行回归,然后使用残差来估计ARMA模型。 This works for forecasting, however the standard errors of the ARMA estimate and the prediction standard errors will be wrong because they don't take the initial estimate into account (AFAIK). 这适用于预测,但是ARMA估计的标准误差和预测标准误差将是错误的,因为它们没有考虑初始估计(AFAIK)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM