GMM estimation -> nested for loop in while loop
3 views (last 30 days)
Show older comments
Hello everyone!
I want to estimate the variable "b" using GMM estimation. I already did the first 2 steps manually:
%d)
%step 1
V1=eye(size(Z,2));
%step 2
%I wrote the function GMM_crit for the function that we have to minimize
%estimate beta(1)
beta0=repmat(-0.28,18,1);
options = optimoptions( ...
@fminunc, ...
'Algorithm', 'quasi-newton', ...
'HessUpdate', 'bfgs', ...
'MaxIter', 100000, ...
'MaxFunEvals', 100000, ...
'TolX', 10^-12, ...
'TolFun', 10^-12,...
'OptimalityTolerance',1e-6);
% Estimate beta(1)
[be1] = fminunc(@GMM_crit, beta0,options, N, Y, X, Z, V1 );
% Estimate V(2)
g_tilde1=Z.*Y-Z.*exp(X*be1);
g_bar1=(1/N)*(g_tilde1);
V2=(1/N)*(g_tilde1-g_bar1)'*(g_tilde1-g_bar1);
%step 3
% Estimate beta(2)
[be2] = fminunc(@GMM_crit, be1,options, N, Y, X, Z, V2 );
with the following function GMM crit:
function crit=GMM_crit(be, N, Y, X, Z, V)
crit=(1/N)*(Z'*(Y-exp(X*be)))'*inv(V)*(1/N)*(Z'*(Y-exp(X*be)));
end
To repeat this procedure until the euclidean norm of beta(j+1)-b(j) is smaller than 0.001, I scripted the following loop:
epsilon=0.002;
j=1;
while epsilon>0.001 && j<100
for j=1;
b(j)=be2 %use my calculated be2 as a starting value
end
for j=j+1;
%calculate new weighting matrix
g_tilde(j)=Z.*Y-Z.*exp(X*b(j))
g_bar(j)=(1/N)*(g_tilde(j))
V(j+1)=(1/N)*(g_tilde(j)-g_bar(j))'*(g_tilde(j)-g_bar(j))
% estimate next step beta
[b(j+1)] = fminunc(@GMM_crit, b(j),options, N, Y, X, Z, V(j+1) )
epsilon=norm(b(j+1)-b(j))
end
end
But I always get the following error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the
right side.
I never scripted a loop so I would be really glad if someone could help me.
Thank you guys.
0 Comments
Answers (0)
See Also
Categories
Find more on Linear Model Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!