-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclark_okun2_new.m
272 lines (222 loc) · 8.01 KB
/
clark_okun2_new.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
% Îöåíêà ñòàíäàðòíîé ìîäåëè
% áåç àâòîðåãðåññèè â öèêëå áåçðàáîòèöû
% áåç ñåçîííîñòè
% íàóêàñò ïðè èçâåñòíîé áåçðàáîòèöå çà 1, 2, 3 ìåñÿöà
% áåç îãðàíè÷åíèé íà òðåíäû âûïóñêà è áåçðàáîòèöû
%%
close all
clear all
global y
cd C:\Users\evsta\OneDrive\Desktop\ðàáîòà\clark_okun
plus_year=2 % 0-1999q1, 1-2000q1
year_start=1999+plus_year;
un_month_known=3; % 2 - äâà ìåñÿöà êâàðòàëà èçâåñòíû, 1 - 1 ìåñÿö, 3 - 3
h_forecast_length=23; % ñêîëüêî ïîñëåäíèõ òî÷åê ïðîãíîçèòü
data=xlsread('GDP_Russia_noseas_1999q1_2020q3.xlsx');
% íà ïîäà÷ó èäåò ìåñÿ÷íàÿ áåçðàáîòèöà ñ òåñòîâîãî ìåñÿöà
unempl = xlsread('U_Russia_noseas_1999m1_2020m9.xlsx');
forecast_arima=xlsread('arima_noseas_Russia_2013q4_2020q3.xlsx');
forecast=zeros(h_forecast_length,1);
unempl = unempl(:,1);
means1 = zeros(1,ceil(length(unempl)/3));
means2 = zeros(1,ceil(length(unempl)/3));
means3 = zeros(1,ceil(length(unempl)/3));
% remainder = length(unempl)/3 - round(length(unempl)/3);
%
% if remainder ~= 0
% if remainder > 0.5
% unempl = [unempl' mean([unempl(length(unempl)-1),unempl(length(unempl))])];
% else
% unempl = [unempl' unempl(length(unempl)) unempl(length(unempl))];
% end
% end
for i = 1:3:length(unempl)
means1(ceil(i/3)) = mean(unempl(i:(i)));
end
for i = 1:3:length(unempl)
means2(ceil(i/3)) = mean(unempl(i:(i+1)));
end
for i = 1:3:length(unempl)
means3(ceil(i/3)) = mean(unempl(i:(i+2)));
end
means1 = means1/100; %ñðåäíèå ïî áåçðàáîòèöå çà êàæäûé êâàðòàë
means2 = means2/100;
means3 = means3/100;
data(:,2) = means3*100';
for sss=h_forecast_length:-1:1
%sss=1
y=[log(data(:,1)) data(:,2)/100]';
tinit=1+4*plus_year; % 1 - 1999q1
tend=length(data)-sss; % 86 - 2020q2,
tt=year_start+(tinit*0.25-0.25):0.25:year_start+(tend*0.25-0.25);
y1=y(:,tinit:tend+1);
y1=y1(2,1:end);
if un_month_known==1
uq_nc=means1;
uq_nc=uq_nc(tinit:tend+1);
y1(1,end)=uq_nc(end);
end
if un_month_known==2
uq_nc=means2;
uq_nc=uq_nc(tinit:tend+1);
y1(1,end)=uq_nc(end);
end
y=y(:,tinit:tend);
t=length(y);
options = optimset('Display','iter',...
'TolFun',1e-6,...
'HessUpdate', 'bfgs',...
'MaxFunEvals',3000,...
'TolX',1e-6);
params_start=[-0.2 1.5 -0.8 0.1 0.1 0.1 0.1 0.1]
[betaest,FVAL,EXITFLAG,OUTPUT,GRAD,HESSIAN] = fminunc(@loglike_clark_okun2,params_start,options)
betaest;
std=((diag(pinv(1/t*HESSIAN))/t).^0.5)';
[betaest
abs(std)
betaest./abs(std)]
params=betaest;
beta=params(1);
phi1=params(2);
phi2=params(3);
sigma2nu=params(4)^2;
sigma2v=params(5)^2;
sigma2eps=params(6)^2;
sigma2ksi=params(7)^2;
sigma2del=params(8)^2;
t1=length(y);
t=length(y1);
%z(t)=[tao(t) mu(t) us(t) c(t) c(t-1) uc(t)]'
H=[1 0 0 1 0 0
0 0 1 0 0 1]';
H1=[0 0 1 0 0 1]';
F=[ 1 1 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 phi1 phi2 0
0 0 0 1 0 0
0 0 0 beta*phi1 beta*phi2 0];
Q=zeros(6,6);
Q(1,1)=sigma2nu+sigma2v;
Q(1,2)=sigma2v;
Q(2,1)=sigma2v;
Q(2,2)=sigma2v;
Q(3,3)=sigma2eps;
Q(4,4)=sigma2ksi;
Q(4,6)=beta*sigma2ksi;
Q(6,4)=beta*sigma2ksi;
Q(6,6)=beta^2*sigma2ksi+sigma2del;
statet_tm1=zeros(6,t+1);
Pt_tm1=zeros(6,6,t+1);
yt_tm1=zeros(2,t);
loglikle=zeros(1,t);
%%%initialisation
%z(t)=[tao(t) mu(t) us(t) c(t) c(t-1) uc(t)]'
statet_tm1(:,1)=[y(1,1) y(1,2)-y(1,1) y(2,1) 0 0 0]';
vecP10=(eye(size(kron(F(4:6,4:6),F(4:6,4:6))))-kron(F(4:6,4:6),F(4:6,4:6)))^(-1)*vec(Q(4:6,4:6));
P10=reshape(vecP10,3,3);
Pt_tm1(:,:,1)=[1000 0 0 0 0 0
0 1000 0 0 0 0
0 0 1000 0 0 0
0 0 0 P10(1,1) P10(1,2) P10(1,3)
0 0 0 P10(2,1) P10(2,2) P10(2,3)
0 0 0 P10(3,1) P10(3,2) P10(3,3)];
for i=1:t
if i<=t1
yt_tm1(:,i)=H'*statet_tm1(:,i);
statet_tm1(:,i+1)=F*statet_tm1(:,i)+F*Pt_tm1(:,:,i)*H*(H'*Pt_tm1(:,:,i)*H)^(-1)*(y(:,i)-H'*statet_tm1(:,i));
Pt_tm1(:,:,i+1)=F*(Pt_tm1(:,:,i)-Pt_tm1(:,:,i)*H*(H'*Pt_tm1(:,:,i)*H)^(-1)*H'*Pt_tm1(:,:,i))*F'+Q;
loglikle(i)=-0.5*log(2*pi)-0.5*log(det((H'*Pt_tm1(:,:,i)*H)))-0.5*(y(:,i)-H'*statet_tm1(:,i))'*(H'*Pt_tm1(:,:,i)*H)^(-1)*(y(:,i)-H'*statet_tm1(:,i));
else
yt_tm1(:,i)=H1'*statet_tm1(:,i);
statet_tm1(:,i+1)=F*statet_tm1(:,i)+F*Pt_tm1(:,:,i)*H1*(H1'*Pt_tm1(:,:,i)*H1)^(-1)*(y1(:,i)-H1'*statet_tm1(:,i));
Pt_tm1(:,:,i+1)=F*(Pt_tm1(:,:,i)-Pt_tm1(:,:,i)*H1*(H1'*Pt_tm1(:,:,i)*H1)^(-1)*H1'*Pt_tm1(:,:,i))*F'+Q;
loglikle(i)=-0.5*log(2*pi)-0.5*log(det((H1'*Pt_tm1(:,:,i)*H1)))-0.5*(y1(:,i)-H1'*statet_tm1(:,i))'*(H1'*Pt_tm1(:,:,i)*H1)^(-1)*(y1(:,i)-H1'*statet_tm1(:,i));
end
end
statet_t=zeros(6,t);
Pt_t=zeros(6,6,t);
Jt=zeros(6,6,t);
statet_T=zeros(6,t);
Pt_T=zeros(6,6,t);
for i=1:t
if i<=t1
yt_tm1(:,i)=H'*statet_tm1(:,i);
statet_t(:,i)=statet_tm1(:,i)+Pt_tm1(:,:,i)*H*(H'*Pt_tm1(:,:,i)*H)^(-1)*(y(:,i)-H'*statet_tm1(:,i));
statet_tm1(:,i+1)=F*statet_tm1(:,i)+F*Pt_tm1(:,:,i)*H*(H'*Pt_tm1(:,:,i)*H)^(-1)*(y(:,i)-H'*statet_tm1(:,i));
Pt_t(:,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.75;
forecast_arima=forecast_arima(end-h_forecast_length+1:end);
tt1=tt((end-h_forecast_length+1:end));
y=[log(data(:,1)) data(:,2)/100]';
test=y(1,end-h_forecast_length+1:end)'
forecast_uc=fliplr(forecast')'
for minus_last=0:2
e_uc=test(1:end-minus_last)-forecast_uc(1:end-minus_last);
e_arima=test(1:end-minus_last)-forecast_arima(1:end-minus_last);
rmse_uc=(1/(h_forecast_length-minus_last)*sum((e_uc).^2))^0.5;
rmse_arima=(1/(h_forecast_length-minus_last)*sum((e_arima).^2))^0.5;
e_uc1=e_uc./test(1:end-minus_last);
e_arima1=e_arima./test(1:end-minus_last);
mape_uc=100/(h_forecast_length-minus_last)*sum(abs(e_uc1));
mape_arima=100/(h_forecast_length-minus_last)*sum(abs(e_arima1));
[rmse_uc, mape_uc]
end
d1=(e_uc).^2-(e_arima).^2;
V1=var(d1);
d2=abs(e_uc)-abs(e_arima);
V2=var(d2);
dm_sqr=mean(d1)/sqrt(V1/length(d1));
dm_abs=mean(d2)/sqrt(V2/length(d2));
% ÄÌ òåñò äëÿ ãîðèçîíòà ìèíóñ 2 ïîñëåäíèõ òî÷êè (ñì. öèêë ñâåðõó)
[dm_abs dm_sqr 2*tcdf(-abs(dm_abs),length(e_uc)-1) 2*tcdf(-abs(dm_sqr),length(e_uc)-1)]
%%
for minus_last=0:2
e_uc=test(1:end-minus_last)-forecast_uc(1:end-minus_last);
e_arima=test(1:end-minus_last)-forecast_arima(1:end-minus_last);
rmse_uc=(1/(h_forecast_length-minus_last)*sum((e_uc).^2))^0.5;
rmse_arima=(1/(h_forecast_length-minus_last)*sum((e_arima).^2))^0.5;
e_uc1=e_uc./test(1:end-minus_last);
e_arima1=e_arima./test(1:end-minus_last);
mape_uc=100/(h_forecast_length-minus_last)*sum(abs(e_uc1));
mape_arima=100/(h_forecast_length-minus_last)*sum(abs(e_arima1));
[rmse_arima, mape_arima]
end
%%
figure
plot(tt1,test,'Linewidth',2)
grid on
xlim([tt1(1) tt1(end)+0.5])
hold on
plot(tt1,forecast_uc,'Linewidth',2)
plot(tt1,forecast_arima,'Linewidth',2)
legend('Ëîãàðèôì ÂÂÏ','Ôèëüòðà Êàëìàíà ïðè èçâåñòíîé áåçðàáîòèöå çà 3 ìåñÿöà','ARIMA','location','NW')
hold off
set(gca,'FontSize',14)
%%