ARIMA模型的计算公式?
Calculation formula of ARIMA model?
在某些情况下,我必须手动进行预测,这意味着使用模型的公式。对于 AR(p) 模型,这很容易。但是对于ARIMA模型(p, d, q),d>=1,我就有点吃力了。
以下示例,我使用模型 AR(2) 进行计算。我有 1990 年到 2010 年的系列,我需要 2011 年的预测:
> a<-c(198,150,120,84,150,136,80,128,160,132,144,234,300,312,400,468,420,500,650,612,516)
> series<-ts(a,frequency=1,start=c(1990))
> fit<-Arima(series,c(2,0,0),method="ML")
> fit
Series: series
ARIMA(2,0,0) with non-zero mean
Coefficients:
ar1 ar2 intercept
1.1923 -0.2881 305.3748
s.e. 0.2174 0.2346 111.5251
sigma^2 estimated as 3727: log likelihood=-117.2
AIC=242.4 AICc=244.9 BIC=246.58
收到模型AR(2)的计算公式:
y[t]=305.3748+1.1923*y[t-1]-0.2881*y[t-2]
我预测 2011 年:
y[2011] = 305.3748+1.1923*y[2010]-0.2881*y[2009]
= 305.3748+1.1923*516-0.2881*612
= 744
然而,当我拟合 ARIMA 模型 (2,1,0) 时:
> fit2<-Arima(series,c(2,1,0),method="ML")
> fit2
Series: series
ARIMA(2,1,0)
Coefficients:
ar1 ar2
0.2561 -0.3494
s.e. 0.2196 0.2117
sigma^2 estimated as 3489: log likelihood=-110.1
AIC=226.2 AICc=227.7 BIC=229.19
不知道d = 1时公式怎么写?
还有一个问题,为什么当我用函数forecast()
做预测时,结果和我用公式计算的结果不一样?
> forecast(fit,h=1)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2011 468.1754 389.9369 546.4138 348.52 587.8308
ARIMA(2,1,0) 只是意味着你
- 区分系列,即将
series
替换为diff(series)
,
- 然后应用 ARIMA(2,0,0) 的公式,但应用于
diff(series)
、
- 最终 "re-integrate" 结果
cumsum
。
函数 fcst_ar2
包含 ARIMA(2,0,0) 的公式,fcst_diff_ar2
ARIMA(2,1,0) 的公式:
library(forecast)
#--------------------------------------------------------
fcst_ar2 <- function( coef, series, horizon )
{
y <- as.vector(series) - coef[3]
for ( i in 1:horizon ) {
y <- c( y, coef[2:1] %*% tail(y,2) )
}
return( y + coef[3] )
}
#--------------------------------------------------------
fcst_diff_ar2 <- function( coef, series, horizon )
{
y <- as.vector(series)
return( cumsum( c(y[1], fcst_ar2( c(coef,0), diff(y), horizon ) ) ) )
}
#========================================================
# Example:
a<-c(198,150,120,84,150,136,80,128,160,132,144,234,300,312,400,468,420,500,650,612,516)
series<-ts(a,frequency=1,start=c(1990))
fit<-Arima(series,c(2,0,0),method="ML")
fit2<-Arima(series,c(2,1,0),method="ML")
#--------------------------------------------------------
fcst_ar2( coef(fit), series, 3 )
# [1] 198.0000 150.0000 120.0000 84.0000 150.0000 136.0000 80.0000 128.0000
# [9] 160.0000 132.0000 144.0000 234.0000 300.0000 312.0000 400.0000 468.0000
# [17] 420.0000 500.0000 650.0000 612.0000 516.0000 468.1754 438.8091 417.5725
forecast( fit, 3 )
# Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
# 2011 468.1754 389.9369 546.4138 348.5200 587.8308
# 2012 438.8091 317.0562 560.5621 252.6041 625.0142
# 2013 417.5725 266.9412 568.2038 187.2018 647.9432
#--------------------------------------------------------
fcst_diff_ar2( coef(fit2), series, 3 )
# [1] 198.0000 150.0000 120.0000 84.0000 150.0000 136.0000 80.0000 128.0000
# [9] 160.0000 132.0000 144.0000 234.0000 300.0000 312.0000 400.0000 468.0000
# [17] 420.0000 500.0000 650.0000 612.0000 516.0000 504.6897 535.3388 547.1413
forecast( fit2, 3 )
# Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
# 2011 504.6897 428.9867 580.3927 388.9119 620.4675
# 2012 535.3388 413.7918 656.8858 349.4487 721.2289
# 2013 547.1413 405.0444 689.2383 329.8228 764.4599
在某些情况下,我必须手动进行预测,这意味着使用模型的公式。对于 AR(p) 模型,这很容易。但是对于ARIMA模型(p, d, q),d>=1,我就有点吃力了。 以下示例,我使用模型 AR(2) 进行计算。我有 1990 年到 2010 年的系列,我需要 2011 年的预测:
> a<-c(198,150,120,84,150,136,80,128,160,132,144,234,300,312,400,468,420,500,650,612,516)
> series<-ts(a,frequency=1,start=c(1990))
> fit<-Arima(series,c(2,0,0),method="ML")
> fit
Series: series
ARIMA(2,0,0) with non-zero mean
Coefficients:
ar1 ar2 intercept
1.1923 -0.2881 305.3748
s.e. 0.2174 0.2346 111.5251
sigma^2 estimated as 3727: log likelihood=-117.2
AIC=242.4 AICc=244.9 BIC=246.58
收到模型AR(2)的计算公式:
y[t]=305.3748+1.1923*y[t-1]-0.2881*y[t-2]
我预测 2011 年:
y[2011] = 305.3748+1.1923*y[2010]-0.2881*y[2009]
= 305.3748+1.1923*516-0.2881*612
= 744
然而,当我拟合 ARIMA 模型 (2,1,0) 时:
> fit2<-Arima(series,c(2,1,0),method="ML")
> fit2
Series: series
ARIMA(2,1,0)
Coefficients:
ar1 ar2
0.2561 -0.3494
s.e. 0.2196 0.2117
sigma^2 estimated as 3489: log likelihood=-110.1
AIC=226.2 AICc=227.7 BIC=229.19
不知道d = 1时公式怎么写?
还有一个问题,为什么当我用函数forecast()
做预测时,结果和我用公式计算的结果不一样?
> forecast(fit,h=1)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2011 468.1754 389.9369 546.4138 348.52 587.8308
ARIMA(2,1,0) 只是意味着你
- 区分系列,即将
series
替换为diff(series)
, - 然后应用 ARIMA(2,0,0) 的公式,但应用于
diff(series)
、 - 最终 "re-integrate" 结果
cumsum
。
函数 fcst_ar2
包含 ARIMA(2,0,0) 的公式,fcst_diff_ar2
ARIMA(2,1,0) 的公式:
library(forecast)
#--------------------------------------------------------
fcst_ar2 <- function( coef, series, horizon )
{
y <- as.vector(series) - coef[3]
for ( i in 1:horizon ) {
y <- c( y, coef[2:1] %*% tail(y,2) )
}
return( y + coef[3] )
}
#--------------------------------------------------------
fcst_diff_ar2 <- function( coef, series, horizon )
{
y <- as.vector(series)
return( cumsum( c(y[1], fcst_ar2( c(coef,0), diff(y), horizon ) ) ) )
}
#========================================================
# Example:
a<-c(198,150,120,84,150,136,80,128,160,132,144,234,300,312,400,468,420,500,650,612,516)
series<-ts(a,frequency=1,start=c(1990))
fit<-Arima(series,c(2,0,0),method="ML")
fit2<-Arima(series,c(2,1,0),method="ML")
#--------------------------------------------------------
fcst_ar2( coef(fit), series, 3 )
# [1] 198.0000 150.0000 120.0000 84.0000 150.0000 136.0000 80.0000 128.0000
# [9] 160.0000 132.0000 144.0000 234.0000 300.0000 312.0000 400.0000 468.0000
# [17] 420.0000 500.0000 650.0000 612.0000 516.0000 468.1754 438.8091 417.5725
forecast( fit, 3 )
# Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
# 2011 468.1754 389.9369 546.4138 348.5200 587.8308
# 2012 438.8091 317.0562 560.5621 252.6041 625.0142
# 2013 417.5725 266.9412 568.2038 187.2018 647.9432
#--------------------------------------------------------
fcst_diff_ar2( coef(fit2), series, 3 )
# [1] 198.0000 150.0000 120.0000 84.0000 150.0000 136.0000 80.0000 128.0000
# [9] 160.0000 132.0000 144.0000 234.0000 300.0000 312.0000 400.0000 468.0000
# [17] 420.0000 500.0000 650.0000 612.0000 516.0000 504.6897 535.3388 547.1413
forecast( fit2, 3 )
# Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
# 2011 504.6897 428.9867 580.3927 388.9119 620.4675
# 2012 535.3388 413.7918 656.8858 349.4487 721.2289
# 2013 547.1413 405.0444 689.2383 329.8228 764.4599