📄 tucker.m
字号:
Core_cmplex=0;
Core_const=0;
G_cons=ConstrG;
if all(ConstrG(:)==1),
ConstrG=0;
end;
if ConstrG==0,
% Convert to new format
clear ff,id1 = 0;
for i = 1:length(DimX)
id2 = sum(DimX(1:i).*Fac(1:i));ff{i} = reshape(Factors(id1+1:id2),DimX(i),Fac(i));id1 = id2;
end
Fact = ff;
G=calcore(reshape(X,DimX),Fact,[],1,MissingExist);
G = reshape(G,size(G,1),prod(size(G))/size(G,1));
Core_uncon=1;
elseif prod(size(ConstrG)==[Fac(1) prod(Fac(2:N))]),
tmpM2=1;
for k=1:N;
if Mth(k)==4,
tmpM1=eye(DimX(k));
else
tmpM1=reshape(Factors(FIdx0(k):FIdx1(k)),DimX(k),Fac(k));
end;
tmpM2=ckron(tmpM2,tmpM1);
end
w=ConstrG(:);
fwz=find(w==1);
fwnn=find(w==2);
fwfix=find(w==3);
G=zeros(prod(Fac),1);
G(fwz)=tmpM2(:,fwz)\X(:);
enda=size(Fac,2); %!
G=reshape(G,Fac(1),prod(Fac(2:enda)));
Core_cmplex=1;
MethodO=2;
UpdateCore=2*ones(1,N);
elseif ConstrG==2,
% Convert to new format
clear ff,id1 = 0;
for i = 1:length(DimX)
id2 = sum(DimX(1:i).*Fac(1:i));ff{i} = reshape(Factors(id1+1:id2),DimX(i),Fac(i));id1 = id2;
end
Fact = ff;
G=calcore(reshape(X,DimX),Fact,[],1,MissingExist);
G = reshape(G,size(G,1),prod(size(G))/size(G,1));
G(find(G<0))=0;
Core_nonneg=1;
elseif ConstrG==3,
Core_const=1;
UpdateCore=0*UpdateCore; %%%Added by CA, 27-01-2002
end;
if prlvl>0
fprintf('Type of algorithm : ');
if MethodO==1,
fprintf('Orthogonal projections.\n');
elseif MethodO==2,
fprintf('Flexible scheme.\n');
end;
fprintf('Core : ');
if Core_cmplex==1 & Core_nonneg==0,
fprintf('Unconstrained composite/sparse core.\n');
elseif Core_cmplex==1 & Core_nonneg==1,
fprintf('Non-negativity constrained and composite/sparse core.\n');
elseif Core_const==1,
fprintf('Fixed core.\n');
else
fprintf('Full unconstrained core.\n');
end;
end;
if prlvl>0,
if MissingExist,
if Options91>=1,
fprintf('Missing data : Yes, 2 active loops (expectation maximization).\n');
fprintf(' %i values (%.2f%%) out of %i are NaNs/missing.\n',prod(size(IdxIsNans)),100*prod(size(IdxIsNans))/prod(size(X)),prod(size(X)));
if usefacinput==0,
fprintf(' Missing values initialized from column and row means.\n');
else
fprintf(' Missing values initialized from model based on the given input.\n');
end;
fprintf('Convergence crit. 1 : %.5g (relative) sum of sq. core elements (corrected for missing values).\n',Options12);
fprintf('Convergence crit. 2 : %.5g (relative) sum of sq. of pred. missing values.\n',Options11);
fprintf('Iteration limit : %i is the maximum number of overall iterations.\n',Options61);
end;
else
if Options91>=1,
fprintf('Missing data : No, 1 active loop.\n');
fprintf('Convergence crit. 1 : %.5g (relative) sum of sq. core elements.\n',Options12);
fprintf('Iteration limit : %i is the maximum number of overall iterations.\n',Options61);
end;
end;
fprintf('\n');
if MissingExist,
str1=' Iter. 1 | Corrected sum of | Sum of sq. miss. | Expl. ';
str2=' # | sq. core elements | values | var. [%]';
fprintf('%s\n',str1);
fprintf('%s\n\n',str2);
else
str1=' Iter. 1 | Sum of | Expl. ';
str2=' # | sq. core elements | var. [%]';
fprintf('%s\n',str1);
fprintf('%s\n\n',str2);
end;
end;
Conv_true=0;
if MethodO==1, %Can use the faster projection technique
SSGOld=0;
Converged2=0;
it1=0;
itlim1=0;
t0=clock;
while ~Converged2,
Converged1=0;
while ~Converged1,
it1=it1+1;
% Iterate over the modes
for c=1:N,
%Compress the data by projections
if Mth(c)~=4,
CurDimX=DimX;
RedData=X;
for k=1:N;
if k~=c,
if Mth(k)~=4,
kthFactor=reshape(Factors(FIdx0(k):FIdx1(k)),DimX(k),Fac(k));
RedData=kthFactor'*RedData;
CurDimX(k)=Fac(k);
else
RedData=RedData;
end,
end,
if k~=N,
newi=CurDimX(k+1);
newj=prod(CurDimX)/newi;
else
newi=CurDimX(1);
newj=prod(CurDimX)/newi;
end;
RedData=reshape(RedData',newi,newj);
end;
%Reshape to the proper unfolding
for k=1:(c-1);
if k~=c,
newi=CurDimX(k+1);
newj=prod(CurDimX)/CurDimX(k+1);
else,
newi=CurDimX(1);
newj=prod(CurDimX)/CurDimX(1);
end;
RedData=reshape(RedData',newi,newj);
end;
%Find a basis in the projected space
%...using the robust SVD
if Mth(c)==1,
if MissingExist,
[U S V]=svd(RedData',0);
cthFactor=V(:,1:Fac(c));
else
[U S V]=svd(RedData',0);
cthFactor=V(:,1:min(Fac(c),size(V,2)));
if size(cthFactor,2)<Fac(c)
cthFactor = [cthFactor rand(size(cthFactor,1),Fac(c)-size(cthFactor,2))];
end
end;
end;
%...using the fast NIPALS
if Mth(c)==2,
if MissingExist,
[cthFactor]=fnipals(RedData,Fac(c),reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c)));
else
[cthFactor]=fnipals(RedData,Fac(c),reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c)));
end;
end;
%...using simplified continuous Gram-Schmidt orthogonalization
if Mth(c)==3,
if MissingExist,
TempMat=RedData*RedData';
cthFactor=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
for i=1:2,
[cthFactor]=gsm(TempMat*cthFactor);
end;
else
TempMat=RedData*RedData';
cthFactor=reshape(Factors(FIdx0(c):FIdx1(c)),DimX(c),Fac(c));
for i=1:2,
[cthFactor]=gsm(TempMat*cthFactor);
end;
end;
end;
%...this is void (no compression for this mode)
if Mth(c)==4,
end;
%...this is void (Keep factors unchanged)
if Mth(c)==7,
end;
%Update the 'Factors' with the current estimates
if Mth(c)~=4 & Mth(c)~=7
Factors(FIdx0(c):FIdx1(c))=cthFactor(:)';
end;
end;
end;
if ~Core_const & Core_uncon==1 & Core_nonneg==0,
% Convert to new format
clear ff,id1 = 0;
for i = 1:length(DimX)
id2 = sum(DimX(1:i).*Fac(1:i));ff{i} = reshape(Factors(id1+1:id2),DimX(i),Fac(i));id1 = id2;
end
Fact = ff;
G=calcore(reshape(X,DimX),Fact,[],1,MissingExist);
G = reshape(G,size(G,1),prod(size(G))/size(G,1));
elseif Core_nonneg==1,
g=T3core(reshape(X,DimX),Fac,Factors(:),0,1);
G=reshape(g,Fac(1),prod(Fac(2:N)));
else
tmpM2=1;
for k=1:N;
if Mth(k)==4,
tmpM1=eye(DimX(k));
else
tmpM1=reshape(Factors(FIdx0(k):FIdx1(k)),DimX(k),Fac(k));
end;
tmpM2=ckron(tmpM2,tmpM1);
end
G=G(:);
G(fwz)=tmpM2(:,fwz)\X(:);
enda=size(Fac,2);
G=reshape(G,Fac(1),prod(Fac(2:enda)));
end;
SSG=sum(sum(G.^2));
if MissingExist,
SSG=SSG-SSMis;
end;
if abs(SSG-SSGOld)<Options12*SSGOld,
Converged1=1;
end;
if it1>=Options61,
itlim1=1;
Converged1=1;
Converged2=1;
end;
SSGOld=SSG;
js=0;
%Save on time count
if Options101>0 & (etime(clock,t0)>Options101),
save('temp.mat','Factors','G','DimX','Fac');
t0=clock;
js=1;
end;
%Save on iteration count
%if (Options101<0) & (mod(it1,abs(Options101))==0),
keval=it1/Options51;
if (Options101<0) & ( abs( keval - floor(keval) ) <=eps),
save('temp.mat','Factors','G','DimX','Fac');
js=1;
end;
%if mod(it1,Options51)==0 | it1==1 | js==1,
keval=it1/Options51;
if (abs( keval - floor(keval) ) <=eps) | it1==1 | js==1, %Matlab 4.2 comp.
% Convert to new format
clear ff,id1 = 0;
for i = 1:length(DimX)
if Fac(i)
id2 = sum(DimX(1:i).*Fac(1:i).*(Fac(1:i)~=0));
ff{i} = reshape(Factors(id1+1:id2),DimX(i),Fac(i));id1 = id2;
else
ff{i}=[];
end
end
Fact = ff;
Xm=nmodel(Fact,reshape(G,FacNew));
Xm = reshape(Xm,DimX(1),prod(DimX(2:end)));
if MissingExist,
X(IdxIsNans)=Xm(IdxIsNans);
SSMis=sum(sum( Xm(IdxIsNans).^2 ));
if abs(SSMis-SSMisOld)<Options11*SSMisOld,
Converged2=1;
end;
SSMisOld=SSMis;
else
Converged2=1;
end;
ExplX=100*(1-sum(sum((X-Xm).^2))/SSX);
pout=pout+1;
if pout>pmore,
if prlvl > 0,
fprintf('%s\n',str1);
fprintf('%s\n',str2);
end;
pout=0;
end;
if prlvl>0,
if MissingExist,
fprintf(' %6i %14.3f %14.3f %8.4f',it1,SSG,SSMis,ExplX);
else
fprintf(' %6i %14.3f %8.4f',it1,SSG,ExplX);
end;
if js,
fprintf(' - saved to ''temp.mat'' \n')
else
fprintf('\n')
end;
end;
end;
end; %Inner loop
end; %Outer loop
if prlvl>0,
if itlim1==0,
fprintf(' Stopped. Convergence criteria reached.\n');
else
fprintf(' Stopped. Iteration limits reached in model and expectation loops.\n');
end;
if MissingExist,
fprintf(' %6i %14.3f %14.3f %8.4f',it1,SSG,SSMis,ExplX);
else
fprintf(' %6i %14.3f %8.4f',it1,SSG,ExplX);
end;
end;
if Options101~=0,
save('temp.mat','Factors','G','DimX','Fac');
if prlvl>0,
fprintf(' - saved to ''temp.mat'' \n')
end;
else
if prlvl>0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -