normalize 2 histogram and plot - matlab

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

Related

Obtaining the mean and standard deviation of normal fitting

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.

How to plot a distribution based on moments

I am wondering how, in Matlab, to plot a continuous pdf with the following information?
mean=-0.3731
standard deviation= 5.6190
skewness=-3.0003
kurtosis=13.1722
or alternative how do I plot a continous pdf that is not normal? (like it is skewness and has kurtosis, etc)
Thanks!
Those parameters don't define a distribution, but normally you would use "makedist" in matlab to generate a probability distribution object and then plot it.
The following thread has some discussion on defining a distribution. How to generate distributions given, mean, SD, skew and kurtosis in R?
Based on your comment below, I think you are looking for something like the following functio that generates a m by n matrix of random values with the following parameters:
r = pearsrnd(mu,sigma,skew,kurt,m,n)

How To Fit Multivariate Normal Distribution To Data In MATLAB?

I'm trying to fit a multivariate normal distribution to data that I collected, in order to take samples from it.
I know how to fit a (univariate) normal distribution, using the fitdist function (with the 'Normal' option).
How can I do something similar for a multivariate normal distribution?
Doesn't using fitdist on every dimension separately assumes the variables are uncorrelated?
There isn't any need for a specialized fitting function; the maximum likelihood estimates for the mean and variance of the distribution are just the sample mean and sample variance. I.e., compute the sample mean and sample variance and you're done.
Estimate the mean with mean and the variance-covariance matrix with cov.
Then you can generate random numbers with mvnrnd.
It is also possible to use fitmgdist, but for just a multivariate normal distribution mean and cov are enough.
Yes, using fitdist on every dimension separately assumes the variables are uncorrelated and it's not what you want.
You can use [sigma,mu] = robustcov(X) function, where X is your multivariate data, i.e. X = [x1 x2 ... xn] and xi is a column vector data.
Then you can use Y = mvnpdf(X,mu,sigma) to get the values of the estimated normal probability density function.
https://www.mathworks.com/help/stats/normfit.html
https://www.mathworks.com/help/stats/mvnpdf.html

Histogram proper fitting

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)

Generate a probability distribution from a histogram with two peaks

I have a histogram with two peaks and I want to generate the corresponding probability distribution. I have used the following MATLAB code:
A=mydata;
M1=max(A);
M2=min(A);
I=(0:100).*(M1-M2)./100+M2;
[n,x]=hist(A,I);
bar(x,n/(1000*0.352))
I found this code frequently to explain how we can find a prob distribution for a histogram of random numbers normally distributed but I don't know whether if this true for a histogram with two peaks and generate a normalised probability distribution.
Try using this FileExchange submission - ALLFITDIST.
Not sure it can fit two peaks. But since they are quite far from each other, you can try to fit by range and then sum them together.