Time Series Analysis
Time Series Analysis
Overview
• Chapter 1: What are time series? Types of data, examples, objectives. Def-
initions, stationarity and autocovariances.
Relevant books
1. P.J. Brockwell and R.A. Davis (2002). Introduction to Time Series and Fore-
casting. Springer.
2. P.J. Brockwell and R.A. Davis (1991). Time Series: Theory and methods.
Springer.
4. R.H. Shumway and D.S. Stoffer (2006). Time Series Analysis and Its Ap-
plications. With R Examples. 2nd edition. Springer.
1
6. W.N. Venables and B.D. Ripley (2002). Modern Applied Statistics with S.
Springer.
Lectures take place Mondays 11-12 and Thursdays 10-11, weeks 1-4, plus
Wednesday Week 1 at 11, and not Thursday Week 3 at 10. There will be two
problem sheets, and two Practical classes Friday of Week 2 and Friday of Week
4 and there will be two Examples classes Tuesday 10-11 of Weeks 3 and 5. The
Practical in Week 4 will be assessed. Your marker for the problem sheets is Yang
Wu; the work is due Friday of Weeks 2 and 4 at 5 pm.
While the examples class will cover problems from the problem sheet, there
may not be enough time to cover all the problems. You will benefit most from the
examples class if you (attempt to) solve the problems on the sheet ahead of the
examples class.
Lecture notes are published at http://www.stats.ox.ac.uk/~reinert/
timeseries/timeseries.htm. The notes may cover more material than the
lectures. The notes may be updated throughout the lecture course.
Time series analysis is a very complex topic, far beyond what could be covered
in an 8-hour class. Hence the goal of the class is to give a brief overview of the
basics in time series analysis. Further reading is recommended.
Examples
• Environmental Modelling
2
• Demographics
• Medicine
• Engineering
• Quality Control
The above setup could be easily generalised: for example, the times of obser-
vation need not be equally spaced in time, the observations may only take values
from a discrete distribution, . . .
3
2 Examples: from Venables and Ripley, data from Diggle (1990)
lh: a series of 48 observations at 10-minute intervals on luteinizing hormone
levels for a human female
3.5
3.0
2.5
lh
2.0
1.5
0 10 20 30 40
Time
deaths: monthly deaths in the UK from a set of common lung diseases for
the years 1974 to 1979
4000
3500
3000
2500
deaths
2000
1500
1000
500
year
1.1 Definitions
Assume that the series Xt runs throughout time, that is (Xt )t=0,±1,±2,... , but is only
observed at times t = 1, . . . , n.
4
So we observe (X1 , . . . , Xn ). Theoretical properties refer to the underlying
process (Xt )t∈Z .
The notations Xt and X(t) are interchangeable.
The theory for time series is based on the assumption of ‘second-order sta-
tionarity’. Real-life data are often not stationary: e.g. they exhibit a linear trend
over time, or they have a seasonal effect. So the assumptions of stationarity below
apply after any trends/seasonal effects have been removed. (We will look at the
issues of trends/seasonal effects later.)
The process is called weakly stationary or second-order stationary if for all inte-
gers t, τ
E(Xt ) = µ
cov(Xt+τ , Xτ ) = γt
have the same distribution for all sets of time points t1 , . . . , tk and all integers τ .
5
We estimate γt by ct , and ρt by rt , where
min(n−t,n)
1 X ct
ct = [Xs+t − X][Xs − X] and rt = .
n c0
s=max(1,1−t)
0.10
0.05
0.00
−0.05
0 5 10 15
Lag
6
Series lh
1.0
0.8
0.6
0.4
ACF
0.2
0.0
−0.2
0 5 10 15
Lag
0.0
−0.5
Lag
Assume we have a time series without trends or seasonal effects. That is, if nec-
essary, any trends or seasonal effects have already been removed from the series.
How might we construct a linear model for a time series with autocorrelation?
Linear processes
The process (Xt ) is called a linear process if it has a representation of the form
∞
X
Xt = µ + cr ǫt−r
r=−∞
7
where µ is a common mean, {cr } is a sequence of fixed constants and {ǫt } are
independent randomP variables with mean 0 and common variance.
We assume c2r < ∞ to ensure that the variance of Xt is finite.
If the {ǫt } are identically distributed, then such a orocess is strictly stationary.
If cr = 0 for r < 0 it is said to be causal, i.e. the process at time t does not depend
on the future, as yet unobserved, values of ǫt .
The AR, MA and ARMA processes that we are now going to define are all
special cases of causal linear processes.
Xt = αXt−1 + ǫt
where ǫt is a white noise time series. [That is, the ǫt are a sequence of uncorrelated
random variables (possibly normally distributed, but not necessarily normal) with
mean 0 and variance σ 2 .]
This model is called an autoregressive (AR) model, since X is regressed on
itself. Here the lag of the autoregression is 1.
More generally we could have an autoregressive model of order p, an AR(p)
model, defined by
p
X
Xt = αi Xt−i + ǫt .
i=1
Xt = αXt−1 + ǫt
P
is not in the linear form Xt = µ + cr ǫt−r . However note that
Xt = αXt−1 + ǫt
= ǫt + α(ǫt−1 + αXt−2 )
= ǫt + αǫt−1 + α2 ǫt−2 + · · · + αk−1 ǫt−k+1 + αk Xt−k
= ǫt + αǫt−1 + α2 ǫt−2 + · · ·
8
which is in linear form.
But the sum converges only if |α| < 1. Thus |α| < 1 is a requirement for the
AR(1) process to be stationary.
We shall calculate the acf later.
Xt = ǫt + βǫt−1 .
9
2.3 ARMA processes
An autoregressive moving average process ARMA(p, q) is defined by
p q
X X
Xt = αi Xt−i + βj ǫt−j
i=1 j=0
where β0 = 1.
A slightly more general definition of an ARMA process incorporates a non-
zero mean value µ, and can be obtained by replacing Xt by Xt − µ and Xt−i by
Xt−i − µ above.
10
or even more concisely
φα (B)X = ǫ.
P
Recall that an MA(q) process is Xt = ǫt + qj=1 βj ǫt−j .
Define, for any complex number z, the moving average polynomial
φβ (z) = 1 + β1 z + · · · + βq z q .
or
X = φβ (B)ǫ.
For an MA(q) process we have already noted that there is no need for a station-
arity condition on the coefficients βj , but there is a different difficulty requiring
some restriction on the coefficients.
Consider the MA(1) process
Xt = ǫt + βǫt−1 .
γ0 = V ar(X0 ) = (1 + β 2 )σ 2
γ1 = Cov(X0 , X1 )
= Cov(ǫ0 , ǫ1 ) + Cov(ǫ0 , βǫ0 ) + Cov(βǫ−1 , ǫ1 ) + Cov(βǫ−1 , βǫ0 )
= Cov(ǫ0 , βǫ0 )
= βσ 2 ,
γk = 0, k > 2.
11
Now consider the identical process but with β replaced by 1/β. From above
we can see that the autocorrelation function is unchanged by this transformation:
the two processes defined by β and 1/β cannot be distinguished.
It is customary to impose the following identifiability condition:
all the zeros of the function φβ (z) lie outside the unit circle in the complex plane.
φα (B)X = φβ (B)ǫ.
2.5 Differencing
The difference operator ∇ is given by
∇Xt = Xt − Xt−1
These differences form a new time series ∇X (of length n−1 if the original series
had length n). Similarly
12
and so on.
If our original time series is not stationary, we can look at the first order dif-
ference process ∇X, or second order differences ∇2 X, and so on. If we find that
a differenced process is a stationary process, we can look for an ARMA model of
that differenced process.
In practice if differencing is used, usually d = 1, or maybe d = 2, is enough.
Hence the only non-zero terms in the sum are of the form σ 2 βi βi+k and we
have ( P
q−|k|
σ 2 i=0 βi βi+|k| |k| 6 q
γk =
0 |k| > q
13
and the acf is obtained via ρk = γk /γ0 .
In particular notice that the acf if zero for |k| > q. This ‘cut-off’ in the acf after
lag q is a characteristic property of the MA process and can be used in identifying
the order of an MA process.
Series ma1.sim
1.0
0.8
0.6
ACF
0.4
0.2
0.0
0 5 10 15 20 25 30
Lag
Series ma2.sim
1.0
0.8
0.6
ACF
0.4
0.2
0.0
0 5 10 15 20 25 30
Lag
14
2.8 Second order properties of AR(p)
0.4
0.2
0.0
0 5 10 15 20 25 30
Lag
15
Simulation: AR(2) with α1 = 0.5, α2 = 0.25
Series ar2.sim
1.0
0.8
0.6
ACF
0.4
0.2
0.0
0 5 10 15 20 25 30
Lag
The simplest way to construct the pacf is via the sample analogues of the Yule-
Walker equations for an AR(p)
p
X
ρk = αi ρ|k−i| k = 1, . . . , p
i=1
16
where we write ai,p to emphasize that we are estimating the autoregressive coeffi-
cients α1 , . . . , αp on the assumption that the underlying process is autoregressive
of order p.
So we have p equations in the unknowns a1,p , . . . , ap,p , which could be solved,
and the pth partial autocorrelation coefficient is ap,p .
and then
σk2 = σk−1
2
(1 − a2k,k ).
17
If the process Xt is genuinely an AR(p) process, then ak,k = 0 for k > p.
So a plot of the pacf should show a sharp drop to near zero after lag p, and this
is a diagnostic for identifying an AR(p).
Series ar1.sim
0.5
0.4
0.3
Partial ACF
0.2
0.1
0.0
0 5 10 15 20 25 30
Lag
Series ar2.sim
0.6
0.4
Partial ACF
0.2
0.0
0 5 10 15 20 25 30
Lag
18
Series ma1.sim
0.3
0.2
Partial ACF
0.1
0.0
−0.1
−0.2
0 5 10 15 20 25 30
Lag
0.0
−0.2
0 5 10 15 20 25 30
Lag
To determine whether the values of the acf, or the pacf, are negligible, we√can
use the approximation that √ they each have a standard deviation of around 1/ n.
So this would give ±2/ n as approximate confidence bounds (2 is an approx-
imation to 1.96). In R these are shown
√ as blue dotted lines.
Values outside the range ±2/ n can be regarded as significant at about the
5% level. But if a large number of rk values, say, are calculated it is likely that
some will exceed this threshold even if the underlying time series is a white noise
sequence.
Interpretation is also complicated by the fact that the rk are√not independently
distributed. The probability of any one rk lying outside ±2/ n depends on the
values of the other rk .
19
3 Statistical Analysis
3.1 Fitting ARIMA models: The Box-Jenkins approach
The Box-Jenkins approach to fitting ARIMA models can be divided into three
parts:
• Identification;
• Estimation;
• Verification.
3.1.1 Identification
This refers to initial preprocessing of the data to make it stationary, and choosing
plausible values of p and q (which can of course be adjusted as model fitting
progresses).
To assess whether the data come from a stationary process we can
• look at the data: e.g. a time plot as we looked at for the lh series;
For stationarity the acf should decay to zero fairly rapidly. If this is not true,
then try differencing the series, and maybe a second time if necessary. (In practice
it is rare to go beyond d = 2 stages of differencing.)
The next step is initial identification of p and q. For this we use the acf and the
pacf, recalling that
20
3.1.2 Estimation: AR processes
This can be used to guide our selection of the appropriate order p. Define an
approximate log likelihood by
σp2 ).
−2 log L = n log(b
Then this can be used for likelihood ratio tests.
Alternatively, p can be chosen by minimising AIC where
AIC = −2 log L + 2k
and k = p is the number of unknown parameters in the model.
If (Xt )t is a causal AR(p) process with i.i.d. WN(0, σǫ2 ), then (see Brockwell
and Davis (1991), p.241) then the Yule-Walker estimator α̂ is optimal with respect
to the normal distribution.
Moreover (Brockwell and Davis (1991), p.241) for the pacf of a causal AR(p)
process we have that, for m > p,
√
nα̂mm
is asymptotically standard normal. However, the elements of the vector α̂m =
(α̂1m , . . . , α̂mm ) are in general not asymptotically uncorrelated.
21
3.1.3 Estimation: ARMA processes
Now we consider an ARMA(p, q) process. If we assume a parametric model for
the white noise – this parametric model will be that of Gaussian white noise – we
can use maximum likelihood.
We rely on the prediction error decomposition. That is, X1 , . . . , Xn have joint
density
n
Y
f (X1 , . . . , Xn ) = f (X1 ) f (Xt | X1 , . . . , Xt−1 ).
t=2
The matrix of second derivatives of −2 log L, evaluated at the mle, is the ob-
served information matrix, and its inverse is an approximation to the covariance
matrix of the estimators. Hence we can obtain approximate standard errors for the
parameters from this matrix.
22
3.1.4 Verification
The third step is to check whether the model fits the data.
Two main techniques for model verification are
• Overfitting: add extra parameters to the model and use likelihood ratio or t
tests to check that they are not significant.
• Residual analysis: calculate residuals from the fitted model and plot their
acf, pacf, ‘spectral density estimates’, etc, to check that they are consistent
with white noise.
where K > p + q but much smaller than n, and rk is the acf of the residual series.
If the model is correct then, approximately,
Q ∼ χ2K−p−q
so we can base a test on this: we would reject the model at level α if Q >
χ2K−p−q (1 − α).
An improved test is the Box-Ljung procedure which replaces Q by
K
X rk2
Q̃ = n(n + 2) .
k=1
n−k
Once we have a suitable model for the time series, we could apply it to es-
timate, say, a trend in a time series. For example, suppose that x1 , . . . , xk are
explanatory variables, that ǫt is an ARMA(p,q)-process, and that we observe a
series yt . Our null model may then be that
Yt = µ + β1 x1 + . . . + βk xk + ǫt , t = 1, . . . , T,
23
and the alternative model could be
Yt = µ + ft (λ) + β1 x1 + . . . + βk xk + ǫt , t = 1, . . . , T,
where ft (λ) is a function for the trend. As ǫt is ARMA, we can write down the
likelihoods under the two models, and then carry out a generalised likelihood ratio
test to assess whether the trend is significant.
For confidence intervals, assume that all errors are independently normally
distributed. Then we can estimate the covariance matrix for ǫt using the Yule-
Walker equations; call this estimate V . Let X be the T × (k + 2) design matrix.
Then we estimate the covariance matrix of (µ̂, λ̂, βˆk ) by
Xt = α cos(ωt) + β sin(ωt) + ǫt
where ǫt is a white noise sequence, ω = 2π/p is the known frequency of the cyclic
fluctuations, and α and β are parameters (which we might want to estimate).
Examining the second-order properties of a time series via autocovariances/autocorrelations
is ‘analysis in the time domain’.
What we are about to look at now, examining the second-order properties by
considering the frequency components of a series is ‘analysis in the frequency
domain’.
24
3.2.1 The spectrum
Suppose we have a stationary time series Xt with autocovariances (γk ).
For any sequence of autocovariances (γk ) generated by a stationary process,
there exists a function F such that
Z π
γk = eikλ dF (λ)
−π
1. F (−π) = 0
3. the increments of F are symmetric about zero, meaning that for 0 6 a <
b 6 π,
F (b) − F (a) = F (−a) − F (−b).
dF (λ)
f (λ) =
dλ
is called the spectral density function and we have
Z π
γk = eikλ f (λ)dλ.
−π
P
It |γk | < ∞, then it can be shown that f always exists and is given by
∞ ∞
1 X iλk γ0 1X
f (λ) = γk e = + γk cos(λk).
2π k=−∞ 2π π k=1
25
By the symmetry of γk , f (λ) = f (−λ).
From the mathematical point of view, the spectrum and acf contain equiv-
alent information concerning the underlying stationary random sequence (Xt ).
However, the spectrum has a more tangible interpretation in terms of the inherent
tendency for realizations of (Xt ) to exhibit cyclic variations about the mean.
[Note that some authors put constants of 2π in different places. For example,
some put a factor of 1/(2π) in the integral expression for γk in terms of F, f , and
then they don’t need a 1/(2π) factor when giving f in terms of γk .]
Example: WN(0, σ 2 )
Note that the frequency is measured in cycles per unit time; for example, at
frequency 21 the series makes a cycle every two time units. The number of time
periods to complete a cycle is 2. In general, for frequency λ the number of time
units to complete a cycle is λ1 .
Data which occurs at discrete time points will need at least two points to de-
termine a cycle. Hence the highest frequency of interest is 12 .
Rπ
The integral −π eikλ dF (λ) is interpreted as a so-called Riemann-Stieltjes in-
tegral. If F is differentiable with derivative f , then
Z π Z π
ikλ
e dF (λ) = eikλ f (λ)dλ.
−π −π
If F is such that
0 if λ < λ0
F (λ) =
a if λ ≥ λ0
then Z π
eikλ dF (λ) = aeikλ0 .
−π
26
The integral is additive; if
0 if λ < λ0
F (λ) = a if λ0 ≤ λ < λ1
a+b if λ ≥ λ1
then
Z π Z λ1 Z π
ikλ ikλ
e dF (λ) = e dF (λ) + eikλ dF (λ)
−π λ0 λ1
ikλ0
= ae + (a + b − a)eikλ1
= aeikλ0 + beikλ1 .
Xt = U1 sin(2πλ0 t) + U2 cos(2πλ0 t)
with U1 , U2 independent, mean zero, variance σ 2 random variables. Then this pro-
cess has frequency λ0 ; the number of time periods for the above series to complete
one cycle is exactly λ10 . We calculate
σ2
γh = (cos(2πλ0 h) − cos(2πλ0 (2t + h))
2
+ cos(2πλ0 h) − + cos(2πλ0 (2t + h)))
= σ 2 cos(2πλ0 h)
σ 2 −2πiλ0 h
= e + e2πiλ0 h .
2
27
σ2
So, with a = b = 2
, we use
0 if λ < −λ0
σ2
F (λ) = 2
if − λ0 ≤ λ < λ0
2
σ if λ ≥ λ0 .
1.0
0.5
frequency
28
Simulation: AR(1) with α = −0.5
Series: ar1b.sim
AR (2) spectrum
2.0
spectrum
1.0
0.5
frequency
Plotting the spectral density f (λ), we see that in the case α > 0 the spectral
density f (λ) is a decreasing function of λ: that is, the power is concentrated at
low frequencies, corresponding to gradual long-range fluctuations.
For α < 0 the spectral density f (λ) increases as a function of λ: that is,
the power is concentrated at high frequencies, which reflects the fact that such a
process tends to oscillate.
ARMA(p, q) process
p q
X X
Xt = αi Xt−i + βj ǫt−j
i=1 j=0
29
Example: AR(1) Here φα (z) = 1 − αz and φβ (z) = 1, so, for −π 6 λ < π,
σ2
f (λ) = |1 − αe−iλ |−2
2π
σ2
= |1 − α cos λ + iα sin λ|−2
2π
σ2
= {(1 − α cos λ)2 + (α sin λ)2 }−1
2π
σ2
=
2π(1 − 2α cos λ + α2 )
as calculated before.
Example: MA(1)
Here φα (z) = 1, φβ (z) = 1 + θz, and we obtain, for −π 6 λ < π,
σǫ2
f (λ) = |1 + θe−iλ |2
2π
σǫ2
= (1 + 2θ cos(λ) + θ2 ).
2π
Plotting the spectral density f (λ), we would see that in the case θ > 0 the
spectral density is large for low frequencies, small for high frequencies. This is
not surprising, as we have short-range positive correlation, smoothing the series.
For θ < 0 the spectral density is large around high frequencies, and small
for low frequencies; the series fluctuates rapidly about its mean value. Thus, to a
coarse order, the qualitative behaviour of the spectral density is similar to that of
an AR(1) spectral density.
30
and the sine/cosine waves of frequency ω. The periodogram I(ω) is given by
n 2
1 X
−iωt
I(ω) = e Xt
2πn t=1
( )2 ( n )2
Xn X
1
= Xt sin(ωt) + Xt cos(ωt) .
2πn t=1 t=1
So the periodogram and the estimated autocovariance function contain the same
information. For the purposes of interpretation, sometimes one will be easier to
interpret, other times the other will be easier to interpret.
1e−02
1e−03
frequency
bandwidth = 0.000144
31
Series: ar1b.sim
Raw Periodogram
1e+01
1e+00
spectrum
1e−01
1e−02
1e−03
frequency
bandwidth = 0.000144
1e−01
1e−02
1e−03
frequency
bandwidth = 0.000144
Note that var[I(ω)] ≈ f (ω)2 , which does not tend to zero as n → ∞. So I(ω)
is NOT a consistent estimator.
32
This can be used to test residuals in a fitted model, for example. If we hope that
our residual series is white noise, the the cumulative periodogram of the residuals
should increase linearly: i.e. we can plot the cumulative periodogram (in R) and
look to see if the plot is an approximate straight line.
2πk
If Xt , t = 0, ±1, ±2, . . . is Gaussian white noise, and if ωk = n
are the
Fourier frequencies, −π < ωk ≤ π, then the random variables
Pi
I(ωk )
Pqk=1 , r = 1, . . . , q − 1,
k=1 I(ωk )
Xt = cos(πt/3) + ǫt t = . . . , 100
where {ǫt } is Gaussian white noise with variance 1. There is a peak in the peri-
odogram at ω17 = 0.34π.
3.2.3 Smoothing
The idea behind smoothing is to take weighted averages over neighbouring fre-
quencies in order to reduce the variability associated with individual periodogram
values.
The main form of a smoothed esimator is given by
Z
ˆ 1 λ−ω
f (ω) = K I(λ)dλ.
h h
Here K is some kernel function (= a probability density function), for example a
standard normal pdf, and h is the bandwidth.
33
The bandwidth h affects the degree to which this process smooths the peri-
odogram. Small h = indictes a little smoothing, large h = a lot of smoothing.
Writing
2π ωj − ω
gj = K
hn h
we calculate that
X
E(fˆ(ω)) ≈ gj f (ωj )
j
and
X Z
2π
V ar(fˆ(ω)) ≈ gj2 f (ωj )2 ≈ f (ω)2 K(x)2 dx
j
nh
as well as
Z
f ′′ (ω) 2
bias(fˆ(ω)) ≈ h x2 K(x)dx,
2
see Venables and Ripley, p.408. Then
q
2bias(fˆ(ω))/f ′′ (ω)
Example series: lh
34
3.5
3.0
2.5
lh
2.0
1.5
0 10 20 30 40
Time
2000
1500
1000
500
year
40
35
30
25
0 1 2 3 4 5 6
frequency
bandwidth = 0.0481, 95% C.I. is (−6.26,16.36)dB
35
Suppose we have estimated the periodogram values I(ω1 ), I(ω2 ), . . . , where
ωj = 2πj/n, j = 1, 2, . . . .
An example of a simple way to smooth is to use a moving average, and so
estimate I(ωj ) by
1 1 1
I(ωj−4 ) + [I(ωj−3 ) + I(ωj−2 ) + · · · + I(ωj+3 )] + I(ωj+4 ).
16 8 16
1
Observe that the sum of the weights above (i.e. the 16 s and the 18 s) is 1.
Keeping the sum of weights equal to 1, this process could be modified by using
more, or fewer, I(ωk ) values to estimate I(ωj ).
Also, this smoothing process could be repeated.
36
lh: Smoothed Periodogram, spans=3
0
−5
spectrum (dB)
−10
−15
frequency
bandwidth = 0.0159, 95% C.I. is (−4.32, 7.73)dB
−8
−10
−12
−14
frequency
bandwidth = 0.0217, 95% C.I. is (−3.81, 6.24)dB
−6
−8
−10
−12
frequency
bandwidth = 0.0301, 95% C.I. is (−3.29, 4.95)dB
37
deaths: Smoothed Periodogram, spans=c(3,3)
55
50
spectrum (dB)
45
40
35
0 1 2 3 4 5 6
frequency
bandwidth = 0.173, 95% C.I. is (−3.81, 6.24)dB
40
35
0 1 2 3 4 5 6
frequency
bandwidth = 0.241, 95% C.I. is (−3.29, 4.95)dB
40
35
0 1 2 3 4 5 6
frequency
bandwidth = 0.363, 95% C.I. is (−2.74, 3.82)dB
38
Series: lh
1.0
0.8
0.6
0.4
0.2
0.0
0.0 0.1 0.2 0.3 0.4 0.5
frequency
Series: deaths
1.0
0.8
0.6
0.4
0.2
0.0
0 1 2 3 4 5 6
frequency
39
Series lh
0.6
0.4
Partial ACF
0.2
0.0
−0.2
5 10 15
Lag
cpgram(lh.ar1$resid)
AR(1) fit to lh
1.0
0.8
0.6
0.4
0.2
0.0
frequency
40
lh.ar <- ar(lh, order.max = 9)
lh.ar$order
lh.ar$aic
with σ 2 = 0.20.
The same order is selected when using
AR(3) fit to lh
1.0
0.8
0.6
0.4
0.2
0.0
frequency
41
Standardized Residuals
2
1
0
−1
0 10 20 30 40
Time
ACF of Residuals
1.0
0.6
ACF
0.2
−0.2
0 5 10 15
Lag
0.8
p value
0.4
0.0
2 4 6 8 10
lag
0 10 20 30 40
Time
ACF of Residuals
1.0
0.6
ACF
0.2
−0.2
0 5 10 15
Lag
0.4
0.0
2 4 6 8 10
lag
42
We might write such a decomposition model as the additive model
Xt = mt + st + Zt
where
Xt = mt st Zt .
It is often possible to look at a time plot of the series to spot trend and seasonal
behaviour. We might look for a linear trend in the first instance, though in many
applications non-linear trend is also of interest and present.
Periodic behaviour is also relatively straightforward to spot. However, if there
are two or more cycles operating at different periods in a time series, then it may
be difficult to detect such cycles by eye. A formal Fourier analysis can help.
The presence of both trend and seasonality together can make it more difficult
to detect one or the other by eye.
Example: Box and Jenkins airline data. Monthly totals (thousands) of inter-
national airline passengers, 1949 to 1960.
600
500
AirPassengers
400
300
200
100
Time
43
airpass.log <- log(AirPassengers)
ts.plot(airpass.log)
6.5
6.0
airpass.log
5.5
5.0
Time
44
The resulting series Yt = Xt − m
b t is the detrended time series.
6.5
6.0
airpass.log
5.5
5.0
time step
time step
2: Smoothing
If the aim is to provide an estimate of the local trend in a time series, then we
can apply a moving average. That is, take a small sequence of the series values
Xt−q , . . . , Xt , . . . , Xt+q , and compute a (weighted) average of them to obtain a
smoothed series value at time t, say m b t , where
q
1 X
m
bt = Xt+j .
2q + 1 j=−q
45
It is useful to think of {m bt } by application of
b t } as a process obtained from {X
P∞
b t = j=−∞ aj Xt+j , with weights aj = 1/(2q + 1), −q 6 j 6 q,
a linear filter m
and aj = 0, |j| > q.
This filter is a ‘low pass’ filter since it takes data Xt and removes from it the
rapidly fluctuating component Yt = Xt − m b t , to leave the slowly varying estimated
trend term m b t.
We should not choose q too large since, if mt is not linear, although the filtered
process will be smooth, it will not be a good estimate of mt .
If we apply two filters in succession, for example to progressively smooth a
series, we are said to be using a convolution of the filters.
By careful choice of the weights aj , it is possible to design a filter that will not
only be effective in attenuating noise from the data, but which will also allow a
larger class of trend functions.
Spencer’s 15-point filter has weights
aj = a−j |j| 6 7
aj = 0 |j| > 7
1
(a0 , a1 , . . . , a7 ) = (74, 67, 46, 21, 3, −5, −6, −3)
320
and has the property that a cubic polynomial passes through the filter undistorted.
spencer.wts <- c(-3,-6,-5,3,21,46,67,74,67,46,21,3,-5,-6,-3)/320
airpass.filt <- filter(airpass.log, spencer.wts)
ts.plot(airpass.log, airpass.filt, lty=c(2,1))
5.5
5.0
Time
46
Detrended series via filtering:
0.15
0.10
0.05
airpass.log − airpass.filt
0.00
−0.05
−0.10
−0.15
Time
3: Differencing
Recall that the difference operator is ∇Xt = Xt −Xt−1 . Note that differencing
is a special case of applying a linear filter.
We can think of differencing as a ‘sample derivative’. If we start with a linear
function, then differentiation yields a constant function, while if we start with a
quadratic function we need to differentiate twice to get to a constant function.
Similarly, if a time series has a linear trend, differencing the series once will
remove it, while if the series has a quadratic trend we would need to difference
twice to remove the trend.
Year
47
3.4 Seasonality
After removing trend, we can remove seasonality. (Above, all detrended versions
of the airline data clearly still have a seasonal component.)
1: Block averaging
The simplest way to remove seasonality is to average the observations at the
same point in each repetition of the cycle (for example, for monthly data average
all the January values) and subtract that average from the values at those respective
points in the cycle.
2: Seasonal differencing
The seasonal difference operator is ∇s Xt = Xt − Xt−s where s is the period
of the seasonal cycle. Seasonal differencing will remove seasonality in the same
way that ordinary differencing will remove a polynomial trend.
airpass.diff<-diff(airpass.log)
airpass.diff2 <- diff(airpass.diff, lag=12)
ts.plot(airpass.diff2)
0.15
0.10
0.05
airpass.diff2
0.00
−0.05
−0.10
−0.15
Time
After differencing at lag 1 (to remove trend), then at lag 12 (to remove seasonal
effects), the log(AirPassengers) series appears stationary.
That is, the series ∇∇12 X, or equivalently the series (1 − B)(1 − B 12 )X,
appears stationary.
R has a function stl which you can use to estimate and remove trend and
seasonality using ‘loess’.
48
stl is a complex function, you should consult the online documentation be-
fore you use it. The time series chapter of Venables & Ripley contains examples
of how to use stl. As with all aspects of that chapter, it would be a good idea for
you to work through the examples there.
Φp (B)ΦP (B s )Y = Φq (B)ΦQ (B s )ǫ
Y = (1 − B)d (1 − B s )D X.
Here:
• s is the number of observations per season, so s = 12 for monthly data;
• d is the order of ordinary differencing (we were content with d = 1 for the
air passenger data).
49
This model is often referred to as an ARIMA((p, d, q) × (P, D, Q)s ) model.
Examples
(1 − αB)Yt = (1 + βB 12 )ǫt
where
Yt = Xt − Xt−12 .
2. The ‘airline model’ (so named because of its relevance to the air passenger
data) is a ARIMA model of order (0, 1, 1) × (0, 1, 1)12 .
This model can be written
Yt = (1 + β1 B)(1 + β2 B 12 )ǫt
50
Then
∞
X ∞
X
XT +k − X̂T,k = cr ǫT +k−r − cr+k ǫT −r
r=0 r=0
k−1
X X∞ ∞
X
= cr ǫT +k−r + cr ǫT +k−r − cs ǫT −s+k
r=0 r=k s=k
k−1
X
= cr ǫT +k−r .
r=0
Thus ∞
X
X̂T,k = cr+k ǫT −r
r=0
is our theoretical optimal predictor.
Note that the mean squared prediction errors are based solely on the uncer-
tainty of prediction; they do not take errors in model identification into account.
For k ≤ 0 we can use thus relation to calculate ǫ̂t for 1 ≤ t ≤ T . For k > 0 we
define ǫ̂t = 0 for t > T , to calculate the forecasts.
The difficulty is how to start off the recursion. Two standard solutions are
Either assume Xt = ǫt = 0 for all t ≤ 0,
or forecast the series in reverse direction to determine estimates of X0 , X−1 , . . . ,
as well as ǫ0 = 0, ǫ−1 = 0, etc.
A superior approach is to recast the model in state space form and apply the
Kalman filter.
51
4 State space models
State-space models assume that the observations (Xt )t are incomplete and noisy
functions of some underlying unobservable process (Yt )t , called the state process,
which is assumed to have a simple Markovian dynamics. The general state space
model is described by
1. Y0 , Y1 , Y2 , . . . is a Markov chain
2. Conditionally on {Yt }t , the Xt ’s are independent, and Xt depends on Yt
only.
When the state variables are discrete, one usually calls this model a hidden Markov
model; the term state space model is mainly used for continuous state variables.
52
so we obtain an MA(1)-process.
Yt = φYt−1 + vt
Xt = Yt + wt ,
where (vt )t and (wt )t are two independent white noise sequences with vt and wt
being mean zero and having variances V 2 and W 2 , respectively. Then
The right-hand side shows that all correlations at lags > 1 are zero. Hence
the right-hand side is equivalent to an MA(1) model, and thus Xt follows an
ARMA(1,1)-model.
ǫ t = v t + wt
gives a mean zero whiteq noise series with variance σǫ2 = V 2 + W 2 . Thus ǫt has
2 2
the same distribution as V W+W2 wt . Putting
r
W2
β=− φ
V 2 + W2
thus gives that
vt + wt − φwt−1 = ǫt + βǫt−1 .
53
4.2 Filtering, smoothing, and forecasting
The primary aims of the analysis of state space models are to produce estimators
for the underlying unobserved signal Yt given the data Xs = (X1 , . . . , Xs ) up to
time s. When s < t the problem is called forecasting, when s = t it is called
filtering, and when s > t it is called smoothing. For a derivation of the results
below see also Smith (2001).
We will throughout assume the white noise to be Gaussian.
The Kalman filter is a method for updating parameter estimates instantly when
a new observation occurs, based on the likelihood of the current data - without
having to re-estimate a large number of parameters using all past data.
The state of the filter is represented by two variables: the estimate of the state
at time t; and the error covariance matrix (a measure of the estimated accuracy of
the state estimate). The Kalman filter has two distinct phases: Predict and Update.
The predict phase uses the state estimate from the previous timestep to produce
an estimate of the state at the current timestep. In the update phase, measurement
information at the current timestep is used to refine this prediction to arrive at a
new, (hopefully) more accurate state estimate, again for the current timestep.
54
distributions. Suppose that
Z1 µ1 Σ11 Σ12
∼ MVN , . (3)
Z2 µ2 Σ21 Σ22
Then the conditional distribution of Z1 given Z2 = z2 is
L(Z1 |Z2 = z2 ) = MVN µ1 + Σ12 Σ−1 −1
11 (z2 − µ2 ), Σ11 − Σ12 Σ11 Σ21 (4)
and conversely, if Z2 ∼ MVN (µ2 , Σ22 ) and if (4) holds, then (3) holds.
and
V ar(Z1 |Z2 , Z3 ) = V ar(Z1 |Z2 ) − Σ13 Σ−1 ′
33 Σ13 .
Now
at+1 = E(Yt+1 |X(t) )
= E(Yt + ηt |X(t) )
= E(Yt |X(t) )
55
and
Pt+1 = V ar(Yt+1 |X(t) )
= V ar(Yt + ηt |X(t) )
= V ar(Yt |X(t) ) + ση2 .
Now we apply the conditional mean and variance formula for multivariate
normally distributed random variables:
E(Yt |X(t) ) = E(Yt |X(t−1) , vt )
= E(Yt |X(t−1) ) + Cov(Yt , vt )V ar(vt )−1 vt ,
where
Cov(Yt , vt ) = E(Yt (xt − at ))
= E[Yt (Yt + ǫt − at )]
= E[Yt (Yt − at )]
= E[(Yt − at )2 ] + at E[E(Yt − at |X(t−1) )]
= E[(Yt − at )2 ]
= E[E{(Yt − at )2 |X(t−1) }]
= E[V ar(Yt |X(t−1) )]
= Pt ,
56
and
V ar(vt ) = Ft
= V ar(Yt + ǫt − at )
= V ar(Yt |X(t−1) ) + σǫ2
= Pt + σǫ2 .
Put
Pt
Kt =
Ft
then, since at = E(Yt |X(t−1) ), we have
E(Yt |X(t) ) = at + Kt vt .
Now
57
Now consider the more general model
Yt = Gt Yt−1 + vt
Xt = Ht Yt + wt ,
with (vt )t independent white noise W N (0, Vt ), and (wt )t ind. W N (0, Wt ). Here,
Yt is a vector representing unknown states of the system, and Xt are the observed
data. . Put Xt = (X1 , X2 , . . . , Xt ), the history of X up to time t, and
Suppose Y00 = µ and P00 = Σ0 , and that the conditional distribution of Yt−1 given
the history Xt−1 up to time t − 1,
E(Xt |Yt ) = Ht Yt
V ar(Xt |Yt ) = Wt .
With
Rt = Gt Pt−1 G−1
t + Vt
Ytt = Gt Yt−1
t−1
+ Rt HtT (Wt + Ht Rt HtT )−1 (Xt − Ht Gt Yt−1
t−1
)
(t−1)
Pt = Rt − Rt HtT (Wt + Ht Rt HtT )−1 Ht Rt .
58
These equations are known as the Kalman filter updating equations. This solves
the filtering problem.
t−1
Have a look at the expression for Ytt . It contains the term Gt Yt−1 , which is simply
t−1
what we would predict if it were known that Yt−1 = Yt−1 , plus a term which
t−1
depends on the observed error in forecasting, i.e. (Xt − Ht Gt Yt−1 ).
Note that we initialized the recursion by X00 = µ and P00 = σ0 . Instead one might
have initialized the recursion by some prior distribution, of by an uninformative
prior X00 = 0, P00 = kI, where I denotes the identity matrix.
s s
For forecasting, suppose t > s. By induction, assume we know Yt−1 , Pt−1 . Then
Yts = Gt Yt−1
s
Pts = Gt Pt−1
s
GTt + Vt .
The R command predict(arima) uses Kalman filters for prediction; see for
example the airline passenger example, with the code on the course website.
This fact is the basis of the prediction error decomposition, giving us a likelihood
for parameter estimation.
where
t−1 T
Jt−1 = Pt−1 H (Ptt−1 )−1 .
Note that these procedures differ for different initial distributions, and sometimes
it may not clear which initial distribution is appropriate.
59
See also Kalman filters made easy by Terence Tong, at
http://openuav.astroplanes.com/library/docs/writeup.pdf.
Example: Johnson & Johnson quarterly earnings per share, 1960-1980. The
model is
Xt = Tt + St + vt , observed
Tt = φTt−1 + wt1 , trend
St = St−1 + St−2 + St−3 + wt2 seasonal component.
Assume that the seasonal components sum to zero over the four quarters, in ex-
pectation. Here wt are i.i.d. mean-zero normal vectors with covariance matrix Q,
and vt are i.i.d. mean-zero normal with covariance R.
The state vector is
Yt = (Tt , St , St−1 , St−2 ).
See Shumway and Stoffer, p.334-336. The initial estimates are as follows.
Growth is about 3 % per year, so choose φ = 1.03. The initial mean is fixed at
(0.5, 0.3, 0.2, 0.1)t , and the initial covariance matrix is diagonal with Σ0,i,i = 0.01,
for i = 1, 2, 3, 4. Initial state covariance values were taken as q11 = 0.01, q22 =
0.1 to reflect relatively low uncertainty in the trend model compared to the sea-
sonal model. All other elements of Q are taken to be 0. We take R = 0.04. Itera-
tive estimation (using the EM algorithm) yielded, after 70 iterations, R = .0086,
φ = 1.035, q11 = 0.0169, q22 = 0.0497, and µ = (.55, .21, .15, .06).
5 Non-linear models
Note that this chapter and the next chapter were not covered in lectures.
Financial time series, e.g. share prices, share price indices, spot interest rates,
currency exchange rates, have led to many specialized models and methods.
• ARCH models
60
• Stochastic Volatility models
Stochastic Volatility
In stochastic volatility models there is some unobserved process known as the
volatility which directly influences the variance of the observed series. That is,
these have some similar characteristics to state space models.
• the yt have heavy tailed distributions (i.e. heavier tails than a normal distri-
bution)
yt | zt ∼ N (µt , σt2 )
61
where zt is some set of conditioning random variables (maybe lagged values of
yt ) and µt and σt2 are functions of zt .
yt | zt ∼ N (0, σt2 )
where
zt = (y1 , . . . , yt−1 )
σt2 = α0 + α1 yt−1 2 2
+ · · · + αp yt−p .
yt | ht ∼ N (0, eht )
where
ht+1 = γ0 + γ1 ht + ηt
ηt ∼ N (0, ση2 )
yt = σt ǫt , σt2 = α0 + α1 yt−1
2
with ǫt ∼ N (0, 1), and the sequence of ǫt variables being independent. Here
α1 > 0 has to be satisfied to avoid negative variances. Note that the conditional
distribution of Yt given Yt−1 = yt−1 is
2
N (0, α0 + α1 yt−1 ).
62
Hence
E(Yt ) = E[E(Yt |Yt−1 )] = 0.
To calculate the variance, we re-write
yt2 = σt2 ǫ2t
2
α0 + α1 yt−1 = σt2
so that
yt2 − (α0 + α1 yt−1
2
) = σt2 ǫ2t − σt2 ,
or
yt2 = α0 + α1 yt−1
2
+ vt ,
with
vt = σt2 (ǫ2t − 1).
Note that ǫ2t ∼ χ21 . Now
E(vt ) = E[E(vt |Yt−1 )]
= E[σt2 E(ǫ2t − 1)] = 0,
and furthermore
Cov(vt+h , vt ) = E(vt vt+h ) = E[E(vt vt+h |Yt+h−1 )]
= E[vt E(vt vt+h |Yt+h−1 )] = 0.
Thus the error process vt is uncorrelated. If the variance of vt is finite and constant
in time, and if 0 ≤ α1 < 1, then yt2 is a causal AR(1)-process. In particular,
α0
E(Yt2 ) = V ar(Yt ) = .
1 − α1
In order for V ar(Tt2 ) < ∞ we need 3α12 < 1.
As the conditional distribution of Yt given the past is normal and easy to write
down, to estimate parameters in an ARCH(1)-model, usually conditional maxi-
mum likelihood is used. For a wide class of processes, asymptotic normality of
the estimators has been proven. A practical difficulty is that the likelihood sur-
face tends to be flat, so that even for the simplest form ARCH(1), the masimum
likelihood estimates of α0 and α1 can be quite imprecise.
63
5.2 GARCH and other models
The ARCH model can be thought of as an autoregressive model in yt2 . An ob-
vious extension of this idea is to consider adding moving average terms as well.
This generalization of ARCH is called GARCH. The simplest GARCH model is
GARCH(1,1):
yt = σt ǫt , σt2 = α0 + α1 yt−1
2 2
+ β1 σt−1
The simplest estimation scheme for the GARCH(1,1) model uses some initial
sample of observations to obtain a crude estimate of σt2 , and then use maximum
likelihood estimation based on the prediction error decomposition.
The R command garch in the tseries package uses the Jarque-Bera test for
normality, based on sample skewness and kurtosis. For a sample x1 , . . . , xn the
test statistic is given by
n 2 (κ − 3)2
s +
6 4
with P
1
n
(xi − x̄)3
s= P 3
1 2 2
n
(x i − x̄)
the sample skewness, and
1
P
n
(xi − x̄)4
κ= P 2
1
n
(xi − x̄)2
the sample kurtosis. For a normal distribution, the expected skewness is 0, and the
expected kurtosis is 3. To test the null hypothesis that the data come from a normal
distribution, the Jarque-Bera statistic is compared to the chi-square distribution
with 2 degrees of freedom.
64
5.3 Stochastic volatility
The basic alternative to ARCH-type models is to allow σt2 to depend not on past
observations but on some unobserved components.
The log-normal stochastic volatility model is
6 Further topics
6.1 Multivariate time series
Virtually all the above discussion generalizes when a vector is observed at each
point in time. In the time domain, analysis would typically use cross-correlations
and vector autoregressive-moving average models. In the frequency domain, de-
pendencies at different frequencies are analysed separately.
Yt+1 = g(Yt ) + ǫt ,
65
where g(y) or g(y, ǫ) is nonlinear.
For nonlinear time series, the amplitude (the periodogram) does not suffice to
estimate the spectral density, and the acf; instead the phase is also needed. That
is, we use vectors of time-delayed observations to describe the evolution of the
system. For example, suppose our time series is
1, 3, 6, 7, 4, 2, 4, 5, 6
and we want to describe it in a 3-dim space, using a delay of 1: then our vectors
are
(1, 3, 6); (3, 6, 7); (6, 7, 4); (7, 4, 2)
and so on, and we can see how these vectors move around in 3-dim space.
The interplay between randomness and nonlinearity generates new effects
such as coexistence of fixed points, periodic points, and chaotic attractors, and
new tools have been developed for these systems. In particular, nonlinear time
series analysis uses many ideas from deterministic chaos theory.
6.4 Chaos
There is a large literature centering around the idea that some simple deterministic
processes generate output that is very like a realization of a stochastic process.
In particular it satisfies sensitivity to the initial conditions. This is a completely
different approach to time series.
66