I have got the 10,000 values for my data using Matlab. When I plotted the histogram and fitted it with normal distribution I got the following figure
Is there some mistake in this histogram fitting or what should I do to scale it properly.
or you could just call
histfit(your_data,num_bins)
Related
I have a data set in excel so I passed it to MATLAB to draw a histogram and append gaussian fitting. My code is below.
vData = xlsread("2.xlsx");
figure(1);
hHist = histogram(vData, -2.7:0.001:-2.4);
As I run my code, I get a histogram like this
histogram of my data
To gaussian fit my histogram, I add some code like this
figure(2);
histfit(vData); % I'm not sure this is the right fitting method
But the result I got is like this
fitting on histogram
I guess the bin size and bin edge is not appropriate for my data because my data is usually clustered around -2.5.
hisfit method doesn't have bin size or bin edge parameter so I think I can't use this method.
I'm wondering how I can get an appropriate gaussian fitting to my histogram. Thank you for you help.
I'm having troubles when fitting a pdf to an histogram in Matlab. I'm using gmdistribution.fit because my data is multi-modal. This is what I have done:
data=[0.35*randn(1,100000), 0.5*randn(1,100000)+5, 1*randn(1,100000)+3]'; %multimodal data
x=min(data):(max(data)-min(data))/10000:max(data);
%Normalized Histogram
[counts,edges]=histcounts(data,500, 'Normalization', 'pdf');
bw=edges(2)-edges(1);
centers=edges(1:end-1)+bw;
H = bar(centers,counts,'hist');
hold on
%Fitting with gmdistribution
rng default
obj=gmdistribution.fit(data,3,'Replicates',5);
%the PDF
PDF=zeros(1,length(x));
for i=1:obj.NumComponents
k=obj.ComponentProportion(i);
u=obj.mu(i);
sigma=obj.Sigma(i);
PDF=PDF+k*normpdf(x,u,sigma);
end
PDF=PDF/trapz(x,PDF); %normalization (just in case)
plot(x,PDF)
%Fitting with ksdensity (for comparison)
[PDF2,xi]=ksdensity(data,x);
plot(x,PDF2)
legend('Normalized Histogram','gmdistribution','ksdensity')
Histogram and PDFs
As you can see, the Gaussian Mixture doesn't fit the histogram properly. The PDF from the ksdensiti function is much better. I have also tried to fit just one gaussian. If you run the same previous code, using
data=[0.35*randn(1,100000)]';
and
obj=gmdistribution.fit(data,1,'Replicates',5);
you get the following
Histogram and PDFs for one gaussian
Again, the pdf from gmdistribution doesn't fit the histogram. It seems that the problem is with the scaling factor in the data generation (the 0.35). What am I doing wrong?
The Sigma parameter of the gmdistribution object corresponds to the covariance, however, the normpdf function needs the standard deviation. The problem is fixed by replacing normpdf(x,u,sigma) with normpdf(x,u,sqrt(sigma)) in the for loop.
I have to distributions p and q I have no knowledge about their mean and variance I want to fit a normal distribution curve to the histograms I have and get the mean and variance of the fitting
when I use
histfit(p);
histfit(q);
I get the results in the figure:
when I use
[f,x]=hist[p];
[mu,sigma]=normfit(p)
pdf=normpdf(x,mu,sigma);
figure;
hold on
bar(x,f);
plot(x,pdf);
I get the results in the figure where I don't see the fitting at all:
Eventually I would like to present graphically histfit,
,but also obtain the true standard deviation and mean of the fitting for further use.
help anyone?
What histfit does is plotting a pdf normalized to the scale of the histogram. A scaling factor of numel(p).*mean(diff(x)) is applied to match the curve with the histogram. It scales the area under the pdf to the area the histogram covers.
I'm new in matlab and i tried to normalize two normal distributions according to How to normalize a histogram in MATLAB?, but i couldn't. Can someone tell me how to normalize the two normal distribution below:
[f,x]=hist(normrnd(25,2.5),50);%# create histogram from a normal distribution.
g=1/sqrt(2*pi)*exp(-0.5*x.^2);%# pdf of the normal distribution
figure(1)
bar(x,f/trapz(x,f));hold on
plot(x,g,'r');hold off
Thanks!
Two suggestions:
Use randn instead of normrnd.
Increase the number of numbers you are trying to generate.
Code:
[f,x]=hist(randn(10000,2.5),50);%# create histogram from a normal distribution.
g=1/sqrt(2*pi)*exp(-0.5*x.^2);%# pdf of the normal distribution
figure(1)
bar(x,f/trapz(x,f));hold on
plot(x,g,'r');hold off
I've used the gmdistribution to fit data to a Gaussian mixture model. I wanted to plot a contour plot http://imgur.com/yVE1M where the contours are obviously missing. For a 1D problem I found fplot, but now I'm stumped.
I ran into a similar problem when I wrote an EM algorithm for gaussian mixtures. Here is the snippet of code that fixed it in my case:
for l=1:k
zz=gmdistribution(MU(l,:),SIG(:,:,l),PI(l));
ezcontour(#(x,y)pdf(zz,[x y]),[minx1 maxx1],[miny1 maxy1],250);
end
The key is to increase N:
ezcontour(...,N) plots FUN over the default domain using an N-by-N
grid. The default value for N is 60.