Anda di halaman 1dari 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:
Observation eq
data

y t Ht Bt Az t e t
Coefficients
or data

Transition eq

Exogenous
regressors

Unobserved
state

t F t1 v t

Dynamics of the state variable

What is a state space model?


y t H t Bt Az t e t
t F t1 v t

The shocks

Assumptions

e t ~iid. N0, R
v t ~iid. N0, Q
Ee 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
Import price

Time
Varying
coefficients

exchange rate

A time-varying parameter model


In SS form

Unobserved state variables


Matrix H

p t t Dt E t e t
t
Dt

t1
Dt1

Transition equation with 0, F 1

v 1,t
v 2,t

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 t1 v 1,t
The cyclical component is a stationary
process C C v
t
t1
2,t

Trend-Cycle model
In SS form
H Matrix

data

Unobserved
states

GDP t
Tt
Ct

Tt

1 1

Ct
c
0

1 0

Tt1

Ct1

v 1,t
v 2,t

Trend-Cycle model
SS form with AR(2) cycle
Tt
GDP t

1 1 0

Ct
C t1

Tt
Ct
C t1

0
0

0 0

T t1

1 2 0

C t1

1 0

C t2

v 1t

v 2t
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
GDP growth in country i, i=1..4

Common factor
In GDP growth

Assume factor follows AR process


F t c 1 F t1 2 F t2 v t

A dynamic factor model


H Matrix

SS form

data

Unobserved states

gdp 1,t

B1 0

gdp 2,t

B2 0

Ft

B3 0

F t1

gdp 3,t
gdp 4,t
Ft
F t1

e 1t

B4 0

f matrix

e 3t
e 4t

1 2

F t1

F t2

e 2t

v 1t
v 2t

Estimation Aims
Aim 1: Estimate parameters of the State
Space

y t Ht Bt Azt e t
t F t1 v t
e t ~iid. N0, R
v t ~iid. N0, Q

Estimation Aims
Aim 2: Recover the unobserved state

y t Ht Bt Az t e t
t F t1 v t
e t ~iid. N0, R
v t ~iid. N0, 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
Model

yt x tt et
t F t1 v t
e t ~iid. N0, R
v t ~iid. N0, Q

Estimate of t conditional on information up


to time t-1

t/t1

Notation
Estimate of t
up to time t

conditional on information

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

P t/t1

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/t1

Notation
Prediction error

t/t1 y t y t/t1
Variance of the prediction error

f t/t1

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

1/1 1/0 K t 1/0


Conditional
on time 1

Information (Kalman gain)

Kalman filter equations


Prediction

Predicted values

t/t1 F t1/t1
P t/t1 FP t1/t1 F Q
t/t1 y t y t/t1 y t x t t/t1

f t/t1 x t P t/t1 x t R
Forecast for y and the prediction error

Where do the prediction


equations come from?
First equation t/t1 F t1/t1
Examine transition eq E F t1 v t

E(v)=0

P t/t1 FP t1/t1 F Q

Second equation
Variance of transition eq

VAR F t1 v t VAR F VAR t1 F VARv t

=0

P t1/t1

=Q

Where do these prediction


equations come from?
Third equation:
Fourth equation

t/t1

y t y t/t1 y t x t t/t1

Forecast
error

f t/t1 x t P t/t1 x t R

f t/t1 VARy t y t/t1 VARx t t e t x t t/t1

E x t t t/t1 e t

Ex t t t/t1 E2e t x t t t/t1 Ee 2t


x t P t/t1 x t

=0

=R

Kalman Filter Equations


Updating
t/t t/t1 K t t/t1
P t/t P t/t1 K t x t P t/t1
1
K t P t/t1 x t f t/t1

Weight
assigned to new information
In the prediction error
Kalman gain

Update estimates using information


contained in the prediction error

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

Relationship
Between Y3 andY2

covariance

FY3 /Y2 , Y1 FY3 /Y1 H32 H1


22 Y2 FY2 /Y1

Updated
Forecast based on Y2

old
Forecast based on Y1
variance

Error in forecasting
Y2 using information
Y1

K t P t/t1

Where do these updating


x f equations come from?
1
t t/t1

Y3 t , Y 2 y t , Y1 x t
t/t t/t1 cov t , y t vary t y t y t/t1

Updating the variance of the


forecast error P t/t

P t/t1 K t x t P t/t1

Use the formula


VARY3 FY3 /Y2 , Y1 H 33 H 32 H1
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/t1 x t f 1
t/t1

P t/t1 x t x t P t/t1 x t

Positive function of uncertainty associated


with t/t1
higher weight to prediction error if this
is higher

Lower weight
If R is high

Overview of the Kalman filter


Starting values (time 0)

Predict state vector


(time 1.)

Calculate prediction
error

Update states

0/0 , P 0/0

t/t1 F t1/t1
P t/t1 FP t1/t1 F Q

t/t1 y t y t/t1 y t x t t/t1


f t/t1 x t P t/t1 x t R

K t P t/t1 x t f 1
t/t1
t/t t/t1 K t t/t1
P t/t P t/t1 K t x t P t/t1

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
t/T
P t/T

t/t P t/t F P t1/t t1/T F t/t


P t1/t
P t/t P t/t F P 1
t1/t P t1/T

P t/t FP 1
t1/t

Kalman Smoother
t/T t/t P t/t F P 1
t1/t t1/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 tt1 t
ct
t

ct1
t1

10

v1t

v2t

t
v1t
v2t

0
~N

0
0

0.6
,

0 0.006
0

0
0.001

2
0
-2
1970

1975

1980

1985

1990

1995

2000

2005

Filtered and Smoothed estimates


Filtered estimate of Autoregressive Coefficient
1.0

Filtered estimate of constant


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

Smoothed estimate of autoregressive coefficient


.7

75

80

85

90

95

00

05

Smoothed estimate of constant


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 tt et
t F t1 v t
e t ~iid. N0, R
v t ~iid. N0, Q

Given normal error terms and state vector


the data is distributed as

y t /x t ~Nx t t/t1 , x t P t/t1 x t R

Maximum Likelihood
The Likelihood function is given by
lnL 12 Tt1 ln2 n detx t P t/t1 x t R

1
2

Tt1 y t x t t/t1 x t P t/t1 x t R 1 y t x t t/t1

or
lnL 12 Tt1 ln2 n detf t/t1

1
2

T
t/t1 f 1
t1
t/t1 t/t1

Overview of the Kalman Filter


with ML
Starting values (time 0)
Initial conditions for filter
and guess for SS
parameters

0/0 , P 0/0 , Q 0 , 0 , F 0 , R0

t/t1 F t1/t1
P t/t1 FP t1/t1 F Q

t/t1 y t y t/t1 y t x t t/t1


f t/t1 x t P t/t1 x t R

K t P t/t1 x t f 1
t/t1

Calculate likelihood
function for each t

t/t t/t1 K t t/t1


P t/t P t/t1 K t x t P t/t1

lnL lnL 12 ln2 n detf t/t1

lnL 12 Tt1 ln2 n detf t/t1

1
2

1
f 1
2 t/t1 t/t1 t/t1

Tt1 t/t1 f 1
t/t1 t/t1

State Space models in Eviews


Go to objectnew objectsspace

Entering your state space


model
An unobserved component model
Data

@signal inf =sv1+ sv2

State variable
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))]

Anda mungkin juga menyukai