%%%%PINK%%%%%%%%%%%%
    H =[0.56   0.61    0.62    0.53    0.54   0.60    0.56    0.59  ...
           0.60   0.60    0.62    0.60    0.58   0.57    0.61    0.64  ...
           0.60   0.61    0.58    0.59    0.55   0.59    0.60    0.65 ];

   [mean(H)  std(H)]  %%% 0.5917    0.0293
          n=24; mu0 =0.6; mu1 = 0.58; s=0.0293;   
alpha=0.05;

  tstat = (mean(H) - 0.6)/(std(H)/sqrt(24))  %%%  -
 %  -1.3938
 
%pvalue
 tcdf(tstat,n-1)
% 0.0884
%Rejection region  (-infinity, tinv(0.05, 24-1)]
tinv(0.05,n-1)  %= -tinv(0.95,24-1)
%-1.7139; RR=(-infinity, -1.7139]

 %Retrospective power
  pow1 = nctcdf( -tinv(1-alpha, n-1), n-1,-abs(mu0-mu1)*sqrt(n)/s)
 %  0.9450 OR, equivalently
  pow1 = nctcdf( tinv(alpha, n-1), n-1,  (mu1-mu0)*sqrt(n)/s )
 % 0.9450
 %pow0 = nctcdf( tinv(alpha, n-1), n-1,  -2/3*sqrt(n) )
 
%%
%%%%BLUE%%%%%
%Two Sided
     H =[0.56   0.61    0.62    0.53    0.54   0.60    0.56    0.59  ...
         0.60   0.60    0.62    0.60    0.58   0.57    0.61    0.64  ...
         0.60   0.61    0.58    0.59    0.55   0.59    0.60    0.65 ];

   [mean(H)  std(H)]  %%% 0.5917    0.0293
          n=24; mu0 =0.6; mu1 = 0.58; s=0.0293;   
alpha=0.05;

  tstat = (mean(H) - 0.6)/(std(H)/sqrt(24))  %%%  -
 %  -1.3938
 
%pvalue
 2*tcdf(tstat,n-1)
% 0.1767
%Better:  2*tcdf(-abs(tstat),n-1)
%Rejection region  (-infinity, tinv(0.025, 24-1)] U [tinv(0.975, 24-1),
%infinity)
tinv(0.025,n-1) 
tinv(0.975,n-1) 
%(-infinity, -2.0687] U  [2.0687, infinity)

 pow2 = nctcdf( -tinv(1-alpha/2, n-1), n-1,(mu1-mu0)*sqrt(n)/s) + ...
        1 - nctcdf( tinv(1-alpha/2, n-1), n-1,(mu1-mu0)*sqrt(n)/s)
  %  0.8928
     pow2 = nctcdf( tinv(alpha/2, n-1), n-1,-abs(mu1-mu0)*sqrt(n)/s) + ...
            1- nctcdf(-tinv(alpha/2, n-1), n-1, -abs(mu0-mu1)*sqrt(n)/s)
 % 0.8928        
 pow2 = nctcdf( -tinv(1-alpha/2, n-1), n-1,(mu1-mu0)*sqrt(n)/s) + ...
               +nctcdf(-tinv(1-alpha/2, n-1), n-1,(mu0-mu1)*sqrt(n)/s)
   % 0.8928
   pow2 = nctcdf( tinv(alpha/2, n-1), n-1,(mu1-mu0)*sqrt(n)/s) + ...
               +nctcdf(tinv(alpha/2, n-1), n-1,(mu0-mu1)*sqrt(n)/s)     
  % 0.8928   
       pow2 = nctcdf( -tinv(1-alpha/2, n-1), n-1,(mu1-mu0)*sqrt(n)/s) + ...
               +nctcdf(-tinv(1-alpha/2, n-1), n-1,(mu0-mu1)*sqrt(n)/s)
   % 0.8928
   pow2 = nctcdf( tinv(alpha/2, n-1), n-1,-abs(mu1-mu0)*sqrt(n)/s) + ...
               +nctcdf(tinv(alpha/2, n-1), n-1, abs(mu0-mu1)*sqrt(n)/s)
 % 0.8928      
 
 
 %%%%VISUAL CELLS
 %%% PINK/YELLOW
 %(a)
 pLpink = 0.9 * 0.9 * 0.8 * 0.8 
 %  0.5184
 
%(b)
%directly as binomial
 3 * pLpink^2 * (1- pLpink) + pLpink^3   % 0.5276 
 %or using cdf
1 - binocdf(1, 3, pLpink)    %ans = 0.5276
% or using pdf
1-binopdf(0, 3, pLpink) - binopdf(1,3, pLpink)   %ans = 0.5276

 %%%%%BLUE%%%%%%%%%%%%
%(a)
pLblue = 0.95 * 0.9 * 0.8 * 0.7    % 0.4788

%(b)
%directly as binomial
3 * pLblue^2 * (1- pLblue) + pLblue^3   % 0.4682
%or using cdf
1 - binocdf(1, 3, pLblue)  %ans =0.4682
% or using pdf
1-binopdf(0, 3, pLblue) - binopdf(1,3, pLblue) % ans =0.4682


%  P(Z < \frac{0 - 0.74}{1.46}) = \Phi(-0.74/1.46)
 %%%%%%%%%%%ULNAR VARIANCE
 
 %(a)
 normcdf(0, 0.74, 1.46)
 % or
 normcdf( (0 - 0.74)/1.46 )
 
 %(b)
 
 
  mu1 = 0.19; mu2 = 1.52;
  sigma1 = 1.43; sigma2 = 1.56;
  
  muC = mu2 - mu1   % muC = 1.3300

  sigmaC = sqrt(  sigma1^2  +  sigma2^2 )  % sigmaC = 2.1162
  
  1 - normcdf( 1, muC, sigmaC)% ans = 0.5620
  
  1 - normcdf( 2, muC, sigmaC)% ans = 0.3758
  




 