% eda08_05
% simple example of varimax procedure applued to 2 factors

% define two non-spiky factors, fs and ft
M=4;
fA = [ 1,  1,  1,  1 ]';
fB = [ 1, -1,  1, -1 ]';
fA = fA / sqrt( fA'*fA ); % normalize to unit length
fB = fB / sqrt( fB'*fB ); % normalize to unit length

% standard varimax procedure to determine rotation angle q
u = fA.^2 - fB.^2;
v = 2* fA .* fB;

A = 2*M*u'*v;
B = sum(u)*sum(v);
top = A - B;

C = M*(u'*u-v'*v);
D = (sum(u)^2)-(sum(v)^2);
bot =  C - D;

q = 0.25 * atan2(top,bot);

% rotate factors
cq = cos(q);
sq = sin(q);
fAp =  cq*fA + sq*fB;
fBp = -sq*fA + cq*fB;

% display results
fA
fB
fAp
fBp


