Linear Prediction for Time Series

36-467/36-667

24 September 2020

\[ \newcommand{\Expect}[1]{\mathbb{E}\left[ #1 \right]} \newcommand{\Var}[1]{\mathrm{Var}\left[ #1 \right]} \newcommand{\SampleVar}[1]{\widehat{\mathrm{Var}}\left[ #1 \right]} \newcommand{\Cov}[1]{\mathrm{Cov}\left[ #1 \right]} \newcommand{\TrueRegFunc}{\mu} \newcommand{\EstRegFunc}{\widehat{\TrueRegFunc}} \DeclareMathOperator{\tr}{tr} \DeclareMathOperator*{\argmin}{argmin} \DeclareMathOperator{\det}{det} \newcommand{\TrueNoise}{\epsilon} \newcommand{\EstNoise}{\widehat{\TrueNoise}} \]

In our last episode

Optimal linear prediction for time series

\[\begin{eqnarray} m(t_0) & = & \alpha + \vec{\beta} \cdot \left[\begin{array}{c} X(t_1) \\ X(t_2) \\ \vdots \\ X(t_n) \end{array}\right]\\ \alpha & = & \Expect{X(t_0)} - \vec{\beta} \cdot \left[\begin{array}{c} \Expect{X(t_1)}\\ \Expect{X(t_2)} \\ \vdots \\ \Expect{X(t_n)}\end{array}\right] ~ \text{(goes away if everything's centered)}\\ \vec{\beta} & = & {\left[\begin{array}{cccc} \Var{X(t_1)} & \Cov{X(t_1), X(t_2)} & \ldots & \Cov{X(t_1), X(t_n)}\\ \Cov{X(t_1), X(t_2)} & \Var{X(t_2)} & \ldots & \Cov{X(t_2), X(t_n)}\\ \vdots & \vdots & \ldots & \vdots\\ \Cov{X(t_1), X(t_n)} & \Cov{X(t_2), X(t_n)} & \ldots & \Var{X(t_n)}\end{array}\right]}^{-1} \left[\begin{array}{c} \Cov{X(t_0), X(t_1)}\\ \Cov{X(t_0), X(t_2)}\\ \vdots \\ \Cov{X(t_0), X(t_n)}\end{array}\right] \end{eqnarray}\]

Interpolation

Back to Kyoto

What we didn’t tell R to make up

When did the cherries flower in 1015?

Similarly for extrapolation

Getting the expectations and covariances

Repeating the experiment

\[\begin{eqnarray} \overline{x}(t) \equiv \frac{1}{n}\sum_{i=1}^{n}{x^{(i)}(t)} & \rightarrow & \Expect{X(t)}\\ \frac{1}{n}\sum_{i=1}^{n}{(x^{(i)}(t) - \overline{x}(t)) (x^{(i)}(s) - \overline{x}(s))} & \rightarrow & \Cov{X(t), X(s)} \end{eqnarray}\]

Making assumptions

Stationarity (assuming covariances are equal)

The autocovariance function

Assuming stationarity…

\[\begin{eqnarray} \mathbf{v} & \equiv & \left[\begin{array}{ccc} \gamma(0) & \gamma(|t_1 - t_2|) & \ldots & \gamma(|t_1 - t_n|)\\ \gamma(|t_2-t_1|) & \gamma(0) & \ldots & \gamma(|t_2-t_n|)\\ \vdots & \vdots & \ldots & \vdots\\ \gamma(|t_n-t_1|) & \gamma(|t_n-t_2|) & \ldots & \gamma(0)\end{array}\right]\\ \mathbf{c} & = & \left[\begin{array}{c} \gamma(|t_0 - t_1|)\\ \gamma(|t_0-t_2|) & \vdots \\ \gamma(|t_0 -t_n|)\end{array} \right]\\ \vec{\beta} & = & \mathbf{v}^{-1}\mathbf{c}\\ m(t_0) & = & \overline{x} + \vec{\beta} \cdot \left[\begin{array}{c} X(t_1) -\overline{x}\\ X(t_2) - \overline{x} \\ \vdots \\ X(t_n) -\overline{x}\end{array}\right] = \overline{x} + \mathbf{c}^{T}\mathbf{v}^{-1} \left[\begin{array}{c} X(t_1) -\overline{x}\\ X(t_2) - \overline{x} \\ \vdots \\ X(t_n) -\overline{x}\end{array}\right]\\ \Var{X(t_0) - m(t_0)} & = & \gamma(0) - \mathbf{c}^T\mathbf{v}^{-1} \mathbf{c} \end{eqnarray}\]

In R

acf(x, lag.max, type, plot, na.action, ...)

In R

kyoto.acf <- acf(kyoto$Flowering.DOY, lag.max = 100, type = "covariance", na.action = na.pass)

In R

kyoto.acf[0:5]
## 
## Autocovariances of series 'kyoto$Flowering.DOY', by lag
## 
##     0     1     2     3     4     5 
## 41.54  8.71  8.30  8.46  6.69  6.78
kyoto.acf[0:5]$acf
## , , 1
## 
##           [,1]
## [1,] 41.540228
## [2,]  8.706940
## [3,]  8.304284
## [4,]  8.464531
## [5,]  6.692514
## [6,]  6.780411

When was the flowering in 1015?

available.years <- with(na.omit(kyoto), Year.AD[Year.AD > 1015 - 49 & Year.AD < 1015 + 
    49])
historical.mean <- mean(kyoto$Flowering.DOY, na.rm = TRUE)
CovYZ <- matrix(kyoto.acf[abs(available.years - 1015)]$acf, ncol = 1)
year.lags <- outer(available.years, available.years, "-")
year.lags <- abs(year.lags)
VarZ <- kyoto.acf[year.lags]$acf
VarZ <- matrix(VarZ, ncol = length(available.years), byrow = FALSE)
Z <- with(kyoto, Flowering.DOY[Year.AD %in% available.years])
(fitted.value <- historical.mean + (Z - historical.mean) %*% solve(VarZ) %*% CovYZ)
##          [,1]
## [1,] 106.2388
(fitted.value.se <- sqrt(as.matrix(kyoto.acf[0]$acf) - t(CovYZ) %*% solve(VarZ) %*% 
    CovYZ))
##         [,1]
## [1,] 5.87276

When was the flowering in 1015?

This is a lot of work…

…and we’d need to re-do most of it for every other year

… so we should write a function

kyoto.prediction <- function(times) {
    historical.mean <- mean(kyoto$Flowering.DOY, na.rm = TRUE)
    historical.variance <- as.matrix(kyoto.acf[0]$acf)
    fits <- matrix(0, nrow = length(times), ncol = 3)
    colnames(fits) <- c("time", "fit", "se")
    fits[, "time"] <- times
    rownames(fits) <- paste(times)
    for (t in times) {
        available.years <- with(na.omit(kyoto), Year.AD[Year.AD > t - 49 & Year.AD < 
            t + 49])
        available.years <- setdiff(available.years, t)
        CovYZ <- matrix(kyoto.acf[abs(available.years - t)]$acf, ncol = 1)
        year.lags <- outer(available.years, available.years, "-")
        year.lags <- abs(year.lags)
        VarZ <- kyoto.acf[year.lags]$acf
        VarZ <- matrix(VarZ, ncol = length(available.years), byrow = FALSE)
        Z <- with(kyoto, Flowering.DOY[Year.AD %in% available.years])
        fits[paste(t), "fit"] <- historical.mean + (Z - historical.mean) %*% solve(VarZ) %*% 
            CovYZ
        fits[paste(t), "se"] <- sqrt(max(0, historical.variance - t(CovYZ) %*% solve(VarZ) %*% 
            CovYZ))
    }
    return(data.frame(fits))
}

Finally, some interpolation: close up

Finally, some interpolation: zoom out

Some extrapolation

plot(Flowering.DOY ~ Year.AD, data = kyoto, type = "p", pch = 16, cex = 0.5, xlim = c(1970, 
    2030))
new.fits <- kyoto.prediction(1970:2030)
lines(fit ~ time, data = new.fits, col = "blue")
lines(fit - se ~ time, data = new.fits, col = "blue", lty = "dashed")
lines(fit + se ~ time, data = new.fits, col = "blue", lty = "dashed")

Some further extrapolation

What’s going on here?

Stationarity and trend-plus-fluctuation

Stationary fluctuations around a trend

Detrending the cherry blossoms with a spline

kyoto.spline <- with(na.omit(kyoto), smooth.spline(x = Year.AD, y = Flowering.DOY))
residuals.with.NAs <- rep(NA, times = nrow(kyoto))
residuals.with.NAs[!is.na(kyoto$Flowering.DOY)] <- na.omit(kyoto$Flowering.DOY) - 
    kyoto.spline$y
kyoto$fluctuation <- residuals.with.NAs

Detrending the cherry blossoms with a spline

Detrending the cherry blossoms with a spline

Detrending the cherry blossoms with a spline

Covariance estimation

Checking stationarity

Checking stationarity: raw data

Checking stationarity: after spline detrending

Checking stationarity

Multiple time series

Where did all this come from?

Wiener’s research in 1942

Summing up

Backup: Not inverting the variance matrix

Backup: How good is the optimal linear predictor?

all.fits$std.residuals <- (kyoto$Flowering.DOY - all.fits$fit)/all.fits$se
mean(all.fits$std.residuals, na.rm = TRUE)
## [1] -0.008035619
sd(all.fits$std.residuals, na.rm = TRUE)
## [1] 1.007411

Ideally: mean 0, standard deviation 1

Backup: How good is the optimal linear predictor?

This is a pretty good random scatter of points

Backup: fancier covariance estimation

Backup: stationary linear predictor vs. spline

References

Kline, Ronald R. 2017. The Cybernetics Moment: Or Why We Call Our Age the Information Age. Baltimore, Maryland: Johns Hopkins University Press.

Kolmogorov, Andrei N. 1941. “Interpolation Und Extrapolation von Stationären Zufälligen Folgen.” Bulletin of the Academy Sciences, USSR, Mathematical Series 5:3–14.

Wiener, Norbert. 1949. Extrapolation, Interpolation, and Smoothing of Stationary Time Series: With Engineering Applications. Cambridge, Massachusetts: The Technology Press of the Massachusetts Institute of Technology.

———. 1953. Ex-Prodigy: My Childhood and Youth. New York: Simon; Schuster.

———. 1954. The Human Use of Human Beings: Cybernetics and Society. 2nd ed. Garden City, New York: Doubleday.

———. 1961. Cybernetics: Or, Control and Communication in the Animal and the Machine. 2nd ed. Cambridge, Massachusetts: MIT Press.