Main Content

Fit a Distribution Using the Distribution Fitter App

R2026b

This example shows how you can use the Distribution Fitter app to interactively fit a probability distribution to data.

Step 1: Load Sample Data

Load the sample data.

load carsmall

Step 2: Import Data

Open the Distribution Fitter app.

distributionFitter

To import the vector MPG into the Distribution Fitter app, click the Data button. The Data dialog box opens.

Data dialog box

The Data field displays all numeric arrays in the MATLAB® workspace. From the list, select MPG. A histogram of the selected data appears in the Data preview pane.

In the Data set name field, type a name for the data set, such as MPG data, and click Create Data Set. The main window of the Distribution Fitter app now displays a larger version of the histogram in the Data preview pane.

Histogram of miles per gallon data in Distribution Fitter

Step 3: Create a New Fit

To fit a distribution to the data, in the main window of the Distribution Fitter app, click New Fit.

To fit a normal distribution to MPG data:

  1. In the Fit name field, enter a name for the fit, such as My fit.

  2. From the list in the Data field, select MPG data.

  3. Confirm that Normal is selected from the Distribution list.

  4. Click Apply.

Edit Fit dialog box with normal distribution results for miles per gallon data

The Results pane displays the mean and standard deviation of the normal distribution that best fits MPG data.

The Distribution Fitter app main window displays a plot of the normal distribution with this mean and standard deviation.

Normal density plot for miles per gallon data in Distribution Fitter

Based on the plot, a normal distribution does not appear to provide a good fit for the MPG data. To obtain a better evaluation, select Probability plot from the Display type list. Confirm that Normal is selected from the Distribution list. The main window displays the following figure.

Normal probability plot for miles per gallon data in Distribution Fitter

The normal probability plot shows that the data deviates from normal, especially in the tails.

Step 4: Create and Manage Additional Fits

The MPG data pdf indicates that the data has two peaks. Try fitting a nonparametric kernel distribution to obtain a better fit for this data.

  1. Click Manage Fits. In the dialog box, click New Fit.

  2. In the Fit name field, enter a name for the fit, such as Kernel fit.

  3. Set Data to MPG data.

  4. Set Distribution to Non-parametric. This enables several options in the Non-parametric pane, including Kernel, Bandwidth, and Domain. For now, accept the default value to apply a normal kernel shape and automatically determine the kernel bandwidth (using Auto). For more information about nonparametric kernel distributions, see Kernel Distribution.

  5. Click Apply.

The Results pane displays the kernel type, bandwidth, and domain of the nonparametric distribution fit to MPG data.

Edit Fit dialog box with nonparametric kernel distribution results for miles per gallon data

The main window displays plots of the original MPG data with the normal distribution and nonparametric kernel distribution overlaid. To visually compare these two fits, set Display type to Density (PDF).

Normal and nonparametric kernel density plots for miles per gallon data in Distribution Fitter

To include only the nonparametric kernel fit line (Kernel fit) on the plot, click Manage Fits. In the Table of fits pane, locate the row for the normal distribution fit (My fit) and clear the box in the Plot column.

Plot showing the nonparametric kernel fit only.

See Also

Topics