Stochastic processes II

The ASTA team

Basic stochastic process models

Stochastic processes

White noise

White noise

Correlogram for white noise

w <- rnorm(100, mean = 0, sd = 1)
par(mfrow = c(2,1), mar = c(4,4,0,0))
ts.plot(w)
acf(w)

Random walk

Random walk

Properties of random walk

\[Cor(X_t, X_{t+k}) = \frac{1}{\sqrt{1+k/t}}\]

Simulation and correlogram of random walk

w <- rnorm(1000)
rw<-w
for(t in 2:1000) rw[t]<- rw[t-1] + w[t]
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
ts.plot(rw)
acf(rw, lag.max = 100)

Differencing

diffrw <- diff(rw)
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
ts.plot(diffrw)
acf(diffrw, lag.max = 30)

Example: Exchange rate

www <- "https://asta.math.aau.dk/eng/static/datasets?file=pounds_nz.dat"
exchange_data <- read.table(www, header = TRUE)[[1]]
exchange <- ts(exchange_data, start = 1991, freq = 4)
plot(exchange)

diffexchange <- diff(exchange)
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
plot(diffexchange)
acf(diffexchange)

First order auto-regressive models AR(\(1\))

Auto-regressive model of order \(1\): AR(\(1\))

Properties of AR(\(1\)) models

Simulation of AR(\(1\)) models

x <- arima.sim(model = list(ar = 0.7), n = 200)
plot(x)

acf(x)
k = 0:30
points(k, 0.7^k, col = "red")

Fitted AR(\(1\)) models

fit <- ar(x, order.max = 1, method="mle")
fit$ar
## [1] 0.7267829
fit$asy.var.coef
##             [,1]
## [1,] 0.002340669
se <- sqrt(fit$asy.var.coef)
se
##            [,1]
## [1,] 0.04838046
ci <- c(fit$ar - 2*se, fit$ar + 2*se)
ci
## [1] 0.6300219 0.8235438

Residuals for AR(\(1\)) models

res <- na.omit(fit$resid)
par(mfrow = c(2,1), mar = c(4,4,1,1))
plot(res)
acf(res)

AR(\(1\)) model fitted to exchange rate

fitexchange <- ar(exchange, order.max = 1, method="mle")
fitexchange$ar
## [1] 0.9437125
resexchange <- na.omit(fitexchange$resid)
par(mfrow = c(2,1), mar = c(4,4,1,1))
plot(resexchange)
acf(resexchange)

dexchange <- diff(exchange)
fitdiff <- ar(dexchange, order.max = 1,method="mle")
fitdiff$ar
## [1] 0.3495624
resdiff <- na.omit(fitdiff$resid)
par(mfrow = c(2,1), mar = c(4,4,1,1))
plot(resdiff)
acf(resdiff)

Prediction/forecasting from AR(\(1\)) model

\[\hat{x}_{n+1} = \bar{x} + \hat{\alpha}_1 \cdot (x_{n} - \bar{x}).\]

pred1 <- predict(fitexchange, n.ahead = 4)
pred1
## $pred
##          Qtr1     Qtr2     Qtr3     Qtr4
## 2000                            3.501528
## 2001 3.473716 3.447469 3.422699         
## 
## $se
##           Qtr1      Qtr2      Qtr3      Qtr4
## 2000                               0.1348262
## 2001 0.1853845 0.2208744 0.2482461
pred10 <- predict(fitexchange, n.ahead = 40)
lower10 <- pred10$pred-2*pred10$se
upper10 <- pred10$pred+2*pred10$se
ts.plot(exchange, pred10$pred, lower10, upper10, lty = c(1,2,3,3))

Auto-regressive models of higher order (AR(p)-models)

Auto-regressive models of higher order

Stationarity for AR(p) models

Estimation of AR(p) models

  1. The maximal non-zero lag \(p\) in the model.
  2. The autoregressive coefficients/parameters \(\alpha_1,\dots,\alpha_p\).

Example of AR(p) model for electricity data

fit <- ar(random, order.max = 5,method="mle")
fit
## 
## Call:
## ar(x = random, order.max = 5, method = "mle")
## 
## Coefficients:
##       1        2        3        4        5  
##  0.2047   0.0714  -0.0280  -0.1975  -0.2440  
## 
## Order selected 5  sigma^2 estimated as  0.0003103
resid <- na.omit(fit$resid)
par(mfrow = c(2,1), mar = c(4,4,1,1))
plot(resid)
acf(resid, lag.max = 30)

abs(polyroot(c(1,-fit$ar)))
## [1] 1.148425 1.415987 1.415987 1.148425 1.549525

Moving average models (MA(q)-models)

The moving average model

Simulation of MA(q) processes

acf(xsim,ylim=c(-1,1))
points(0:25, c(1,-.65, .36, -.11, rep(0,22)), col = "red")

Estimation of MA(q) models

xfit <- arima(xsim, order = c(0,0,3))
xfit
## 
## Call:
## arima(x = xsim, order = c(0, 0, 3))
## 
## Coefficients:
##           ma1     ma2      ma3  intercept
##       -0.8026  0.3898  -0.1843     0.0217
## s.e.   0.0737  0.0918   0.0790     0.0301
## 
## sigma^2 estimated as 1.097:  log likelihood = -293.42,  aic = 596.84
AIC(xfit)
## [1] 596.8406

Auto-regressive moving average models (ARMA)

Mixed models: Auto-regressive moving average models

xarma <- arima.sim(model = list(ar = -0.6, ma = 0.5), n = 200)
plot(xarma)

Example with exchange rate data

For the exchange rate data we may e.g. suggest either a AR(\(1\)), MA(\(1\)) or ARMA(\(1,1\)) model. We can compare fitted models using AIC (smaller is better):

exchange_ar <- arima(exchange, order = c(1,0,0))
AIC(exchange_ar)
## [1] -37.40417
exchange_ma <- arima(exchange, order = c(0,0,1))
AIC(exchange_ma)
## [1] -3.526895
exchange_arma <- arima(exchange, order = c(1,0,1))
AIC(exchange_arma)
## [1] -42.27357
exchange_arma
## 
## Call:
## arima(x = exchange, order = c(1, 0, 1))
## 
## Coefficients:
##          ar1     ma1  intercept
##       0.8925  0.5319     2.9597
## s.e.  0.0759  0.2021     0.2435
## 
## sigma^2 estimated as 0.01505:  log likelihood = 25.14,  aic = -42.27
par(mfrow = c(2,1), mar = c(4,4,1,1))
resid_arma <- na.omit(exchange_arma$residuals)
plot(resid_arma)
acf(resid_arma)