File Exchange

image thumbnail

Fit all valid parametric probability distributions to data

version 1.4 (5.3 KB) by

ALLFITDIST Fit all valid parametric probability distributions to data.

43 Ratings



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

ALLFITDIST Fit all valid parametric probability distributions to data.
[D PD] = ALLFITDIST(DATA) fits all valid parametric probability distributions to the data in vector DATA, and returns a struct D of fitted distributions and parameters and a struct of objects PD representing the fitted distributions. PD is an object in a class derived from the ProbDist class.

[...] = ALLFITDIST(DATA,SORTBY) returns the struct of valid distributions sorted by the parameter SORTBY
NLogL - Negative of the log likelihood
BIC - Bayesian information criterion (default)
AIC - Akaike information criterion
AICc - AIC with a correction for finite sample sizes

[...] = ALLFITDIST(...,'DISCRETE') specifies it is a discrete distribution and does not attempt to fit a continuous distribution to the data

[...] = ALLFITDIST(...,'PDF') or (...,'CDF') plots either the PDF or CDF of a subset of the fitted distribution. The distributions are plotted in order of fit, according to SORTBY.

List of distributions it will try to fit
Continuous (default)
Extreme value
Generalized extreme value
Generalized Pareto
Inverse Gaussian
t location-scale

Discrete ('DISCRETE')
Negative binomial

Optional inputs:
[...] = ALLFITDIST(...,'n',N,...)
For the 'binomial' distribution only:
'n' A positive integer specifying the N parameter (number of trials). Not allowed for other distributions. If 'n' is not given it is estimate by Method of Moments. If the estimated 'n' is negative then the maximum value of data will be used as the estimated value.
[...] = ALLFITDIST(...,'theta',THETA,...)
For the 'generalized pareto' distribution only:
'theta' The value of the THETA (threshold) parameter for the generalized Pareto distribution. Not allowed for other distributions. If 'theta' is not given it is estimated by the minimum value of the data.

Note: ALLFITDIST does not handle nonparametric kernel-smoothing, use FITDIST directly instead.

Given random data from an unknown continuous distribution, find the best distribution which fits that data, and plot the PDFs to compare graphically.
data = normrnd(5,3,1e4,1); %Assumed from unknown distribution
[D PD] = allfitdist(data,'PDF'); %Compute and plot results
D(1) %Show output from best fit

Given random data from a discrete unknown distribution, with frequency data, find the best discrete distribution which would fit that data, sorted by 'NLogL', and plot the PDFs to compare graphically.
data = nbinrnd(20,.3,1e4,1);
values=unique(data); freq=histc(data,values);
[D PD] = allfitdist(values,'NLogL','frequency',freq,'PDF','DISCRETE');

Although the Geometric Distribution is not listed, it is a special case of fitting the more general Negative Binomial Distribution. The parameter 'r' should be close to 1. Show by example.
data=geornd(.7,1e4,1); %Random from Geometric
[D PD]= allfitdist(data,'PDF','DISCRETE');

Compare the resulting distributions under two different assumptions of discrete data. The first, that it is known to be derived from a Binomial Distribution with known 'n'. The second, that it may be Binomial but 'n' is unknown and should be estimated. Note the second scenario may not yield a Binomial Distribution as the best fit, if 'n' is estimated incorrectly. (Best to run example a couple times to see effect)
data = binornd(10,.3,1e2,1);
[D1 PD1] = allfitdist(data,'n',10,'DISCRETE','PDF'); %Force binomial
[D2 PD2] = allfitdist(data,'DISCRETE','PDF'); %May be binomial
PD1{1}, PD2{1} %Compare distributions

Comments and Ratings (59)

Great code!!!. Some internal functions can be updated but the code is extremely useful.

M ghavidel

Thank you for the great codes and explanations.
Can we use this code for non-nested models?

Luca Amerio

Simply GREAT!

Charles Onu

Charles Onu

I don't think that is working properly, in the first example a set of normally distribuited data are best fitted with a Raileigh distribution!!!


Jon (view profile)

Sharath M N

Kiran Karra

