% LMEx050205
% Not working; the likelihood function has area >1.
% An example of customized pdf fitting.
% Based on Gallagher's LMEx050202.m
% From Larsen & Marx (2006). Introduction to Mathematical Statistics,
% Fourth Edition. page 358
% Note that LM use this example to demonstrate method of moments
% Dept. Environmental, Earth & Ocean Sciences
% Written by Eugene.Gallagher@umb.edu 10/29/10, revised 3/3/11
% 
% Fit the custom pdf using a Matlab anonymous function @
%
X=[0.42 0.10 0.65 0.23];
fyytheta=@(y,theta)(theta.*y.^(theta-1));
theta=0.54; % starting guess
[THETA, thetaCI]= mle(X,'pdf',fyytheta,'start',theta,'lowerbound',0,'upperbound',1)
% Note that must specify the lower bound for theta is 0 as specified
% in the problem or negative pdf's are produced
% THETA=0.7889
ezplot('(0.7889.*y)^(0.7889-1)',[0.0001 0.999]);
figure(gcf);pause


% Plot the likelihood function
y=0:.01:1; theta=THETA;
fyy=theta.* y.^(theta-1);
fX=theta.* X.^(theta-1);
plot(y,fyy,'LineWidth',2);
axis([0 1 -0.002 2])
ax1=gca;
hold on
h1=stem(X,fX,':k','filled');
set(h1,'LineWidth',2);
s=sprintf('Example 5.2.5, theta=%6.4f, Likelihood=%7.3g',theta,prod(fX));
title(s,'FontSize',22)
figure(gcf);pause
hold off

% Plot a different value of theta
thetan=0.54;
y=0.001:.001:1;
fyy=thetan.* y.^(thetan-1);
fX=thetan.* X.^(thetan-1);
plot(y,fyy,'LineWidth',2);
axis([0 1 -0.002 2])
ax1=gca;
hold on
h1=stem(X,fX,':k','filled');
set(h1,'LineWidth',2);
s=sprintf('Example 5.2.5, theta=%6.4f, Likelihood=%7.3g',thetan,prod(fX));
title(s,'FontSize',22)
figure(gcf);pause
hold off

syms y theta
int(theta*y^(theta-1),y,0,1)
