%transformations for gof
clear all
close all  force
  disp('Elimination of Unknown Parameters by Transformations')
  disp('Exponential and Normal Examples')
  set(0, 'DefaultAxesFontSize', 16);
  lw =3;  fs = 16;  msize = 10;
s = RandStream('mt19937ar','Seed',0);
RandStream.setGlobalStream(s);
%
% EXPONENTIAL Exp(lambda)
n=100;
lambda=2.5;  %lambda arbitrary
X = exprnd(1/lambda,[1 n]);
%
Xsorted = sort(X);
differences =[Xsorted(1) diff(Xsorted,1)];
n1i=  n + 1 - (1:n);
di = n1i .* differences;
css=cumsum(di)/sum(di);
U1=css(1:n-1); %U1's are uniform U(0,1)
figure(1)
hist(U1, 30)
% 
% NORMAL N(mu, sigma^2)
n= 100;
mu=3;  sigma = 2; %Generate 100 normals with
% arbitrary mean and standard deviation
X =  normrnd(mu, sigma, [1, n]);
% elimiantion of the mean
k=floor(n/2)+1; %Pick any k between 1 and n, say in the middle
mk = 1/(n + sqrt(n)) * sum(X) + 1/(1 + sqrt(n))*X(k);
%
for i = 1:n-1
   Y(i) = mk-X(i + (i >= k));
end
 % Y's are iid normal N(0, sigma^2)
 % Transformation of Y's to unifrom U(0,1)
 Y2=Y.^2;
U2=[];  
for j=1:n-2
  parvar  = 1/(n-j-1) * sum(Y2(j+1:n-1));
  U2= [U2 tcdf( Y(j)/sqrt(parvar), n-j-1)];
end
 % U2's are iid uniform U(0,1).
 figure(2)
hist(U2, 30)
figure(3)
hist(rand(1,10000), 30)
