Professional Documents
Culture Documents
Recitation 2: Time Series in Matlab
Recitation 2: Time Series in Matlab
clear;
a = [1 0.5]; % AR coeffs
b = [1 0.4 0.3]; % MA coeffs
T = 1000;
e = randn(T,1); % generate gaussian white noise
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
Impulse-Response
To graph the impulse response of an ARMA, use fvtool
1
2
Sample Covariances
1
2
3
4
[c lags]=xcov(y,'biased');
figure;
plot(lags,c);
title('Sample Covariances');
1
T
T k
t=1
(yt y)(y
tk y); 'unbiased' would use
Spectral Analysis
Population spectral density for an ARMA:
1
2
3
4
% population density
w = 0:0.1:pi;
% frequencies to compute density at
h = freqz(b,a,w); % returns frequency response = b(e{iw})/a(e{iw})
sd = abs(h).2./sqrt(2*pi); % make into density
Parametric methods These estimate an AR(p) and use it to compute the spectrum.
2
3
4
5
Non-parametric methods
Filtering
Remark 2. The sample periodogram is equal to the Fourier transform of the sample autoco
variances
T
T 1
1 it 2
S() = |
e
xt | =
k eik
T t=1
k=T 1
using sample
rT = round(sqrt(T));
ST
k gT (k)eik
k=ST
1
2
3
4
5
6
7
8
9
10
wb = 0:0.1:pi;
rT = sqrt(T);
[c t]=xcov(y,'biased');
weight = 1abs(t)/rT;
weight(abs(t)>rT) = 0;
for j=1:length(wb)
sdb(j) = sum(c.*weight.*exp(i*wb(j)*t));
end
Filtering
See: http://ideas.repec.org/c/wpa/wuwppr/0507001.html for code for common lters.
1
2
clear;
4
5
6
7
8
9
%
a
b
T
e
y
= [1 0.7]; % AR coeffs
= 200;
= filter(b,a,e); % generate y
10
11
12
13
14
15
16
% plot y
figure;
plot(y);
xlabel('t');
ylabel('y');
title('ARMA(1,2)');
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
% 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
34
35
36
37
38
39
40
% 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
41
42
43
44
45
46
47
48
[c lags]=xcov(y,'biased');
t = (T1):(T1);
weight = 1abs(t')/rT;
weight(abs(t')>rT) = 0;
wb = ww;
for j=1:length(wb)
ARMA(1,2)
15
10
10
15
49
50
51
50
100
t
150
200
sdb(j) = sum(c.*weight.*exp(i*wb(j)*((T1):(T1))'));
end
52
53
54
55
56
% population density
w = wb;
h = freqz(b,a,w);
sd = abs(h).2./sqrt(2*pi);
57
58
59
60
61
figure;
plot(wp,sdp,wc,sdc,ww,sdw,wb,sdb,wy,sdy,w,sd);
Impulse Response
2.5
Amplitude
1.5
0.5
0
0
10
15
20
25
Samples
Sample Covariances
16
14
12
10
200
150
100
50
50
100
150
200
70
raw periodogram
parametric AR
smoothed periodogram
bartlett weighted cov
YuleWalker
population density
60
50
40
30
20
10
0.5
1.5
2.5
3.5
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.