Gokhan Kirlik

Thanks for the wonderful script. does the job effectively.


Rita (view profile)

One question! Can I use this function if I have gaps in my data?


KBundy (view profile)

Hi Mike

Thank you for your helpful script.


The Probability Density plot is actually scaled to the bin width (see line 349 in the code). I suppose this is done to obtain comparable values for the bar-plot (empirical) and the results of the fit (pdf-function on line 351).

I would like to know why the bar-plot is scaled to the bin width, rather than using the same bin width for bar- and pdf-plots.

Also, in the bar plot the maximum value of the data is never considered: on line 348 histc(data,xi-dx) is used rather than histc(data,xi). Why is this?


tafteh (view profile)

Hi Mike,

Thanks for your brilliant job in this script.

However I came across one weird results:
The probability Density Function plot produces the y-axis scaled from 0 to 2.5, and the peak of the fitted distributions are going high up to "2." Is it right?

I would appreciate any help,

Danilo Gaspar

Very useful script.

Hi Abdullahi Salman, to show the output results properly you should index the variable, as shown by the example, D(1).

Awesome script. Am however having a little problem. D and PD are not outputting any result. I can see the plot of the pdf though. this is what am getting:

D =
1x6 struct array with fields:

PD =

Columns 1 through 5

[1x1 ProbDistUnivParam] [1x1 ProbDistUnivParam] [1x1 ProbDistUnivParam] [1x1 ProbDistUnivParam] [1x1 ProbDistUnivParam]

Column 6

[1x1 ProbDistUnivParam]

I will appreciate any help. Thank you.

Anshul Goyal

very easy and direct to use

John Knag

Super easy to use and very helpful. Thank you.


Hi Mike,

that's a very nice script.
It would be also useful to test against mixtures, for instance when the data can be fit to a mixture of two or more gaussians, with the parameter k increasing...
see fitgmdist Matlab function.



best MATLAB code so far


The allfitdist function for normally distributed data return 'rayleigh' as best fit distribution! So weird as it is an example included in file.

commands: data = normrnd(5,3,1e4,1); [D PD] = allfitdist(data,'PDF'); D(1)

output: ans =

DistName: 'rayleigh'
NLogL: 2.4515e+04 - 1.5959e+03i
BIC: 4.9038e+04 - 3.1919e+03i
AIC: 4.9031e+04 - 3.1919e+03i
AICc: 4.9031e+04 - 3.1919e+03i
ParamNames: {'B'}
ParamDescription: {'scale'}
Params: 4.1166
Paramci: [2x1 double]
ParamCov: 4.2366e-04
Support: [1x1 struct]


Hi, I was wondering how could I plot both PDF, CDF and the error graph any ideas?


debora (view profile)


I've the same problem. You need to change all ~ (line 245 and others) by another letter.


