gpt4 book ai didi

r - R 中的 Monty Hall 游戏,带有基本函数

转载 作者:行者123 更新时间:2023-12-04 09:13:58 26 4
gpt4 key购买 nike

只是为了好玩和训练 R,我试图证明 Monty Hall 游戏规则(打开一扇门后改变你的选择会让你更有可能获胜),我制作了这个可重现的代码(每一步的解释都在代码中):

## First I set the seed

set.seed(4)

## Then I modelize the presence of the prize as a random variable between gates 1,2,3


randomgates <- ceiling(runif(10000, min = 0, max = 3))

## so do I with the random choice.

randomchoice <- ceiling(runif(10000, min = 0, max = 3))

## As the opening of a gate is dependent from the gate you chose (the gate you chose cannot be opened)
## I modelize the opening of the gate as a variable which cannot be equal to the choice.

options <- c(1:3)

randomopen <- rep(1,10000)

for (i in 1:length(randomgates)) {
realoptions <- options[options != randomchoice[i]]
randomopen[i] <- realoptions[ceiling(runif(1,min = 0, max = 2))]
}

##Just to make data more easy to handle, I make a dataset

dataset <- cbind(randomgates, randomchoice, randomopen)

## Then I creat a dataset which only keeps the realization of the games in which we carry on (
## the opened gate wasn't the one with the price within)

steptwo <- dataset[randomopen != randomgates,]

## The next step is just to check if the probability of carry on is 2/3, which indeed is

carryon <- randomopen != randomgates

sum(carryon)/length(randomgates)

## I format the dataset as a data frame

steptwo <- as.data.frame(steptwo)

## Now we check what happens if we hold our initial choice when game carries on

prizesholding <- steptwo$randomgates == steptwo$randomchoice

sum(prizesholding)

## creating a vector of changing option, dependant on the opened gate, in the dataset that
## keeps only the cases in which we carried on playing (the opened gate wasn't the one with the prize)

switchedchoice <- rep(1,length(steptwo$randomgates))

for (i in 1:length(steptwo$randomgates)) {
choice <- options[options != steptwo$randomchoice[i]]
switchedchoice[i] <- choice[ceiling(runif(1,min = 0, max = 2))]
}

## Now we check how many times you guess the prize gate when you switch your initial choice

prizesswitching <- steptwo$randomgates == switchedchoice

sum(prizesswitching)/length(steptwo$randomgates)

在游戏进行的情况下,当我在不改变我最初选择的情况下检查概率时(开门与奖品不匹配),我得到了我所期望的(接近中奖概率的 1/3) ,它指的是以下指令:
carryon <- randomopen != randomgates

sum(carryon)/length(randomgates)

当我在更改选择后检查中奖概率时出现了问题(有条件,显然没有打开持有奖品的门),而不是像蒙蒂霍尔所说的那样获得 1/2,而是获得 1/4,它引用以下指令:
prizesswitching <- steptwo$randomgates == switchedchoice

sum(prizesswitching)/length(steptwo$randomgates)

我知道我在做坏事,因为 Monty Hall 已经证明了这一点,但我无法检测到缺陷。有谁知道它是什么?

如果您不知道 Monty Hall 问题是什么,您可以在 wikipedia 上找到易于阅读的信息:

Monty Hall Game

编辑:正如@Dason 指出的,问题之一是我在更改初始选择时引入了某种随机性,这没有意义,因为只剩下一个选项。

另一个问题是,我没有在蒙蒂霍尔知道奖品在哪里的假设下接近这个问题。我把我的代码从初始改成了这个,问题解决了:
# Prepare each variable for 10000 experiments

## First I set the seed

set.seed(4)

## Then I modelize the presence of the prize as a random variable between gates 1,2,3


randomgates <- ceiling(runif(10000, min = 0, max = 3))

## so do I with the random choice.

randomchoice <- ceiling(runif(10000, min = 0, max = 3))

## As the opening of a gate is dependent from the gate you chose (the gate you chose cannot be opened
##, neither the one with the prize does), I modelize the opening of the gate as a variable which cannot be equal to the choice.

options <- c(1:3)

randomopen <- rep(1,10000)

for (i in 1:length(randomgates)) {
randomopen[i] <- options[options != randomchoice[i] & options != randomgates[i]]
}

##Just to make data more easy to handle, I make a dataset

dataset <- cbind(randomgates, randomchoice, randomopen)

## I format the dataset as a data frame

steptwo <- as.data.frame(dataset)

## Now we check what happens if we hold our initial choice when game carries on

steptwo$prizesholding <- steptwo$randomgates == steptwo$randomchoice

with(steptwo, sum(prizesholding))

## creating a vector of changing option, dependant on the opened gate, in the dataset that
## keeps only the cases in which we carried on playing (the opened gate wasn't the one with the prize)

steptwo$switchedchoice <- rep(1,length(steptwo$randomgates))

for (i in 1:length(steptwo$randomgates)) {
steptwo$switchedchoice[i] <- options[options != steptwo$randomchoice[i] & options != steptwo$randomopen[i]]
}

## Now we check how many times you guess the prize gate when you switch your initial choice

steptwo$prizesswitching <- steptwo$randomgates == steptwo$switchedchoice

with(steptwo, sum(prizesswitching)/length(randomgates))

最佳答案

每一轮,都有一个 Prize_door 和一个 selected_door。 Monty Hall 将打开一个不是 Prize_door 或 selected_door 的门(setdiff 在 1:3 和向量 (prize_door, selected_door) 之间,如果 setdiff 是两个元素,则在两者之间随机选择)。那么开关门就是没有选择或打开的门。

n <- 1e4
set.seed(2020)
df <-
data.frame(
prize_door = sample(1:3, n, replace = TRUE),
chosen_door = sample(1:3, n, replace = TRUE))

df$opened_door <-
mapply(function(x, y){
available <- setdiff(1:3, c(x, y))
available[sample(length(available), 1)]
}, df$prize_door, df$chosen_door)

df$switch_door <-
mapply(function(x, y) setdiff(1:3, c(x, y)),
df$chosen_door, df$opened_door)



with(df, mean(prize_door == chosen_door))
# [1] 0.3358
with(df, mean(prize_door == switch_door))
# [1] 0.6642

随着 n 增加的概率图
probs <- 
data.frame(
chosen_p = with(df, cumsum(prize_door == chosen_door))/(1:n),
switch_p = with(df, cumsum(prize_door == switch_door))/(1:n))

plot(probs$switch_p, type = 'l', ylim = c(0, 1))
lines(probs$chosen_p, col = 'red')
abline(h = 1/3)
abline(h = 2/3)

enter image description here

关于r - R 中的 Monty Hall 游戏,带有基本函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59684522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com