简体   繁体   English

R的箱子模拟中的球

[英]Balls in Bins simulation for R

I would like to simulate the distribution for a fixed number of balls m in a fixed number of bins n in R. Up till now I have been using the Poisson approximation with rpois() . 我想模拟固定数量的球分布m的固定数目个二进制位n在R.到现在为止我一直在使用泊松逼近rpois() This is a decent approximation for a large number of balls in n bins. 对于n箱中的大量球来说,这是一个不错的近似值。

However, rpois() only allows you to indicate a rate lambda , which is m/n . 但是, rpois()仅允许您指示速率lambda ,即m/n As a consequence, the number of positive bins is often smaller than the number of balls. 结果,正箱的数量通常小于球的数量。

Would anybody know of a function or script that allows me to randomly distribute balls into bins? 有人会知道允许我随机将球分配到箱子中的功能或脚本吗?

Ultimately I seek to calculate the confidence intervals of -log(empty bins/total bins) by bootstrapping. 最终,我试图通过自举来计算-log(empty bins/total bins)的置信区间。 This problem is 'breaking my balls' so to speak. 这个问题可以说是“打破我的球”。

I think you want the multinomial distribution. 我想你想要多项分布。

Here's a quick function - we take m balls in n bins, and give x results, returning a vector of your metric for each of the x trials: 这是一个快速的功能 - 我们在n个箱子中取m个球,然后给出x个结果,为每个x试验返回一个度量向量:

myfunc <- function(m,n,x){
  out <- rmultinom(x,m,rep(1,n))
  -log(colSums(out == 0)/n)
}

myfunc(10,40,10)
[1] 0.1923719 0.2548922 0.2231436 0.2548922 0.2876821 0.2876821 0.2231436 0.2231436 0.2231436 0.2548922

You can then get the quantiles/Confidence intervals: 然后,您可以获得分位数/置信区间:

out = myfunc(10,40,1000)
quantile(out, c(0.05,0.95))
       5%       95% 
0.1923719 0.2876821 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM