【问题标题】:Simulating tournament results with R用 R 模拟比赛结果
【发布时间】:2018-07-06 11:11:38
【问题描述】:

a在R,如何运行锦标赛模拟?

我有每支队伍战胜其他组合的概率,例如:

prob_res <- matrix(round(runif(64),2), 8, 8)
prob_res[lower.tri(prob_res, diag = TRUE)] <- 0
prob_res <- as.data.frame(prob_res)
colnames(prob_res) <- 1:8
rownames(prob_res) <- 1:8

意思是这样的:

  1    2    3    4    5    6    7    8
1 0 0.76 0.35 0.81 0.95 0.08 0.47 0.26
2 0 0.00 0.24 0.34 0.54 0.48 0.53 0.54
3 0 0.00 0.00 0.47 0.51 0.68 0.50 0.80
4 0 0.00 0.00 0.00 0.52 0.59 0.38 0.91
5 0 0.00 0.00 0.00 0.00 0.05 0.88 0.64
6 0 0.00 0.00 0.00 0.00 0.00 0.23 0.65
7 0 0.00 0.00 0.00 0.00 0.00 0.00 0.77
8 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00

下一步是运行一组模拟,比如n = 100000

首先进入四分之一决赛(3 次中最好的):

1 vs 8
2 vs 7
3 vs 6
4 vs 5

然后每对的胜者将在半决赛中对决:

1-8 winner VS 4-5 winner
2-7 winner VS 3-6 winner

获胜者进入决赛。 3 中最好的。

我可以使用什么方法/包来运行支架模拟?我确实找到了一个名为 mRchmadness 的包,但它太具体了,无法处理这个模拟。

【问题讨论】:

  • 为什么假设有一个现成的包来运行支架模拟?即使有,在 Stack Overflow 上寻求图书馆推荐也是题外话。自己写应该不会太难。但是,简单地询问如何在 R 中这样做是一个过于宽泛的问题。如果遇到麻烦,请开始编写代码并提出更集中的问题。
  • 锦标赛在上面有完整的描述。有 8 支球队,他们按照规定进行比赛。 Team 1 vs team 8,等等。我认为我需要某种类型的包来运行模拟(任何类型的),如果我遇到错误,我很抱歉。此外,我不觉得这个问题过于宽泛,因为我只需要以他们描述的方式模拟这 8 个团队。我没有在 R 中做过任何模拟,所以我在这里从头开始,因此提出了问题。
  • 从头开始编写模拟需要编写程序,这在某种意义上是广义的。您可以从编写一个函数开始,该函数采用一对团队并模拟一轮最好的 3 轮比赛,并返回获胜者。然后编写另一个模拟单个锦标赛的函数。然后编写一个调用 100,000 次的函数,收集您感兴趣的任何统计信息。

标签: r simulation probability


【解决方案1】:

我创建了一些虚拟代码,可以帮助您弄清楚如何去做。代码根本没有优化,但它是相当线性的,你可以理解如何去做。

prob_res <- matrix(round(runif(64),2), 8, 8)
prob_res[lower.tri(prob_res, diag = TRUE)] <- 0
prob_res <- as.data.frame(prob_res)
colnames(prob_res) <- 1:8
rownames(prob_res) <- 1:8
prob_res

## Total number of combinations
posscombi<-t(combn(1:8, 2))   

## This function gives you winners of the match with n repetitionmatches against every other team possible combination of teams. 
## It "reproduces" like the whole league assuming winning probabilities are static.

League <- function(repetitionMatches, posscomb , prob_res)
    {
    TotalVect<-integer(0)
    for(i in 1:nrow(posscomb)){
        pair <- posscomb[i,]
        Vect<-sample(pair, 
                     size = repetitionMatches, 
                     prob = c(prob_res[pair[1], pair[2]], 1-prob_res[pair[1], pair[2]]),
                     replace = TRUE)
        TotalVect <- c(TotalVect, Vect)
        }    
    return(table(TotalVect))
    }

Result<-League(100,posscomb = posscombi, prob_res= prob_res) 

Myorder<-order(Result)
### Quarters 
pair1<- c(names(Result)[Myorder[c(1,8)]]) 
pair2<- c(names(Result)[Myorder[c(2,7)]])
pair3<- c(names(Result)[Myorder[c(3,6)]])
pair4<- c(names(Result)[Myorder[c(4,5)]])
## This function gives you the results to n matches (being 3 in the example)
PlayMatch<-function(pairs, numMatches){
    Res <-sample(pairs, size = numMatches, 
       prob = c(prob_res[pairs[1], pairs[2]], 1-prob_res[pairs[1], pairs[2]]),
       replace = TRUE)
    return(table(Res))
        }
# Results of the matches
winner1<-PlayMatch(pairs = pair1, 3)
winner2<-PlayMatch(pairs = pair2, 3)
winner3<-PlayMatch(pairs = pair3, 3)
winner4<-PlayMatch(pairs = pair4, 3)

## Semis
#Choosing the winning teams
pair1<- c(names(winner1)[which.max(winner1)],names(winner2)[which.max(winner2)])
pair2<- c(names(winner3)[which.max(winner3)],names(winner4)[which.max(winner4)])
winner1<-PlayMatch(pairs = pair1, 3)
winner2<-PlayMatch(pairs = pair2, 3)

## Final
# Same as before
pair1<- c(names(winner1)[which.max(winner1)],names(winner2)[which.max(winner2)])
winner1<-PlayMatch(pairs = pair1, 3)
paste0( "team ",names(winner1)[which.max(winner1)],  " is the winner!")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多