% LMcs050301_4th.m
% Confidence intervals
% From Larsen & Marx (2006). Introduction to Mathematical Statistics,
% Fourth Edition. page 367
% Dept. Environmental, Earth & Ocean Sciences
% Written by Eugene.Gallagher@umb.edu 10/29/10, revised 3/4/11
DATA=[141 148 132 138 154 142 150
    146 155 158 150 140 147 148
    144 150 149 145 149 158 143
    141 144 144 126 140 144 142
    141 140 145 135 147 146 141
    136 140 146 142 137 148 154
    137 139 143 140 131 143 141
    149 148 135 148 152 143 144
    141 143 147 146 150 132 142
    142 143 153 149 146 149 138
    142 149 142 137 134 144 146
    147 140 142 140 137 152 145];
DATA=DATA(:); % convert to a single column vector
meanD=mean(DATA);sigma=6;
CI=[meanD-norminv(0.975)*sigma/sqrt(length(DATA)) ...
    meanD+norminv(0.975)*sigma/sqrt(length(DATA))];
fprintf('The mean is %4.1f with CI: [%4.1f %4.1f]\n',meanD,CI)
% Could have been obtained using normfit.
[MUHAT,SIGMAHAT,MUCI,SIGMACI]=normfit(DATA);
fprintf('Using normfit.m:\n')
fprintf('The mean is %4.1f with CI: [%4.1f %4.1f]\n',MUHAT,MUCI)

% Plot the histogram with superimposed pdf, from LMcs050202_4th.m
Binsize=4;
EDGES=124:Binsize:164;N = histc(DATA,EDGES);
bar(EDGES,N,'histc')
set(get(gca,'Children'),'FaceColor',[.8 .8 1])
title('Case Study 5.3.1','FontSize',22)
xlabel('Maximum Head Breadths (mm)','FontSize',20); 
ylabel('Frequency','FontSize',20);
figure(gcf);
hold on
x=124:0.1:164;
y=normpdf(x,MUHAT,6)*length(DATA)*Binsize;
plot(x,y,'--r','Linewidth',3)
v=axis;
plot([132.4 132.4]',[0 v(4)]','-.b','LineWidth',3)
figure(gcf);pause
hold off