- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 R 中编写一个函数来对 Monty Hall 问题执行蒙特卡罗模拟。当门没有开关时,该函数正在工作 switch == FALSE
,但是当我调用 mean(replicate(10000, monty_hall(switch = TRUE)))
时,预期答案约为 0.66
,但实际上我得到了 0.25
左右。
这是该函数的代码:
monty_hall = function(switch = logical()){
doors <- c(1,2,3)
names(doors) <- rep(c("goat", "car"), c(2,1))
prize_door <- doors[3]
guess <- sample(doors, 1)
revealed_door <- sample(doors[!doors %in% c(guess, prize_door)],1)
if(switch){
switched_door <- sample(doors[!doors %in% c(guess, revealed_door)],1)
prize_door == switched_door
} else {
prize_door == guess
}
}
我应该进行哪些更改才能获得大约 0.66
的正确输出?
最佳答案
只需将门向量更改为字符即可
monty_hall = function(switch = logical()){
doors <- c("1","2","3")
names(doors) <- rep(c("goat", "car"), c(2,1))
prize_door <- doors[3]
guess <- sample(doors, 1)
revealed_door <- sample(doors[!doors %in% c(guess, prize_door)],1)
if(switch){
switched_door <- sample(doors[!doors %in% c(guess, revealed_door)],1)
prize_door == switched_door
} else {
prize_door == guess
}
}
假设该人选择了 1 号门,奖品在 2 号门,那么剩下要揭晓的是 3 号门。
您将拥有revealed_door <- sample(3,1)
这并不像你期望的那样工作,这变成 revealed_door <- sample(c(1,2,3),1)
在函数文档中,只需输入 ?sample
If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x)
我认为最简单的解决方法是更改为字符,但如果您必须使用数值,只需检查向量的长度并返回该值(如果为 1),否则进行示例
关于r - R 中 Monty Hall 问题的蒙特卡罗模拟不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56095777/
我正在编写一个 c 脚本来将 pi 近似与 OpenMp 并行化。我认为我的代码运行良好,输出令人信服。我现在用 4 个线程运行它。我不确定的是这段代码是否容易受到竞争条件的影响?如果是,我该如何协调
我现在正在学习拉斯维加斯和蒙特卡洛算法自己,有两个问题可能很简单但我无法回答,如果有人能帮助我......提前谢谢 考虑针对问题 P 的蒙特卡洛算法 A,其预期运行时间在任何大小为 n 的实例上至多为
在 Sutton's book on RL ,在蒙特卡罗政策评估下,他在第 111 页提到注意估计单个状态值的计算费用与状态数量无关。然而,对于蒙特卡洛来说: 状态的平均返回是从第一次遇到该状态时到该
我是一名优秀的程序员,十分优秀!