Categories &

Functions List

Function Reference: normfit

statistics: muhat = normfit (x)
statistics: [muhat, sigmahat] = normfit (x)
statistics: [muhat, sigmahat, muci] = normfit (x)
statistics: [muhat, sigmahat, muci, sigmaci] = normfit (x)
statistics: […] = normfit (x, alpha)
statistics: […] = normfit (x, alpha, censor)
statistics: […] = normfit (x, alpha, censor, freq)
statistics: […] = normfit (x, alpha, censor, freq, options)

Estimate parameters and confidence intervals for the normal distribution.

[muhat, sigmahat] = normfit (x) estimates the parameters of the normal distribution given the data in x. muhat is an estimate of the mean, and sigmahat is an estimate of the standard deviation.

[muhat, sigmahat, muci, sigmaci] = normfit (x) returns the 95% confidence intervals for the mean and standard deviation estimates in the arrays muci and sigmaci, respectively.

  • x can be a vector or a matrix. When x is a matrix, the parameter estimates and their confidence intervals are computed for each column. In this case, normfit supports only 2 input arguments, x and alpha. Optional arguments censor, freq, and options can be used only when x is a vector.
  • alpha is a scalar value in the range (0,1) specifying the confidence level for the confidence intervals calculated as 100×(1 – alpha)%. By default, the optional argument alpha is 0.05 corresponding to 95% confidence intervals. Pass in [] for alpha to use the default values.
  • censor is a logical vector of the same length as x specifying whether each value in x is right-censored or not. 1 indicates observations that are right-censored and 0 indicates observations that are fully observed. With censoring, muhat and sigmahat are the maximum likelihood estimates (MLEs). If empty, the default is an array of 0s, meaning that all observations are fully observed.
  • freq is a vector of the same length as x and it typically contains non-negative integer counts of the corresponding elements in x. If empty, the default is an array of 1s, meaning one observation per element of x. To obtain the weighted MLEs for a data set with censoring, specify weights of observations, normalized to the number of observations in x. However, when there is no censored data (default), the returned estimate for standard deviation is not exactly the WMLE. To compute the weighted MLE, multiply the value returned in sigmahat by (SUM (freq) - 1) / SUM (freq). This correction is needed because normfit normally computes sigmahat using an unbiased variance estimator when there is no censored data. When there is censoring in the data, the correction is not needed, since normfit does not use the unbiased variance estimator in that case.
  • options is a structure with the control parameters for fminsearch which is used internally to compute MLEs for censored data. By default, it uses the following options:
    • options.Display = "off"
    • options.MaxFunEvals = 400
    • options.MaxIter = 200
    • options.TolX = 1e-6

Further information about the normal distribution can be found at https://en.wikipedia.org/wiki/Normal_distribution

See also: normcdf, norminv, normpdf, normrnd, normlike, normstat

Source Code: normfit

Example: 1

 

 ## Sample 3 populations from 3 different normal distibutions
 randn ("seed", 1);    # for reproducibility
 r1 = normrnd (2, 5, 5000, 1);
 randn ("seed", 2);    # for reproducibility
 r2 = normrnd (5, 2, 5000, 1);
 randn ("seed", 3);    # for reproducibility
 r3 = normrnd (9, 4, 5000, 1);
 r = [r1, r2, r3];

 ## Plot them normalized and fix their colors
 hist (r, 15, 0.4);
 h = findobj (gca, "Type", "patch");
 set (h(1), "facecolor", "c");
 set (h(2), "facecolor", "g");
 set (h(3), "facecolor", "r");
 hold on

 ## Estimate their mu and sigma parameters
 [muhat, sigmahat] = normfit (r);

 ## Plot their estimated PDFs
 x = [min(r(:)):max(r(:))];
 y = normpdf (x, muhat(1), sigmahat(1));
 plot (x, y, "-pr");
 y = normpdf (x, muhat(2), sigmahat(2));
 plot (x, y, "-sg");
 y = normpdf (x, muhat(3), sigmahat(3));
 plot (x, y, "-^c");
 ylim ([0, 0.5])
 xlim ([-20, 20])
 hold off
 legend ({"Normalized HIST of sample 1 with mu=2, σ=5", ...
          "Normalized HIST of sample 2 with mu=5, σ=2", ...
          "Normalized HIST of sample 3 with mu=9, σ=4", ...
          sprintf("PDF for sample 1 with estimated mu=%0.2f and σ=%0.2f", ...
                  muhat(1), sigmahat(1)), ...
          sprintf("PDF for sample 2 with estimated mu=%0.2f and σ=%0.2f", ...
                  muhat(2), sigmahat(2)), ...
          sprintf("PDF for sample 3 with estimated mu=%0.2f and σ=%0.2f", ...
                  muhat(3), sigmahat(3))}, "location", "northwest")
 title ("Three population samples from different normal distibutions")
 hold off

                    
plotted figure