# Forecasts with ARIMA Models

In our time series class this morning, I was discussing forecasts with ARIMA Models. Consider some simple stationnary AR(1) simulated time series

```> n=95
> set.seed(1)
> E=rnorm(n)
> X=rep(0,n)
> phi=.85
> for(t in 2:n) X[t]=phi*X[t-1]+E[t]
> plot(X,type="l")``` If we fit an AR(1) model,

```> model=arima(X,order=c(1,0,0),
+             include.mean = FALSE)
> plot(P\$pred)
> lines(P\$pred+2*P\$se,col="red")
> lines(P\$pred-2*P\$se,col="red")
> abline(h=0,lty=2)
> abline(h=2*P\$se,lty=2,col="red")
> abline(h=-2*P\$se,lty=2,col="red")``` we observe the exponential decay of the forecast towards 0, and the increasing confidence interval (where the variance increases, from the variance of the white noise to the variance of the stationnary time series). Plain lines are conditional forecast (given our latest observation, since the AR(1) is a first order Markov process), and dotted lines are unconditional. Let us store some values, to use them as benchmark

```> s=P\$se
> y=P\$pred```

If we fit a MA(1) model

```> model=arima(X,order=c(0,0,1),
+             include.mean = FALSE)
> plot(P\$pred)
> lines(P\$pred+2*P\$se,col="red")
> lines(P\$pred-2*P\$se,col="red")
> abline(h=0,lty=2)
> abline(h=2*s,lty=2,col="red")
> abline(h=-2*s,lty=2,col="red")
> lines(y,col="grey")``` after two lags, the forecast is null, and the (conditional) variance remains constant. But if we consider a moving average process with a longer order,

```> model=arima(X,order=c(0,0,14),
+             include.mean = FALSE)
> plot(P\$pred)
> lines(P\$pred+2*P\$se,col="red")
> lines(P\$pred-2*P\$se,col="red")
> abline(h=0,lty=2)
> abline(h=2*s,lty=2,col="red")
> abline(h=-2*s,lty=2,col="red")
> lines(y,col="grey")``` we get an output that can be compared with the AR(1) processes. Which makes sense since our AR(1) process can also be seen as a MA(∞), with infinite order.

But if we think that our time series is not stationary, an we fit an integrated model

```> model=arima(X,order=c(0,1,0),
+             include.mean = FALSE)
> plot(P\$pred)
> lines(P\$pred+2*P\$se,col="red")
> lines(P\$pred-2*P\$se,col="red")
> abline(h=0,lty=2)
> abline(h=2*s,lty=2,col="red")
> abline(h=-2*s,lty=2,col="red")
> lines(y,col="grey")``` we observe the (standard) martingale property: the forecast is flat, and the confidence interval keeps increasing, and actually, the variance increases towards infinity (at a linear rate). So one should be very careful when differentiation a time series… it will have a huge impact on the forecasts….

# Simulation de séries temporelles

Un billet rapide pour reprendre le code tapé en cours, la semaine passée. Considérons un processus autorégressif d’ordre 1, où est un bruit blanc, stationnaire, i.e. appartient à l’intervalle . Le code pour simuler un tel processus est

```n=1000
bruit=rnorm(n)
phi1= .85
X=rep(NA,n)
X=0
for(t in 2:n){X[t]=phi1*X[t-1]+bruit[t]}
plot(acf(X),lwd=5,col='blue')
plot(pacf(X),lwd=5,col='blue')```  ou avec un autocorrélation au premier ordre négative,

`phi1= -0.7`  On peut aussi regarder un processus autorégressif au second ordre, sur la figure ci-dessous (avec en haut à gauche le triangle de stationnarité du couple de paramètres).

```phi1=  0.3
phi2=  0.5
X=rep(NA,n)
X[1:2]=0
for(t in 3:n){
X[t]=phi1*X[t-1]+phi2*X[t-2]+bruit[t]}``` Histoire de changer un peu, on peut regarder un processus moyenne mobile au premier ordre, où est un paramètre dans .

```theta1=  .8
X=rep(NA,n)
X=0
for(t in 2:n){
X[t]=bruit[t]+theta1*bruit[t-1]}``` ou une moyenne mobile du second ordre,

```theta1= -.6
theta2=  .5
X=rep(NA,n)
X[1:2]=0
for(t in 3:n){
X[t]=bruit[t]+theta1*bruit[t-1]+
theta2*bruit[t-2]}``` 