Random Numbers from Stationary Distribution

I have a set of pre-generated probabilities, ie. a=1/120, b=1/120 etc. and I need to generate n random numbers from the set [1:100] that follows the distribution of the pre-generated probabilities.
N = (1:100)
GenerateRandomNumbers = rand(1,N)
I'm struggling since rand only likes scalar entries and I don't know how to make n scalar. I also don't know how to fit the numbers to the probabilities I have.
D1 = 1/120;
D2 = 1/120;
D3 = 1/120;
D4 = 1/120;
D5 = 1/120;
D6 = 1/120;
D7 = 1/120;
D8 = 1/120;
D9 = 1/120;
D10 = 1/120;
D11 = 1/120;
D12 = 1/120;
D13 = 1/120;
D14 = 1/120;
D15 = 1/120;
D16 = 1/120;
D17 = 1/120;
D18 = 1/120;
D19 = 1/120;
D20 = 1/120;
D21 = 1/60;
D22 = 1/60;
D23 = 1/60;
D24 = 1/60;
D25 = 1/60;
D26 = 1/60;
D27 = 1/60;
D28 = 1/60;
D29 = 1/60;
D30 = 1/60;
D31 = 1/120;
D32 = 1/120;
D33 = 1/120;
D34 = 1/120;
D35 = 1/120;
D36 = 1/120;
D37 = 1/120;
D38 = 1/120;
D39 = 1/120;
D40 = 1/120;
D41 = 1/120;
D42 = 1/120;
D43 = 1/120;
D44 = 1/120;
D45 = 1/120;
D46 = 1/120;
D47 = 1/120;
D48 = 1/120;
D49 = 1/120;
D50 = 1/120;
D51 = 1/120;
D52 = 1/120;
D53 = 1/120;
D54 = 1/120;
D55 = 1/120;
D56 = 1/120;
D57 = 1/120;
D58 = 1/120;
D59 = 1/120;
D60 = 1/120;
D61 = 1/120;
D62 = 1/120;
D63 = 1/120;
D64 = 1/120;
D65 = 1/120;
D66 = 1/120;
D67 = 1/120;
D68 = 1/120;
D69 = 1/120;
D70 = 1/120;
D71 = 1/60;
D72 = 1/60;
D73 = 1/60;
D74 = 1/60;
D75 = 1/60;
D76 = 1/60;
D77 = 1/60;
D78 = 1/60;
D79 = 1/60;
D80 = 1/60;
D81 = 1/120;
D82 = 1/120;
D83 = 1/120;
D84 = 1/120;
D85 = 1/120;
D86 = 1/120;
D87 = 1/120;
D88 = 1/120;
D89 = 1/120;
D90 = 1/120;
D91 = 1/120;
D92 = 1/120;
D93 = 1/120;
D94 = 1/120;
D95 = 1/120;
D96 = 1/120;
D97 = 1/120;
D98 = 1/120;
D99 = 1/120;
D100 = 1/120;
Here I've got the probabilities that X=1, X=2 etc. I have the D there just to keep tabs on what's a number and what is X = . I don't know if MatLab likes P(X=1) = 1/120
How do I make the randomly generated numbers follow the probabilities given above?

 Réponse acceptée

John D'Errico
John D'Errico le 23 Avr 2022
Modifié(e) : John D'Errico le 23 Avr 2022
Simpler to accomplish than others have said. But first, do NOT define those probabilities as separate variables!!!!!!!!!!!!!!!!!!!!!!! That makes it nearly impossible to accomplish. LEARN TO USE VECTORS AND MATRICES IN MATLAB.
D = repmat(1/120,1,100);
D(21:30) = 1/60;
D(71:80) = 1/60;
Next, test that your probabilities sum to 1.
sum(D)
ans = 1.0000
Good. They do.
Next, you want to generate random numbers, N of them.
cumulativeD = cumsum([0,D]); % best for the rest of our work, if we append a 0 at the beginning.
N = 1000000;
r = rand(N,1);
x = discretize(r,cumulativeD);
Does X follow the indicated distribution?
histogram(x,100,'norm','pdf')
And that looks just about what I would expect for a random sample of that size, for this distribution. As you can see, generating the samples took not much more than a call to discretize, and it is fully vectorized.
But no, MATLAB does not like something like P(X=1) = 1/120. You really just need to learn MATLAB. Start with the basic tutorials.

1 commentaire

Lou
Lou le 23 Avr 2022
How would I merge the histogram for 10^4 trials?
I really struggle with making loops remember what they've done and adding to it. Aside from counting the number of times a number comes up individually and makiong a histogram of that, I don't know how to do it.

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 23 Avr 2022

0 votes

You need to do inverse transform sampling. Not sure it MATLAB has anything built-in for that but you can do it "manually" like I did for a rayleigh distribution in the attached demo, and is described here:
Basically you need to get the CDF of your specified distribution and use that to derive the distribution samples. See my demo for a worked out example. I trust you can adapt it to your situation.
Torsten
Torsten le 23 Avr 2022
Modifié(e) : Torsten le 23 Avr 2022
If you write D as an array
D=[1/120,1/120,...,1/120]
then the following code should work:
DC = cumsum(D);
for i = 1:100
r = rand;
random_number(i) = find(r-DC<=0,1,'first');
end

Catégories

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by