Saving nested for loop output in a single column
Show older comments
Hello everyone,
I am estimating time-series models with different AR, MA, and lag components - am using nested for loops. My problem is simple but am new to MATLAB environment. Issue: I am scoring different information criteria (such as AIC) within the loop structure. I would like to put the outputs of information criteria into a new column, one for each of the information criteria (see below pl).
Below is what I have tried. Rather than putting the outputs for a specific information criteria in one column, it creates an array with x*y*z form. I am sure I am missing something very obvious in the code below, especially in the last lines. Any help will be truly appreciated!
pmax = 3; %Max order of AR
dmax = 1; %Max order of difference
qmax = 3; % Max order of MA
%Pre-allocation
a = pmax + 1;
b = dmax + 1;
c = qmax + 1;
aa = combnk(a,1);
bb = combnk(b,1);
cc = combnk(c,1);
combinations = (aa*bb*cc)+1;
aiccol = zeros(combinations,1);
caiccol = zeros(combinations,1);
icompcol = zeros(combinations,1);
t = 499;
vt = Data(:,4);
n=length(vt);
for k = 0:pmax
for j = 0:dmax
for i = 0:qmax
Md1 = arima(k,j,i);
[EstMd1,EstParamcov,logL]= estimate(Md1,vt);
[res,~,logL] = infer(EstMd1,vt);
sig2 = (vt'*vt)/t;
s=rank(EstParamcov);
tr=trace(EstParamcov);
d=det(EstParamcov);
AIC=n*log(2*pi)+n*log(sig2)+n+2*(k+i+1)
CAIC=n*log(2*pi)+n*log(sig2)+n+2*(k+i+1)*(log(n)+1)
ICOMP=n*log(2*pi)+n*log(sig2)+n+(k+i+1)+s*log(tr/s)-2*log(abs(d))
aiccol(k+1,j+1,i+1) = AIC; %????? *Something wrong here*
caiccol(k+1,j+1,i+1) = CAIC; %????? *Something wrong here*
icompcol(k+1,j+1,i+1) = ICOMP; %????? *Something wrong here*
end
end
end
Answers (1)
KSSV
on 6 Mar 2018
Try this..
pmax = 3; %Max order of AR
dmax = 1; %Max order of difference
qmax = 3; % Max order of MA
%Pre-allocation
a = pmax + 1;
b = dmax + 1;
c = qmax + 1;
aa = combnk(a,1);
bb = combnk(b,1);
cc = combnk(c,1);
combinations = (aa*bb*cc)+1;
% aiccol = zeros(combinations,1);
% caiccol = zeros(combinations,1);
% icompcol = zeros(combinations,1);
aiccol = zeros(pmax+1,dmax+1,qmax+1) ;
caiccol = zeros(pmax+1,dmax+1,qmax+1) ;
icompcol = zeros(pmax+1,dmax+1,qmax+1) ;
t = 499;
vt = Data(:,4);
n=length(vt);
for k = 0:pmax
for j = 0:dmax
for i = 0:qmax
Md1 = arima(k,j,i);
[EstMd1,EstParamcov,logL]= estimate(Md1,vt);
[res,~,logL] = infer(EstMd1,vt);
sig2 = (vt'*vt)/t;
s=rank(EstParamcov);
tr=trace(EstParamcov);
d=det(EstParamcov);
AIC=n*log(2*pi)+n*log(sig2)+n+2*(k+i+1)
CAIC=n*log(2*pi)+n*log(sig2)+n+2*(k+i+1)*(log(n)+1)
ICOMP=n*log(2*pi)+n*log(sig2)+n+(k+i+1)+s*log(tr/s)-2*log(abs(d))
aiccol(k+1,j+1,i+1) = AIC; %????? *Something wrong here*
caiccol(k+1,j+1,i+1) = CAIC; %????? *Something wrong here*
icompcol(k+1,j+1,i+1) = ICOMP; %????? *Something wrong here*
end
end
end
Categories
Find more on Loops and Conditional Statements 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!