Monty Hall Simulations
2016-02-13 10:00
591 查看
The Monty Hall Problem
There’ve been many simulations of the Monty Hall-problem done in R. But since I’m trying to learnR, I wanted to try to simulate the paradox over many different trails and plot them all using ggplot2.The problem was originally posed as follows (from wikipedia):
“Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1 [but the door is not opened], and the host, who knows what’s behind the doors, opens another door, say
No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?”
Writing the R code
Most of you already now what strategy is best. But to try it out I wrote the following code:door we must switch to by 2 + (winning.door.matrix != 2). We can never choose to switch to either door 2 or 3 in the same game. Because if door 2 is the winner, door 3 will be opened by the host and if door 3 is the winner, door 2 will be opened by
the host. Therefore, if we switch to 2 and 2 is the winner the condition in this calculation will be FALSE, hence 2 will be added to switch.door.matrix. And, likewise, if door 2 is not the winner, the condition will be TRUE and 3 will
be added toswitch.door.matrix. If the winning door is 1 the host will open either door 2 or 3. And we will add 2 to our matrix, because the condition is not met. Since we record switches in this way, we can calculate winnings by switch.door.matrix
== winning.door.matrix.
Results
I ran the codes using different values for trails (T) and guesses (N). I started with 100 trails x 10 guesses and ended with 100 trails x 10 000, and it clearly demonstrates that we need quite a lot of guesses before the lines converge towards the expectedvalue. The 100 x 10 000 simulation gave a mean probability of winning if you chose not to change door = 0.333701 and 0.666299 if you always switched door. But already at the 100 x 100 plot we can see that it’s always better to switch door.
Monty Hall 100 x 10
Monty Hall 100 x 50
Monty Hall 100 x 100
Monty Hall 100 x 500
Monty Hall 100 x 5000
Monty Hall 100 x 10000
Resources used
For this R session I used the following books:Matloff, N. (2011). The Art of R Programming. San Francisco: No Starch Pess.
Wickham, H. (2009). ggplot2: elegant graphics for data analysis. New York: Springer.
相关文章推荐
- 软件开发技术:DRY
- javascript实现查找数组中最大值方法汇总
- 文章标题
- Mac下VIM配置
- qt调用vc编写库文件的方法
- 线程基础:JDK1.5+(8)——线程新特性(上)
- 程序的组成部分
- win10系统下安装Marvell网卡后电脑无法联网怎么办?
- JavaScript常用数组算法小结
- hdu 1055(贪心)
- hdu3341Lost's revenge (AC自动机+变进制dp)
- Apache Shiro
- HDU 5135 Little Zu Chongzhi's Triangles
- C语言几种整形类型的区别
- Javascript 字符串模板的简单实现
- 从windows客户端sqlplus连接Oracle 12c PDB报错ORA-65162: The password has expired
- 关于博客的头像问题
- 学习笔记
- 如何在Android应用程序中保存数据
- bzoj1692 队列变换