This is a static copy of a profile report

Home

normfit (3 calls, 0.010 sec)
Generated 05-Nov-2014 07:53:55 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/stats/stats/normfit.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ml_estpdffunction3
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
98
ncen = sum(freq.*censoring); %...
30.010 s100.0%
137
return
30 s0%
126
if nargout > 2
30 s0%
121
sigmahat = sqrt(sum(conj(xc).*...
30 s0%
117
xc = x - muhat;
30 s0%
All other lines  0 s0%
Totals  0.010 s100% 
Children (called functions)
No children
Code Analyzer results
Line numberMessage
200The value assigned here to 'nll' appears to be unused. Consider replacing it by ~.
200The value assigned here to 'lagrange' appears to be unused. Consider replacing it by ~.
222The value assigned here to 'nlogL' appears to be unused. Consider replacing it by ~.
224The value assigned here to 'nlogL' appears to be unused. Consider replacing it by ~.
Coverage results
[ Show coverage for parent directory ]
Total lines in function229
Non-code lines (comments, blank lines)96
Code lines (lines that can run)133
Code lines that did run28
Code lines that did not run105
Coverage (did run/can run)21.05 %
Function listing
   time   calls  line
1 function [muhat, sigmahat, muci, sigmaci] = normfit(x,alpha,censoring,freq,options)
2 %NORMFIT Parameter estimates and confidence intervals for normal data.
3 % [MUHAT,SIGMAHAT] = NORMFIT(X) returns estimates of the parameters of
4 % the normal distribution given the data in X. MUHAT is an estimate of
5 % the mean, and SIGMAHAT is an estimate of the standard deviation.
6 %
7 % [MUHAT,SIGMAHAT,MUCI,SIGMACI] = NORMFIT(X) returns 95% confidence
8 % intervals for the parameter estimates.
9 %
10 % [MUHAT,SIGMAHAT,MUCI,SIGMACI] = NORMFIT(X,ALPHA) returns 100(1-ALPHA)
11 % percent confidence intervals for the parameter estimates.
12 %
13 % [...] = NORMFIT(X,ALPHA,CENSORING) accepts a boolean vector of the same
14 % size as X that is 1 for observations that are right-censored and 0 for
15 % observations that are observed exactly.
16 %
17 % [...] = NORMFIT(X,ALPHA,CENSORING,FREQ) accepts a frequency vector of the
18 % same size as X. FREQ typically contains integer frequencies for the
19 % corresponding elements in X, but may contain any non-integer
20 % non-negative values.
21 %
22 % [...] = NORMFIT(X,ALPHA,CENSORING,FREQ,OPTIONS) specifies control
23 % parameters for the iterative algorithm used to compute ML estimates
24 % when there is censoring. This argument can be created by a call to
25 % STATSET. See STATSET('normfit') for parameter names and default values.
26 %
27 % Pass in [] for ALPHA, CENSORING, or FREQ to use their default values.
28 %
29 % With no censoring, SIGMAHAT is computed using the square root of the
30 % unbiased estimator of the variance. With censoring, SIGMAHAT is the
31 % maximum likelihood estimate.
32 %
33 % See also NORMCDF, NORMINV, NORMLIKE, NORMPDF, NORMRND, NORMSTAT, MLE, STATSET.
34
35 % References:
36 % [1] Evans, M., Hastings, N., and Peacock, B. (1993) Statistical
37 % Distributions, 2nd ed., Wiley, 170pp.
38 % [2] Lawless, J.F. (1982) Statistical Models and Methods for Lifetime
39 % Data, Wiley, New York, 580pp.
40 % [3} Meeker, W.Q. and L.A. Escobar (1998) Statistical Methods for
41 % Reliability Data, Wiley, New York, 680pp.
42
43 % To compute weighted maximum likelihood estimates (WMLEs) for mu and
44 % sigma, you can provide weights, normalized to sum to LENGTH(X), in FREQ
45 % instead of frequencies. In this case, NORMFIT computes the WMLE for
46 % mu. However, when there is no censoring, the estimate computed for
47 % sigma is not exactly the WMLE. To compute the WMLE, multiply the value
48 % returned in SIGMAHAT by (SUM(FREQ) - 1)/SUM(FREQ). This correction is
49 % needed because NORMFIT normally computes SIGMAHAT using an unbiased
50 % variance estimator when there is no censoring in the data. When there
51 % is censoring, the correction is not needed, since NORMFIT does not use
52 % the unbiased variance estimator in that case.
53
54 % Copyright 1993-2011 The MathWorks, Inc.
55 % $Revision: 1.1.8.3 $ $Date: 2011/05/09 01:26:19 $
56
57 % Illegal data return an error.
3 58 if ~isvector(x)
59 if nargin < 3
60 % Accept matrix data under the 2-arg syntax. censoring and freq
61 % will be scalar zero and one.
62 [n,ncols] = size(x); % all columns have same number of data
63 else
64 error(message('stats:normfit:InvalidData'));
65 end
3 66 else
3 67 n = numel(x); % a scalar -- all columns have same number of data
3 68 ncols = 1;
3 69 end
70
3 71 if nargin < 2 || isempty(alpha)
3 72 alpha = 0.05;
3 73 end
3 74 if nargin < 3 || isempty(censoring)
3 75 censoring = 0; % make this a scalar, will expand when needed
76 elseif ~isempty(censoring) && ~isequal(size(x), size(censoring))
77 error(message('stats:normfit:InputSizeMismatchCensoring'));
78 end
3 79 if nargin < 4 || isempty(freq)
3 80 freq = 1; % make this a scalar, will expand when needed
81 elseif isequal(size(x), size(freq))
82 n = sum(freq);
83 zerowgts = find(freq == 0);
84 if numel(zerowgts) > 0
85 x(zerowgts) = [];
86 if numel(censoring)==numel(freq), censoring(zerowgts) = []; end
87 freq(zerowgts) = [];
88 end
89 else
90 error(message('stats:normfit:InputSizeMismatchFreq'));
91 end
3 92 if nargin < 5 || isempty(options)
3 93 options = [];
3 94 end
95
3 96 classX = class(x);
97
0.01 3 98 ncen = sum(freq.*censoring); % a scalar in all cases
3 99 nunc = n - ncen; % a scalar in all cases
3 100 sumx = sum(freq.*x);
101
102 % Weed out cases which cannot really be fit, no data or all censored. When
103 % all observations are censored, the likelihood surface is at its maximum
104 % (zero) for any mu > max(x) at the boundary sigma==0.
3 105 if n == 0 || nunc == 0
106 muhat = NaN(1,ncols,classX);
107 sigmahat = NaN(1,ncols,classX);
108 muci = NaN(2,ncols,classX);
109 sigmaci = NaN(2,ncols,classX);
110 return
111
112 % No censoring, find the parameter estimates explicitly.
3 113 elseif ncen == 0
3 114 muhat = sumx ./ n;
3 115 if n > 1
3 116 if numel(muhat) == 1 % vector data
3 117 xc = x - muhat;
118 else % matrix data
119 xc = x - repmat(muhat,[n 1]);
120 end
3 121 sigmahat = sqrt(sum(conj(xc).*xc.*freq) ./ (n-1));
122 else
123 sigmahat = zeros(1,ncols,classX);
124 end
125
3 126 if nargout > 2
127 if n > 1
128 parmhat = [muhat; sigmahat];
129 ci = statnormci(parmhat,[],alpha,x,[],freq);
130 muci = ci(:,:,1);
131 sigmaci = ci(:,:,2);
132 else
133 muci = repmat(cast([-Inf; Inf],classX),1,ncols);
134 sigmaci = repmat(cast([0; Inf],classX),1,ncols);
135 end
136 end
3 137 return
138 end
139 % Past this point, guaranteed to have only one vector of data, with censoring.
140
141 % Not much can be done with Infs, either censored or uncensored.
142 if ~isfinite(sumx)
143 muhat = sumx;
144 sigmahat = NaN(classX);
145 muci = NaN(2,1,classX);
146 sigmaci = NaN(2,1,classX);
147 return
148 end
149
150 % When all uncensored observations are equal and greater than all the
151 % censored observations, the likelihood surface becomes infinite at the
152 % boundary sigma==0. Return something reasonable anyway.
153 xunc = x(censoring==0);
154 rangexUnc = range(xunc);
155 if rangexUnc < realmin(classX)
156 if xunc(1) == max(x)
157 muhat = xunc(1);
158 sigmahat = zeros(classX);
159 if nunc > 1
160 muci = [muhat; muhat];
161 sigmaci = zeros(2,1,classX);
162 else
163 muci = cast([-Inf; Inf], classX);
164 sigmaci = cast([0; Inf], classX);
165 end
166 return
167 end
168 end
169 % Otherwise the data are ok to fit, go on.
170
171 % First, get a rough estimate for parameters using the "least squares" method
172 % as a starting value...
173 if rangexUnc > 0
174 if numel(freq) == numel(x)
175 [p,q] = ecdf(x, 'censoring',censoring, 'frequency',freq);
176 else
177 [p,q] = ecdf(x, 'censoring',censoring);
178 end
179 pmid = (p(1:(end-1))+p(2:end)) / 2;
180 linefit = polyfit(-sqrt(2)*erfcinv(2*pmid), q(2:end), 1);
181 parmhat = linefit([2 1]);
182
183 % ...unless there's only one uncensored value.
184 else
185 parmhat = [xunc(1) 1];
186 end
187
188 % The default options include turning statsfminbx's display off. This
189 % function gives its own warning/error messages, and the caller can
190 % turn display on to get the text output from statsfminbx if desired.
191 options = statset(statset('normfit'), options);
192 tolBnd = options.TolBnd;
193 options = optimset(options);
194 dfltOptions = struct('DerivativeCheck','off', 'HessMult',[], ...
195 'HessPattern',ones(2,2), 'PrecondBandWidth',Inf, ...
196 'TypicalX',ones(2,1,classX), 'MaxPCGIter',1, 'TolPCG',0.1);
197
198 % Maximize the log-likelihood with respect to mu and sigma.
199 funfcn = {'fungrad' 'normfit' @negloglike [] []};
200 [parmhat, nll, lagrange, err, output] = ...
201 statsfminbx(funfcn, parmhat, [-Inf; tolBnd], [Inf; Inf], ...
202 options, dfltOptions, 1, x, censoring, freq);
203 if (err == 0)
204 % statsfminbx may print its own output text; in any case give something
205 % more statistical here, controllable via warning IDs.
206 if output.funcCount >= options.MaxFunEvals
207 wmsg = 'Maximum likelihood estimation did not converge. Function evaluation limit exceeded.';
208 else
209 wmsg = 'Maximum likelihood estimation did not converge. Iteration limit exceeded.';
210 end
211 warning('stats:normfit:IterOrEvalLimit',wmsg);
212 elseif (err < 0)
213 error(message('stats:normfit:NoSolution'));
214 end
215
216 muhat = parmhat(1);
217 sigmahat = parmhat(2);
218
219 if nargout > 2
220 parmhat = parmhat(:);
221 if numel(freq) == numel(x)
222 [nlogL, acov] = normlike(parmhat, x, censoring, freq);
223 else
224 [nlogL, acov] = normlike(parmhat, x, censoring);
225 end
226 ci = statnormci(parmhat,acov,alpha,x,censoring,freq);
227 muci = ci(:,:,1);
228 sigmaci = ci(:,:,2);
229 end

Other subfunctions in this file are not included in this listing.