-
Notifications
You must be signed in to change notification settings - Fork 1
/
AR1.m
243 lines (201 loc) · 7.86 KB
/
AR1.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
% function [] = modelestimation(process)
clear all
close all
clc
disp('AR1')
rng('default')
% global Y epsY
% Monte-Carlo runs
runs = 100;
%AR(1) model specs
T = 300;
Y = NaN(T,runs);
theta = [0.2 ; 0.7]; %[mu_y sigma phi_1]
p = 1;
q = 0;
%residuals
epsY = theta(1)*randn(T,runs);
%Generate the AR(1) process
Y(1,:) = epsY(1,:);
for t = 1:T-1
Y(t+1,:) = theta(2)*Y(t,:) + epsY(t+1,:);
end
% % To plot some stuff
% subplot(2,1,1);
% autocorr(Y(:,1),100);
%
% subplot(2,1,2);
% [f,xi] = ksdensity(Y(:,1));
% ts1 = f;
% plot(ts1);
% estimation in ARMA(p,q) model
thetaStart = [0.1 ; 0.5];
options = optimset('TolX', 0.0001, 'Display', 'iter-detailed', 'Maxiter', 5000, 'MaxFunEvals', 5000, 'LargeScale', 'off', 'HessUpdate', 'bfgs');
%% DL/Inn and MLE
for i = 1:runs
objfun = @(thetaStart)(-loglikeAR1(Y(:,i), thetaStart, T));
[theta_mle_AR1(:,i), dLogLikAR1(i),~,~,~,hess] = fminunc(objfun, thetaStart, options);
% disp(i)
invhess = inv(hess);
SEAR1(i) = 1.96*sqrt(invhess(2,2));
objfun = @(thetaStart)(-loglikeMA1(Y(:,i), thetaStart, T));
[theta_mle_MA1(:,i), dLogLikMA1(i),~,~,~,hess] = fminunc(objfun, thetaStart, options);
invhess = inv(hess);
SEMA1(i) = 1.96*invhess(2,2);
%
% objfun = @(thetaStart)(-loglikeARMA11(Y(:,i), [0.1 ; 0.5 ; 0.1], T));
% [theta_mle_ARMA11(:,i), dLogLikARMA11(i),~,~,~,hess] = fminunc(objfun, [0.1 ; 0.5 ; 0.1], options);
% invhess = inv(hess);
% SEARMA11 = 1.96*invhess(2,2);
% SEARMA11th = 1.96*invhess(3,3);
% ================
res(i) = Hevia_arma_mle(Y(:,i), 1, 1);
theta_mle_ARMA11(1,i) = res(i).sigma;
theta_mle_ARMA11(2,i) = res(i).ar;
theta_mle_ARMA11(3,i) = res(i).ma;
invhess = inv(res(i).hess);
SEARMA11 = 1.96*invhess(1,1);
SEARMA11th = 1.96*invhess(2,2);
dLogLikARMA11(i) = res(i).loglike;
% ================
% ToEstMdl = arima(1,0,0);
% [EstMdl,EstParamCov,logL,info] = estimate(ToEstMdl,Y(:,i)-mean(Y(:,i)));
% theta_mle_AR1(1,i) = sqrt(info.X(3));
% theta_mle_AR1(2,:) = info.X(2);
% SEAR1(i) = 1.96*theta_mle_AR1(1,i); %????
% dLogLikAR1(i) = logL;
%
% ToEstMdl = arima(0,0,1);
% [EstMdl,EstParamCov,logL,info] = estimate(ToEstMdl,Y(:,i)-mean(Y(:,i)));
% theta_mle_MA1(1,i) = sqrt(info.X(3));
% theta_mle_MA1(2,:) = info.X(2);
% SEMA1(i) = 1.96*theta_mle_MA1(1,i); %????
% dLogLikMA1(i) = logL;
%
% ToEstMdl = arima(1,0,1);
% [EstMdl,EstParamCov,logL,info] = estimate(ToEstMdl,Y(:,i)-mean(Y(:,i)));
% if ~(size(info.X,1)<4)
% theta_mle_ARMA11(1,i) = sqrt(info.X(4));
% theta_mle_ARMA11(2,i) = info.X(2);
% theta_mle_ARMA11(3,i) = info.X(3);
% SEARMA11 = 1.96*theta_mle_ARMA11(1,i); %????
% SEARMA11th = 1.96*theta_mle_ARMA11(1,i); %????
% dLogLikARMA11(i) = logL;
% end
% ================
% finding all sample autocov values
% meanY = mean(Y(:,i));
% gammaY = zeros(n,1);
% for k = 1:n
% for t = 1:n+1-k
% gammaY(k) = gammaY(k) + (Y(t,i)-meanY)*(Y(t+k-1,i)-meanY);
% end
% gammaY(k) = gammaY(k)/(n+1-k);
% end
% [YhatDL(:,i), vDL(:,i)] = durblev(Y(:,i), gammaY);
% [YhatInn(:,i), vInn(:.i)] = innov(Y, gammaY);
% ===============
% % DL
% objfun = @(thetaStart)(durblev(Y(:,i)-thetaStart(1),thetaStart));
% %Inn
% % objfun = @(thetaStart)(innov(Y(:,i),gammaY,thetaStart));
% %MLE
% [theta_mle(:,i), dLogLik] = fminunc(objfun, thetaStart, options);
if mod(i,100)==0
disp(i);
end
end
%% MLE
% for i = 1:runs
% % objfun = @(thetaStart)(-loglikeARMA(thetaStart, Y(:,i), epsY, p, q));
% % objfunTemp = 0;
% % for t = 1:n
% % objfunTemp = objfunTemp + log(vDL(t,i)) + (Y(t,i) - YhatDL(t,i))^2/vDL(t,i);
% % end
% % objfun = @(thetaStart)(durblev(Y));
%
% [theta_mle(:,i), dLogLik] = fminunc(objfun, thetaStart, options);
% disp(i)
% end
%% Display
% fprintf ('Log Likelihood value = %g \r', durblev());
theta_mle_AR1_1 = mean(theta_mle_AR1(1,:));
% theta_mle_AR1_2 = mean((2*normcdf(theta_mle_AR1(2,:))-1));
theta_mle_AR1_2 = mean(theta_mle_AR1(2,:));
loglikeAR1 = mean(dLogLikAR1);
display(theta_mle_AR1_1);
display(theta_mle_AR1_2);
% SE_21 = theta_mle_2 + 1.96*theta_mle_1/sqrt(T);
% SE_22 = theta_mle_2 - 1.96*theta_mle_1/sqrt(T);
SEar1 = mean(SEAR1);
SEar_21 = theta_mle_AR1_2 + SEar1;
SEar_22 = theta_mle_AR1_2 - SEar1;
display(SEar_21);
display(SEar_22);
f1 = figure;
histfit(theta_mle_AR1(2,:),25,'kernel');
line([theta_mle_AR1_2, theta_mle_AR1_2], ylim, 'LineWidth',1,'Color','r','LineStyle','-.')
line ([SEar_21 SEar_21 NaN SEar_22 SEar_22] , [ylim NaN ylim],'LineWidth', 0.5, 'Color', 'g','Displayname','St. Dev.')
theta_mle_MA1_1 = mean(theta_mle_MA1(1,:));
% theta_mle_MA1_2 = mean((2*normcdf(theta_mle_MA1(2,:))-1));
theta_mle_MA1_2 = mean(theta_mle_MA1(2,:));
loglikeMA1 = mean(dLogLikMA1);
display(theta_mle_MA1_1);
display(theta_mle_MA1_2);
% SE_21 = theta_mle_2 + 1.96*theta_mle_1/sqrt(T);
% SE_22 = theta_mle_2 - 1.96*theta_mle_1/sqrt(T);
SEma1 = mean(SEMA1);
SEma_21 = theta_mle_MA1_2 + SEma1;
SEma_22 = theta_mle_MA1_2 - SEma1;
display(SEma_21);
display(SEma_22);
f2 = figure;
histfit(theta_mle_MA1(2,:),25,'kernel');
line([theta_mle_MA1_2, theta_mle_MA1_2], ylim, 'LineWidth',1,'Color','r','LineStyle','-.')
line ([SEma_21 SEma_21 NaN SEma_22 SEma_22] , [ylim NaN ylim],'LineWidth', 0.5, 'Color', 'g','Displayname','St. Dev.')
theta_mle_ARMA11_1 = mean(theta_mle_ARMA11(1,:));
% theta_mle_2 = mean((2*normcdf(theta_mle_AR1(2,:))-1));
theta_mle_ARMA11_2 = mean(theta_mle_ARMA11(2,:));
theta_mle_ARMA11_th = mean(theta_mle_ARMA11(3,:));
loglikeARMA11 = mean(dLogLikARMA11);
display(theta_mle_ARMA11_1);
display(theta_mle_ARMA11_2);
display(theta_mle_ARMA11_th);
% SE_21 = theta_mle_2 + 1.96*theta_mle_1/sqrt(T);
% SE_22 = theta_mle_2 - 1.96*theta_mle_1/sqrt(T);
SEarma11 = mean(SEARMA11);
SEarma11th = mean(SEARMA11th);
SEarma_21 = theta_mle_ARMA11_2 + SEarma11;
SEarma_22 = theta_mle_ARMA11_2 - SEarma11;
SEarma_21th = theta_mle_ARMA11_th + SEarma11th;
SEarma_22th = theta_mle_ARMA11_th - SEarma11th;
display(SEarma_21);
display(SEarma_22);
display(SEarma_21th);
display(SEarma_22th);
f3 = figure;
histfit(theta_mle_ARMA11(2,:),25,'kernel');
line([theta_mle_ARMA11_2, theta_mle_ARMA11_2], ylim, 'LineWidth',1,'Color','r','LineStyle','-.')
line ([SEarma_21 SEarma_21 NaN SEarma_22 SEarma_22] , [ylim NaN ylim],'LineWidth', 0.5, 'Color', 'g','Displayname','St. Dev.')
f4 = figure;
histfit(theta_mle_ARMA11(3,:),25,'kernel');
line([theta_mle_ARMA11_th, theta_mle_ARMA11_th], ylim, 'LineWidth',1,'Color','r','LineStyle','-.')
line ([SEarma_21th SEarma_21th NaN SEarma_22th SEarma_22th] , [ylim NaN ylim],'LineWidth', 0.5, 'Color', 'g','Displayname','St. Dev.')
%% Criteria Testing
% Y_AIC(1) = loglikeAR1(Y(:,1), [theta_mle_1 ; theta_mle_2], T, 1);
% Y_BIC(1) = loglikeAR1(Y(:,1), [theta_mle_1 ; theta_mle_2], T, 2);
% Y_AICC(1) = loglikeAR1(Y(:,1), [theta_mle_1 ; theta_mle_2], T, 3);
%
% Y_AIC(2) = loglikeMA1(Y(:,1), [theta_mle_1 ; theta_mle_2], T, 1);
% Y_BIC(2) = loglikeMA1(Y(:,1), [theta_mle_1 ; theta_mle_2], T, 2);
% Y_AICC(2) = loglikeMA1(Y(:,1), [theta_mle_1 ; theta_mle_2], T, 3);
Y_AIC(1) = -2*loglikeAR1 + 4;
Y_BIC(1) = -2*loglikeAR1 + 4*T/(T-3);
Y_AICC(1) = -2*loglikeAR1 + 2*log(T)/T;
Y_AIC(2) = -2*loglikeMA1 + 4;
Y_BIC(2) = -2*loglikeMA1 + 4*T/(T-3);
Y_AICC(2) = -2*loglikeMA1 + 2*log(T)/T;
Y_AIC(3) = -2*loglikeARMA11 + 6;
Y_BIC(3) = -2*loglikeARMA11 + 6*T/(T-3);
Y_AICC(3) = -2*loglikeARMA11 + 4*log(T)/T;
% end