% LMcs110204_4th.m
% Larsen & Marx Case Study 11.2.4, an example of exponential regression
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu; written 11/23/10; revised 11/23/10
DATA=[0 4.5e3;3 2.9e4;7 9e4;10 2.29e5;14 1.2e6;18 3.1e6;20 5.5e6];
[r,c]=size(DATA);
X=[ones(r,1) DATA(:,1)];Y=log(DATA(:,2));
B=X\Y;
fprintf('\nThe Y intercept = %8.3f and slope = %5.3f\n',B(1),B(2));
s=sprintf('y=%6.1f e^{%5.3f x}',exp(B(1)),B(2));
Yest=X*B;
Resid=Y-Yest;
% sort by the independent variable so the lines are smooth
[Xs,k]=sort(X(:,2));
% sort the estimated values using the index vector k:
Ys=Yest(k);
plot(X(:,2),Y,'ob',Xs,Ys,'-m');
xlabel('Years after 1975','Fontsize',20)
ylabel('Transistors per chip','Fontsize',20)
figure(gcf)
pause
% Generated a fine-scale estimated curve
xintrp=[0:0.1:20]';
[r,c]=size(xintrp);
xintrp=[ones(r,1) xintrp];
yestintrp=xintrp*B;
plot(DATA(:,1),DATA(:,2),'ob',xintrp(:,2),exp(yestintrp),'--r');
xlabel('Years after 1975','Fontsize',20),
ylabel('Transistors per chip','Fontsize',20)
axis([-.5 22 -5e4 7e6])
text(5,3.5e6,s,'Fontsize',20);
title('Figure 11.2.8','Fontsize',22)
figure(gcf)
pause
plot(Yest,Resid,'ok');
xlabel('Expected value','Fontsize',20)
ylabel('Residual','Fontsize',20);
grid;figure(gcf)

% Use the Matlab statistical toolbox's regress for confidence limits
[B,BINT] = regress(Y,X);
fprintf(...
'The doubling time for transistors per chip is %4.2f years\n',...
log(2)./B(2)); 
% Is Moore's Law's 2-year estimate within the 95% CI?
fprintf(...
'The 95%% confidence limits for the doubling time are: %3.2f and %3.2f years\n',...
fliplr(log(2)./BINT(2,:))); 