writing if statement using probabilities

1 回表示 (過去 30 日間)
Jessie Ray
Jessie Ray 2017 年 11 月 6 日
編集済み: Jessie Ray 2017 年 11 月 6 日
ds

採用された回答

Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2017 年 11 月 6 日
Assuming you already have y, ymaybe, t and s:
if t > s
y = ymaybe; % ymaybe becomes the new y
else
pr = exp(-s + t);
my_randomnumber = rand;
if my_randomnumber < pr
y = ymaybe; % ymaybe becomes the new y with probability exp(-s+t)
end
end
The first part (t>s) is obvious. The second part is slightly tricky: whenever you want an event to happen with probability p, you simply draw a random number from a uniform distribution between 0 and 1 using rand function. Since the CDF of a uniform distribution U[0,1] grows linearly with unit slope, this random number will be less than p with probability p. Since you want to switch ymaybe for y with probability p, all you need to do is assign y=ymaybe if this random number is less than p. This trick can be handy in many places, so make sure you understand why this works.
  1 件のコメント
Jessie Ray
Jessie Ray 2017 年 11 月 6 日
awesome, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by