Random Numbers from Stationary Distribution
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (2)
Image Analyst
el 23 de Abr. de 2022
0 votos
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.
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
Categorías
Más información sobre Univariate Discrete Distributions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
