% LMEx050101_4th.m
% An example of binomial MLE fitting
% Written by Eugene.Gallagher@umb.edu 10/29/10
% Solution of Larsen & Marx Example 5.1.1
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th Edition
% Page 344-345
% Requires the statistics toolbox
hold off; clf
X=[1 1 0]; N=length(X);[Phat,PCI] = binofit(sum(X),N,0.05);
fprintf('The MLE is %5.3f with 95%% CI: [%5.3f %5.3f]\n',Phat,PCI)
ezplot('p^2*(1-p)',0,1);figure(gcf)
ax1=gca;
set(ax1,'FontSize',18)
% Plot a nicer figure with vertical line.
% Use Figure 3.2.1 from Example 3.2.4 as a model.
p=0:.005:1;
fy=p.^2.*(1-p);
plot(p,fy,'LineWidth',2);
xlabel('p','FontSize',20)
ylabel('p^2(1-p)','FontSize',20)
hold on
% This plot simply puts a vertical line in the graph
plot([Phat Phat],[0 Phat^2*(1-Phat)],'--b','LineWidth',2);
legend('p^2(1-p)','Location','NorthWest')
title('Figure 5.1.2','FontSize',22)
figure(gcf)
hold off
% As described on the top of page 346, find the value of p for which
% the derivative is 0 (actually must find the maximum), on p 346, they
% solve the more general problem for observing k heads in n trials. That
% is doable in Matlab too, but not here.
syms P
solve(diff(P^2*(1-P)))