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.
Related
I have run into a very peculiar problem. It might seem silly to a lot of you. But I am in dire need of a way out. I am analyzing sets of high-speed images with MATLAB. The image of interest (https://www.dropbox.com/s/h4h26y3mvpao8m6/sample.png?dl=0) is an average of 3000 images (background subtracted). As shown in the picture, I am reading the pixel intensities/values along columns. As this is a laser beam, the shape or beam profile away from the wall has the shape of a Gaussian distribution. As I approach to the wall (the brightest part at the right of the image) because of some effect the shape is turning into one like a log-normal distribution. In this spreadsheet (https://www.dropbox.com/s/yeim06a5cq3iqg8/sample.xlsx?dl=0) I have pasted the raw intensities as I read thru from point A to point B. The column D has the raw intensities and the column E has the values achieved with a 'sgolay' fit of the column D values. If I plot these it pretty much has the shape of a lognormal distribution. I can get the mu and sigma with the 'lognfit' or 'fitdist' functions. Now the question is what is the equation [expressed as a function of pixel location (x) or the pixel intensity (y)] of the fitted 'lognormal curve' that could be used to recreate the fitted curve? Your help is highly appreciated.
The lognfit extracts the mu and the sigma of the lognormal distribution. The mu is the mean of logarithmic values and sigma the standard deviation of logarithmic values. You can refer to https://en.wikipedia.org/wiki/Log-normal_distribution for the shape of the function given mu and sigma.
With logrnd(mu,sigma) you can generate samples from the same distribution:
https://it.mathworks.com/help/stats/lognrnd.html?searchHighlight=lognrnd&s_tid=srchtitle_lognrnd_1
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.
this is my problem:
I have the next data "A", which looks like:
As you can see, I have drawn with red circles the apparently peaks, the most defined are 2 and 7, I say that they are defined because its standard deviation is low in comparison with the other peaks (especially the second one).
What I need is a way (anyway) to get the values and the standard deviation of n peaks in a numeric array.
I have tried with "clusters", but I got no good results:
First of all, I used "kmeans" MATLAB function, and I realize that this algorithm doesn't group peaks as I need. As you can see in the picture above, in the red circle, that cluster has at less 3 or 4 peaks. And kmeans need that you set the number of clusters, and I need to identify it automatically.
I hope that anyone can give me some ideas, or a way to get better results, thanks.
Pd: I leave the data "A" in the next link.
https://drive.google.com/file/d/0B4WGV21GqSL5a2EyQ2l0SHZURzA/edit?usp=sharing
The problem is that your axes have very different meaning.
K-means optimizes variance. But variance in X is something entirely different than variance in Y, isn't it? Furthermore, each of these methods will split your data in both X and Y, whereas I assume you want the data to be partitioned on the X axis only.
I suggest the following: consider the Y axis to be a weight, and X axis to be a position.
Then perform weighted density estimation, and look for low density to separate your clusters.
I can't help you with MATLAB. I don't use it.
Mathematically, what you want to do is place a Gaussian at each point, with area Y and center X. Then find minima and maxima on the sum of these Gaussians. See Wikipedia, Kernel Density Estimation for details; except that you want to use the Y axis as weights. You could maybe also use 1/Y as standard deviation, if you don't want to use weights.
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)
I know that I can fit a histogram with a gamma distribution in this way:
histfit(data,bins-number,'gamma');figure(gcf);
And I know too that I can normalize a histogram with histnorm. But how can I create a normalized gamma distribution with its histogram?
Any idea or suggestion? Thanks for any help!
EDIT:
In response to BruceWarrior's comment below, histfit will normalize the data for you... just replace x with your data. If you want to know how to normalize a histogram yourself such that it is a probability density, see my answer to that very question. Note that the accepted answer will not give you a probability density (i.e., the area under the curve will not be 1).
You can use the gamrnd function to generate random variables with a Gamma distribution for a given shape parameter a and scale parameter b. You can then call histfit on this data to fit the Gamma distribution to the normalized histogram. Here's an example:
x=gamrnd(1,2,1000,1);
histfit(x,50,'gamma')
a=1,b=2
a=2, b=2