% LMcs090301_4th.m
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th ed
% Case Study 9.3.1, an example of the 2-sample Variance test
% Written by Eugene.Gallagher@umb.edu
% 
alpha=0.05;
Nonconfined=[10.7 10.7 10.4 10.9 10.5 10.3 9.6 11.1 11.2 10.4]';
Solitary=[9.6 10.4 9.7 10.3 9.2 9.3 9.9 9.5 9.0 10.9]';
fprintf(...
 'The variance of solitary and nonconfined groups were %6.3f and %6.3f\n',...
    var(Solitary),var(Nonconfined));
fprintf('The ratio of variances was %6.4f\n',var(Solitary)/var(Nonconfined))
boxplot([Solitary;Nonconfined],[ones(10,1);zeros(10,1)],...
    'labels',{'Nonconfined','Solitary'});figure(gcf);pause
[H,P,CI,STATS] = vartest2(Solitary,Nonconfined,alpha,'both');
df1=STATS.df1;df2=STATS.df2;
fprintf('The F statistic with %2.0f, %2.0f df is %6.4f\n',...
    STATS.df1,STATS.df2,STATS.fstat)
fprintf('The p value is %6.4f with CI: %6.4f %6.4f.\n',P,CI)
% Plot Figure 9.3.3 using Figures 4.3.6 (LMex040307_4th.m) & Figure 9.2.1 
% as model.
X=0:0.01:5;
Y = fpdf(X,df1,df2);
Ytest=fpdf(STATS.fstat,df1,df2);
plot(X,Y,'r','LineWidth',2);
axis([-.65 5 0 0.75]);
title('Figure 9.3.3','FontSize',22);
ax1=gca;
xlabel('F','FontSize',20),
ylabel('f_y(y)','FontSize',20);
ax1=gca;
alpha=0.05;Fcritl=finv(alpha/2,df1,df2);Fcritu=finv(1-alpha/2,df1,df2);
set(ax1,'xtick',[0 0.248 1 1.70 Fcritu],'FontSize',18);
set(ax1,'ytick',0:.1:0.7,'FontSize',18)
hold on;
plot([STATS.fstat STATS.fstat]',[0 Ytest]','--r','Linewidth',2)
xfu=Fcritu:0.01:5;yfu=fpdf(xfu,df1,df2);
fill([Fcritu xfu 5]',[0 yfu 0]',[.8 .8 1])
xfl=0:0.005:Fcritl;yfl=fpdf(xfl,df1,df2);
fill([0 xfl Fcritl]',[0 yfl 0]',[.8 .8 1])

text(-.6,  0.15,'Area=0.025','FontSize',18);
text(Fcritu,0.1,'Area=0.025','FontSize',18)
figure(gcf);pause
hold off;
