This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Compare Multiple Distribution Fits

This example shows how to fit multiple probability distribution objects to the same set of sample data, and obtain a visual comparison of how well each distribution fits the data.

Step 1. Load sample data.

Load the sample data.

load carsmall;

This data contains miles per gallon (MPG) measurements for different makes and models of cars, grouped by country of origin (Origin), model year (Model_Year), and other vehicle characteristics.

Step 2. Create a nominal array.

Transform Origin into a nominal array and remove the Italian car from the sample data.

Origin = nominal(Origin);
MPG2 = MPG(Origin~='Italy');
Origin2 = Origin(Origin~='Italy');
Origin2 = droplevels(Origin2,'Italy');

Since there is only one Italian car, fitdist cannot fit a distribution to that group. Removing the Italian car from the sample data prevents fitdist from producing an error.

Step 3. Fit multiple distributions by group.

Use fitdist to fit Weibull, normal, logistic, and kernel distributions to each country of origin group in the MPG data.

[WeiByOrig,Country] = fitdist(MPG2,'weibull','by',Origin2);
[NormByOrig,Country] = fitdist(MPG2,'normal','by',Origin2);
[LogByOrig,Country] = fitdist(MPG2,'logistic','by',Origin2);
[KerByOrig,Country] = fitdist(MPG2,'kernel','by',Origin2);
WeiByOrig =

  1×5 cell array

  Columns 1 through 2

    [1×1 prob.WeibullDistribution]    [1×1 prob.WeibullDistribution]

  Columns 3 through 4

    [1×1 prob.WeibullDistribution]    [1×1 prob.WeibullDistribution]

  Column 5

    [1×1 prob.WeibullDistribution]

Country =

  5×1 cell array


Each country group now has four distribution objects associated with it. For example, the cell array WeiByOrig contains five Weibull distribution objects, one for each country represented in the sample data. Likewise, the cell array NormByOrig contains five normal distribution objects, and so on. Each object contains properties that hold information about the data, distribution, and parameters. The array Country lists the country of origin for each group in the same order as the distribution objects are stored in the cell arrays.

Step 4. Compute the pdf for each distribution.

Extract the four probability distribution objects for USA and compute the pdf for each distribution. As shown in Step 3, USA is in position 5 in each cell array.

WeiUSA = WeiByOrig{5};
NormUSA = NormByOrig{5};
LogUSA = LogByOrig{5};
KerUSA = KerByOrig{5};

x = 0:1:50;
pdf_Wei = pdf(WeiUSA,x);
pdf_Norm = pdf(NormUSA,x);
pdf_Log = pdf(LogUSA,x);
pdf_Ker = pdf(KerUSA,x);

Step 5. Plot pdf the for each distribution.

Plot the pdf for each distribution fit to the USA data, superimposed on a histogram of the sample data. Scale the density by the histogram area for easier display. Create a histogram of the USA sample data

data = MPG(Origin2=='USA');
[n,y] = hist(data,10);
b = bar(y,n,'hist');

% Scale the density by the histogram area for easier display
area = sum(n)*(y(2)-y(1));
time = 0:50;
pdfWei = pdf(WeiUSA,time);
pdfNorm = pdf(NormUSA,time);
pdfLog = pdf(LogUSA,time);
pdfKer = pdf(KerUSA,time);

% Plot the pdf of each fitted distribution
hold on
title('MPG for Cars from USA')
hold off

Superimposing the pdf plots over a histogram of the sample data provides a visual comparison of how well each type of distribution fits the data. Only the nonparametric kernel distribution KerUSA comes close to revealing the two modes in the original data.

Step 6. Further group USA data by year.

To investigate the two modes revealed in Step 5, group the MPG data by both country of origin (Origin) and model year (Model_Year), and use fitdist to fit kernel distributions to each group.

[KerByYearOrig,Names] = fitdist(MPG,'Kernel','By',{Origin Model_Year});

Each unique combination of origin and model year now has a kernel distribution object associated with it.

Step 7. Compute the pdf for each group.

Extract the three probability distributions for each USA model year, which are in positions 12, 13, and 14 in the cell array KerByYearOrig. Compute each pdf.

USA70 = KerByYearOrig{12};
USA76 = KerByYearOrig{13};
USA82 = KerByYearOrig{14};

pdf70 = pdf(USA70,x);
pdf76 = pdf(USA76,x);
pdf82 = pdf(USA82,x);

Step 8. Plot pdf for each group.

Plot the pdf for each group on the same figure.

hold on
title('MPG in USA Cars by Model Year')
hold off

When further grouped by model year, the pdf plots reveal two distinct peaks in the MPG data for cars made in the USA — one for the model year 1970, and one for the model year 1982. This explains why the smooth curve produced by the kernel distribution for the combined USA miles per gallon data shows two peaks instead of one.

See Also

Related Examples

More About

Was this topic helpful?