ARMA 预测,趋势消除问题
ARMA forecasting, problems with trend elimination
我想为它做一个粗略的ARMA预测,即了解更多关于如何使用统计模型库的知识,看看它是如何工作的。所以首先我启动了网络上某处的示例,但是 ARMA 拟合和预测不起作用,因为 MLE 不收敛。我决定系列不是静止的,所以首先,我想消除趋势,这对我来说是一个挑战。这是代码:
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()
所以,打印 'atrend' 得到:
[[ 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. ]]
我完全不明白。
我要求有一个 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
对于抛物线,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
对于三次方程 y = 0.292x3 - 9.649x2 + 92.319x + 917.47。
即使是现在,我也不知道如何将这些值插入到
arma =tsa.ARMA(f['Close'].values, order =(2,2)) 以检查我是否可以完成下一个任务。
总而言之,我的想法是简要介绍一下 ARMA 预测在 python 中的使用是多么容易,但对我来说并非如此。
这应该很容易,但解释变量、exog 和趋势的处理仍然有一些棘手的部分。 (exog
处理是 ARMA 的后期添加。)
ARMA 有一个 exog
关键字用于平均预测的附加解释变量。该常量通过 fit
方法的 trend
关键字包含,目前不能包含在 exog
中。不幸的是,趋势关键字不允许任意顺序趋势。
所以在你的情况下,这样的事情应该有效:
arma = tsa.ARMA(f['Close'].values, exog=f[['trend1', 'trend2']] order=(2,2))
对于 predict
中的样本外预测,您将需要相同类型的趋势 exog
。
这里 https://gist.github.com/josef-pkt/1ea164439b239b228557 是我的秘密要点,我曾经弄清楚如何在 ARMA 中使用 exog。
当前的 master 和即将发布的 statsmodels 0.7 修复了我们使用 exog 进行样本外预测时的一些计时问题。
一般来说,可以用OLS等方法对解释变量的时间序列进行回归,然后用残差估计一个ARMA模型。这适用于预测,但是 ARMA 估计的标准误差和预测标准误差将是错误的,因为它们没有考虑初始估计 (AFAIK)。
我想为它做一个粗略的ARMA预测,即了解更多关于如何使用统计模型库的知识,看看它是如何工作的。所以首先我启动了网络上某处的示例,但是 ARMA 拟合和预测不起作用,因为 MLE 不收敛。我决定系列不是静止的,所以首先,我想消除趋势,这对我来说是一个挑战。这是代码:
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()
所以,打印 'atrend' 得到:
[[ 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. ]]
我完全不明白。 我要求有一个 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
对于抛物线,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
对于三次方程 y = 0.292x3 - 9.649x2 + 92.319x + 917.47。 即使是现在,我也不知道如何将这些值插入到 arma =tsa.ARMA(f['Close'].values, order =(2,2)) 以检查我是否可以完成下一个任务。
总而言之,我的想法是简要介绍一下 ARMA 预测在 python 中的使用是多么容易,但对我来说并非如此。
这应该很容易,但解释变量、exog 和趋势的处理仍然有一些棘手的部分。 (exog
处理是 ARMA 的后期添加。)
ARMA 有一个 exog
关键字用于平均预测的附加解释变量。该常量通过 fit
方法的 trend
关键字包含,目前不能包含在 exog
中。不幸的是,趋势关键字不允许任意顺序趋势。
所以在你的情况下,这样的事情应该有效:
arma = tsa.ARMA(f['Close'].values, exog=f[['trend1', 'trend2']] order=(2,2))
对于 predict
中的样本外预测,您将需要相同类型的趋势 exog
。
这里 https://gist.github.com/josef-pkt/1ea164439b239b228557 是我的秘密要点,我曾经弄清楚如何在 ARMA 中使用 exog。
当前的 master 和即将发布的 statsmodels 0.7 修复了我们使用 exog 进行样本外预测时的一些计时问题。
一般来说,可以用OLS等方法对解释变量的时间序列进行回归,然后用残差估计一个ARMA模型。这适用于预测,但是 ARMA 估计的标准误差和预测标准误差将是错误的,因为它们没有考虑初始估计 (AFAIK)。