You are on page 1of 31

Applied Econometrics

Lecture 9
Time Series Filters

Please note that for interactive manipulation you need Mathematica 6 version of this .pdf. Mathematica 6 is available at all Lab's Computers at IES

http://staff.utia.cas.cz/barunik
Jozef Barunik ( barunik @ utia. cas . cz )

¢ | £
2 Lecture8.nb

Outline

Why Filters? - they has its very interesting area of use in analysis of time series

Linear Filters

Time Domain Filters


Infinite Impulse Response (IIR)
Finite Impulse Response (FIR): MA, EWMA, Zero-LagFilter

Frequency Domain Filters


Frequency, Period, Sampling
Fourier Transforms

Filters in Practice
Problems with Filters

¢ | £
Lecture8.nb 3

Linear Filters - Motivation

A discrete time series as a sequence of observations ordered by a time t, 8xt <¶


t=-¶

we apply filter to extract certain features from time series xt :


trend
seasonalities
business cycles
noise

of course this is very tricky, as we have to choose to what extent we want to "filter" the series
(Do we really filter only noise? Or some important infomration? How do we know?)

¢ | £
4 Lecture8.nb

Linear Filters in Time Domain

Linear filter simply converts a time series xt into another time series yt by a linear transformation

xt Ø FILTER Ø yt

The output yt is result of convolution of the input xt with a coefficient vector wt :


Hwt are also called filter coefficients)

yt = ڦ
i=-¶ wi xt-i

where convolution is formally defined as


w * xt = ڦ
i=-¶ wi xt-i

This might be a problem, as future realizations are needed, thus we may restrict convolution to:
yt = ڦ
i=0 wi xt-i , where only past is utilized (Casual or physically realizable filter)

¢ | £
Lecture8.nb 5

Linear Filters in Time Domain cont.

We classify linear filters according to their response to an impulse signal

if the impulse response of filter is finite, we have finite impulse response filter - FIR filter

if the impulse response of filter is infinite, we have IIR filter

¢ | £
6 Lecture8.nb

Infinite Impulse Response (IIR) Filters

Consider a form of constant coefficient linear difference equation:

yt = ⁄Li=1 ai yt-i + ⁄i=0


M
wi xt-i ,
where the current value of the output is determined by L lagged values of output yt and M
lagged values of input xt , as well as current input value.

Consider this first-order difference equation: yt = a yt-1 + xt .

does it look familiar to you?? But it does not have solution without previous information about
yt
by recursive substitution ( y1 = a y0 + x1, y2 = a y1 + x2 ... ) we get general solution
yt = a t y0 + ⁄t-1 i
i=0 a xt-i

¢ | £
Lecture8.nb 7

Finite Impulse Response (FIR) Filters

General form of an FIR filter is:

M
yt = ⁄i=-N wi xt-i ,

where filter processes M future and M past values as well as the current value of the input.
thus filter is noncausual

Anyone remember how is this equation called?

¢ | £
8 Lecture8.nb

Finite Impulse Response (FIR) Filters cont.

YES !

CONVOLUTION

w * xt
¢ | £
Lecture8.nb 9

Finite Impulse Response (FIR) Filters cont.

very common example is Simple Centered Moving Average:


1
yt = M+N+1 Hxt-M + ... + xt-1 + xt + xt+1 + ... + xt+N L

The impulse response of this filter is finite:


1
M+N+1 ,
if i = -N, ..., -1, 0, 1, ..., M
wi = :
0 otherwise

¢ | £
10 Lecture8.nb

Finite Impulse Response (FIR) Filters cont. - SMA

General form migh be reduced to causal filter by imposing the restriction N=0.
Future values will be ignored by filter, which is inevitable for applications like forecasting

M
yt = ⁄i=0 wi xt-i ,

This form of FIR filter is more common, consider for example Simple Moving Average (SMA):
1 M
yt = M+1 ⁄i=0 xt-i

¢ | £
Lecture8.nb 11

