-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSAprojPartB.m
465 lines (309 loc) · 10.6 KB
/
TSAprojPartB.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
%% PROJECT TSA 2019 with Joel Bluhme cont...
%% PART B Conclusions:
% A good Box-Jenkins-model is saved in BJ and the steps for getting to it
% and the predictions with it on val and test data is found below
% BJ =
% Discrete-time BJ model: y(t) = [B(z)/F(z)]u(t) + [C(z)/D(z)]e(t)
% B(z) = 1.672 (+/- 0.05988) + 1.179 (+/- 0.05664) z^-1
%
% C(z) = 1 + 0.0531 (+/- 0.02778) z^-24
%
%
% D(z) = 1 - 1.34 (+/- 0.02336) z^-1 + 0.3929 (+/- 0.02343) z^-2
% - 0.1327 (+/- 0.02484) z^-23 + 0.1078 (+/- 0.02423) z^-24
%
% F(z) = 1 - 0.4636 (+/- 0.02291) z^-2
% As seen are all parameters highly significant except c24 which is right
% on edge of the confidence interval (estimate+-2 stdv). It does however improve
% val.data predictions and is therefore kept.
% This BJ model yields much better pred. error variance on the validation data set
% then the ARMA model M1 (in TSAprojPartA). Especially the 7 step and
% 26-step predictions are extraordinary improvement
clear
clc
%% Pretty succesful BJ attempt
%% Working BJ Model and prediction with it
%% Define data
clear
clc
load('climate67.dat')
Mdldata=climate67(3400:5000,:);
Valdata=climate67(5001:5600,:);
Test1data=climate67(5601:5768,:);
Test2data=climate67(7501:7668,:);
%% Transforming u with log and making zero mean & making y zero mean
OptLambda=bcNormPlot(climate67(1:8500,6)) % =0.02 so log transformation seems reasonable
% We need to shift u up before taking log
u=Mdldata(:,6);
u=u+150; % Shifts u 150 units up to ensure positivity
u=log(u);
MeanLogu=mean(u);
u=u-mean(u);
y=Mdldata(:,8); %Output modelling vector
meanY=mean(y);
y=y-meanY; % Makes y zero mean
%%
%% Model u as ARMA
figure(1)
phi = pacf( u, 100,0.05, 1, 0 );
title("PACF for u with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( u, 100,0.05, 1, 0 );
title("ACF for u with 95% confidence interval (asymptotic interval)");
%% Clear 24 periodicity in acf and prominent 1 and 2 lags in pacf ->
A24=[1 zeros(1,23) -1];
A=conv([1 1 1],A24);
C=[1 zeros(1,23) 1];
M1u = idpoly ( A,[],C);
M1u.Structure.a.Free =A;
M1u.Structure.c.Free = C;
data = iddata(u);
modelU = pem(data,M1u);
r=resid(data,modelU);
figure(1)
phi = pacf( r.y, 100,0.05, 1, 0 );
figure(2)
rho = acf( r.y, 100,0.05, 1, 0,0 );
figure(3)
whitenessTest(r.y,0.01)
figure(4)
plot(r.y)
present(modelU)
% Very white residual!!!
%% Transform (pre-whiten) y and u with inverse input arma model
upw=filter(modelU.a,modelU.c,u);
ypw=filter(modelU.a,modelU.c,y);
upw=upw(25:end);
ypw=ypw(25:end);
M=100;
crosscorre(upw,ypw,M)
%% CCF above -> r=2 s=1 d=0 seems good choice of orders
% We tried s=0 first but the resulting CCF between res and u was bad then
A2 = [1 1 1]; % r=2
B =[1 1]; % s=1
H = idpoly ([1] ,[B] ,[] ,[] ,[A2]);
zpw = iddata(ypw,upw);
Mba2 = pem(zpw,H);
present(Mba2)
vhat = resid(Mba2,zpw);
%% CCF between vhat=ypw-H(z)upw and upw
crosscorre(vhat.y,upw,M) % looks relatively good
%% Modelling residual res=y-H(z)u as ARMA
res=y-filter(Mba2.b,Mba2.f,u);
z = iddata(y,u);
r=resid(z,Mba2);
%% CCF between res=y-H(z)u and u
M=100
crosscorre(res,u,M) % not perfect uncorrelation but looks good enough to move on
%% ACF and PACF of Res
phi = pacf( res, 50,0.05, 1, 0 );
title("PACF for u with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( res, 50,0.05, 1, 0,0 );
title("ACF for u with 95% confidence interval (asymptotic interval)");
%% Seems to be a dependency at lag 1,2 and 23, 24 in PACF:
% We also add a c24 coeff.
A1=[1 1 1 zeros(1,20) 1 1];
C1=[1 zeros(1,23) 1];
ar2=idpoly(1,[],C1,A1,[]);
ar2.Structure.d.Free =A1;
ar2.Structure.c.Free =C1;
data=iddata(res);
NoiseMdl=pem(data,ar2);
r=resid(data,NoiseMdl);
present(NoiseMdl)
%% Plots of ehat
phi = pacf( r.y, 100,0.05, 1, 0 );
title("PACF for e with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( r.y, 100,0.05, 1, 0,0 );
title("ACF for e with 95% confidence interval (asymptotic interval)");
figure(3)
normplot(phi)
title("Normal probability plot of pacf");
whitenessTest(r.y)
% relatively white
%% Finally estimate all paramters together, with PEM
% we remove lag 1 in A2 polynomial since it is severely unsignificant
A1=[1 1 1 zeros(1,20) 1 1 0];
C1=[1 zeros(1,23) 1];
B =[1 1];
A2 = [1 0 1];
Mi = idpoly (1 ,B ,C1 ,A1 ,A2);
Mi.Structure.d.Free =A1;
Mi.Structure.c.Free =C1;
Mi.Structure.b.Free =B;
Mi.Structure.f.Free =A2;
z = iddata(y,u);
BJ= pem(z,Mi);
present(BJ)
ehat=resid(BJ,z);
M= 100; stem(-M:M,xcorr(ehat.y,u,M,'biased'));
title('Cross correlation function'), xlabel('Lag')
hold on
plot(-M:M, 2/sqrt(length(u))*ones(1,2*M+1),'--')
plot(-M:M, -2/sqrt(length(u))*ones(1,2*M+1),'--')
hold off
%%
phi = pacf( ehat.y, 100,0.05, 1, 0 );
title("PACF for e with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( ehat.y, 100,0.05, 1, 0,0 );
title("ACF for e with 95% confidence interval (asymptotic interval)");
figure(3)
whitenessTest(ehat.y,0.01)
%% Predictions on val. data:
%% 1 step pred
k=1;
% Transform BJ into ARMAX with:
B=conv(BJ.b,BJ.d);
A=conv(BJ.f,BJ.d);
C=conv(BJ.c,BJ.f);
ufut1=u(k+1:end);
SF=50; % Safety factor, begin predicting SF time units before val to handle the initial corruptness of the data
% yhat_1(1)=prediction of y(end-SF+1)=ynew(2) thus
% yhat_1(1+SF)=prediction of ynew(SF+2)=yval(1) ie the first "wanted" prediction
% yhat_1(end)=prediction of yval(end) ie the last "wanted" pred.
y=Mdldata(:,8); % Our zero mean output modelling and val. data vector
yval=Valdata(:,8);
y=y-meanY;
yval=yval-meanY;
u=Mdldata(:,6); % Our shifted,log,zero mean input modelling and val. data vector
uval=Valdata(:,6);
u=u+150;
uval=uval+150;
u=log(u);
uval=log(uval);
u=u-MeanLogu;
uval=uval-MeanLogu;
ynew=[y(end-SF:end); yval];
unew=[u(end-SF:end); uval];
unewfu1=unew(k+1:end); % Future u vector ie unefu1(i)=unew(i+1)
[F,G]=Diophantine(C,A,k);
[Fhat,Ghat]=Diophantine(conv(B,F),C,k)
yhat_1=filter(Ghat,C,unew(1:end-k))+filter(G,C,ynew(1:end-k))+filter(Fhat,1,unewfu1);
figure(1)
plot(yhat_1(2+SF-k:end)+meanY)
hold on
plot(yval(1:end)+meanY)
legend('1-step pred','True value')
hold off
pe1=yval(1:end)-yhat_1(2+SF-k:end); % 1-step pred error
figure(2)
rho = acf( pe1, 100,0.05, 1,0 ,0 );
title("ACF for pe1");
figure(3)
whitenessTest(pe1,0.01)
V_pe1=var(pe1) %=0.2261
mean(pe1) % =0.0566
%% 7 step pred.
k=7;
% Same SF as for 1-step
unewfu7=unew(k+1:end); % Future u vector ie unefu7(i)=unew(i+7)
[F,G]=Diophantine(C,A,k);
[Fhat,Ghat]=Diophantine(conv(B,F),C,k)
yhat_7=filter(Ghat,C,unew(1:end-k))+filter(G,C,ynew(1:end-k))+filter(Fhat,1,unewfu7);
figure(1)
plot(yhat_7(2+SF-k:end)+meanY)
hold on
plot(yval(1:end)+meanY)
legend('7-step pred','True value')
hold off
pe7=yval(1:end)-yhat_7(2+SF-k:end); % 7-step pred error
figure(2)
rho = acf( pe7, 100,0.05, 1, 6 );
title("ACF for pe7"); % Should me MA(7-1) which seems reasonable
V_pe7=var(pe7) % = 1.96
mean(pe7) % 0.5049 Slight underestimation again
%% 26 step pred.
k=26;
unewfu26=unew(k+1:end); % Future u vector ie unefu26(i)=unew(i+26)
[F,G]=Diophantine(C,A,k);
[Fhat,Ghat]=Diophantine(conv(B,F),C,k)
yhat_26=filter(Ghat,C,unew(1:end-k))+filter(G,C,ynew(1:end-k))+filter(Fhat,1,unewfu26);
figure(1)
plot(yhat_26(2+SF-k:end)+meanY)
hold on
plot(yval(1:end)+meanY)
legend('26-step pred','True value')
hold off
pe26=yval(1:end)-yhat_26(2+SF-k:end); % 26-step pred error
figure(2)
rho = acf( pe26, 100,0.05, 1, 25 );
title("ACF for pe26"); % Should me MA(26-1) which seems reasonable
V_pe26=var(pe26) % =2.9880
mean(pe26) % =1.0774
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Pred on test data
Test1data=climate67(5601:5768,:);
Test2data=climate67(7501:7668,:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 7 step pred. test1 data
k=7;
% Same SF as for 1-step
yval=Valdata(:,8); % Our val. data vector and test1 data
yval=yval-meanY;
ytest1=Test1data(:,8);
ytest1=ytest1-mean(Mdldata(:,8)); % Our zero mean test1 data
uval=Valdata(:,6);
uval=uval+150;
uval=log(uval);
uval=uval-MeanLogu;
utest1=Test1data(:,6);
utest1=utest1+150;
utest1=log(utest1);
utest1=utest1-MeanLogu;
ynew=[yval(end-SF:end); ytest1]; % Concatenate last SF-1 values of val data and test1 data
unew=[uval(end-SF:end); utest1];
unewfu7=unew(k+1:end); % Future u vector ie unefu7(i)=unew(i+7)
%
[F,G]=Diophantine(C,A,k);
[Fhat,Ghat]=Diophantine(conv(B,F),C,k)
yhat_7=filter(Ghat,C,unew(1:end-k))+filter(G,C,ynew(1:end-k))+filter(Fhat,1,unewfu7);
figure(1)
plot(yhat_7(2+SF-k:end)+meanY)
hold on
plot(ytest1(1:end)+meanY)
legend('7-step pred','True value')
hold off
pe7=ytest1(1:end)-yhat_7(2+SF-k:end); % 7-step pred error
figure(2)
rho = acf( pe7, 100,0.05, 1, 6 );
title("ACF for pe7"); % Should me MA(7-1) which seems reasonable
V_pe7=var(pe7) % = 1,7438 lower than on validation data
mean(pe7) % 0.4265 Slight underestimation again
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 7 step pred. test2 data
k=7;
% Same SF as for 1-step
yprev=climate67(7401:7500,8); % Our zero mean adj. output data vector of the 100 data points right before ytest2
yprev=yprev-mean(Mdldata(:,8));
ytest2=Test2data(:,8);
ytest2=ytest2-mean(Mdldata(:,8)); % Our zero mean test1 data
uprev=climate67(7401:7500,8); % Our input data vector of the 100 data points right before utest2
uprev=uprev+150;
uprev=log(uprev);
uprev=uprev-MeanLogu;
utest2=Test2data(:,6);
utest2=utest2+150;
utest2=log(utest2);
utest2=utest2-MeanLogu;
ynew=[yprev(end-SF:end); ytest2]; % Concatenate last SF-1 values of prev data and test2 data
unew=[uprev(end-SF:end); utest2];
unewfu7=unew(k+1:end); % Future u vector ie unefu7(i)=unew(i+7)
%
[F,G]=Diophantine(C,A,k);
[Fhat,Ghat]=Diophantine(conv(B,F),C,k)
yhat_7=filter(Ghat,C,unew(1:end-k))+filter(G,C,ynew(1:end-k))+filter(Fhat,1,unewfu7);
figure(1)
plot(yhat_7(2+SF-k:end)+meanY)
hold on
plot(ytest2(1:end)+meanY)
legend('7-step pred','True value')
hold off
pe7=ytest2(1:end)-yhat_7(2+SF-k:end); % 7-step pred error
figure(2)
rho = acf( pe7, 100,0.05, 1, 6 );
title("ACF for pe7"); % Should me MA(7-1) which seems reasonable
V_pe7=var(pe7) % = 1,3152 lower than on test1, most likely due to lower temperatures and thus lower absolut variability
mean(pe7) % -1.2544 Slight overestimation again