% LMex070403_4th.m
% Application of the one sample t test using 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.ed 11/14/2010, revised: 3/12/2010
% Percentage loan approval from different banks
X=[59 65 69 53 60 53 58 64 46 67 51 59]';
fprintf('The mean of X is %6.3f %%\n',mean(X));
meanHo=62;
fprintf('The true mean being tested is %6.3f%%\n',meanHo);
alpha=0.05;
% Matlab's 1-sample t test is:
[h,p,ci,stats] = ttest(X,meanHo)
% The book reports the critical values for the 2-tailed t
% statistic based on 11 df.
df=stats.df;
fprintf(...
'The 2 tailed critical values for D=0, %d df and alpha=0.05, are %4.2f and %4.2f\n',...
df,tinv(alpha/2,df),tinv(1-alpha/2,df));
fprintf('The observed mean was %6.3f with 95%% CI: [%6.3f %6.3f]\n',mean(X),ci);
fprintf(...
'The 2-tailed p of observing a difference of %6.3f by chance is %5.3f.\n',...
mean(X)-meanHo,p)
% Test the 3 banks separately, Table 7.5.4, page 492:
BANK=['AU';'TU';'TU';'FT';'FT';'AU';'FT';'FT';'AU';'TU';'AU';'FT'];
% Find how many different unique banks
UB=unique(BANK,'rows');
[r,c]=size(UB);
alpha=0.05;
for i=1:r
    j=strmatch(UB(i,:),BANK); % finds indices for all rows with the 1st unique bank name
fprintf('\nThe Bank being analyzed is %3s\n',UB(i,:));
x=X(j);
fprintf('The mean Percentage approved is %5.2f%% vs. %5.2f%% expected\n',mean(x),meanHo);
% Matlab's 1-sample t test is:
[h,pvalue,ci,stats] = ttest(x,meanHo,alpha);
D=mean(x)-meanHo;
% The book reports the critical values for the 2-tailed t
% statistic based on 11 df.
    fprintf(...
        'For Bank %s, the difference is %5.2f%%, with 2-tailed p value=%5.3f\n',...
        UB(i,:),D,pvalue); 
    if pvalue<alpha
        fprintf('For bank %s, reject the null hypothesis at alpha=%5.3f\n',...
            UB(i,:),alpha);
    else
        fprintf('For bank %s, fail to reject the null hypothesis at alpha=%5.3f\n',UB(i,:),alpha);
    end
    df=stats.df;
    fprintf(...
    'The 2 tailed critical values for the t statistic=%4.2f and alpha=%5.3f, are %4.2f and %4.2f\n',...
    t,alpha,tinv(alpha/2,df),tinv(1-alpha/2,df));
end
% HOWEVER, if the 3 banks were not idenitified in advance, then a Bonferroni correction
% to alpha should be used.
fprintf('\nAnalyses using Bonferroni adjustment based on number of tests (3)\n')
alpha=0.05;
alpha=0.05;ntests=r; % r is 3, the 3 types of banks
alpha=1-exp(log(1-alpha)/ntests);  % I'll overwrite alpha to avoid having to recode the if loop
fprintf('The Bonferroni adjusted alpha, based on %d tests is %5.3f.\n',ntests,alpha);
for i=1:r
    j=strmatch(UB(i,:),BANK); % finds indices for all rows with the 1st unique bank name
fprintf('\nThe Bank being analyzed is %3s\n',UB(i,:));
x=X(j);
fprintf('The mean Percentage approved is %5.2f%% vs. %5.2f%% expected\n',mean(x),meanHo);
% [D,t,df,pvalue,CI]=stud1sample(x,meanHo,alpha);
% Matlab's 1-sample t test is:
[h,pvalue,ci,stats] = ttest(x,meanHo,alpha);
D=mean(x)-meanHo;
% The book reports the critical values for the 2-tailed t
% statistic based on 11 df.
    fprintf(...
        'For Bank %s, the difference is %5.2f%%, with 2-tailed p value=%5.3f\n',...
        UB(i,:),D,pvalue); 
    if pvalue<alpha
        fprintf(...
'For bank %s, reject the null hypothesis at Bonferroni-adjusted alpha=%5.3f\n',...
            UB(i,:),alpha);
    else
        fprintf(...
'For bank %s, fail to reject the null hypothesis at Bonferroni-adjusted alpha=%5.3f\n',UB(i,:),alpha);
    end
    df=stats.df;
    fprintf(...
    'The 2 tailed critical values for the t statistic=%4.2f and alpha=%5.3f, are %4.2f and %4.2f\n',...
    t,alpha,tinv(alpha/2,df),tinv(1-alpha/2,df));
end