Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Вероятность того, что взятая красная таблетка была из правой руки — ?% (100% (судя по дальнейшему описанию) из 56.52% (то есть 56.52 %).

function test1()
{
define('LEFT', 0);
define('RIGHT', 1);
define('BLUE', '0000FF');
define('RED', 'FF0000');
$hands = [];
$hands[LEFT] = [];
$hands[RIGHT] = [];
$config = [
LEFT => [BLUE => 7, RED => 3],
RIGHT => [BLUE => 5, RED => 8],
];
foreach ($config as $hand => $handConfig) {
foreach ($handConfig as $color => $tabletCount) {
for ($i = 0; $i < $tabletCount; $i++) {
$hands[$hand][] = ['color' => $color, 'hand' => $hand];
}
}
}
$totalCount = 100000;
$redTabletCount = 0;
$rightHandCount = 0;
for ($i = 0; $i < $totalCount; $i++) {
$hand = mt_rand(LEFT, RIGHT);
$tablets = $hands[$hand];
$tablet = $tablets[mt_rand(0, count($tablets) - 1)];
if ($tablet['color'] == RED) {
$redTabletCount++;
if ($hand == RIGHT) {
$rightHandCount++;
}
}
}
echo $rightHandCount.' / '.$redTabletCount.' = '.($rightHandCount * 100 / $redTabletCount).'%';
}
function test2()
{
define('LEFT', 0);
define('RIGHT', 1);
define('BLUE', '0000FF');
define('RED', 'FF0000');
$hands = [];
$hands[LEFT] = [];
$hands[RIGHT] = [];
$config = [
LEFT => [BLUE => 7, RED => 3],
RIGHT => [BLUE => 5, RED => 8],
];
foreach ($config as $hand => $handConfig) {
foreach ($handConfig as $color => $tabletCount) {
for ($i = 0; $i < $tabletCount; $i++) {
$hands[$hand][] = ['color' => $color, 'hand' => $hand];
}
}
}
$totalCount = 100000;
$redTabletCount = 0;
$rightHandCount = 0;
for ($i = 0; $i < $totalCount; $i++) {
//$hand = mt_rand(LEFT, RIGHT); // changed
$tablets = array_merge($hands[LEFT], $hands[RIGHT]); // changed
$tablet = $tablets[mt_rand(0, count($tablets) - 1)];
if ($tablet['color'] == RED) {
$redTabletCount++;
if ($tablet['hand'] == RIGHT) { // changed
$rightHandCount++;
}
}
}
echo $rightHandCount.' / '.$redTabletCount.' = '.($rightHandCount * 100 / $redTabletCount).'%';
}
set.seed(123) # to reproduce results with RNG
sample.size<-1000000
pills.1<-sample(c("blue_left", "red_left", "blue_rigth", "red_right"), sample.size, TRUE, c(7/23, 3/23, 5/23, 8/23))
1/(1+sum(pills.1=="red_left")/sum(pills.1=="red_right"))
pills.2<-sample(c("blue_left", "red_left", "blue_rigth", "red_right"), sample.size, TRUE, c(7/20, 3/20, 5/26, 8/26))
1/(1+sum(pills.2=="red_left")/sum(pills.2=="red_right"))
Получив ответ, я заметил, что он один в один совпадает...
В ходе обсуждения задачи часто возникает тенденция принять за должное, что вероятность получения таблетки из правой и левой руки 50/50. На самом деле в самой задаче этого нигде не сказано. Мы интуитивно ожидаем такого подхода к методу выбора таблетки из руки. На практике же про то, как была выбрана таблетка ничего не сказано. Значит и придумывать отсебятину не стоит.В таком случае вероятность взять таблетку из правой руки такая же как вероятность встретить на улице динозавтра — 50% :-)
Байес и задача про Морфеуса