%IVF In Vitro Fertilization Success Rates (CDC data)
Age =[ 24  25  26  27  28  29  30 31  32  33  34  35 36 37  38  39 40  41 42  43 44  45  46];
Success=[  38.7  38.6  38.9 41.4  39.7  41.1  38.7  37.6 36.3 36.9  35.7 33.8...
 33.2  30.1  27.8  22.7  21.3 15.4  11.2  9.2  5.4  3.0  1.6];
a = [Age' Success']

X=Age(:); Y=Success(:);   %X,Y column vectors
n=length(X) 
if (length(X)~=length(Y)) {error('Lengths for X, Y should match')}
end
Xbar=mean(X)
Ybar=mean(Y)
SXX=(X-Xbar)' * (X-Xbar)
SYY=(Y-Ybar)' * (Y-Ybar)
SXY=(X-Xbar)' * (Y-Ybar)

r_XY = SXY/sqrt(SXX * SYY)

plot(X, Y, 'o', 'markersize', 7)

%%
  
  
 clear all
HR = [94 96 95 95 94 95 94 104 104 016 108 110 113 113 118 115 121 127 135];
VO2 = [0.473 0.753 .929 .939 .832 0.983 1.049 1.178 1.176 1.292 ...
1.403 1.499 1.529 1.599 1.749 1.746 1.897 2.040 2.231];

r = corr(HR', VO2')
%fisherz = @(x)  1/2*log( (1+x)/(1-x) ); 
w = 1/2 * log( (1+r)/(1-r))
n=length(HR)
LB = w - norminv(1-0.05/2) / sqrt(n-3)
UB = w + norminv(1-0.05/2) / sqrt(n-3)
%invfisherz = @(x)  (exp(2 * x) - 1)/(exp(2 * x) + 1)
L=(exp(2*LB)- 1)/(exp(2*LB)+ 1)
U=(exp(2*UB)- 1)/(exp(2*UB)+ 1)


%%
%Sayed Data
sayed =[...
    2.9370    0.0694;       2.9284    0.0433;       2.9212    0.0331; 
    2.9196    0.0288;       2.9149    0.0221;       2.9106    0.0121;
    2.9047    0.0080;       2.9047    0.0069;       2.9031    0.0049;
    2.9320    0.0714;       2.9154    0.0427;       2.9165    0.0336;
    2.9085    0.0292;       2.9090    0.0240;       2.9047    0.0197;
    2.9058    0.0052;       2.9025    0.0104;       2.9025    0.0087;
    2.8976    0.0078;       2.8971    0.0065;       2.8976    0.0062];
recsep = sayed(:,1);   
logwl  = sayed(:,2);
r = corr(recsep, logwl)
fisherz = @(x)  1/2*log( (1+x)/(1-x) ); 
invfisherz = @(x)  (exp(2 * x) - 1)/(exp(2 * x) + 1);
w = fisherz(r)
n = length(recsep);
% CI
alpha=0.05; %95% CI
wL = w - norminv(1-alpha/2)/sqrt(n+3)
wU = w + norminv(1-alpha/2)/sqrt(n+3)
%
rL = invfisherz(wL)
rU = invfisherz(wU)
%Test H0 : rho = 0.8 vs rho > 0.8
w0 = fisherz(0.8)
zz = (w  - w0)*sqrt(n - 3)
pval = 1 - normcdf(zz)
%Find the power against alternative rho=0.96
crit = tinv(1-alpha/2, n-2);
walpha = sqrt( crit^2/(crit^2 + (n-2)) )
w0 = fisherz(0.8)
w1 = fisherz(0.94)
power = normcdf( (abs(w0 - w1) - walpha)*sqrt(n-3) )


%%{\it Papilio} & 24 & 14 & 21 & 15 & 20 & 17.5 & 21.5 & 16.5 \\
% {\it multicaudatus}& 21.5 & 16 & 25.5 & 16 & 25.5 & 17.5 & 28.5 & 16.5 \\
%    & 23.5 & 15 & 22 & 15.5 & 22.5 & 17.5 & 20.5 & 19 \\
%    & 21 & 13.5 & 19.5 & 19 & 26 & 18 & 23 & 17 \\
%    & 21 & 18 & 21 & 17 & 20.5 & 16 & 22.5 & 15.5 \\
% \hline
% {\it Papilio} &  20 & 11.5 & 21.5 & 11 & 18.5 & 10 & 20 & 11 \\
% {\it rutulus} &  19 & 11 & 20.5 & 11 & 19.5 & 11 & 19 & 10.5 \\
%               &  21.5 & 11 & 20 & 11.5 & 21.5 & 10 & 20.5 & 12 \\
%               &  20 & 10.5 & 21.5 & 12.5 & 17.5 & 12 & 21 & 12.5\\
%               & 21 & 11.5 & 21 & 12 & 19 & 10.5 & 19 & 11\\
%               & 18 & 11.5 & 21.5 & 10.5 & 23 & 11 & 22.5 & 11.5\\
%               & 19 & 13 & 22.5 & 14 & 21 & 12.5 & 19.5 & 12.5\\


%%------Data-----------------------------
PapilioM=[24, 14;  21, 15; 20, 17.5; 21.5, 16.5; ...
         21.5, 16; 25.5, 16; 25.5, 17.5; 28.5, 16.5;...
         23.5, 15;  22 , 15.5;  22.5, 17.5; 20.5, 19; ...
         21, 13.5; 19.5, 19;  26, 18;  23, 17; ...
         21, 18;  21, 17; 20.5, 16; 22.5, 15.5];
%
PapilioR=[ 20, 11.5; 21.5, 11;  18.5, 10; 20, 11;...
        19,  11;  20.5, 11; 19.5, 11; 19,  10.5;...
        21.5 ,11;  20, 11.5;  21.5, 10;  20.5,  12;...
        20, 10.5;  21.5, 12.5;  17.5, 12;  21, 12.5; ...
        21, 11.5;  21, 12; 19, 10.5; 19, 11; ...
       18, 11.5;  21.5, 10.5;  23, 11;  22.5, 11.5; ...
       19, 13;  22.5,  14;  21 ,12.5; 19.5, 12.5];
%
PapilioMX=PapilioM(:,1); %  X_m
PapilioMY=PapilioM(:,2); %  Y_m
%
PapilioRX=PapilioR(:,1); %  X_r
PapilioRY=PapilioR(:,2); %  Y_r

n1=length(PapilioMX);
n2=length(PapilioRX);

r1=corr(PapilioMX, PapilioMY); %  -0.1120
r2=corr(PapilioRX, PapilioRY); %   0.1757
%test for rho1 = 0
  pval1 = 2* tcdf(  - abs(r1*sqrt(n1-3)/sqrt(1-r1^2)) , n1 - 3);
  %  0.6480
  %test for rho2 = 0
  pval2 = 2* tcdf(  - abs(r2*sqrt(n2-3)/sqrt(1-r2^2)) , n2 - 3);
  % 0.3806

fisherz = @(x)  1/2*log( (1+x)/(1-x) );
     %fisher z transform as pure function
w1 = fisherz(r1);  %-0.1125
w2 = fisherz(r2);  %0.1776  

   %test for rho1 = rho2  vs rho1 ~= rho2
z = (w1 - w2)/sqrt( 1/(n1-3) + 1/(n2-3)) %-0.9228
pval = 2 * normcdf(-abs(z)) %0.3561
