Notes On Programs Tramo and Seats: 11 March 2003
Notes On Programs Tramo and Seats: 11 March 2003
NOTES ON PROGRAMS
TRAMO AND SEATS
PART I
Introduction and Brief Review of Applied Time Series Analysis
Agustín Maravall
Bank of Spain
Thanks are due to Victor Gómez, Gianluca Caporello, Fernando Sánchez, and Nieves Morales.
x:\...\maravall\tramseat\cursos\reviewts.ts
Victor Gómez and Agustín Maravall, Programs TRAMO and SEATS, 1996.
1
1. INTRODUCTION
2
In our application,
we center on series observed with a 1, 2, 3, 4, 6, and 12 times a
year frequency. The most relevant ones:
TIME SERIES ≡ [ x1 , x2 , K, xT ]
12 - 36 ≤ T ≤ 600 observations
3
Monthly Time Series
700
600
500
400
300
200
100
0
1 9 17 25 33 41 49 57 65 73 81 89 97 105 113 121 129 137
Forecast
1200
1000
800
600
400
132
144
156
168
4
Missing Observations
500
450
400 *
350
* *
300 *
250
*
200
150
100
50
0
1 9 17 25 33 41 49 57 65 73 81 89 97 105 113 121 129 137
Interpolated Series
700
600
500
400
300
200
100
0
1 9 17 25 33 41 49 57 65 73 81 89 97 105 113 121 129 137
5
Outlier Contamination
140
120
100
80
60
40
20
0
1 10 20 30 40 50 60 70 80 90 100 110 120 130 140
Outliers
30
LS TC AO
20
10
-10
-20
-30
1 10 20 30 40 50 60 70 80 90 100 110 120 130 140
6
Series with Intervention Variable
140
120
100
80
60
40
20
0
1 10 20 30 40 50 60 70 80 90 100 110 120 130 140
20
10
-10
-20
-30
1 10 20 30 40 50 60 70 80 90 100 110 120 130 140
7
Regression variable and special effects
100
100
100
Seasonal Factors
140
130
120
110
100
90
80
70
1 8 15 22 29 36 43 50 57 64 71 78 85 92 99 106 113 120 127 134
8
Seasonally Adjusted Series
700
600
500
400
300
200
100
0
1 13 25 37 49 61 73 85 97 109 121 133
Trend-Cycle
700
600
500
400
300
200
100
0
1 13 25 37 49 61 73 85 97 109 121 133
9
Seasonal
Forecast: Factors
Seasonal Factors
140
130
120
110
100
90
80
70
120 132 144 156 168
1200
1000
800
600
400
200
0
120 132 144 156 168
10
ST and LT Trends
120
100
80
60
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
Business Cycle
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
11
Standard ("routine") treatment at present typically solves the
previous problems using different procedures, that often have little
to do with each other. For ex.:
* Forecast of a trend: ?
Often: Fit ARIMA to some trend, and obtain ARIMA forecasts
(not recommended)
* SE of x̂ at ? ( x at : SA series)
Important issue (Bach Commitee, Moore Committee,…)
and so on...
12
We shall present a methodology that
in an EFFICIENT way.
* interpretation
For example, the model may specify that the sum of
the seasonal component over a 12 consecutive-month
period is a zero-mean, small variance, stationary process.
* diagnostics
The joint distribution of the estimators can be derived,
and hence standard tests can be performed.
* inference
For example, we can obtain optimal forecasts of the
rate of growth of the SA series, with the associated SE.
13
The methodology is based on:
E (signal | observations)
14
2. BRIEF REVIEW OF APPLIED TIME SERIES ANALYSIS
15
General Framework:
Stochastic process:
zt ~ f t ( z t )
stochastic process.
16
STATIONARITY AND DIFFERENCING
Basic condition:
1 2 k+1 k+2
Ez t = µ
Vz t = Vz
both:
- are finite
- do not depend on t
17
In practice, constant variance is achieved through:
- log-level transformation
+
- outlier correction
NONLINEAR FILTERS
18
Roughly:
- "Scale" free
On the negative side, it may induce BIASES (due to the fact that
geometric means underestimate aritmetic means).
19
Concerning STATIONARITY IN MEAN, most economic time
series display a mean (i.e., a “local level”) that cannot be
assumed constant. The two most important reasons:
Trend
0
1 13 25 37 49 61 73 85 97 109 121 133 145 157 169 181 193 205 217 229
Obviously, the mean of the series in the first years is not the same
as the one for the last years.
20
b) The presence of seasonality:
Seasonality
140
130
120
110
100
90
80
70
60
1 9 17 25 33 41 49 57 65 73 81 89 97 105 113 121 129 137 145 153 161
Obviously, the level of the series depends on the period within the
year.
21
To achieve Constant Mean:
Let:
B: "Backward" operator; B j z t = z t- j
We use operators:
∇ = 1- B (regular difference)
∇ s = 1- Bs (seasonal difference)
s-1
Ss = 1 + B + K B (sum over a year)
22
* Assume xt is a linear trend
xt = a + b t ;
then,
∇x t = b
or
∇2 xt = 0 .
In general:
23
Note: Cosine function
x t = m t cos( ω t + ω 0 ) , t = 0, 1, … (A)
m=1
ω0
m
t
τ
ω 0 = Phase (angle at t = 0)
24
* Using expression for cos (a + b), (A) above can also be
rewritten as
x t = m t ( C cos ω t + D sin ω t ) .
* Recall:
(A) is solution of 2nd order difference equation (with real
coefficients)
x t + φ 1 x t −1 + φ 2 x t − 2 = 0 ,
when roots are complex
⇒ always, pairs of complex conjugates.
(see below)
25
Consider a monthly series
π
given that cos (t − 12) = cos π t − 2π = cos π t
6 6 6
ω=π/6
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
π
x t = cos t
2
ω=π /2
1
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
26
So: what is the complete solution of
∇12 x t = 0 ?
Let equation be
x t + φ 1 x t −1 + ... + φ p x t −p = 0 , t = 1, 2, …
r p + φ 1 r p −1 + ... + φ p −1 r + φ p = 0 .
27
This equation has
some real
p roots
some complex
(always in pairs of complex conjugates:
r1 = a + b i
r2 = a − b i )
p
x t = ∑ c j r jt ,
j =1
where
r j : a root of the charact. equat.
c j : arbitrary constants.
The final way in which the roots are expressed is the following
(the c’s are always constants, to be determined from the starting
conditions).
28
1) Single real root
x t = c j r jt .
rj ≤1 .
x t = ( c 0 + c 1 t + ... + c k t k ) r t ,
where r 1 = ... = r k +1 = r .
29
Let
r1 = a + b i
r2 = a − b i
i
r1
r
b
ω
real
a
r2
r = ( a + b ) 1/ 2 ,
ω = a cos
a
,
r
then
x t = c 0 r t cos ( ω t + c 1 ) .
( c 0 , c 1 : constants)
30
4) Multiple complex roots
Very rarely encountered.
x t = A mixture of previous solutions.
r =1 ,
r ≤1
In summary:
x t = Sum of
31
Remark
Using the backward operator B, the difference equation can be
written as
φ(B ) x t = 0 ,
where
φ ( B ) = 1 + φ 1 B + ... + φ p B p .
1
* roots of [ φ ( B ) = 0 ] = .
r
i
Thus,
In terms of the roots of φ ( B ) = 0 , the condition
r ≤1
becomes
b ≥1 ,
32
Back to the ∇ 12 example.
or r = (1)1/12 ,
imaginary
C1
ω=π/6 real
-1 1
C2
-1
33
* 10 complex roots,
in pairs of complex conjugates
A cos (ωt + B)
34
π
Consider the frequency ω = .
6
How many periods are needed to complete a full circle?
⇒ 12 periods.
Hence frequency implies 1 circle (or cycle) per year.
π
For frequency ω = 2 , 6 periods are needed to complete the
6
π
circle, hence ω = implies that 2 circles per year are
3
3π π
completed. For frequency ω = = , 4 periods are needed to
6 2
π
complete the circle, hence ω = ⇒ 3 circles completed per
2
year, and so on.
…..
π
Finally, for ω=6 = π , the root is real and equal to r = -1.
6
For this frequency, a full circle is completed in two periods.
Hence, for monthly data,
35
In short:
6 π
X t = C + ∑ A j cos j t + B j
j =1 6
π
j : seasonal frequencies.
6
j=1 once a year : “ Fundamental “ frequency
j=2 twice a year
... ............ “ harmonics “
j=6 six times a year
36
ω =π /6
1
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
ω =π /3
1
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
ω =π /2
1
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
ω =2π /3
1
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
ω =5π /6
1
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
ω=π
1
-1
0 1 2 3 4 5 6 7 8 9 10 11 12
37
Another way to look at it:
∇12 can be factorized as
AR factors Frequency
(1 - B + B 2 ) x Twice a year
(1 + B 2 ) x 3 times a year
(1 + B + B 2 ) x 4 times a year
(1 + 3 B + B2 ) x 5 times a year
( 1+ B ) 6 times a year
= (1 − B) S
38
Hence, for example,
∇ ∇12 x t = 0 , (1)
since ∇ ∇12 = ∇ 2 S ,
will cancel
st= ∑ A j cos (ω j t + B j )
j
2π
ωj = j , j = 1,2,…, 6.
12
39
Notice that, when specifying stochastic ARIMA models, we will
∇ ∇12 x t = z t
That is, ∇ ∇12 x t will on average be zero and will not depart too
much from it.
so that
a, b → a( t ) , b( t )
C → C( t )
Aj → Aj
(t )
Bj → Bj
(t )
and so on.
40
DISTRIBUTION OF THE STATIONARY SERIES
δ (B) = ∇ d ∇Ds
z t = δ (B) x t is stationary.
distribution.
PROCESSES
distributed.
41
Expectation of a missing value:
42
Stationarity ⇒
Ez t = µ
Vz t = Vz
Cov ( z t , z t − k ) = γ k
Thus,
( z1, K, zT ) ~ NT ( µ, Σ )
γz γ1 γ2 ... γ T −1
γz γ1 ...
∑= ... ... ...
γz γ1
(sym.) γ z
43
More parsimonious representation:
AUTOCOVARIANCE FUNCTION:
A Cov F = γk as a function of k
Let F = B −1 ( i . e ., F z t = z t +1 ) ;
F ≡ "Forward" Operator
ACov G . F . = γ ( B, F )
γ ( B, F ) = γ0 + γ1 ( B + F ) + γ 2 ( B2 + F2 ) + K
∞
= γ0 + ∑ γ j ( B j + F j )
j =1
44
Better (scale free) measure: autocorrelation
γk
ρk = = Lag - k autocorrelation
γ0
AUTOCORRELATION FUNCTION
ACF ≡ ρ as function of k
( since symmetric, only needed for k > 0 )
∞
ACGF ≡ ρ ( B, F ) = 1 + ∑ ρ j ( B j + F j )
j =1
ACF
0,5
-0,5
45
Results:
If zt stationary, then
1) ρ0 = 1,
2) ρ j = ρ- j , ( symmetric )
3) | ρk | < 1, k ≠ 0
4) ρk → 0 as k → ∞
∞
5) ∑ | ρk | < ∞ (Convergence condition)
k =0
µ , γ 0 , ρ ( B, F )
46
Wold Representation
z t = a t + ψ1 a t −1 + ψ 2 a t − 2 + ...
∞
= ∑ ψ j a t − j , (ψ 0 = 1),
j=0
where
a t = z t − ẑ t | t −1
47
Hence
ψj → 0
j→∞
∞
∑ | ψj | < ∞ .
0
In short:
∞
z t = ψ (B) a t ψ(B) = ∑ ψj Bj
j=0
Useful result:
γ z ( B , F ) = ψ ( B ) ψ ( F ) Va
2 2
γ 0 = (1 + ψ1 + ψ 2 + K ) V a
48
ACF: Basic tool in "Time Domain analysis" of a series.
Spectrum
x t = [x1, x 2 , ... , x T ]
x t = a 0 + a 1 t + ... + a T −1 t T −1
49
To simplify, assume T is even, so that
T = 2q
x t = ∑ ( a j cos ω j t + b j sin ω j t )
q
j=1
Letting t = 1, 2, …, T ,
a linear system of T equations in the T unknowns : (a j
, bj ) ,
j = 1, …, q , is obtained.
A 2j = a 2j + b 2j
50
Example:
-1
-2
-3
1 2 3 4 5 6 7 8 9 10
1,00
0,80
0,60
0,40
0,20
0,00
-0,20
-0,40
-0,60
-0,80
-1,00
0 1 2 3 4 5 6 7 8 9 10
51
In general, group the cosine functions by intervals of frequency
ω (frequency)
0 π
52
Given that
cos (α ) = cos (α + 2π) ,
cos (α ) = cos ( −α ) ,
enough to consider: 0 ≤ ω ≤ π
2π
∫ g ( ω ) dω = γ0 ( variance of series )
0
π
(or ∫ )
−π
SPECTRUM SERIES
−π 0 dω π
53
ω = frequency in radians
Recall:
2π
ω=
τ τ = Period
1.5
0.5
0
t
-0.5
-1
-1.5
ω
0 dω π
54
Consider the once-a-year frequency in monthly data
months
τ = 12
2π π
τ = 12 ⇒ ω = =
12 6
in gx (ω) :
SPECTRUM AR(2)
0 π/6 π
55
For trend:
τ→∞ ⇒ ω→0
g(ω)
0 π
56
If the spectrum of a quarterly series is, for example:
gx( ω)
SPECTRUM SERIES
0 π/2 π
0 π/2 π
Peak for:
ω= 0 Trend
π
ω= → Once a year
2
seasonal frequencies
ω = π → Twice a year
57
To extract some signal from a series, for example, to S.A. a
series:
where
c (B, F) = c 0 + ∑ c j (B j + F j )
j
is
c (ω) = c 0 + 2 ∑ c j cos ( j ω)
~
j
gn̂ (ω ) = [~
c (ω )] 2 g x (ω )
58
Hence:
Spectrum of n̂ t = ( Squared gain of filter ) x ( Spectrum of series )
0 π/2 π
59
SPECTRUM OF A LINEAR PROCESS
(AND OF AN ARIMA MODEL)
γ( B, F ) = γ 0 + ∑ γ j ( B j + F j )
B → e -i ω
F.T.
j + j ⇒ 2 cos j ω
B F
0≤ ω≤ 2π
g ( ω)=
1
[
γ + 2 ∑ γ j cos j ω
2π 0
]
g ( ω ) = 2π g (ω) ,
60
Ex. 1:
ACF and spectrum of MA (2)
x t = ( 1 + θ1 B + θ2 B2 ) a t [ Va = 1]
(1 + θ1 B + θ 2 B 2 ) (1 + θ1 F + θ 2 F 2 )
+ θ1 ( 1 + θ2 ) [ B + F ] coeff . of B and F : γ1
+ θ2 [ B2 + F2 ] coeff. of B2 and F2 : γ 2
In short
B k + F k → 2 cos k ω
61
Ex. 2:
0,8
0,6
0,4
0,2
ω 0
1 2 3 4 5 6 7 8 9 10 11 12
0
π
Ex. 3:
AR(1)
z t + φ z t −1 = a t , | φ | <1
(1 + φ B ) z t = a t ,
1
zt = at
1 + φ B
1
z t = ψ (B) a t → ψ (B) = (Wold representation)
1 + φB
1 1
γz ( B , F ) = Va =
( 1+ φ B ) ( 1+ φ F )
1
=
1 + φ2 + φ ( B + F )
62
Hence the spectrum is: (B + F → 2 cos ω)
1
gz ( ω ) = Va
1 + φ2 + 2 φ cos ω
g z (ω)
φ <0
ACF ( 1+.8B)xt = at
ACF(1-.8B)xt=at
1
0,8
0,6
0,4
0,2
0
1 2 3 4 5 6 7 8 9 10 11 12
0 π
φ>0 ACF(1+.8B)xt=at
ACF ( 1-.8B)xt = at
1
0,8
0,6
0,4
0,2
0
-0,2
-0,4
-0,6
-0,8
-1
1 2 3 4 5 6 7 8 9 10 11 12
0 π
63
Ex. 4 : PSEUDO-SPECTRUM
∇z t = a t (va = 1)
zt = at + at-1 + at-2 + at-3 + …
As t → ∞
* Mean is not defined ("0 ⋅ ∞") .
* Variance goes to ∞ .
1 1
“Pseudo” A Cov. GF = va
1− B 1− F
(does not converge)
64
1
F.T . ≡ g z ( ω ) =
2 ( 1 - cos ω )
ω=0 ⇒ g→∞
SPECTRUM SERIES
SPECTRUM SERIES
ω
0 π/2 π
p-spectrum is:
- informative
- well-behaved
65
For example, consider the two trend spectra:
Trend Spectra
0,5
0
0 π
Trend
4,65
4,6
4,55
4,5
4,45
4,4
4,35
4,3
4,25
1 10 19 28 37 46 55 64 73 82 91 100 109 118 127 136 145 154
66
Similarly, from the two spectra:
0,5
67
Seasonal Component
-1
-2
-3
Seasonal Component
-1
-2
-3
As was the case with the trend, the narrow spectral peaks
produce stable seasonal components.
68
Ex. 5 : AR (2)
(1 + φ1 B + φ2 B 2 ) z t = a t
1 + φ1 B + φ2 B 2 = 0
Either:
- 2 real roots ( each ~ AR(1) )
- Complex conjugate root, → z t = r cos (ωt ) + L
with
* modulus r = φ2
φ
* frequency ω = arcos 1 (in rads.)
2r
2π
period τ =
ω
SPECTRUM SERIES
0 ω π/2
69
General Hint: Useful way to look at AR(p) :
Factorize it as:
0 π
0 ω π
70
Ex. 6 : SEASONAL SERIES
∇4 xt = zt ,
z t : stationary process.
Then,
1 1
xt = zt = zt =
∇4 ∇S
1 1
= zt =
∇ 1 + B + B2 + B3
1 1
= zt
∇ (1 + B) (1 + B 2 )
1 1 1
p ACGF( x ) = γ z (B, F) .
(1 − B) (1 − F) (1 + B) (1 + F) (1 + B 2 ) (1 + F2 )
71
Operating and using B j + F j = 2 cos jω , the p-spectrum is
1
gx (ω) = . ∞ for ω = 0
2 (1 − cos ω)
1
. ∞ for ω = π
2 (1 + cos ω)
(seasonal freq.)
1 π
. ∞ for ω =
2 (1 + cos 2ω) 2
(seasonal freq.)
γ z (ω) bounded.
72
SPECTRUM QUARTERLY SERIES
0 π/2 π
73
ARIMA models
series:
z t = ψ( B ) a t
θ(B )
ψ(B )=
φ(B )
θ( B ) = finite degree q
φ( B ) = finite degree p
Therefore,
θ(B )
zt = at ,
φ(B )
or:
φ (B) z t = θ ( B ) a t
74
Autoregressive Moving-Average Models: ARMA models
AR (p) polynomial: 1 + φ1 B +… + φp Bp
MA (q) polynomial: 1 + θ1 B +… + θq Bq
( 1 + φ1 B + K + φp Bp ) z t = (1 + θ1 B + K + θ q B q ) a t
If δ ( B ) = ∇ d
x t ~ ARIMA ( p, d, q ) model
Model:
φ ( B ) δ ( B ) x t = θ ( B ) at
φ ( B ) : stationary
θ ( B ) : invertible
δ ( B ) : unit roots (Nonstationary roots)
75
Stationarity of ARMA Models:
2
i
1 modulus
ω: frecuency
real
-2 -1 1 2
-1
Four roots of unit circle
76
Ex:
a) AR(1) : x1 + φ x t −1 = a t
(1 + φ B) x t = a t
1
* root of 1 + φ B = 0 ⇒ B = -
φ
1
mod (B) = − > 1⇒ | φ |<1
φ
to be > 1 in moduli.
77
Useful diagram:
φ2
φ1
-2 -1 1 2
-2
78
If zt is stationary, φ B) –1 converges, and hence we can write
zt = [ φ(B)-1 θ(B) ] at .
Invertibility
If zt is invertible,
- it accepts a convergent AR representation
Remark:
79
Model:
( 1 - B) x t = (1 + B) a t
0 π
Model:
( 1+ B ) x t = ( 1 - B ) at
ARroot
AR root(1-B)
(1+B) ; MA
; MA root
root (1-B)
(1+B)
0 π
80
TWO USEFUL ALTERNATIVE REPRESENTATIONS:
ARMA model:
φ ( B ) at = θ ( B ) at
θ(B ) 2
(a) xt = at = ( 1 + ψ1 B + ψ2 B + K ) a t
φ(B )
or
x t = ψ(B) a t
* If x t is stationary:
ψ - weights → 0
81
(b) Alternatively,
φ (B)
x t = a t , or
θ (B)
( 1 + π1 B + π 2 B 2 + K ) x t = a t ,
π(B) x t = a t
* If xt is invertible,
π - weights → 0
82
For seasonal data, often the general multiplicative ARIMA model
is used:
Φ (B s ) ≡ seasonal AR pol. ( in Bs )
83
"Box-Jenkins"-type approach:
IDENTIFICATION
of the model
ESTIMATION
DIAGNOSIS
O.K.?
No
Yes
INFERENCE
84
IDENTIFICATION OF THE ARIMA MODEL
( 1 + φ1 B + K + φpBp ) x t = ( 1 + θ1 B + K + θ q B q ) a t
ACF:
ρk ≡ Lag-k autocorrelation of xt, as a function of k:
It will display
* q starting conditions,
after which the AR difference equation
* ρk + φ1 ρk-1 + K + φp ρ k - p = 0
holds. Hence, for k > q , ρk is solution of
φ ( B ) ρk = 0
where B operates on k.
85
Thus: "Eventual ACF"
ACF (1-.8B)xt=at
0.5
ACF (1+.8B)xt=at
0.5
0
1 3 5 7 9 11 13 15 17 19 21 23
-0.5
-1
0,5
-0,5
-1
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
86
At present, "identification" uses more efficient procedures
Notes:
87
THEORETICAL ACF
0.5
1 4 8 12 16 20 24
SAMPLE ACF
0,5
-0,5
88
PARAMETER ESTIMATION (more later)
Rough intuition : [ x1 K xT ]
Assume:
x t = a t + θ a t −1 MA (1), | θ |< 1
or
a t = x t − θ a t −1
Compute sequentally:
a10 = x1 − θ0 a0
a02 = x 2 − θ0 a10
………………..
a0T = x T − θ0 a0T −1
____________
( )
T 2
SS 0 = ∑ a 0j
1
89
Varying θ :
SS
SSSS
0
−1 θ0 θ̂ 1
θ̂ = min. SS
Notice:
a t = x t − θ a t −1
a t −1 = x t −1 − θ a t − 2
at − 2 = x t − 2 − θ at −3
…..
yields
90
DIAGNOSTICS (more later)
* In-sample
* out-of-sample
x t = x̂ t | t −1 + a t
= x t − x̂ t | t −1 ( ≡ innovation )
91
Forecast:
x̂ t + k | t = MMSE t ( x t + k ) = (under our assumptions) =
= E ( x t + k | x1 K x t )
Forecast function :
x̂ t + k | t as a function of k.
Forecast function:
* q starting conditions,
x̂ t + k | t + φ1 x̂ t + k - 1 | t + K + φp x̂ t + k - p | t = 0
φ ( B ) x̂ t + k | t = 0
where B operates on k.
92
Note:
Eventual Forecast Function and ACF are solution of the same AR
finite difference equation
Use ψ - weights :
x t+k = a t + k + Ψ1 a t + k - 1 + K +
+ Ψ k - 1 a t + 1 + Ψ k at + Ψ k + 1 a t −1 + K ;
since
x̂ t + k | t = E t x t + k = ψ k a t + ψ k +1 a t −1 + ... ;
93
Thus, FORECAST ERROR
e t+k|t = x t + k - x̂ t + k | t
= a t + k + ψ1 a t +k - 1 + K + ψ k - 1 a t + 1
144444424444443
MA(k(k-1)
MA - 1) ofof" future"
“ futureinnovation
“ innovations.
s
e t +1| t ~ N ( 0 , V a I ) …
94