%Matlab code for two-sample Kuiper-Test visualization
%Generate random samples on [0,2pi)
% modified from user Bscan's work on 2 sampled KS-test
num_samps =100;
mean1 = pi; std1 = pi/2;
mean2 = pi/4; std2 = pi/2;
randn
dat1 = mod(mean1 + std1*randn(num_samps,1),2*pi);
dat2 = mod(mean2 + std2*randn(num_samps,1), 2*pi);
%Get emprical cdfs
[f1,x1] = ecdf(dat1);
[f2,x2] = ecdf(dat2);
for k1 = 1:length(x1);
[~,k2] = min(abs(x2 - x1(k1))); % closest x2 value to x1
cdf_diff(k1) = f1(k1) - f2(k2);
end
[D_plus,ind_plus] = max(cdf_diff);
[D_minus,ind_minus] = max(-cdf_diff);
V = D_plus + D_minus;
%Plot the two ecdfs
figure; hold on;
stairs(x1,f1,'b-','LineWidth',2);
stairs(x2,f2,'r-','LineWidth',2);
set(gca,'FontSize',16) %Increase font size
plot([x1(ind_plus) x1(ind_plus)], [f1(ind_plus), f1(ind_plus)-cdf_diff(ind_plus)],'k')
plot([x1(ind_minus) x1(ind_minus)], [f1(ind_minus), f1(ind_minus)-cdf_diff(ind_minus)],'k')
grid on; box on; %Add grid to figure
xlim([0 2*pi]); ylim([0 1])
xlabel('X (Radians)')
ylabel('Cumulative Probability')
text(x1(ind_plus)+0.05,f1(ind_plus)-0.4*cdf_diff(ind_plus),'D_+','FontSize',18);
text(x1(ind_minus)+0.1,f1(ind_minus)-0.4*cdf_diff(ind_minus),'D_-','FontSize',18);
annotation('textbox', [0.15 0.75 0.2 0.1], 'String', 'V = D_++ D_-','FontSize',18, 'FitBoxToText','on','FaceAlpha',1)