generating random numbers from binomial distribution
Afficher commentaires plus anciens
To generate Bin(4, 3/4), use the following table:
x 0 1 2 3 4
P(X=x) 1/256 3/64 54/256 27/64 81/256
I want to write the MATLAB code to generate 5 random numbers from this distribution. I must use the following algorithm:
Algorithm:
1.Generate u from UNIF(0,1).
2.If P(X <= j-1) <= u <= P(X <= j) then set X=j for j=1,2,3,4
I have written the following code. But I think this code is a general code for generating from Binomial. How can I specialize it to my algorithm?
function X = dene(n,p,N)
X = zeros(1,N); % Generate the uniform random numbers: % N variates of n trials.
U = rand(N,n); % Loop over the rows, finding the number % less than p
for i = 1:N
ind = find(U(i,:) <= p);
X(i) = length(ind);
end
end
Réponse acceptée
Plus de réponses (1)
the cyclist
le 23 Jan 2012
0 votes
I have not looked at your algorithm in detail, but here's a guess. Where you have p inside your loop, I think you might want cumsum( p ) instead. Then you are checking to see if your randomly generated value is less that the cumulative probability distribution, which I think is what you want.
2 commentaires
Atakan
le 23 Jan 2012
the cyclist
le 23 Jan 2012
OK. I'll try to take a closer look. Maybe you could add a little more details about why you think the results are incorrect. How many trials are you running?
Catégories
En savoir plus sur Binomial Distribution dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!