Part of PCA code, I'm confusing about why need to decompose twice.
for s=1:4
%Quadratic variation for each asset
Var{s}=diag(diag(X{s}'*X{s}));
%Quadradic correlation matrix
QV{s}=(X{s}*sqrt(inv(Var{s})))'*X{s}*sqrt(inv(Var{s}));
[V{s},D{s}]=eig(QV{s});
[DD{s},ID]=sort(diag(D{s}),'descend');
V{s}=V{s}(:,ID);
Lambda{s}=sqrt(Var{s})*V{s}(:,1:K);
tildeC=X{s}*Lambda{s}*inv(Lambda{s}'*Lambda{s})*Lambda{s}';%What's this for???
tildeQV{s}=(tildeC)'*tildeC;
[tildeV{s},tildeD{s}]=eig(tildeQV{s});
[tildeDD{s},tildeID]=sort(diag(tildeD{s}),'descend');
tildeV{s}=tildeV{s}(:,tildeID);
Lambda{s}=tildeV{s}(:,1:K);
Lambda{s}=Lambda{s}*diag(sign(mean(X{5}*Lambda{s})));
V{s}=tildeV{s};
end