Define a density function and draw N samples of it

5 views (last 30 days)
Hi,
So i want to define a density function and draw N samples of it. does anyone know how can i do it?
for example if i want a gaussian mixture (with M=4 gaussians with diffrent expectations mu's) i defined:
f1=(1/(2*pi*sigma2*M))*(exp((-1/(2*sigma2))*((vecnorm((X-mu(1,:)).').').^2))+...
exp((-1/(2*sigma2))*((vecnorm((X-mu(2,:)).').').^2))+...
exp((-1/(2*sigma2))*((vecnorm((X-mu(3,:)).').').^2))+...
exp((-1/(2*sigma2))*((vecnorm((X-mu(4,:)).').').^2)));
But im not sure how to chose X such as the samples will represent the distrebution (that is more samples in the center of the gauusians and less in its sides).
The gaussian mixture is just an exaple i am looking for a way to do it to any density function.
Thanks

Answers (2)

Jeff Miller
Jeff Miller on 26 Apr 2021
I don't think that density function is right--seems like you at least need to include the mixture probabilities in there--e.g., 1/4 * each density if the 4 gaussians are equally likely.
You might have a look at Cupid which supports densities and random number generation for mixtures of lots of density functions. It might already do what you want; if not, you might get some useful formulas.
  2 Comments
Orr Streicher
Orr Streicher on 27 Apr 2021
As far as i understand i did the normalization you talked about, with the coeffetiont (1/(2*pi*sigma2*M)) (M=4).
Cupid looks great, i will take a look. thanks!
Jeff Miller
Jeff Miller on 27 Apr 2021
M=4 seems right--I did not notice that.
If you know the cdf's of all your distributions, then the cdf of the mixture at each x is the sum of the cdf_i(x)*p*i values, and you can use the inverse transform sampling technique suggested by Paul.
Alternatively, you can generate random numbers by first choosing one of the distributions randomly with probabilities p_i and then choosing a random number from that distribution. (This is what Cupid does for mixtures.)

Sign in to comment.


Paul
Paul on 27 Apr 2021
The Statistics and Machine Learning Toolbox has lots of typical densities that can be used to generate samples of random variables. If you have a custom-defined probability density function for which you can define the CDF, you can try an inverse transform sampling technique, which can be implemented numerically if you don't have a closed form expression for the CDF.

Tags

Community Treasure Hunt

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

Start Hunting!