State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last...

32
State Space Models and the Kalman Filter May 25, 2012

Transcript of State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last...

Page 1: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

State Space Models and the Kalman Filter

May 25, 2012

Page 2: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

State Space Models and the Kalman Filter

What we did last time:

I The scalar filter

I Combining period t prior and signal is analogous to a simpleminimum variance problem with two signals

I Derived the multivariate filter using

I The projection theoremI Projecting onto orthogonal variablesI The Gram-Schmidt procedure

Page 3: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The basic formulas

The state space system and the Kalman update equation

Xt = AXt−1 + Cut : ut ∼ N(0, I )

Zt = DXt + vt : vt ∼ N(0,Σvv )

Xt|t = AXt−1|t−1 + Kt

(Zt − DXt|t−1

)where Kt is the Kalman gain and Xt|t = E

[Xt | Z t ,X0|0

]I Filter is also the linear minimum variance estimator of Xt even

if shocks are non-gaussian.

Page 4: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The basic formulas

Most of the time, all you really need to know is how to put theseformulas into a computer

Xt|t = AXt−1|t−1 + Kt

(Zt − DAXt−1|t−1

)

Kt = Pt|t−1D′ (DPt|t−1D

′ + Σvv

)−1

Pt+1|t = A(Pt|t−1 − Pt|t−1D

′ (DPt|t−1D′ + Σvv

)−1DPt|t−1

)A′

+CC ′

This will give you a recursive estimate of Xt

I Remember: Pt|t−s ≡ E (Xt − Xt|t−s)(Xt − Xt|t−s)′

Page 5: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The scalar filter

xt = ρxt−1 + ut : ut ∼ N(0, σ2u) (state equation)

zt = xt + vt : vt ∼ N(0, σ2v ) (measurement equation)

gives the Kalman update equations

xt|t = ρxt−1|t−1 + kt(z1 − ρxt−1|t−1

)kt = pt|t−1(pt|t−1 + σ2

v )−1

pt|t−1 = ρ2(pt−1|t−2 − p2

t−1|t−2(pt−1|t−2 + σ2v )−1

)︸ ︷︷ ︸

pt−1|t−1

+ σ2u

Page 6: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Combining information in prior and signal

Kalman filter optimally combine information in prior ρxt−1|t−1 andsignal zt to form posterior estimate xt|t with covariance pt|t

xt|t = (1− kt)ρxt−1|t−1 + ktz1

I More weight on signal (large kalman gain kt) if prior varianceis large or if signal is very precise

I Prior variance can be large either because previous stateestimate was imprecise (i.e. pt−1|t−1 is large) or because stateinnovations are large (i.e.σ2

u is large)

Page 7: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Example 1

Set

I ρ = 0.9

I σ2u = 1

I σ2v = 5

Page 8: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Example 1

0 20 40 60 80 100 120 140 160 180 200-15

-10

-5

0

5

10

15

X

Z

0 20 40 60 80 100 120 140 160 180 200-5

0

5

X

Xt|t

Page 9: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Example 2

Set

I ρ = 0.9

I σ2u = 1

I σ2v = 1

Page 10: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Example 2: Smaller measurement error variance

0 20 40 60 80 100 120 140 160 180 200-6

-4

-2

0

2

4

6

8

10

X

Z

0 20 40 60 80 100 120 140 160 180 200-6

-4

-2

0

2

4

6

8

X

Xt|t

Page 11: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Convergence to time invariant filter

If ρ < 1 and if ρ, σ2u and σ2 are constant, the prior variance of the

state estimate

pt|t−1 = ρ2(pt−1|t−2 − p2

t−1|t−2(pt−1|t−2 + σ2v )−1

)+ σ2

u

will converge to

p = ρ2(p − p2(p + σ2

v )−1)

+ σ2u

The Kalman gain will then also converge:

k = p(p + σ2v )−1

I We can illustrate this by starting from the boundaries ofpossible values for p1|0

I Remember: σ2u < pt|t−1 < σ2

u(1− ρ2)−1 (Why?)

Page 12: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Convergence to time invariant filter

0 5 10 15 20 250

2

4

6

0 5 10 15 20 250

0.1

0.2

Pt|t

Kt

Page 13: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Convergence to time invariant filter

-3 -2 -1 0 1 2 3 4 5 60

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9priorposteriort=1

t=2

t=1

t=3t=4

t=2

t=3

t=4

Figure: Propagation of prior and posterior distributions:x0 = 1, p0 = 1, σ2

u = 1, σ2v = 1, z t =

[3.4 2.2 4.2 5.5

]

Page 14: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

State space models are very flexible

We can put all models we have discussed so far in the state spaceform

Xt = AXt−1 + Cut : ut ∼ N(0, I )

Zt = DXt + vt : vt ∼ N(0,Σvv )

This includes “observable” VAR(p), MA(p) and VARMA(p,q)processes as well as principal components and related models withexplicitly latent factor models.

Page 15: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

VAR (p) in State Space Form

yt = φ1yt−1 + φ2yt−2 + ...+ φpyt−1 + ut

A =

φ1 φ2 · · · φpI 0 0

0. . .

. . .

0 0 I 0

,C =

I000

ut

D =[I 0 · · · 0

],Σvv = 0

Page 16: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

MA(1) in State Space Form

yt = εt + θεt−1

can be written as

[εtεt−1

]=

[0 01 0

] [εt−1

εt−2

]+

[10

]εt

yt =[

1 θ]

Page 17: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Maximum Likelihood Estimation of an MA(1)process

Maximum Likelihood Estimation of an MA(1) process

yt = εt + θεt−1

I No need to assume that ε1 = 0 and we can estimate exact ML

