MATLAB: How to compute the confidence interval for the mean of the gamma distribution in Statistics Toolbox 7.1 (R2009a)

Statistics and Machine Learning Toolbox

I would like to compute the confidence interval for the mean of the gamma distribution:
a = 1.23; b = 3.45;
x = gamrnd(a,b,100,1);

Best Answer

This functionality does not exist as a builtin command in Statistics Toolbox 7.1 (R2009a). However it can be implemented by, for example, this function:
function muCI=gamMuCI(x)
%%%%%%%%%%%%%

% Compute confidence interval for the mean of the gamma distribution
%%%%%%%%%%%%%
% Fit to the standard parameterization using maximum likelihood. Compute CIs (confidence intervals)
% using approximate normality of parameter MLEs on the log scale, transformed
% back to original scale
[paramEsts1,CI1] = gamfit(x);
% The MLE for the mean is found by plugging the shape and scale MLEs into
% the formula for the mean.
aHat = paramEsts1(1);
bHat = paramEsts1(2);
muHat = aHat*bHat;
% Create a reparameterized version of the gamma PDF. Use the log of the shape
% parameter to be consistent with what gamfit does.
gampdf2 = @(x,loga,mu) gampdf(x,exp(loga),mu/exp(loga));
% Pass that to the MLE function and compute the CIs in
% the new parameterization. We can give it a (very) good starting point.
% Unlog the CI for the shape parameter
[paramEsts2,CI2] = mle(x,'pdf',gampdf2,'start',[aHat,muHat]);
aCI = exp(CI2(:,1));
% confidence interval for the mean of the gamma distribution
muCI = CI2(:,2);