Well i`m using r2009a. and using the file i've got this error:
??? Error: File: allfitdist.m Line: 245 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.

[D PD] = allfitdist(data,'CCDF');
??? Error: File: allfitdist.m Line: 245 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.
data = normrnd(5,3,1e4,1);
>> [D PD] = allfitdist(data,'CCDF');
??? Undefined function or method 'allfitdist' for input arguments of type 'double'.
Is there any restriction for the file?


Very useful script

I am using Matlab R2008a version I am trying to use this code but its not working Its showing no distributions were found for the example no 1. I checked my matlab version and it contains Statistics toolbox. Now what should I do. Please help.


Dear Mr. Sheppard,

I have been used your code to fit several datasets that I have. I found it really useful. My question is (I am very new in Matlab as well as statistics)... how do you define the "best" distribution? Based on p-values of KSTest?

Manuel Kuhs

Really appreciate your function, was doing this manually for a while!

I apologise in advance if this is an ignorant question, as I'm a very basic MatLab user.

Would it be possible to amend your script to take into account for situations in which you know some data is missing? The particular type I'm interested in is when I know that my data actually only represents e.g. the first 70% on the CDF.

I hope this question makes sense. I'm not even sure of the right terminology to use!


Nitin (view profile)

Olga Petrik

Roni Peer

Roni Peer (view profile)

Great Job.
I've changed it a bit to suit my needs, and going to add a GUI to allow the user to fit just a specific distribution, or select some of them. ALL of them would be a default.

Mike Sheppard

Mike Sheppard (view profile)

Hi Roni,

The "Best Fit" can be found by the output by either D(1) or PD{1}, depending on if you want a structure or ProbDist class object. You can use the class object directly in other statistical functions, such as:


The reason for including all valid distributions is that depending on preferences of model selection or assumptions from the data the distribution that you may prefer to use may be the 2nd or even 3rd "best" from the output, or not given at all. This is especially true if the SORTBY values are close in value, or if a parameter in a given distribution is close to a simpler special case.

Example 3 is an example of the latter; should you use as a model the Negative Binomial Distribution with r=.98 or assume it is actually the more simpler Geometric Distribution with r=1 which is not given as an output?

The error graph is displayed when 'CDF' is given as an input. You can change the number of distributions to include in the plot by adjusting the max_num_dist variable in the plotfigs subfunction.

Hope that helps,


Roni Peer

Roni Peer (view profile)

Hi Mike,

Why not add a "Best Fit" output also?
For example, if the best distribution which represents this data is "Weibull", return it as another output.
This can be used to find "Best Fit" for this data, which can be really useful.
I would also add a summary graph, which shows the error on all types of distributions, and what was the best one.


Eric Diaz

Very useful indeed!

Hi Guys,

the problem at lines 247 etc is resolved by replacing the tilde operator with any name for a variable that will remain unused, but for the problem that also Olivier noted, this is due to the fact that function fitdist is missing in matlab 7.7


Hi people,

This script is not working on matlab 7.7.

Matlab recognises an error in the code at line 247. It says:

Parse error at ',': usage might be invalid matlab syntax
Parse error at ']': usage might be invalid matlab syntax

And the error is repeated for lines 249 249 251 253.

Is there any way of getting it working on 7.7???


Mike Sheppard

Mike Sheppard (view profile)

Warwick, thanks for your note. I am updating the file a bit, and the functionality of custom distributions seems interesting.

If you like, you can e-mail me directly with your improved functionality and I can include it in the next update with acknowledgment.


Mike, I am sorry and aghast about the rating. I actually meant to leave the rating blank. On further experiment, there seems to be no way to go back to a null rating once my cursor merely touches the rating banner of stars (using iMac and the beta R2012a) . Anyway, I was able to use the file to obtain sorted best-fit curves on the type of problems I have and even added custom dist.

Mike Sheppard

Mike Sheppard (view profile)

Warwick, for a "potentially a very useful script" I'm sorry you felt it was only worth a rating of one. Do you have suggestions on how it can be improved? Constructive criticism or ways to improve the program/functionality are always welcome, but I did not see any in your comment, other than asking for specific help after giving it a poor rating.

Please re-read the help section; specifically Example 2.


Mike, this is potentially a very useful script for me. How can I use it for this example problem? I have frequency data describing number of events against day number. Logically the day number must be an integer from 1.
Eg, for discrete days 1:10 and the Yobs are [1099 478 263 159 99 64 41 28 18 12]. Exponential and Weibull are fair candidate distrubution and I have previously fitted these as curves using LS or weighted LS, but an MLE approach ( ie, use neg log likelihood) would be much better as there can be a lot of noise in the tails. Thanks, Warwick


jiro (view profile)


Do you have Statistics Toolbox? It's required to use this function.

Tony Dalton


Great idea, good examples, functional code (style could be better).

Does not work on Matlab 7.7
(Or I misunderstood how to use it)

>> [D, PD] = allfitdist(randn(1000,1)) ;
??? Error using ==> allfitdist at 238
No distributions were found



Updated help section


Fixed frequency data with binomial; generalized pareto as special case; and cleaned up code


Corrected y-axis labels


Included error checking for NaNs in data set and/or frequency; and dimension mismatch between data and frequency

MATLAB Release
MATLAB 7.12 (R2011a)

Inspired: kstest_plot(X,cdfModel,cdfData)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today