Finite Impulse Response (FIR) Filters cont. - EMA

Of course we can upgrade SMA to have i.e. linearly declining weights, example of SMA(6):
2
yt = H6L H7L H6 xt + 5 xt-1 + 4 xt-2 + 3 xt-3 + 2 xt-4 + xt-5L

Exponential Moving Average (EMA) brings idea of weighting lagged observations exponentially.
yt = a xt-1 + H1 - aL yt-1,

2
where a is smoothing factor a = N+1 , N number of lags included, xt is observation in time, and yt
is EMA in time.
alternativelly, EMAt = EMAt-1 + aHprice - EMAt-1L

BUT these filters has quite large lag (see next slide for example)

¢ | £
12 Lecture8.nb

Finite Impulse Response (FIR) Filters cont. - SMA vs. EMA


Comparison of Simple Moving Average and Exponential Moving Average
MA1HML 50

EMA2HML 50

zoom interval Hin daysL 1130

Show whole period

35

30

25

20
2004 2005 2006 2007 2008

from - to 1

¢ | £
Lecture8.nb 13

Finite Impulse Response (FIR) Filters cont. - Zero Lag Filters

Well SMA/EMA filters do not use future values, they are casual, but in cost of quite large lag,
thus they react very slowly as you can see from demonstration.

Solution might be simple idea of differencing 2 EMA:

EMAHML = 2 µ EMAHML - EMAH2 M - 1L,


where M is the period of EMA.

Of course there are many other possibilities


¢ | £
14 Lecture8.nb

Finite Impulse Response (FIR) Filters cont. - Zero Lag Filters

Comparison of Zero-Lag and EMA filter (choose number of lags to include in filter again)
M 50

zoom interval Hin daysL 1130

Show whole period

35

30

25

20
2004 2005 2006 2007 2008

from - to 1

¢ | £
Lecture8.nb 15

Linear Filters in Frequency Domain

until now, we discussed filters with time domain only

let's draw attention to frequencies (in the Fourier space) of time series instead of quantities

the idea is very simple: each time series can be decomposed into a weighted sum of much
simpler sinusoidal components

thus we are approaching time series as a weighted sum of harmonic functions (sines and cosines)

¢ | £
16 Lecture8.nb

Frequency, Period

f is frequency of cycles per second, (c.p.s., Hz)

w = 2 p f - angular frequency (radians per second)

T = 1 ê f - period

¢ | £
Lecture8.nb 17

Sampling

The process of converting a signal into a numeric sequence (in other words, continuous time or
space to discrete time or space)

The Nyquist frequency is equal to one-half of the sampling frequency


T-1
wNYQUIST = 2

The Nyquist frequency is the highest frequency that can be measured in a signal

¢ | £
18 Lecture8.nb

Sampling - An Undersampled Signal


sinH2 p f tL

Consider signal with frequency f = 7kHz, and use sampling rate of fs = 8 kHz
This is an undersampled signal

Use following interactive example to sample your own signal:


frequency
f @HzD 6877

sinH2 p f tL
sampling rate
Ê
Ê
fs 8 kHz 16 kHz 48 kHz
Ê
Ê
Ê

Ê
Ê Ê
Ê
0.0000 0.0002 0.0004 0.0006 0.0008 0.0010

tHsL

Author: Carsten Roppel


Source: http://demonstrations.wolfram.com/SamplingTheorem/
¢ | £
Lecture8.nb 19

Sampling cont.

According to the sampling theorem, following may occur:

f § fs ê 2: the samples uniquely represent the sine wave of frequency f .

f > fs ê 2: aliasing occurs, because the replicated spectra begin to overlap.

0 § f § fs ê 2: a spectral line appears at the frequency f - fs .

(Again try to use the interactive example from previous slide to see this)
¢ | £
20 Lecture8.nb

Fourier Theory - Mathematical Prerequisities First

A transform takes one function (or signal) and turns it into another function (signal)

