Имитация результатов турнира с R - PullRequest
0 голосов
/ 06 июля 2018

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 , но он слишком специфичен для этой симуляции.

1 Ответ

0 голосов
/ 06 июля 2018

Я создал несколько фиктивных кодов, которые помогут вам понять, как это сделать. Код вообще не оптимизирован, но вы понимаете, как это сделать, линейно.

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