Anda di halaman 1dari 7

Time Series in Matlab

1

14.384 Time Series Analysis, Fall 2007 Recitation by Paul Schrimpf Supplementary to lectures given by Anna Mikusheva September 17, 2007

Recitation 2

Time Series in Matlab

Simulating an ARMA Model

a(L)y t = b(L)e t

1 clear;

2 a = [1 0.5]; % AR coeffs

3 b = [1 0.4 0.3]; % MA coeffs

4 T = 1000;

5 e = randn(T,1); % generate gaussian white noise

6 y = filter(b,a,e); % generate y

The filter function can be used to generate data from an ARMA model, or apply a ﬁlter to a series.

Impulse-Response

To graph the impulse response of an ARMA, use fvtool

1 % create an impulse response

2 fvtool(b,a,'impulse');

Sample Covariances

1 [c lags]=xcov(y,'biased');

2 figure;

3 plot(lags,c);

4 title('Sample Covariances');

The option, 'biased', says to use γˆ k =

γˆ k =

T k t=1

1

T k

(y t y¯)(y tk y¯)

1

T

T k

t=1

(y t y¯)(y tk y¯); 'unbiased' would use

Spectral Analysis

2

Spectral Analysis

Population spectral density for an ARMA:

1 % population density

2 w = 0:0.1:pi;

3 h = freqz(b,a,w); % returns frequency response = b(eˆ{iw})/a(eˆ{iw})

4 sd = abs(h).ˆ2./sqrt(2 pi); % make into density

*

% frequencies to compute density at

Estimating the Spectral Density

Parametric methods

These estimate an AR(p) and use it to compute the spectrum.

 1 [sdc wc] = pcov(y,8); % estimate spectral density by fitting AR(8) 2 3 [sdy wy] = pyulear(y,8); % estimate spectral density by fitting AR(8) 4 % using 5 % the Yule−walker equations

Non-parametric methods

Deﬁnition 1. The sample periodogram of {x t } t=1 is S(ω) =

Remark 2. The sample periodogram is equal to the Fourier transform of the sample autoco- variances

T

ˆ

T | t

1

T

=1

e iωt x

t | 2

ˆ

S (ω) =

1

T T 1

| 2 =

T | e iωt x t

t=1

k= T 1

γˆ k e iωk

1 [sdp wp] = periodogram(y,[],'onesided'); % estimate using sample

Deﬁnition 3. A smoothed periodogram estimate of the spectral density is

S ˆ (ω) =

π 1

π

h T (λ

ω)

T

|

T

e iλt x t

t=1

| 2

where h T () is some kernel weighting function.

A smoothed periodogram is a weighting moving average of the sample periodogram.

The

width T .

following code estimates a smoothed periodogram using a Parzen kernel with band-

Filtering

3

1 rT = round(sqrt(T));

2 [sdw ww] = pwelch(y,parzenwin(rT),rT1,[],'onesided'); % smoothed periodogram

Deﬁnition 4. A weighted covariance estimate of the spectrum is:

where g T (k) is some kernel.

ˆ

S (ω) =

S

T

k=S T

γˆ k g T (k)e

iωk

1 % bartlett weighted covariances

2 wb = 0:0.1:pi;

3 rT = sqrt(T);

4 [c t]=xcov(y,'biased');

5 weight = 1abs(t)/rT;

6 weight(abs(t)>rT) = 0;

7 for j=1:length(wb)

8 sdb(j) = sum(c. * weight. * exp(i * wb(j) * t));

9 end

10 sdb = sdb / sqrt(2 * pi);

Filtering

See: http://ideas.repec.org/c/wpa/wuwppr/0507001.html for code for common ﬁlters.

Example: Simulating an ARMA and estimating the spectrum

clear;
1
close all; % closes all open figure windows
2
3
% model: y t = 0.9 y {t−1} + b(L) e t
4
a = [1 −0.7]; % AR coeffs
5
b = [1 0.3 2]; % MA coeffs
6
T = 200;
7
e = randn(T,1); % generate gaussian white noise
8
y = filter(b,a,e); % generate y
9
10
% plot y
11
figure;
12
plot(y);
13
xlabel('t');
14
ylabel('y');
15
title('ARMA(1,2)');
16

Example: Simulating an ARMA and estimating the spectrum

4

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

% create an impulse response

fvtool(b,a,'impulse');

% calculate and plot sample autocovariances

[c lags]=xcov(y,'biased');

figure;

plot(lags,c);

title('Sample Covariances');

% estimate spectral density

% parametric

[sdc wc] = pcov(y,8); % estimate spectral density by fitting AR(8)

[sdy wy] = pyulear(y,8); % estimate spectral density by fitting AR(8) % using % the Yulewalker equations

% nonparametric

[sdp wp] = periodogram(y,[],'onesided'); % estimate using unsmoothed % periodogram

rT = round(sqrt(T)) 3;

*

[sdw ww] = pwelch(y,parzenwin(rT),rT1,[],'onesided'); % smoothed

periodogram

% bartlett weighted covariances

[c lags]=xcov(y,'biased');

t = (T1):(T1);

weight = 1abs(t')/rT;

weight(abs(t')>rT) = 0;

wb = ww;

for j=1:length(wb)

sdb(j) = sum(c. * weight. * exp(i * wb(j) * ((T1):(T1))'));

end

sdb = sdb / sqrt(2 pi);

*

% population density

w = wb;

h = freqz(b,a,w);

sd = abs(h).ˆ2./sqrt(2 pi);

*

figure;

plot(wp,sdp,wc,sdc,ww,sdw,wb,sdb,wy,sdy,w,sd);

legend('raw periodogram','parametric AR','smoothed periodogram',

'bartlett weighted cov','YuleWalker','population density');

Example: Simulating an ARMA and estimating the spectrum

5

y

15

10

5

0

−5

−10

−15

ARMA(1,2)

0
50
100
150
200

t

Impulse Response

2.5
2
1.5
1
0.5
0
0
5
10
15
20
25
Amplitude

Samples

Example: Simulating an ARMA and estimating the spectrum

6

Sample Covariances

16
14
12
10
8
6
4
2
0
−2
−4
−200
−150
−100
−50
0
50
100
150
200

Example: Simulating an ARMA and estimating the spectrum

7

70

60

50

40

30

20

10

0

raw periodogram
parametric AR
smoothed periodogram
bartlett weighted cov
Yule−Walker
population density
0
0.5
1
1.5
2
2.5
3
3.5