用R

用R,r,simulation,probability,R,Simulation,Probability,aIn 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 下一步是运行一组模拟,比如n=1000

aIn 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
下一步是运行一组模拟,比如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
优胜者进入决赛。三选一最好


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

我创建了一些虚拟代码,可以帮助您了解如何执行此操作。代码根本没有经过优化,但对于您来说,理解如何进行优化是非常线性的

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

为什么要假设有一个现成的包来运行支架模拟?即使有,请求库建议也不是堆栈溢出的主题。写自己的应该不会太难。但是,简单地问如何在R中这样做是一个过于宽泛的问题。开始编写代码,如果遇到麻烦,可以问一个更为集中的问题。上面已经详细介绍了比赛。有8支球队,他们按照规定比赛。团队1和团队8等等。我假设我需要某种类型的软件包来运行任何类型的模拟,如果我遇到错误,我很抱歉。此外,我不觉得这个问题过于宽泛,因为只有这8支球队我需要按照他们描述的方式模拟。我没有在R中做过任何模拟,所以我在这里从头开始,因此这个问题。从广义上讲,从头开始编写模拟需要编写一个程序。您可以从编写一个函数开始,该函数包含两个团队,并模拟一轮三选一的最佳结果,返回优胜者。然后编写另一个模拟单个锦标赛的函数。然后编写一个调用100000次的函数,收集您感兴趣的任何统计数据。
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!")