You are on page 1of 41

State Space models and the

Kalman filter
Haroon Mumtaz
Uses of state space models and
the Kalman filter
Can be used to answer questions such as:
• Has the relationship between Y and X
changed over time
• Does GDP have a permanent and
transitory component?
• Is there a common factor driving energy
prices across the world?
What is a state space model?
A state space model consists of two
equations:
Measurement equation: An equation that
describes the relationship between
observed and unobserved variables.
Transition equation: An equation that
describes the dynamics of the unobserved
variables.
What is a state space model?
The two equations are as follows: Exogenous
regressors

Observation eq y t  Ht Bt  Az t  e t
data Coefficients Unobserved
or data state

Transition eq  t    F t−1  v t

Dynamics of the state variable


What is a state space model?
y t  H t Bt  Az t  e t
The shocks
 t    F t−1  v t

Assumptions

e t ~iid. N0, R
v t ~iid. N0, Q
Ee t , v t   0
Examples of econometric
models in state space form
• A time-varying parameter model linking
import price inflation and the nominal
exchange rate

Δp t   t  D t E t  e t

Time
Import price Varying exchange rate
coefficients
A time-varying parameter model
• In SS form Unobserved state variables
Matrix H

Δp t   t  Dt E t  e t
t  t−1 v 1,t
 
Dt Dt−1 v 2,t

Transition equation with   0, F  1


Examples: A trend-Cycle model
• Decomposing GDP into a permanent
(Trend) and transitory (Cycle) component
trend
cycle

GDP t  T t  C t
• We assume that the trend component
follows a random walk T t  c  T t−1  v 1,t
• The cyclical component is a stationary
process C  C  v
t t−1 2,t
Trend-Cycle model
• In SS form
data H Matrix
Unobserved
states

Tt
GDP t  1 1
Ct

Tt c 1 0 Tt−1 v 1,t
  
Ct 0  0 Ct−1 v 2,t

μ F
Trend-Cycle model
SS form with AR(2) cycle

Tt
GDP t  1 1 0 Ct
C t−1

Tt c 1 0 0 T t−1 v 1t
Ct  0  1 2 0 C t−1  v 2t
C t−1 0 0 1 0 C t−2 0
Examples: A dynamic factor
model
• Common factor in GDP growth across
Latin America
Idiosyncratic factor
In GDP growth

Δgdp i,t  Bi F t  e it
Common factor
GDP growth in country i, i=1..4
In GDP growth
• Assume factor follows AR process
F t  c   1 F t−1   2 F t−2  v t
A dynamic factor model
H Matrix
• SS form
Unobserved states

Δgdp 1,t B1 0 e 1t
Δgdp 2,t B2 0 Ft e 2t
 
Δgdp 3,t B3 0 F t−1 e 3t
data
Δgdp 4,t B4 0 e 4t

Ft 1 2 F t−1 v 1t
 
F t−1 1 0 F t−2 v 2t

f matrix
Estimation Aims
Aim 1: Estimate parameters of the State
Space

y t  Ht Bt  Azt  e t
 t    F t−1  v t
e t ~iid. N0, R
v t ~iid. N0, Q
Estimation Aims
Aim 2: Recover the unobserved state

y t  Ht Bt  Az t  e t
 t    F t−1  v t
e t ~iid. N0, R
v t ~iid. N0, Q
Estimation
• We use the Kalman filter for this purpose

• The Kalman filter is a recursive algorithm


that provides an optimal estimate of  t
conditional on an information set and
knowledge of the parameters of the state
space Ht , A, , F, R, Q
Notation
yt  x tt  et
Model  t    F t−1  v t
e t ~iid. N0, R
v t ~iid. N0, Q

Estimate of  t conditional on information up


to time t-1

 t/t−1
Notation
• Estimate of  t conditional on information
up to time t

 t/t
• Covariance of  t conditional on
information up to t-1

P t/t−1
Notation
• Covariance of  t conditional on
information up to t-1

P t/t
• Forecast of y given information up to time
t-1

y t/t−1
Notation
• Prediction error

 t/t−1  y t − y t/t−1
• Variance of the prediction error

