Why does this probability density function looks off?

I have a random variable x which has a mean of 10 and a variance of 16. I used the following code to generate the array and the PDF:
x= randn(100,1) * sqrt(16)+10;
mu = 10;
sigma = 4;
pd = makedist('Normal',mu,sigma);
y=pdf(pd,x);
hist(y)
Should i be plotting with something other than hist or is x itself wrong? Thank you.

 采纳的回答

The mistake is in using hist. Plot x vs. y instead, because y is the value of the pdf itself.

x= randn(100,1) * sqrt(16)+10;
mu = 10;
sigma = 4;
pd = makedist('Normal',mu,sigma);
y=pdf(pd,x);
figure
plot(x,y,'.')

5 个评论

Ok but it doesn't look like the probabilities add to 1?
Remember, it is not the values of pdf(x) that sum to 1. It is dx*pdf(x) that sums to one.
You have not chosen evenly spaced x values, and they are not sorted, so it is just a bit tricky to prove it.
[sorted_x,sortingIndex] = sort(x);
dx = [0; diff(sorted_x)];
probabilitySum = sum(dx.*y(sortingIndex))
This gives 0.9866 in the case I tested, which is about as close as I would expect for N = 100.
Ok cool, but for instance when I try to figure out the probability that x>5 I get an oddly low value of around 7. When I feel like it should be closer to 100. Here's how I coded it:
P=x>5;
Probability_x_greater_than_5=sum(P.*y);

You fell into the same trap of not multiplying by dx. I'm not certain, but you may also failed to account for the fact that your values of y are not ordered. So, I think you may have gotten a more-or-less random answer.

There is no need to choose your x at random. It would be much better to choose an evenly spaced array of x from the beginning, and your whole problem becomes simpler.

Like this ...

mu = 10;
sigma = 4;
dx = 0.01;
x = mu - 5*sigma : dx : mu + 5*sigma;
pd = makedist('Normal',mu,sigma);
y=pdf(pd,x);
figure
plot(x,y)
probability_total = sum(dx*y)
Probability_x_greater_than_5 = sum(dx.*y(x>5))

I got 0.8941 for that probability.

Notice that these are probabilities, so they lie between 0 and 1. If you want them represented as percentages, then divide by 0.01.

Awesome thank you very much that was a good explanation!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by