如何随机化矢量而不重复预定义三元组中的特定元素?

fre*_*ist 6 conditional r sample

我从一个据称简单的设置开始,结果变得非常具有挑战性:

比如,我们有一个碗,其中包含W = 60个白球,B = 10个蓝色球,G = 10个绿球和Y = 10个黄色球.现在我开始从那个碗里画出三角形并存放它们,直到碗是空的.但是,有一条规则:

规则:

每个三联体可能不包含多个相同颜色的非白球!

完成后,我对分别为0,1,2和3个非白球的三元组的比例感兴趣.

为了解决这个问题,我开始考虑绘制和拒绝样本,直到有一个样本,它填满了上面的规则.

我试过这个(希望可重复的)代码:

W = rep(0, times = 60)
BGY = c(rep(1, times = 10),rep(2, times = 10),rep(3, times = 10))
sumup = matrix(c(rep(1,times=3)),byrow=FALSE)
OUTPUT = c(0,0,0,0) 

getBALLS = function(W,BGY){
  k = 0
  while (k == 0){
    POT = c(W, BGY)
    STEPS = (length(W) + length(BGY))/3 
    randPOT <<- sample(POT, STEPS*3, replace=FALSE)
    for(j in 1:STEPS){
      if (.subset2(randPOT,3*j-2)!=.subset2(randPOT,3*j-1) &&
          .subset2(randPOT,3*j-2)!= .subset2(randPOT,3*j) && 
          .subset2(randPOT,3*j-1)!=.subset2(randPOT,3*j)){
        next
      }
      else getBALLS(W, BGY)
    }
    k = 1
  }
  TABLES = matrix(randPOT, nrow=3, byrow=FALSE)
  Bdistr = t(TABLES) %*% sumup
  for(i in 1:STEPS){
    if (.subset2(Bdistr,i)==1) OUTPUT[1] <<- .subset2(OUTPUT,1)+1
    else if (.subset2(Bdistr,i)==0) OUTPUT[4] <<- .subset2(OUTPUT,4)+1
    else if (.subset2(Bdistr,i)==2) OUTPUT[2] <<- .subset2(OUTPUT,2)+1
    else OUTPUT[3] <<- .subset2(OUTPUT,3)+1
  }
  rOUTPUT = OUTPUT/ STEPS
  return(rOUTPUT)
}    

set.seed(1)
getBALLS(W,BGY)
Run Code Online (Sandbox Code Playgroud)

不幸的是我遇到了两个问题

  1. 循环迭代次数太多了!似乎规则经常被违反,这使得以这种方式进行抽样可能是不可行的.
  2. 虽然我试图调用最有效的函数,但是当有多种方法(例如.subset2调用)时,我有一种感觉,这个代码在解决这个问题时非常低效.

接下来我尝试了两阶段采样(更具体的是mstage来自sampling包的功能):

Stage1 = c( rep(0,12), rep(1,3), rep(2,3) )
Stage2 = c( rep(0,12), rep(1,3), rep(2,3) )
b = data.frame(Stage1, Stage2)
probs = list( list( (1/12) , (1/3), (1/3) ), list( rep(1/12,12),rep(1/3,3),rep(1/3,3) ) )
m = mstage( b, stage = list("cluster","cluster"), varnames = list("Stage1","Stage2"), 
            size = list(3,c(1,1,1)), method = "systematic", pik = probs)
Run Code Online (Sandbox Code Playgroud)

虽然这也没有成功,但我也觉得这种方法不适合我的问题!

总而言之,在我看来,我有点像使用大锤来破解坚果,我觉得有一种更有效的方法来解决这个问题(特别是因为我之后想要进行一些蒙特卡罗模拟).

我很感激任何帮助!提前致谢!

Hen*_*nry 2

这是一种替代方法,毫无疑问可以改进,但我认为它具有某种统计意义(在三个样本中具有特定颜色使得另一种颜色不太可能在同一个三个样本中出现)。

coloursinsamples <- function (W,B,G,Y){
    WBGY <- c(W,B,G,Y)
    if(sum(WBGY) %% 3 != 0){ warning("cannot take exact full sample") }
    numbersamples <- sum(WBGY) / 3 
    if(max(WBGY[2:4]) > numbersamples){ warning("too many of a colour") }

    weights <- rep(3,numbersamples)
    sampleB <- sample(numbersamples, size=WBGY[2], prob=weights)
    weights[sampleB] <- weights[sampleB]-1
    sampleG <- sample(numbersamples, size=WBGY[3], prob=weights)
    weights[sampleG] <- weights[sampleG]-1
    sampleY <- sample(numbersamples, size=WBGY[4], prob=weights)
    weights[sampleY] <- weights[sampleY]-1

    numbercolours <- table(table(c(sampleB,sampleG,sampleY)))
    result <- c("0" = numbersamples - sum(numbercolours), numbercolours)
    if(! "1" %in% names(result)){ result <- c(result, "1"=0) }
    if(! "2" %in% names(result)){ result <- c(result, "2"=0) }
    if(! "3" %in% names(result)){ result <- c(result, "3"=0) }
    result[as.character(0:3)]
    }

set.seed(1)
coloursinsamples(6,1,1,1)
coloursinsamples(60,10,10,10)
coloursinsamples(600,100,100,100)
coloursinsamples(6000,1000,1000,1000)
Run Code Online (Sandbox Code Playgroud)