function test_equality(n,R,alpha)
%function test_equality(n,R,alpha)
%Test for equal eigs
% Normal distribution
% n sample dimension
% R corr. matrix
% alpha sign.level

[V,E]=eig(R);
[e,id]=sort(-diag(E)); e=abs(e); V=V(:,id);
p = size(R,1);

fprintf('\n')
% Test per completa indipendenza
chi = -(n - 1/6*(2*p+5))*log(prod(e));
chi_ref=chi2inv(1-alpha, p/2*(p-1));
fprintf('****Test di indipendenza\n')
fprintf('chi     chi_ref\n')
fprintf('%5.2f   %5.2f\n', chi,chi_ref);
fprintf('\n')
%[chi, chi2inv(1-alpha, p/2*(p-1)]
if (chi>chi_ref), fprintf('correlation exists \n');end
fprintf('\n')
fprintf('\n')

N = n-1;
% Test di sfericita' (all equal eigs) p.203
chi= log(prod(e(1:p))) - p* log( sum(e(1:p))/p);
chi = -(n-1/(6*p)*(2*p^2+p+2))*chi;
chi_ref=chi2inv(1-alpha, 1/2*(p+2)*(p-1));
fprintf('****Test di sfericita\n')
fprintf('chi     chi_ref\n')
fprintf('%5.2f   %5.2f\n', chi,chi_ref);
fprintf('\n')
if (chi>chi_ref), fprintf('eigenvalues differ\n');end
fprintf('\n')
fprintf('\n')
%[chi, chi2inv(1-alpha, 1/2*(p+2)*(p-1))]

