I have started a new book called The Art of R Programming by Norman Matloff and I’m really digging it. I won’t blog about each chapter the way I did Machine Learning for Hackers, but I did come across something I thought made good blog material.

In Chapter 8 (Doing Math and Simulations in R), Matloff presents an example on combinatorial simulation. The motivation for the example is the following problem:

Three committees, of size 3, 4 and 5, are chosen from 20 people. What is the probability that persons A and B are chosen for the same committee?

Says Matloff: “This problem is not hard to solve analytically, but we may wish to check our solution using simulation…” I suppose it’s not that hard, but if you don’t think about these kinds of problems on a regular basis they can trip you up. He doesn’t give the analytic solution but does provide code for a neat simulation of it:

# 8.6.3 combinatorial simulation

sim <- function(nreps) {commdata <- list()commdata$countabsamecomm <- 0for (rep in 1:nreps) {commdata$whosleft <- 1:20commdata$numabchosen <- 0commdata <- choosecomm(commdata,5)if (commdata$numabchosen > 0) nextcommdata <- choosecomm(commdata,4)if (commdata$numabchosen > 0) nextcommdata <- choosecomm(commdata,3)}print(commdata$countabsamecomm/nreps)
}

choosecomm <- function(comdat,comsize) {committee <- sample(comdat$whosleft, comsize)comdat$numabchosen <- length(intersect(1:2,committee))if (comdat$numabchosen == 2)comdat$countabsamecomm <- comdat$countabsamecomm + 1comdat$whosleft <- setdiff(comdat$whosleft,committee)return(comdat)
}

That’s quite a bit of code. The first block is the main function, called “sim”. The second block is the other function, “choosecomm”, that gets called in the first function. The whole thing makes creative use of a list and the intersect() and setdiff() functions. If you want to simulate selecting three committees of 3, 4 and 5 people 10,000 times and see how many time persons 1 and 2 are on the same committee, enter sim(100000) at the command line. You should get about 0.10.

Now how do we solve this using math and probability? First off, instead of 20 people, I like to think of 20 chips, 18 of which are white and 2 that are red. Imagine they’re in a bag and we reach in and scoop out 12 and immediately and randomly divide into three groups of 3, 4 and 5. What is the probability one of those groups has both red chips? To calculate this we need to enumerate the possible selections that include both red chips and divide that by all possible selections . First let’s do it for the committee of 5. All possible combination of 5 from 20 is . All possible combinations including both red chips are . We can calculate this in R using the choose() function as follows:

c5 <- choose(18,3)/choose(20,5)

We then repeat this for the other two committees of 4 and 3, like so:

c4 <- choose(18,2)/choose(20,4)
c3 <- choose(18,1)/choose(20,3)

Summing this up we get c3 + c4 + c5 = 0.10. Now it may seem strange that we always set up our denominator as if we’re drawing from 20. After all, in the simulation above, there’s a specific order. First 5 are chosen. If the two people are not both on that committee, then we draw 4 and then 3. It seems that should be taken into account when you solve this mathematically. But you don’t have to. That’s why I like to imagine just scooping 12 people up and assigning instant membership to them on a random basis. While that’s not how committee selection usually happens in “real life” it makes thinking about an analytic solution easier.

引用: http://www.clayford.net/statistics/a-combinatorial-simulation/

转载于:https://www.cnblogs.com/jinyulogin/archive/2013/06/11/3131790.html

A Combinatorial Simulation相关推荐

  1. 第四讲:debugging simulation mismatches

    关于竞争冒险: 1.use +race utility to locate race condition code **** 2.use $vcdplusdeltacycleon to locate ...

  2. CVPR2021满分论文 | GeoSim: Camera Simulation

    作者丨陈云@知乎 来源丨https://zhuanlan.zhihu.com/p/377570852 编辑丨3D视觉工坊 <GeoSim: Realistic Video Simulation ...

  3. Badread: simulation of error-prone long reads

    Badread: simulation of error-prone long reads     Badread:模拟容易出错的长read Ryan R Wick1 1 Department of ...

  4. Plant Simulation常用命令

    Plant Simulation 是面向对象的三维离散事件仿真软件,使您能够快速.直观地构建逼真的物流模型. 您还可以使用高级统计工具执行复杂的生产分析.以下介绍该软件的一些常用命令. 1. clea ...

  5. 英语单词 voltage simulation synthesize junction asynchronous mega optimize

    voltage 英 [ˈvəʊltɪdʒ]美 [ˈvoʊltɪdʒ] n. 电压;伏特数 [例句] The output voltage rises, compensating for the ori ...

  6. Network simulation using OPNET

    When using OPNET for simulating, 2T(topology and traffic),2S(statistics and simulation),and 2R(re-si ...

  7. Vivado simulation使用简介

    作为FPGA入门小白,使用vivado simulation进行仿真分析是必不可少的,但是对simulation界面的使用一直不是很熟悉,现在此做详细的总结. 基本操作 Vivado Simulato ...

  8. 文献记录(part#)--Automatic diagnosis of ECG disease based on intelligent simulation modeling

    学习笔记,仅供参考,有错必究 文章目录 Automatic diagnosis of ECG disease based on intelligent simulation modeling 摘要 I ...

  9. SAP CRM Fiori应用Simulation pipeline的刷新问题

    在Simulation pipeline里修改opportunity的status之后,有两种方式trigger OData update request到后来来保存这个change: 最直接的就是点 ...

最新文章

  1. 聊聊Cassandra的FailureDetector
  2. LeetCode Kth Smallest Element in a BST(中序遍历)
  3. distance在函数 int_TensorFlow函数:tf.losses.cosine_distance
  4. 收藏:软件开发、软件收藏
  5. 选购光纤交换机时需要注意光模块的哪些配置?
  6. 【计算机网络复习 数据链路层】3.5.2 ALOHA协议
  7. Java加密与解密的艺术~DES实现
  8. 路由技术(来自百度百科)
  9. 关于TCP三次握手过程
  10. Javascript与未来十年的数据编程
  11. Blueprint:一个让你获取示例代码的Flash Builder扩展
  12. 详细安装 kali 教程 和 基本命令使用
  13. WebView---Android与Javascript交互之入门
  14. Fishermen(2018 ICPC SouthEastern European)
  15. 为什么qq不出linux版本,腾讯QQ官方Linux版本要发布了
  16. eot文件html,html – 为什么IE8没有加载我的eot文件?
  17. 阿里云被攻击封多久,又该怎么解决?
  18. web程序设计(5)——个人主页(布局)
  19. 【节目单】科幻春晚,陪你过年!
  20. 深入理解JVM:java对象的创建过程?

热门文章

  1. 为什么叫python编程-月薪上万的Python编程,为什么你还没入门就放弃了?
  2. python爬虫实例-Python爬虫案例集合
  3. python3.6安装pygame-python3.6 安装pygame
  4. python自动化办公 百度网盘-Python自动化办公实战教程2020最新带源码
  5. php和python哪个工资高-Java、Python、Php学哪个好,哪个更有前景?
  6. python网络爬虫教程-终于明了python网络爬虫从入门到实践
  7. python恶搞代码-搞笑的程序猿:看看你是哪种Python程序员
  8. python语言特点有哪些-python的优点和缺点是什么?
  9. python对象编程例子-python面向对象编程练习
  10. python代码需要背吗-Python代码需要缩进吗