Incomplete Normal Distribution Plot on MATLAB

13 vues (au cours des 30 derniers jours)
Abdul Moeez Baig
Abdul Moeez Baig le 17 Oct 2021
Commenté : Dave B le 17 Oct 2021
There is alot of data that has negative and positive numbers both. I am unsure if I am using the function properly. I want to plot normal distribution of the curve however for some plots, the curve is not symmetric (isn't bell shaped) and even if I include the rest of the axis, it doesnt exist. I want to know where I am making the mistake..
data = sort(data);
data = data(~isnan(data));
uu = mean(data,'omitnan');
sigma = std(data,'omitnan');
y=normpdf(data,uu,sigma);
plot(data,y);

Réponses (1)

Dave B
Dave B le 17 Oct 2021
The function normpdf(x,mu,sigma) will return the pdf of the normal distribution with mean mu and standard deviation sigma, evaluated at the values in x.
x=linspace(-5,5,100);
mu=0;
sigma=1;
y=normpdf(x,mu,sigma);
plot(x,y)
If your data are very non normal, this wouldn't look very bell shaped, because you'd be plotting it based on x but x wouldn't sample the area around the mean. Here's an extreme case to illustrate:
x=[repelem(1,100) 2:10];
y=normpdf(x,mean(x),std(x));
plot(x,y)
title(sprintf('mean(x) = %0.2f; std(x) = %0.2f', mean(x), std(x)))
  2 commentaires
Abdul Moeez Baig
Abdul Moeez Baig le 17 Oct 2021
Thank you for the answer and to confirm that it is not always bell shaped because I couldn't believe that based on information found online.
So this means there is nothing wrong with my code? (the one you see above).
Also, the curve is smooth but missing from the start, now the data does not have values below 0 as seen in the curve but it could have been a steeper slope in the start for the sake of completion, or is this the possible correct plot?
Dave B
Dave B le 17 Oct 2021
Sorry I should have included some more words in my answer. You're plotting a normal distribution, a curve, which is by all means symmetrical and bell shaped, but you're plotting it for a set of x which does not sample that region. It looks to me like your data do not include values less than 0, if you asked MATLAB min(data) I suspect it would be 0.
It's hard to say if this represents an error in your code, or your goals, or if this is what you should expect. You could visualize more of the curve by plotting:
data = sort(data);
data = data(~isnan(data));
uu = mean(data,'omitnan');
sigma = std(data,'omitnan');
x = linspace(-1000,1000,100)
y=normpdf(x,uu,sigma);
plot(x,y);
Or if you prefer you could define x based on uu and sigma, i.e. pick some number of standard deviations above and below the mean that you'd like to plot:
n_std = 5;
n_pts = 100;
x = linspace(uu - sigma * n_std,uu + sigma * n_std, n_pts)
...

Connectez-vous pour commenter.

Produits


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by