Anyone remember how the functions sin(x) and cos(x) are defined ?

¢ | £
Lecture8.nb 21

Fourier Theory - Mathematical Prerequisities First

Definitions are following (these has very nice property, that they are infinite):

x3 x5 x7 ¶ H-1Ln x 2 n+1
sin HxL = x - 3! + 5! - 7! + ... = ⁄n=0 H2 n+1L!
x2 x4 x6 ¶ H-1Ln x 2 n
cos HxL = 1 - 2! + 4! - 6! + ... = ⁄n=0 H2 nL!

now consider definition of ex :

xn x2 x3
ex = ڦ
n=0 n! =1+x + 2! + 3! + ...

¢ | £
22 Lecture8.nb

Fourier Theory - The Complex Exponential

The complex exponential ei x is defined to be the complex variable whose real and imaginary
parts are cos(x) and sin(x): Also known as Euler relationship:

ei x = cosHxL + i sinHxL
¢ | £
Lecture8.nb 23

Fourier Transform

Any infinite sequence xt may be viewed as a combination of an infinite number of sinusoids


with different amplitudes and phases

Discrete Fourier Transform (DFT) is defined as:

XH f L = ڦ
t=-¶ xt e
-i2p f t

where xt are fourier coefficients and can be obtained from the inverse Fourier transfrom:

1 p
xt = 2 p -p XH f
Ÿ L ei 2 p f t „ f ,

¢ | £
24 Lecture8.nb

Fourier Series at Work

Nice example of approximation of five different periodic functions by Fourier Series conver-
gence
See how Fourier series approximate continuous function vs. "step" function
function step sawtooth parabola cubic half-wave rectifier

number of terms 17

x range 2p

1.0

0.5

-6 -4 -2 2 4 6

-0.5

-1.0

Author : Alain Goriely


Source : http : // demonstrations.wolfram.com/FourierSeriesOfSimpleFunctions/
¢ | £
Lecture8.nb 25
¢ | £
26 Lecture8.nb

Filters in Practice - EMA Volatility Estimation

EMA filter plays important role in the risk management


l

S&P standard deviation estimate with EMA Hl=0.9L

0.08

0.06

0.04

0.02

0.00
1980 1990 2000

¢ | £
Lecture8.nb 27

Filters in Practice - Hodrick-Prescott (HP) Filters

HP filter was developed basically fo business cycles analysis, but can also be used to other series.
The standard (unvariate) HP filter finds a smoothed series based only on the time series proper-
ties of the original data. It does so by finding the values of t that minimise the function:

L = ⁄Tt=1 H yt - tt L2 + l ⁄T-1
t=2 HHtt+1 - tt L - Htt - tt-1LL
2

where the weight on smoothness (l).

main drawback of HP filter is that it


¢ | £
28 Lecture8.nb

Filters in Practice - Hodrick-Prescott (HP) Filters

l 2162

zoom interval Hin daysL 347

Show whole period

25

24

23

22

21

20

19
Jan Jul Jan

from - to 1

¢ | £
Lecture8.nb 29

Problems with Filters

filters are very good tool for extraction of certain features, i.e. cyclical parts, etc.

they can not be used for forecasting ! (look at the construction)

denoising - it is often difficult to find "the best" treshold, we never now what kind of informa-
tion we can lose if we extract what appears to be "noise"

¢ | £
30 Lecture8.nb

Further Readings

Other type of powerfull Nonlinear filters is Wavelets

students interested in filters are adviced to use following literature:

Gencay R., Selcuk F., Whitcher B. : An Introduction to Wavelets and Other Filtering Methods
in Finance and Economics, Academic Press, ISBN 0122796705

Percival D.B., Walden A.T. : Wavelet Methods for Time Series Analysis, Cambridge University
Press, ISBN 0521685087

¢ | £
Lecture8.nb 31

Questions

Other examples during seminar

THANK YOU FOR ATTENTION !

¢ | £

You might also like