function [traA1,layA1,traB1,layB1,A1,B1] = test_regionsize(data,type,Dist,A1,A2,A1h,A2h,B1,B2,B1h,B2h)
% computing propagation distances in region A1 & B1
% testing dense or sparse with t-test & Mann-Whitney test
alpha1 = 0.05;
alpha2 = 0.05;
tail1 = 'left'; % alternative hypotheses: x<y
tail2 = 'right'; % alternative hypotheses: x>y
k = 5;
% prepare data
[traA1, Ra1] = propag_intra(Dist,A1,A1(1));
[layA1, Ra2] = propag_center(data,Dist,A1,B1,type);
if ~isempty(A2)
[traA2, Rb1] = propag_intra(Dist,A2,A2(1));
[layA2, Rb2] = propag_center(data,Dist,A2,A1,type);
X = [Ra1 Ra2];
Y = [Rb1 Rb2];
% test if x>y
[h1,stats] = bitest(X,Y,alpha1,alpha1,tail2);
if h1
if ~isempty(A1h)
A1f = setdiff(A1,A1h);
Q = border_points(Dist,A1f,B1,1);
[traA3, Rc1] = propag_intra(Dist,A1f,Q);
[layA3, Rc2] = propag_center(data,Dist,A1f,B1,type);
X = [Rc1 Rc2];
[traA4, Rd1] = propag_intra(Dist,A1h,A1h(1));
[layA4, Rd2] = propag_center(data,Dist,A1h,B1,type);
Y = [Rd1 Rd2];
% test if x<y
[h1,stats] = bitest(X,Y,alpha1,alpha1,tail1);
if h1 % reduce A1
A1 = A1h; traA1 = traA4; layA1 = layA4;
end
end
else
% test if kx<y. A tighter condition for enlargement
[h1,stats] = bitest(k*X,Y,alpha1,alpha2,tail1);
if h1 % enlarge A1
if isempty(A2h)
A1 = [A1; A2];
else
A1 = [A1; A2h];
end
[traA1, Ra1] = propag_intra(Dist,A1,A1(1));
[layA1, Ra2] = propag_center(data,Dist,A1,B1,type);
end
end
end
% prepare data
[traB1, Ra1] = propag_intra(Dist,B1,B1(1));
[layB1, Ra2] = propag_center(data,Dist,B1,A1,type);
if isempty(B2)
return
end
[traB2, Rb1] = propag_intra(Dist,B2,B2(1));
[layB2, Rb2] = propag_center(data,Dist,B2,B1,type);
X = [Ra1 Ra2];
Y = [Rb1 Rb2];
% test if x>y
[h2,stats] = bitest(X,Y,alpha1,alpha1,tail2);
if h2
if ~isempty(B1h)
B1f = setdiff(B1,B1h);
Q = border_points(Dist,B1f,A1,1);
[traB3, Rc1] = propag_intra(Dist,B1f,Q);
[layB3, Rc2] = propag_center(data,Dist,B1f,A1,type);
X = [Rc1 Rc2];
[traB4, Rd1] = propag_intra(Dist,B1h,B1h(1));
[layB4, Rd2] = propag_center(data,Dist,B1h,A1,type);
Y = [Rd1 Rd2];
% test if x<y
[h2,stats] = bitest(X,Y,alpha1,alpha1,tail1);
if h2 % reduce B1
B1 = B1h; traB1 = traB4; layB1 = layB4;
end
end
else
% test if kx<y. A tighter condition for enlargement
[h2,stats] = bitest(k*X,Y,alpha1,alpha2,tail1);
if h2 % enlarge B1
if isempty(B2h)
B1 = [B1; B2];
else
B1 = [B1; B2h];
end
[traB1, Ra1] = propag_intra(Dist,B1,B1(1));
[layB1, Ra2] = propag_center(data,Dist,B1,A1,type);
end
end
function [h,stats] = bitest(X,Y,alpha1,alpha2,tail)
% H=1, reject hypothesis that X has a normal distribution at alpha1 level
[h1,p] = lillietest(X,alpha1); % Lilliefors test
h2 = 1;
if h1 == 0
[h2,p] = lillietest(Y,alpha1);
end
if h1 == 0 && h2 == 0
% Assuming: two samples come from normal distr. with unknown unequal variances
% It uses Satterthwaite's approximation for the degrees of freedom.
[h,p,ci,stats] = ttest2(X,Y,alpha2,tail,'unequal');
stats.p=p;
stats.ci=ci;
else
[h, stats] = mannwtest(X,Y,alpha2,tail);
end