f t/t−1
Kalman filter
• Assume parameters , F, R, Q are known
The Kalman filter recursion consists of three
steps
1. Start with guess of state a time 0  0/0
and P 0/0
2. Prediction: At time 1 form an optimal
prediction y 1/0 using an estimated
value for  1/0
Kalman Filter
3. Updating: Use the observed value of y at
time 1 to calculate the prediction error
 1/0  y 1 − y 1/0

Æ this prediction error contains


information we can use to refine our
guess about  weight assigned to new
Information (Kalman gain)
 1/1   1/0  K t  1/0
Conditional
on time 1
Kalman filter equations
Predicted values
Prediction
 t/t−1    F t−1/t−1
P t/t−1  FP t−1/t−1 F ′  Q
 t/t−1  y t − y t/t−1  y t − x t  t/t−1

f t/t−1  x t P t/t−1 x t  R

Forecast for y and the prediction error


Where do the prediction
equations come from?
First equation  t/t−1    F t−1/t−1 E(v)=0

Examine transition eq E  F t−1  v t 

Second equation P t/t−1  FP t−1/t−1 F ′  Q

Variance of transition eq
VAR  F t−1  v t   VAR  F  VAR t−1   F ′  VARv t 

=0 =Q
 P t−1/t−1
Where do these prediction
equations come from?
Forecast
Third equation:  t/t−1  y t − y t/t−1  y t − x t  t/t−1 error

Fourth equation f t/t−1  x t P t/t−1 x ′t  R

f t/t−1  VARy t − y t/t−1   VARx t  t  e t  − x t  t/t−1 

2
 E x t  t −  t/t−1   e t 

2
 Ex t  t −  t/t−1   E2e t x t  t −  t/t−1   Ee 2t 

 x t P t/t−1 x ′t =0 =R
Kalman Filter Equations
Updating

 t/t   t/t−1  K t  t/t−1


P t/t  P t/t−1 − K t x t P t/t−1
−1
K t  P t/t−1 x ′t f t/t−1

Weight
assigned to new information Update estimates using information
In the prediction error contained in the prediction error
Kalman gain
Where do the updating
equations come from?
• We need to update the forecast of  t
based on new information contained in t
• Updating a linear projection: In general if
want to calculate an updated forecast
covariance Relationship
Between Y3 andY2

FY3 /Y2 , Y1   FY3 /Y1   H32 H−1


22 Y2 − FY2 /Y1 

old
Forecast based on Y1 Error in forecasting
Updated
Forecast based on Y2 variance Y2 using information
Y1
Where do these updating
K t  P t/t−1 x f equations come from?
′ −1
t t/t−1

Y3   t , Y 2  y t , Y1  x t 
 t/t   t/t−1  cov t , y t vary t y t − y t/t−1 

Updating the variance of the


forecast error P t/t
P t/t−1 − K t x t P t/t−1
Use the formula

VARY3 − FY3 /Y2 , Y1   H 33 − H 32 H−1


22 H 23
The Kalman Gain
• The Kalman gain or weight given to new
information (about the state) contained in
the prediction error is given by
K t  P t/t−1 x ′t f −1
t/t−1
−1
 P t/t−1 x ′t x t P t/t−1 x ′t  R
Positive function of uncertainty associated
Lower weight
with  t/t−1 higher weight to prediction error if this
If R is high
is higher
Overview of the Kalman filter
Starting values (time 0)  0/0 , P 0/0

Predict state vector  t/t−1    F t−1/t−1


(time 1….) P t/t−1  FP t−1/t−1 F ′  Q

Calculate prediction  t/t−1  y t − y t/t−1  y t − x t  t/t−1


error f t/t−1  x t P t/t−1 x ′t  R

Update states K t  P t/t−1 x ′t f −1


t/t−1
 t/t   t/t−1  K t  t/t−1
P t/t  P t/t−1 − K t x t P t/t−1
Kalman Smoother
• The Kalman filter provides inference for
the state vector using information up to
time t  t/t
• We can get estimate of the state vector
using information up to the end of the
sample  t/T using the Kalman smoother
−1
 t/T   t/t  P t/t F P t1/t  t1/T − F t/t −
′ 

P t/T  P t/t  P t/t F′ P −1
t1/t P t1/T
− P t1/t  P t/t F′P −1
t1/t
Kalman Smoother

 t/T   t/t  P t/t F ′ P −1


