function [phat, pci] = raylfit(data,alpha)
%RAYLFIT Parameter estimates and confidence intervals for Rayleigh data.
%   RAYLFIT(DATA,ALPHA) returns the maximum likelihood estimates of the  
%   parameter of the Rayleigh distribution given the data in the
%   vector DATA.  
%
%   [PHAT, PCI] = RAYLFIT(DATA,ALPHA) gives the MLE and 100(1-ALPHA) 
%   percent confidence interval given the data. By default, the
%   optional parameter ALPHA is 0.05, corresponding to 95% confidence
%   intervals.
%
%   See also RAYLCDF, RAYLINV, RAYLPDF, RAYLRND, RAYLSTAT, MLE. 

%   Reference:
%      [1]  Johnson, Norman L., Kotz, Samuel, & Kemp, Adrienne W.,
%      "Univariate Discrete Distributions, Second Edition", Wiley
%      1992 p. 124-130.

%   Tom Lane 11-10-99
%   Copyright 1993-2000 The MathWorks, Inc. 
% $Revision: 1.3 $  $Date: 2000/05/26 18:53:34 $

if nargin < 2, alpha = 0.05; end

[m,n] = size(data);
if (m>1 & n>1), error('First argument must be a vector.'); end

if m == 1
   m = n;
   data = data(:);
end

% The maximum likelihood estimate has a simple form
phat = sqrt(0.5 * mean(data.^2));

% The exact confidence interval is based on chi-square
if nargout == 2
   p_int = [1-alpha/2; alpha/2];
   pci = sqrt(2 * m * phat.^2 ./ chi2inv(p_int, 2*m));
end
