Pick a value with some probability
Show older comments
Hello everyone. Let's suppose we have a Gaussian distribution on people's height. there will be an average value with higher probability and values that deviate with lower probability. In the example, x contains some height samples and y contains the probability with which that element occurs.
x = [1.50 1.60 1.70 1.70 1.70 1.80 1.90];
mu = mean(x);
s = std(x);
y = normpdf(x, mu, s);
Let's suppose we want to take a random value from the Gaussian distribution keeping in mind the various different probabilities. How can i do that?
1 Comment
John D'Errico
on 21 May 2023
Edited: John D'Errico
on 21 May 2023
I think you don't understand what a gaussian distribution means. The mean and standard deviation of those points do not imply a Gaussian that has the same distribution as that set of heights. The mean and variance will be known, but those points do not follow a Gaussian.
As such, you are not taking those probabilities into account, IF you use that normal PDF in y.
If instead, what you really want to do is sample from the given distribution, then you could use a discrete distribution, with the specific probabilities.
Answers (2)
Image Analyst
on 22 May 2023
r = mu + s * randn(1000, 1); % 1000 random numbers
John D'Errico
on 22 May 2023
Edited: John D'Errico
on 22 May 2023
Please stop asking the same question. There is NO way to know what distribution any set of data comes from. You can use tools to fit a distribution to some data. But that does not prove it is the true distribution. And even when you do, for example, fit a normal distribution to your data, that won't insure that the random samples from that normal distribution have the same distribution as your data.
You CAN decide to use a discrete distribution. Here, for example, you have a sample where 1.70 arises 3 times as often as the others.
help datasample
x = [1.50 1.60 1.70 1.70 1.70 1.80 1.90];
xsam = datasample(x,20)
So the vector x contains ONLY elments which lie in the original data set. And they will have the same relative frequency. It is my guess this what you want.
histogram(datasample(x,1000000),'norm','pdf')
But if you use a normal approximation to that distribution, the relative frequencies of the data will not match the original data set at all well.
[muhat,sigmahat] = normfit(x)
fplot(@(x) normpdf(x,muhat,sigmahat),[1.4,2])
hold on
histogram(x,5,'norm','pdf')
As you can see, a normal pdf fits that data like I fit into the suit I wore when I got married.
Categories
Find more on Exploration and Visualization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
