histfit vs fitdist giving different results

조회 수: 7(최근 30일)
T0m4
T0m4 2019년 6월 12일
답변: jiawei shi 2021년 9월 3일
Hello,
I want to get the fitdist plot extracted from histfit without using histfit but it's not working in my case.
Using histfit and fitdist I can't get the same results with both of them:
clear;
close all;
load('data.mat');
for ii=1:8
h=histfit(data{ii}, [], 'Exponential');
HX(:,ii)=h(2).XData;
HY(:,ii)=h(2).YData;
leg{ii}=num2str(ii);
x=0:0.001:1200;
pde=fitdist(data{ii},'exponential');
EX(:,ii)=x;
EY(:,ii)=exppdf(x, pde.mu);
EYs(:,ii)=exppdf(x, pde.mu)*sum(data{ii});
close all;
end
figure
s(1)=subplot(3,1,1);
hold on;
title('From histfit');
plot(HX, HY, 'linewidth', 2);
legend(leg);
hold off;
s(2)=subplot(3,1,2);
hold on;
title('From fitdist');
plot(EX, EY, 'linewidth', 2);
legend(leg);
hold off;
s(3)=subplot(3,1,3);
hold on;
title('From fitdist*sum(data(ii))');
plot(EX, EYs, 'linewidth', 2);
legend(leg);
hold off;
linkaxes(s, 'x');
xlim([0 200]);
Here are the results:
The data file is data.mat
If you have any idea about what I'm doing wrong...
Thx for your help.

답변(1개)

jiawei shi
jiawei shi 2021년 9월 3일
I'v got the same problem. And I checked the code of histfit by using "edit histfit", then I saw part of the code as following:
pd = fitdist(data,dist);
% Normalize the density to match the total area of the histogram
binwidth = binedges(2)-binedges(1); % Finds the width of each bin
area = n * binwidth;
y = area * pdf(pd,x);
It indicates that the distribution plot extracted from hisfit has been nomalized and it's not the original distribution as got from fitdist. So if you want to get the same distribution from the function fitdist, you have to manually normalize the output data to match the histogram plot, just as the code showed above. Hope this will help you!

Community Treasure Hunt

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

Start Hunting!

Translated by