
close all
clear all

n=200;
X = unifrnd(-10, 10, [1 n]);
%    X = normrnd(0, 10, [1 n]);
q=((0.5/n):1/n:(1-0.5/n));
qY = norminv(q);
qZ = unifinv(q);
qX = sort(X);
figure(1); plot(qX, qY, '*')
      xlabel('Empirical Quantiles'); ylabel('Theoretical Quantiles')
figure(2); plot(qX, qZ, 'r*')
      xlabel('Empirical Quantiles'); ylabel('Theoretical Quantiles')


%%


%Mendel crossed peas that were heterozygotes for Smooth/Wrinkled, where Smooth is dominant.
%The expected ratio in the offspring is 3 smooth: 1 wrinkled. He observed 423 smooth
%and 133 wrinkled.
%The expected frequency of smooth is calculated by multiplying the sample size (556) by
%the expected proportion (3/4) to yield 417. The same is done for wrinkled to yield 139.
 %The number of degrees of freedom when an extrinsic hypothesis is used is the number
 % of values of the nominal variable minus one. In this case, there are two values
 % (smooth and wrinkled), so there is one degree of freedom.
 
chisq = (556/4 - 133)^2/(556/4) + (556*3/4 - 423)^2/(556*3/4)
% chisq = 0.3453
1 - chi2cdf(chisq, 2-1)
% ans = 0.5568
%=====with Yates correction=====
 chisqy = (abs(556/4 - 133)-0.5)^2/(556/4) + ...
   (abs(556*3/4 - 423)-0.5)^2/(556*3/4)
 %chisq = 0.2902
  1-chi2cdf(chisqy, 2-1)
 %ans = 0.5901
 %We conclude that the theoretical odds 3:1 in favor of smooth peas
 %are consistent with the observations at level $\alpha = 0.05.$

 
 
 %%
 edata = exprnd(2, [1, 1000]);
[h,p,stats] = ...
chi2gof(edata,'cdf',@(z)expcdf(z,mean(edata)),'nparams',1,'nbins',8)

 %%
% Horse-kick Fatalities

fatalities =[0 1 2 3 4];
corpsyears =[109 65 22 3 1];
n = sum(corpsyears);
lambda = sum(fatalities .* corpsyears)/n;
pi = [poisspdf(0, lambda) poisspdf(1, lambda) poisspdf(2, lambda) ...
    poisspdf(3, lambda) 1-poisscdf(3, lambda) ];
sum(pi)


   
chisquare =  sum(  (corpsyears - n * pi).^2 ./ (n * pi) )

1 - chi2cdf(chisquare, 5 - 1- 1)


%%

%===================================
% rand('state', 0);
 n = 30;  i = 1:n;
% x = exprnd(1/2,[1,n]); x = sort(x);
x = [...
    0.0256    0.0334    0.0407    0.0434    0.0562    0.0575...
    0.0984    0.1034    0.1166    0.1358    0.1518    0.2427...
    0.2497    0.2523    0.3608    0.3921    0.4052    0.4455...
    0.4511    0.5208    0.6506    0.7324    0.7979    0.8077...
    0.8079    0.8679    0.9870    1.4246    1.9949    2.309];
distances = [i./n - expcdf(x, 1/2); expcdf(x, 1/2) - (i-1)./n ];
Dn = max(max(distances))          %0.1048
pval = 1 - kscdf(sqrt(n)*Dn)      %0.8966

1 - kscdf(sqrt(n) * Dn + 1/(6 * sqrt(n)) ) %0.8582
%
% form theoretical cdf
t = 0:0.01:20;
y = expcdf(t,1/2);
cdf = [t'  y'];
[decision, pval, KSstat, critValue] = kstest(x, cdf, 0.01, 0)
%decision = 0
%pval = 0.8626
%KSstat = 0.1048
%critValue = 0.2899

%
%===================================
xx = 0:0.01:4;
plo = cdfplot(x); set(plo,'LineWidth',2);
hold on
plot(xx,expcdf(xx, 1/2),'r-','LineWidth',2);
legend('Empirical','Theoretical Exponential',...
'Location','SE')


%%


%%

 clear all
 %
 % JARQUE-BERA and LILLIEFOR TESTS for
 % assessing Gaussianity
 x = frnd(5, 10, [1, 30]);
alpha=0.05;

[h1,p1] = jbtest(x, alpha)
%h1 = 1
%p1 = 1.0000e-003
[h2,p2] = lillietest(x, alpha)
%h2 = 1
%p2 = 0.0084

 
 