Stochastic processes I

The ASTA team

Concepts, terminology and examples

AP <- AirPassengers
plot(AP, ylab = "Bookings (1000s)")

APyear <- aggregate(AP, FUN = mean)
plot(APyear)

Such a systematic change in a time series that is not periodic is known as the trend of the series.

cyc <- cycle(AP)
cyc <- factor(cyc, labels = month.abb)
boxplot(AP ~ cyc)

APJun55Dec56 <- window(AP, start = c(1955, 6), end = c(1956, 12))
APJun55Dec56
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1955                     315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
APAug <- window(AP, start = c(1949, 8), end = end(AP), freq = TRUE)
ts.plot(APyear, APAug, col = c("black", "red"))
legend("topleft", legend = c("Mean", "August"), col = c("black", "red"), lty = 1)

Stochastic trend

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

Multiple series

CBEdata <- read.table("https://asta.math.aau.dk/eng/static/datasets?file=cbe.dat",
                      header = TRUE)
CBE <- ts(CBEdata, start = 1958, freq = 12)
plot(CBE)

APCBE <- ts.intersect(AP, CBE)
plot(APCBE)

cor(APCBE)
##                  AP   CBE.choc   CBE.beer   CBE.elec
## AP        1.0000000  0.6375808 -0.4434747  0.8841668
## CBE.choc  0.6375808  1.0000000 -0.6204467  0.7644756
## CBE.beer -0.4434747 -0.6204467  1.0000000 -0.3120179
## CBE.elec  0.8841668  0.7644756 -0.3120179  1.0000000

However, this does not imply that US air passengers really influence the electricity consumption in Australia (or vice versa)! Time series with similar trends and seasonality will typically show strong correlation even though they are unrelated. Better explanations may be due to similar population growth, seasonal patterns, etc.

Decomposition

Decomposition - trend term

choc <- CBE[, "choc"]
i <- 7
(choc[i-6]/2 + sum(choc[(i-5):(i+5)]) + choc[i+6]/2)/12
## [1] 2413.625

This computation can be done for all time points (except the first six and last six) using decompose, which returns a list with a component trend (and other things to be discussed shortly):

choc_decomp <- decompose(choc)
choc_trend <- choc_decomp$trend
window(choc_trend, end = c(1958, 8))
##           Jan      Feb      Mar      Apr      May      Jun      Jul
## 1958       NA       NA       NA       NA       NA       NA 2413.625
##           Aug
## 1958 2409.500

Decomposition - seasonal term

choc_no_trend <- choc - choc_trend
plot(choc_no_trend)

choc_jan <- mean(window(choc_no_trend, st = c(1958, 1), freq = TRUE), na.rm = TRUE)
choc_jan
## [1] -2450.538
choc_may <- mean(window(choc_no_trend, st = c(1958, 5), freq = TRUE), na.rm = TRUE)
choc_may
## [1] 1171.215

It appears that chocolate consumption typically is much higher in May than in January. The season effects are also calculated by decompose and stored in the list as seasonal:

plot(choc_decomp$seasonal)

Decomposition - random term

plot(choc_decomp)

Stationarity and autocorrelation

choc_rand <- na.omit(choc_decomp$random)
plot(choc_rand)

This is a single realization of something we may think of as a random experiment. If things had been different (different weather, different chocolate advertisements, different economic fluctuations, …) we could imagine another series.

This hypothetical scenario applies to any time series \(x_t\) where we imagine our dataset is somehow randomly seleceted among an entire ensemble of possible time series.

i <- 1:(length(choc)-1)
plot(choc[i], choc[i+1])
abline(lsfit(choc[i], choc[i+1]))

cor(choc[i], choc[i+1])
## [1] 0.8025919
i <- 1:(length(choc_rand)-1)
plot(choc_rand[i], choc_rand[i+1])
abline(lsfit(choc_rand[i], choc_rand[i+1]))

cor(choc_rand[i], choc_rand[i+1])
## [1] -0.02278652

Correlogram (empirical acf)

choc_acf <- acf(choc)

This is called the correlogram.

acf(choc_rand)

Typical patterns in correlograms

It can be illustrative to consider a few specific examples of correlograms:

x <- rnorm(100)
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
ts.plot(x)
acf(x, main = "", lag.max = 30)

x <- 1:100
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
ts.plot(x)
acf(x, main = "", lag.max = 30)

x <- sin((1:100)*2*pi/10)
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
ts.plot(x)
acf(x, main = "", lag.max = 100)

x0 <- c(3,5,2,1,4)
x <- rep(x0, 20)
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
ts.plot(x)
acf(x, main = "", lag.max = 30)

Partial autocorrelation function

x <- 1:100
par(mfrow = c(2,1), mar = c(4,4,0.5,0.5))
ts.plot(x)
pacf(x, main = "")