version (3.06 KB) by J. David Cogdell
Fits normal, lognormal, and weibull distributions to data


Updated 23 Mar 2006

No License

%function [pdffit,offset,A,B,resnorm,mode,medpdf,ndata,h] = distributionfit(data,distribution,nbins,figureWH)
%PURPOSE jdc rev. 23-Mar-06
% Fit one of three probability distributions (normal, lognormal, weibull)
% to input data vector. If the distribution is specified as 'best' the dis-
% tribution that best fits the data is selected automatically.
% If nargin==1, "distribution" is prompted for and entered interactively
% data - n x 1 or 1 x n input data vector
% distribution - probability distribution to fit to "data". Can
% be 'normal', 'lognormal', 'weibull', or 'best' ... default: 'best'
% nbins - number of bar-chart bins ................................... default: sqrt(length(data))
% figureWH - figure size [width height] (inches) ????... default: [4 3]
% pdffit - fitted probability density function - n x 2 matrix with column 1 the
% x-values, column 2 the y values
% offset - amount by which the data was offset for lognormal and weibull fits
% (to satisfy the positive-definite requirements for these distributions).
% Note: this is approximately equivalent to fitting a 3- rather than a 2-parameter
% distribution.
% A,B - distribution parameters - mu and sigma for normal and lognormal distributions,
% scale and shape parameters for weibull distribution
% resnorm - measure of goodness of fit: sum of squares of point-by-point differences
% between experimental and best-fit cumulative distribution function
% mode - 2-element vector: [distribution mode density at mode]
% medpdf - 2-element vector: [data median pdf density at median]
% h - handles to the bar chart and probability density curve
%TYPICAL FUNCTION CALLS (using random number vectors for input data)
% distributionfit(randn(10000,1));
% distributionfit(wblrnd(2,3,10000,1));
% distributionfit(wblrnd(2,3,10000,1),'weibull');
% distributionfit(lognrnd(1.5,.5,10000,1),'lognormal');
% distributionfit(lognrnd(1.5,.5,10000,1),'best');
% Statistics Toolbox Version 3.0.2, function HISTFIT.M
% 09jun05 Revised pdf plot to reflect input rather than zero-shifted data
% " Fixed some problems with lognormal distribution related to
% logarithms and exponents of very small numbers
% 10jun05 Added mode and median to arguments out
% 23mar06 Set xlim(2) to max(data)
% " Decreased text fontsize & fixed text placement problem

Cite As

J. David Cogdell (2022). DistributionFit (https://www.mathworks.com/matlabcentral/fileexchange/10501-distributionfit), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!