% LMcs040204_4th.m
% Case Study 4.2.4 Mauna Loa eruptions & exponential distribution P 290-291
% in Larsen & Marx (2006) Introduction to Mathematical Statistics 4th Ed.
% Written by Eugene Gallagher for EEOS601
% Eugene.Gallagher@umb.edu http://www.es.umb.edu/edgwebp.htm
% Written 1/7/11, Revised 1/9/11
% Fitting the exponential pdf to intervals between eruptons
% Data = interval between 37 consecutive eruptions of Mauna Loa
DATA=[126 73 3 6 37 23
73 23 2 65 94 51
26 21 6 68 16 20
6 18 6 41 40 18
41 11 12 38 77 61
26 3 38 50 91 12];
DATA=DATA(:);
lambda=0.027;
fprintf('Eruptions occuring at %5.3f per month or once every %3.2f ',...
    lambda,1/(12*lambda))
fprintf('years.\n')
% histc is the Matlab counting function
% Plot the data using LMFig030402_4th.m as a model
ObservedI=histc(DATA,0:20:140);
bar(0:20:140,ObservedI/(sum(ObservedI)*20),'histc');
set(get(gca,'Children'),'FaceColor',[.8 .8 1])
axis([0 145 0 0.033]);
figure(gcf)
xlabel('Interval between eruptions (in months)','FontSize',16)
ylabel('Density','FontSize',16)
ax1=gca; % get the handle for the bar chart's axes;
pause
y=0:140;
fy=lambda*exp(-lambda*y);
fyy=exppdf(y,1/lambda);  % equivalent Matlab statement
fy-fyy;  % within 10^-16;
h2=line(y,fy,'Color','r','LineStyle','--','Linewidth',3);
set(ax1,'YTick',[0:0.01:0.03],'FontSize',14)
text(34,0.015,'f_y(y)=0.027e^{-0.027y}','FontSize',18)
title('Figure 4.2.3','FontSize',20)
figure(gcf)
pause