t1/t  t1/T − F t/t − 

Starting point is output of Kalman Filter at time


T
Update state estimate as a weighted average
of filtered estimate and the error in ‘predicting’
the state variable
Example of filtering and smoothing
Persistence of CPI inflation in the
UK
CPI Inflation
t  ct  tt−1  t 10

ct ct−1 v1t
  8
t t−1 v2t
6

t 0 0.6 0 0
4
v1t ~N 0 , 0 0.006 0
v2t 0 0 0 0.001 2

-2
1970 1975 1980 1985 1990 1995 2000 2005
Filtered and Smoothed estimates
Filtered estimate of Autoregressive Coefficient Filtered estimate of constant
1.0 2.0

1.6
0.8

1.2
0.6
0.8

0.4
0.4

0.2 0.0
70 75 80 85 90 95 00 05 70 75 80 85 90 95 00 05

Smoothed estimate of autoregressive coefficient Smoothed estimate of constant


.7 1.6

1.4
.6
1.2
.5 1.0

.4 0.8

0.6
.3
0.4

.2 0.2
70 75 80 85 90 95 00 05 70 75 80 85 90 95 00 05
Kalman Filter and Maximum
Likelihood estimation
• Up to now we have assumed that the
parameters of the state space Ht , A, , F, R, Q
are known
• Generally this is not the case and these
parameters have to be estimated
• The Kalman filter also provides us with
the likelihood function which can be
maximised wrt these parameters
Maximum Likelihood
• Recall our Time-varying parameter model
yt  x tt  et
 t    F t−1  v t
e t ~iid. N0, R
v t ~iid. N0, Q
Given normal error terms and state vector
the data is distributed as
y t /x t ~Nx t  t/t−1 , x t P t/t−1 x ′t  R
Maximum Likelihood
The Likelihood function is given by
lnL  − 12 ∑ Tt1 ln2 n detx t P t/t−1 x ′t  R − 1
2
∑ Tt1 y t − x t  t/t−1  ′ x t P t/t−1 x ′t  R −1 y t − x t  t/t−1

or

lnL  − 12 ∑ Tt1 ln2 n detf t/t−1  − 1


2
∑ t1
T
 ′t/t−1 f −1
t/t−1  t/t−1
Overview of the Kalman Filter
with ML
Starting values (time 0)  0/0 , P 0/0 , Q 0 ,  0 , F 0 , R0
Initial conditions for filter ↓
and guess for SS
 t/t−1    F t−1/t−1
parameters
P t/t−1  FP t−1/t−1 F ′  Q

 t/t−1  y t − y t/t−1  y t − x t  t/t−1
f t/t−1  x t P t/t−1 x ′t  R

K t  P t/t−1 x ′t f −1
t/t−1
 t/t   t/t−1  K t  t/t−1
P t/t  P t/t−1 − K t x t P t/t−1
Calculate likelihood ↓
function for each t lnL  lnL  − 12 ln2 n detf t/t−1  − 1 ′
f −1
2  t/t−1 t/t−1  t/t−1

lnL  − 12 ∑ Tt1 ln2 n detf t/t−1  − 1


2
∑ Tt1  ′t/t−1 f −1
t/t−1  t/t−1
State Space models in Eviews
Go to objectÆnew objectÆsspace
Entering your state space
model
An unobserved component model
Data State variable

@signal inf =sv1+ sv2 Observation equation

@state sv1 = sv1(-1) + [var =exp(c(2))]

@state sv2 = c(4)*sv2(-1) + [var =exp(c(3))]

Transition equations
Entering your state space model
• A time-varying parameter model
@signal inf =sv2+ sv1*inf1 + [var = exp(c(1))]

@state sv1 = sv1(-1) + [var =exp(c(2))]

@state sv2 = sv2(-1) + [var =exp(c(3))]


Specifying starting values for
the parameters
param c(1) -2 c(2) -2 c(3) -2

@signal inf =sv2+ sv1*inf1 + [var = exp(c(1))]

@state sv1 = sv1(-1) + [var =exp(c(2))]

@state sv2 = sv2(-1) + [var =exp(c(3))]

You might also like