フィルターのクリア

Write a program to produce the probability density function of binomial distribution

2 ビュー (過去 30 日間)
Vin Sen  Lee
Vin Sen Lee 2016 年 1 月 17 日
コメント済み: Walter Roberson 2016 年 1 月 17 日
i.e. a function that only returns 2 possible values(0 and 1). The probability of returning 0 is p, the probability returning 1 is q. where p + q = 1

回答 (1 件)

Star Strider
Star Strider 2016 年 1 月 17 日
One approach:
R = @(p,n) rand(1,n) > (1 - p); % Probability = p, Number Of Trials = n
Trials = R(0.2, 50);
  2 件のコメント
Vin Sen  Lee
Vin Sen Lee 2016 年 1 月 17 日
hi could you please explain your code. I don't really get it. thank you
Star Strider
Star Strider 2016 年 1 月 17 日
編集済み: Star Strider 2016 年 1 月 17 日
The rand functions produces uniformly-distributed random numbers from 0 to 1. My little function tests to see if that value is greater than ‘q’, and if so, defines as a logical value of 1. Otherwise, it is defined as zero. (Logical values are automatically converted to double values if you use them in a calculation.)
The rand function can produce a vector of such random values, so I added the ‘n’ argument to tell it how long a vector to produce. (The ‘n’ value must be an integer.) This avoids your needing to call it in a loop to generate a vector of random values. If you only need one value, set ‘n’ to 1.
EDIT — To clarify, my function does not generate a binomial distribution. It yields a Bernoulli trial. A binomial distribution is the sum of such trials. See the binornd function for binomial random numbers.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by