필터 지우기
필터 지우기

Generate samples from a normal distribution

조회 수: 13 (최근 30일)
Kash022
Kash022 2016년 11월 25일
답변: Image Analyst 2016년 11월 25일
Hello,
I have a Gaussian mixture distribution which looks as attached. I now want to sample and generate 1000 samples from this and plot their histogram. I tried using randi function but it does not work. Please let me know how to do this.
y = randi(my_pdf,100); %%my_pdf is my distribution
Thanks!
  댓글 수: 5
Kash022
Kash022 2016년 11월 25일
편집: Kash022 2016년 11월 25일
Its a gaussian mixture distribution with different means. This is the code snippet used to generate it.
clear all;
XX= [-2:0.01:20];
some_rand_noise = 0.1;
my_pdf = zeros(1,length(XX));
for k = 1:256
for ii=1:256
val(ii) = hw(bitxor(ii-1,k-1))+hw(k-1); %%hw is a function used to generate hamming weights i.e no of ones %%%
my_pdf = my_pdf + normpdf(XX,val(ii),some_rand_noise);
end
end
figure();plot(XX,my_pdf/256); hold on;
Image Analyst
Image Analyst 2016년 11월 25일
Please supply the hw function so we can run your code.

댓글을 달려면 로그인하십시오.

답변 (3개)

Image Analyst
Image Analyst 2016년 11월 25일
If you have that function already, then use inverse transform sampling: https://en.wikipedia.org/wiki/Inverse_transform_sampling. In short, compute the CDF of your function and use rand() to pick a value.
I attach an example of how I used it for the Rayleigh distribution.

CarrotCakeIsYum
CarrotCakeIsYum 2016년 11월 25일
randi will generate rando integers.
To generate a random sample from a vector, use randsample. see:
help randsample
I'd imagine you'd want:
y = randsample(my_pdf,100);
To plot a histogram use the 'hist' command.
  댓글 수: 2
Kash022
Kash022 2016년 11월 25일
thanks! but how do I do this for different means? as you can see in my attachment, I have different means,for each of them I need to generate the samples and then plot their histogram. Thanks!
CarrotCakeIsYum
CarrotCakeIsYum 2016년 11월 25일
Sorry I don't follow... your population will only have one mean!
For the JPEG you've posted, the mean will be at 7.5 (since it is a symmetrical distribution).

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 11월 25일
Maybe use randn() to get a list of a few million numbers taken from those 13 distributions (call randn thirteen times), then pick one of the values at random.

Community Treasure Hunt

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

Start Hunting!

Translated by