%% Camera for Cataract
%
Control  = [158, 182, 182 ,  191, 177, 145,  156, 170 ,  152, 164,  169, 141];
Cataract  = [161, 140 , 136, 171,  116,  149 ,  143,  152];

%1.(a)
m1 = mean(Control) %165.5833
m2 =mean(Cataract) %146
n1  = 12; n2=8;
var1=var(Control) %246.4470
var2=var(Cataract) %277.1429
std1=std(Control) %15.6986
std2=std(Cataract) %16.6476
%First,  decide on t-test scenario by comparing the
%variances. If not significant --
% use pooled standard deviation
F=var1/var2  %0.8892
p = 2*min(fcdf(F, n1-1, n2-1), 1-fcdf(F, n1-1, n2-1))
%p=0.8267
%Since 0.8267 > 0.05 assume  the same population variances ,
% and use pooled std
sp  = sqrt(    ((n1-1)*var1+ (n2-1)*var2)/(n1+n2-2)    ) 
% sp=16.0743  (between std1 and std2)
t = (m1 - m2)/(sp * sqrt(1/n1 + 1/n2)) %2.6692
%H1: mu_cataract ~= mu_control (two sided alternative)
pval = 2 * min(tcdf(t, n1+n2-2), 1-tcdf(t, n1+n2-2)) %0.0156
%or
pval = 2 *  tcdf(-abs(t), n1+n2-2)  %0.0156
% ALTERNATIVELY, as MATLAB's built in:
[h p]=ttest2(Control, Cataract, 0.05, 'both')  %[1    0.0156]

%1.(b)
%Page 362 gray box
sigma=16; diff=10; alpha = 0.05; beta=0.2;
n = 2*sigma^2/diff^2 * (norminv(1-alpha/2)+norminv(1-beta))^2
%n=40.1863 approx 41.

%If the part (a) of the problem is solved without assumption
%of equal population variances:
ndf = (std1^2/n1 + std2^2/n2 )^2 /( (std1^2 /n1)^2/(n1-1) + ...
             (std2^2/n2)^2 /(n2-1) )  %14.5138
t = (m1 - m2)/sqrt( std1^2/n1 + std2^2/n2 )  %2.6363
pval = 2*tcdf(-abs(t),ndf)  %  0.0191