L(Z | θ) = (−T/2) log(2π)− T

2log |Ωt | −

1

2

T∑t=1

Z ′tΩ−1t Zt

where

Zt = Zt − DAXt|t

Xt|t = AXt−1|t−1 + Kt

(Zt − DAXt−1|t−1

)Ωt = DPt|t−1D

′ + Σvv

We can start the Kalman filter recursions from the unconditional mean

and variance, i.e. X0 = 0 and P0|0 = σ2ε × I .

Page 18: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Alternative state space representations

Sometimes there are more than one state space representation of agiven system: But are both[

εtεt−1

]=

[0 01 0

] [εt−1

εt−2

]+

[10

]εt

yt =[

1 θ]

and [εtεt−1

]=

[0 00 0

] [εt−1

εt−2

]+

[εtεt−1

]yt =

[1 θ

]valid state space representations of an MA(1) process?

Page 19: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Maximum Likelihood and Unobserved ComponentsModels

Unobserved Component model of Inflation

πt = µt + ηt

τt = τt−1 + εt

Decomposes inflation into permanent (τ) and transitory (η)component

I Fits the data well

I But we may be concerned about having an actual unit rootroot in inflation on theoretical grounds

I Based on simplified (constant parameters) version of Stockand Watson (JMCB 2007)

Page 20: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The basic formulas

We want to:

1. Estimate the parameters of the system, i.e. estimate σ2η and

σ2ε

1.1 Parameter vector is given by Θ =σ2η, σ

1.2 Θ = arg maxθ∈Θ L(πt | Θ)

2. Find an estimate of the permanent component τt at differentpoints in time

Page 21: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The Likelihood function

We have the state space system

πt = µt + ηt (measurement equation)

τt = τt−1 + εt (state equation)

implying that A = 1,D = 1,C =√σ2ε ,Σv = σ2

η. The likelihoodfunction for a state space system is (as always) given by

L(Z | Θ) = −nT

2log(2π)− T

2log |Ωt | −

1

2

T∑t=1

Z ′tΩ−1t Zt

where

Zt = Zt − DAXt−1|t−1

Ωt = DPt|t−1D′ + Σvv

and n is the number of observable variables, i.e. the dimension ofZt .

Page 22: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Starting the Kalman recursions

How can we choose initial values for the Kalman recursions?

I Unconditional variance is infinite because of unit root inpermanent component

I A good choice is to choose “neutral” values, i.e. somethingakin to uninformative priors

I One such choice is X0|0 = π1 and P0|0 very large (but finite)and constant

L(Z | Θ) = −nT

2log(2π)− T

2log |Ωt | −

1

2

T∑t=1

Z ′tΩ−1t Zt

Page 23: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Maximizing the Likelihood function

How can we find Θ = arg maxθ∈Θ L(πt | Θ)?

I The dimension of the parameter vector is low so we can usegrid search

Define grid for variances σ2ε and σ2

η

σ2ε = 0, 0.001, 0.002, ..., σ2

ε,maxσ2η = 0, 0.001, 0.002, ..., σ2

η,max

and evaluate likelihood function for all combinations.How do we choose boundaries of grid?

I Variances are non-negative

I Both σ2ε and σ2

η should be smaller than or equal to the sample

variance of inflation so we can set σ2ε,max = σ2

η,max = 1T

∑π2t

Page 24: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Maximizing the Likelihood function

11.5

22.5

33.5

44.5

55.5

6

0

5

10

15

20

25

30700

710

720

730

740

750

760

770

780

790

Page 25: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Maximizing the Likelihood function

Estimated parameter values:

I σ2ε = 0.0028

I σ2η = 0.0051

We can also estimate the permanent component

Page 26: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Actual Inflation and filtered permanent component

0 20 40 60 80 100 120 140 160 180 200 220-0.02

-0.01

0

0.01

0.02

0.03

0.04

0.05

πτ

Page 27: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The Estimated path of permanent component andthe Kalman Smoother

The standard filter gives an optimal real time estimate of thelatent state

I Sometimes we are interested in the best estimate given thecomplete sample, i.e. Xt|T

Xt|T = E[Xt | ZT ,X0|0

]The Kalman smoother can be used to find Xt|T

Page 28: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The Kalman Smoother: Implementation

Run filter forward, then backward.

Xt|T = Xt|t + Jt−1

(Xt+1|T − Xt+1|t

)(1)

whereJt = Pt|tA

′P−1t+1|t (2)

The covariances of the smoothed state estimation errors can becomputed as

Pt|T = Pt|t + Jt(Pt+1|T − Pt+1|t

)J ′t

(for more details, see Hamilton 1994).

Page 29: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

0 50 100 150 200 250-0.02

-0.01

0

0.01

0.02

0.03

0.04

τt|t

τt|T

Page 30: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

The Kalman Simulation Smoother

Sometimes we want to know something about the uncertainty ofour smoothed estimate.

I One way to illustrate this is to use the Kalman simulationsmoother to simulate the conditional distribution of X

p(Xt | ZT ,X0|0

)= N(Xt|T ,Pt|T )

Pt|T = Pt|t + Jt(Pt+1|T − Pt+1|t

)J ′t

See Durbin and Koopman (2002) for more details.

Page 31: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

Smoothed Estimate and Prob Intervals

0 50 100 150 200-0.2

-0.15

-0.1

-0.05

0

0.05

0.1

0.15

0.2

95%τ

t|T

5%

Page 32: State Space Models and the Kalman FilterState Space Models and the Kalman Filter What we did last time: I The scalar lter I Combining period t prior and signal is analogous to a simple

That’s it for this morning.