function [pks,pad,observed_D,observed_A]=simulateksdist(x)
% simulates the kolmogorov-smirnov distance and Anderson-Darling distance for exponential fit to data in vector x
KS=[]; AD=[];   n=length(x);  delta=1/n;  x=sort(x);
lam=1/mean(x);     F=1-exp(-lam*x); % value of the mean parameter
observed_D=max(max(abs(F-(0: delta:(1-delta))),abs(F-(delta:delta:1))));
w=(2*(1:n)-1)/n;   observed_A=-n-sum(w.*log(F.*(1-F)));
for i=1:10000
	 x=sort(exprnd((1/lam),1,n));                          % generates a new sample same parameter
     l=1/mean(x);  F=1-exp(-l*x);                          %computes theoretical c.d.f.
	 D=max(max(abs(F-(0: delta:(1-delta))),abs(F-(delta:delta:1))));  % KS statistic
    KS=[KS D];
    w=(2*(1:n)-1)/n;           A=-n-sum(w.*log(F.*(1-F)));
    AD=[AD A];
 end
pks=mean(KS>=observed_D); pad=mean(AD>observed_A);
hist(KS,40); text(observed_D,0,'D');  title('Histogram of simulated values of KS statistic') 
figure;  hist(AD,40);   text(observed_A,0,'A');  title('Histogram of simulated values of AD statistic') 