Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adapting histfit.m to lognfit.m - help

189 views
Skip to first unread message

Andy M

unread,
Oct 17, 2008, 11:50:03 AM10/17/08
to
Hello,

I need a lognormal distribution to fit a histogram. I have adapted histfit.m (which is originally for normal distributions only) like below. When I tested it on a data set, the plot of the density function is not fully drawn. Why is that? I appreciate any help! Thanks ahead!

Regards
Andy

function h = histfit(data,nbins)
%HISTFIT Histogram with superimposed fitted normal density.
% HISTFIT(DATA,NBINS) plots a histogram of the values in the vector DATA.
% using NBINS bars in the histogram. With one input argument, NBINS is set
% to the square root of the number of elements in DATA.
%
% H = HISTFIT(DATA,NBINS) returns a vector of handles to the plotted lines.
% H(1) is a handle to the histogram, H(2) is a handle to the density curve.

% Copyright 1993-2004 The MathWorks, Inc.
% $Revision: 2.13.2.1 $ $Date: 2003/11/01 04:26:28 $

[row,col] = size(data);
if min(row,col) > 1
error('stats:histfit:VectorRequired','DATA must be a vector.');
end

if row == 1
data = data(:);
end
row = sum(~isnan(data));

if nargin < 2
nbins = ceil(sqrt(row));
end

[n,xbin]=hist(data,nbins);

lognpara=lognfit(data);
mu=lognpara(1);
sigma=lognpara(2);
mean=lognstat(mu,sigma);

%mr = nanmean(data); % Estimates the parameter, MU, of the normal distribution.
%sr = nanstd(data); % Estimates the parameter, SIGMA, of the normal distribution.

x=(-3*sigma+mu:0.1*sigma:3*sigma+mu)';% Evenly spaced samples of the expected data range.

hh = bar(xbin,n,1); % Plots the histogram. No gap between bars.

np = get(gca,'NextPlot');
set(gca,'NextPlot','add')

xd = get(hh,'Xdata'); % Gets the x-data of the bins.

rangex = max(xd(:)) - min(xd(:)); % Finds the range of this data.
binwidth = rangex/nbins; % Finds the width of each bin.


y = lognpdf(x,mu,sigma);
y = row*(y*binwidth); % Normalization necessary to overplot the histogram.

hh1 = plot(x,y,'r-','LineWidth',2); % Plots density line over histogram.

if nargout == 1
h = [hh; hh1];
end

set(gca,'NextPlot',np)

Andy M

unread,
Oct 17, 2008, 12:00:19 PM10/17/08
to
nevermind, I know now how to do it

Jik

unread,
Jul 28, 2011, 5:12:10 AM7/28/11
to
I'm having the same problem right now. So how did you solve it?

Jik

unread,
Jul 28, 2011, 5:40:31 AM7/28/11
to
is it because of this x-Vector?

what is
-3sigma + mu : 0.1*sigma : sigma + mu
anyway? where does it come from?

Tom Lane

unread,
Jul 28, 2011, 1:04:27 PM7/28/11
to

It is likely the intent was to use 3:sigma at the beginning and end.

But, in relatively recent versions of MATLAB, the histfit function supports
the lognormal distribution directly:

load carsmall
histfit(Weight,20,'lognormal')

-- Tom

Tom Lane

unread,
Jul 29, 2011, 9:15:42 AM7/29/11
to
>> -3sigma + mu : 0.1*sigma : sigma + mu
>> anyway? where does it come from?
>
> It is likely the intent was to use 3:sigma at the beginning and end.

I meant 3*sigma:

-3*sigma + mu : 0.1*sigma : 3*sigma + mu

0 new messages