不应该是fitdist(数据,'Lognormal')在Matlab中给出与fitdist(log(data),'Normal')相同的结果和图?

时间:2017-02-27 20:04:25

标签: matlab histogram curve-fitting normal-distribution

我正在尝试将分布曲线拟合到某些数据的直方图。 (我在这里使用了一些模型数据,因为很难上传实际数据。我在问题之后已经包含了完整的代码。)

因为当我在logscale中绘制x轴时,直方图看起来是正态分布的,所以我先将数据转换为正态分布,然后得到以下结果:

>>pdn=fitdist(log(data),'Normal')
pdn =     
  Normal distribution
       mu = -0.334458   [-0.34704, -0.321876]
    sigma =  0.351478   [0.342804, 0.360605]

当我用直方图绘制pdf时,我得到了这个: enter image description here

结果对我来说似乎很合理。然后我发现在Matlab fitdist()中,它已经有一个'Lognormal'选项,我真的不需要先转换我的数据,这就是我得到的:

>>pdln = fitdist(data,'Lognormal')
pdln = 
  Lognormal distribution
       mu = -0.334458   [-0.34704, -0.321876]
    sigma =  0.351478   [0.342804, 0.360605]

与我之前的平均值和标准差完全相同。然而,当我用直方图绘制出来时,我得到了一条不同的曲线: enter image description here

该曲线更适合数据,但平均值和平均值+/-标准点的位置并不像我预期的那样(即峰值处的平均值和相同水平的平均值+/-标准值)。

在我的问题中,为什么fitdist(data,'Lognormal')会给出与fitdist(log(data),'Normal')相同的结果,但却有不同的情节?我查看了Matlab帮助页面,但仍然无法理解为什么,或者我的错误在哪里,请帮助。

我所有这一切的目标是在不同条件下获得关于我的数据分布的一些数值参数,并比较它们以查看是否存在任何差异。目前,我不确定哪种方式可以给我可靠的估算方法和标准偏差。

图表的代码如下:

%random data in lognormal distribution
mu=-0.335742;
sigma=0.35228;
data=lognrnd(mu,sigma,[3000 1]);

%make histogram
interval=0.1;
svalue=sort(data);
bx(1)=interval/2;
i=2;
while bx(i-1)<=max(svalue)
    bx(i)=bx(i-1)+interval;
    i=i+1;
end
by=hist(svalue,bx);

subplot(211)
h = bar(bx,by,'hist'); 
set(h,'FaceColor',[.9 .9 .9]);
set(gca,'xlim',[0.05 10]);
xticks=[0.05 0.1 0.2 0.5 1 2 5 10];
set(gca,'xscale','log','xminortick','on')   
set(gca,'xtick',xticks)
ylabel('counts')

subplot(212)
h = bar(bx,by,'hist'); 
set(h,'FaceColor',[.9 .9 .9]);
set(gca,'xlim',[0.05 10]);
xticks=[0.05 0.1 0.2 0.5 1 2 5 10];
set(gca,'xscale','log','xminortick','on')   
set(gca,'xtick',xticks)
ylabel('counts')

% fit distribution curves
pdf_x = 0:0.01:max(data);
max_by=max(by);     % for scaling the pdf to the histogram

% case 1 - PDF fitted using fitdist(log(data),'Normal')
subplot(211)
hold on
pdn = fitdist(log(data),'Normal') 
pdf_y = pdf(pdn,log(pdf_x));
h1=plot(pdf_x,pdf_y./max(pdf_y).*max_by,'-k');
range=[exp(pdn.mu-pdn.sigma) exp(pdn.mu+pdn.sigma)];
h2=plot(exp(pdn.mu),pdf(pdn,(pdn.mu))./max(pdf_y).*max_by,'sk') ;
h3=plot(range,pdf(pdn,log(range))./max(pdf_y).*max_by,'ok') ;
title('PDF fitted using fitdist(log(data),''Normal'')');
legend([h1 h2 h3],'pdf','mean','meam+/-std');

% case 2 - PDF fitted using fitdist(data,'Lognormal')
subplot(212)
hold on
pdln = fitdist(data,'Lognormal') 
pdf_y = pdf(pdln,pdf_x);
h1=plot(pdf_x,pdf_y./max(pdf_y).*max_by,'-b');
range=[exp(pdln.mu-pdln.sigma) exp(pdln.mu+pdln.sigma)];
h2=plot(exp(pdln.mu),pdf(pdln,exp(pdln.mu))./max(pdf_y).*max_by,'sb');  
h3=plot(range,pdf(pdln,range)./max(pdf_y).*max_by,'ob') ;
title('PDF fitted using fitdist(data,''Lognormal'')');
legend([h1 h2 h3],'pdf','mean','meam+/-std');

0 个答案:

没有答案