generating random numbers from binomial distribution
古いコメントを表示
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
採用された回答
その他の回答 (1 件)
the cyclist
2012 年 1 月 23 日
0 投票
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 件のコメント
Atakan
2012 年 1 月 23 日
the cyclist
2012 年 1 月 23 日
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?
カテゴリ
ヘルプ センター および File Exchange で Binomial Distribution についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!