用 R 模拟锦标赛结果

Simulating tournament results with R

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

四分之一决赛第一名(三局中最好的):

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 个中最好的。

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

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

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!")