# Define a density function and draw N samples of it

7 ビュー (過去 30 日間)
Orr Streicher 2021 年 4 月 26 日
コメント済み: Jeff Miller 2021 年 4 月 27 日
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

サインインしてコメントする。

### 回答 (2 件)

Jeff Miller 2021 年 4 月 26 日
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 件のコメント表示非表示 1 件の古いコメント
Jeff Miller 2021 年 4 月 27 日
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.)

サインインしてコメントする。

Paul 2021 年 4 月 27 日
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.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!