% LMEx050302_4th
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Case Study 5.3.2 p.370-371
% Confidence limits for binomial proportion, k/n
% input alpha, e.g., alpha=0.05 for 95% CI's
% output CI    [Lower CI p Upper CI]
% Median test for an exponential pdf
% Other m.files using the exponential distribution
%    LMcs030401_4th
% Requires the statistics and symbolic math toolboxes
% The exponential parameter, not provided, is 1.0, based on the equation
% Written October 2010, revised 3/5/11
syms y m;
fprintf('s is the integral of the exponential function')
s=int(exp(-y),y, 0, m) % integral of exponential pdf
fprintf(...
    'Solve for the value of m such that the integral is 0.5, the median\n')
solve(s-0.5,m)
median=eval(solve(s-0.5,m));
fprintf(...
'The median of the exponential distribution with mean 1 is %7.5f\n',median)
% Generate 60 random numbers from the exponential distribution
% Larsen & Marx did 1 trial of size 60; this will do 100,000 trials
trials=1e5; % 1e6 trials produced an out of memory error
n=60;
Y=exprnd(1,n,trials);
Y=Y<median;
z=norminv(0.975);
p=sum(Y)./n; % p is a vector of length trials
CI=[sum(Y)./n-z*sqrt(p.*(1-p)./n);sum(Y)./n+z.*sqrt(p.*(1-p)./n)];
Results=~[CI(1,:)<0.5 & CI(2,:)>0.5]; % a 1 only if CI doesn't include 0.5
fprintf('Median Test: In %5.0f trials, %3.1f%% outside 95%% CI.\n',...
    trials,sum(Results)/trials*100);
