-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBJModel.m
280 lines (192 loc) · 5.74 KB
/
BJModel.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
%% 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:5800,:);
Test2data=climate67(8000-500:9200-500,:);
%% Transforming u with log and making 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, 1 );
title("PACF for u with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( u, 100,0.05, 1, 1 );
title("ACF for u with 95% confidence interval (asymptotic interval)");
%%
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, 1 );
figure(2)
rho = acf( r.y, 100,0.05, 1, 1 );
figure(3)
whitenessTest(r.y,0.01)
figure(4)
plot(r.y)
present(modelU)
%% 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=40
crosscorre(upw,ypw,M)
%% r=2 s=1 d=0
% We tried s=0 first but the resulting CCF between res and u was bad then
A2 = [1 1 1];
B =[1 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)
%% 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
crosscorre(res,u,M)
%% ACF and PACF of Res
phi = pacf( res, 50,0.05, 1, 1 );
title("PACF for u with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( res, 50,0.05, 1, 1 );
title("ACF for u with 95% confidence interval (asymptotic interval)");
%% Seems to be a dependency at lag 1,2 and 23, 24 in PACF
%
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, 1 );
title("PACF for e with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( r.y, 100,0.05, 1, 1 );
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
A1=[1 1 1 zeros(1,20) 1 1 0];
C1=[1 zeros(1,23) 1];
B =[1 1];
A2 = [1 1 1];
Mi = idpoly (1 ,B ,C1 ,A1 ,A2);
Mi.Structure.d.Free =A1;
Mi.Structure.c.Free =C1;
Mi.Structure.b.Free =B;
z = iddata(y,u);
BJ= pem(z,Mi);
present(BJ)
ehat=resid(BJ,z);
M= 40; 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, 1 );
title("PACF for e with 95% confidence interval (asymptotic interval)");
figure(2)
rho = acf( ehat.y, 100,0.05, 1, 1 );
title("ACF for e with 95% confidence interval (asymptotic interval)");
figure(3)
whitenessTest(ehat.y,0.05)
%% 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, 1 );
title("ACF for pe1");
figure(3)
whitenessTest(pe1,0.01)
V_pe1=var(pe1) %=0.2304
mean(pe1) % =0.0647
%% 7 step pred.
k=7;
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.
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, 1 );
title("ACF for pe7"); % Should me MA(7-1) which seems reasonable
V_pe7=var(pe7) % = 2,00
mean(pe7) % 0.58 Slight underestimation again