generating random variable between 0 and 1 with probabilities

조회 수: 11 (최근 30일)
william Smith
william Smith 2019년 3월 10일
댓글: william Smith 2019년 3월 10일
Hi all,
I have two questions please:
1) how do i generate a random number either 0 or 1 with equal chances of accept and reject 50-50 chance?
2) how do i generate a random number either 0 or 1 with percent shift in probablity in order to accept "bias' between -0.5 and 0.5
if percent shift=0 then 50-50 chance of accept
if percent shift=0.1 then 60% accept - 40% reject
if percent shift=0.2 then 70% accept - 30% reject
if percent shift=0.3 then 80% accept - 20% reject
if percent shift=0.4 then 90% accept - 20% reject
if percent shift=0.5 then 100% accept - 0% reject
if percent shift=-0.1 then 40% accept - 60% reject
if percent shift=-0.2 then 30% accept - 70% reject
if percent shift=-0.3 then 20% accept - 80% reject
if percent shiftt=-0.4 then 10% accept - 90% reject
if percent shift=-0.5 then 0% accept - 100% reject
Thanks!
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 3월 10일
If the percent_shift is 0.4 then you total 110% probability.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 3월 10일
rand() < (percent_shift + 0.5)
  댓글 수: 5
Walter Roberson
Walter Roberson 2019년 3월 10일
The assignment to i before the function definition is in a different workspace than the function uses. Inside your function, both i (lower-case I) and l (lower-case L) are undefined. You also switch back and forth between using i or l as the subscript.
You assign a value to choice_ in the case of bias 0.5 but you never use that variable.
You only assign a value to the output variable choice_1 in the case that the input bias is -0.5
You try to test 0.5 and -0.5 for equality using = as the comparison operator, but = is only ever assignment (except some cases having to do with older versions of the symbolic toolbox.) == is the comparison test.
You input customer_1 but you never use it.
You use choice_1 to pass a size argument to rand() but you have not defined choice_1 by that point.
It is not clear what your intended output is. Is it the random variable such as 0.013433807 ? Is it 0.0 or 0.1 (double precision numbers) ? Is it uint8(0) or uint8(1) ? Is it false or true (logical values) ?
Why are you indexing bias? Are you expecting it to be a vector of values?
Each of your cases appears to have the same action except for 0.5 and -0.5 . Why not just test those two specifically, like
if bias(i) == 0.5
choice_ = 1;
elseif bias(i) == -0.5
choice_1 = 0;
else
rand(choice_1) < (bias(i) + 0.5)
end
william Smith
william Smith 2019년 3월 10일
Awsesome!
Thank you so much!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Regression에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by