pbig= .1
psmall=.9

pvec = rand(10000,1)>psmall;
classsmall = randn(10000,1);
classbig = randn(10000,1)*(10);

mixedsamp = pvec.*classbig + (1-pvec).*classsmall;
smallsamp = classsmall;

hist(mixedsamp)
hist(mixedsamp,[-20:0.5:20])
hold on
hist([smallsamp;mixedsamp],[-20:0.5:20])
var(mixedsamp)

%%% Kurtosis is measure of peakiness - kurtosis of Gaussian is 3
%%% do help kurtosis and help moment for more info

kurtosis(mixedsamp)
kurtosis(smallsamp)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% bringing in non-parametric tests
%%%% two normal distributions with different means
%%%% comparing the t-test to the mann-whitney U test
count=0;
countu=0;
for i=1:100
 c1 = randn(25,1);           %%% mean of 0
 c2= randn(25,1)+1;          %%% mean of 1
[p,hu]=ranksum(c1,c2,.05);
[h,sig]=ttest2(c1,c2,.05,0);
 % h = 0 means NULL hypothesis can't be rejected at the sig level given
 if h~=0
  sprintf('%f %f\n',h,sig);
   count=count+1; 
 end
 if hu~=0
   sprintf('%f %f\n',hu,p);
   countu=countu+1;
 end
 
end
count
countu
%%% countu should be almost as large as count indicating almost as powerful a test


%%%% Bringing in non-parametric tests
%%%% two normal distributions with same means
%%%% comparing the t-test to the mann-whitney U test
count=0;
countu=0;
for i=1:100
 c1 = randn(25,1);
 c2= randn(25,1);
[p,hu]=ranksum(c1,c2,.05);
[h,sig]=ttest2(c1,c2,.05,0);
 if h~=0
  sprintf('%f %f\n',h,sig)
   count=count+1; 
 end
 if hu~=0
   sprintf('%f %f\n',hu,p)
   countu=countu+1;
 end
end
count
countu
%%% ideally count shoud be 5 (for 5% false rejection rate)
%%% countu is similar sometimes smaller sometimes larger



%%% bringing in Non-parametric tests (Wilcoxon rank-sum/ Mann-Whitney U
%test)
%% different means,  mixture classes
countt=0
countu=0
pbig=.1
psmall=.9
for i=1:100
 pvec = rand(25,1)>psmall;
 classsmall = randn(25,1);
 classbig = randn(25,1)*(10);
 c1 = pvec.*classbig + (1-pvec).*classsmall;   %%% mean 0
 pvec = rand(25,1)>psmall;
 classsmall = normrnd(1,1,25,1);
 classbig = normrnd(1,10,25,1);
 c2= pvec.*classbig + (1-pvec).*classsmall;    %%% mean 1
[ht,sig]=ttest2(c1,c2,.05,0);
[p,hu]=ranksum(c1,c2,.05);
 if ht~=0
  sprintf('ttest %f %f\n',h,sig)
   countt=countt+1; 
 end
 if hu~=0
  sprintf('utest %f %f\n',h,sig)
   countu=countu+1; 
 end

end
countt
countu
%% notice how the utest is much better at rejecting the Null in this case where 
%% the classes are not Gaussian

%%%%%%%%%%%%%%%%%%

%%% bringing in Non-parametric tests (Wilcoxon rank-sum/ Mann-Whitney U
%test)
%% same means,  mixture classes
countt=0
countu=0
pbig=.1
psmall=.9
for i=1:100
 pvec = rand(25,1)>psmall;
 classsmall = randn(25,1);
 classbig = randn(25,1)*(10);
 c1 = pvec.*classbig + (1-pvec).*classsmall;    %%% mean 0
 pvec = rand(25,1)>psmall;
 classsmall = normrnd(0,1,25,1);
 classbig = normrnd(0,10,25,1);
 c2= pvec.*classbig + (1-pvec).*classsmall;    %%%% also mean 0
[ht,sig]=ttest2(c1,c2,.05,0);
[p,hu]=ranksum(c1,c2,.05);
 if ht~=0
  sprintf('ttest %f %f\n',h,sig)
   countt=countt+1; 
 end
 if hu~=0
  sprintf('utest %f %f\n',h,sig)
   countu=countu+1; 
 end

end
countt
countu
%% in this example the utest and ttest give similar results and rarely
%% reject the null hypothesis



