@@ -122,10 +122,8 @@ compareChanceNPlayers <- function(k1, t, p, simulation){
122
122
}
123
123
124
124
# estimated probability that each player wins
125
- pSimulated <- vector()
126
- for (i in 1 : length(p )){
127
- pSimulated [i ] <- length(which(recordGame == i ))/ simulation
128
- }
125
+ pSimulated <- table(factor (recordGame , levels = seq_along(p )))
126
+ pSimulated <- c(pSimulated / sum(pSimulated ))
129
127
130
128
pCumulative <- matrix (0 , nrow = length(p ), ncol = simulation )
131
129
pCumulative [recordGame [1 ],1 ] <- 1
@@ -139,18 +137,45 @@ compareChanceNPlayers <- function(k1, t, p, simulation){
139
137
}
140
138
}
141
139
140
+
142
141
# # Calculating the probability using negative multinomial distribution and resursive function
143
142
pCalculated <- 0
143
+
144
+ combsK <- combinations(k )
144
145
for (l in 1 : (prod(k )/ k [1 ])){
145
- pCalculated <- pCalculated + exp(MGLM :: dnegmn(Y = combinations(k )[l ,2 : length(k )],
146
- beta = combinations(k )[1 ], prob = p [2 : length(p )]))
146
+ # pCalculated <- pCalculated + exp(MGLM::dnegmn(Y = combinations(k)[l,2:length(k)],
147
+ # beta = combinations(k)[1], prob = p[2:length(p)]))
148
+ pCalculated <- pCalculated + c(exp(dnegmnManual(Y = combsK [l ,2 : length(k )], beta = combsK [1 ], prob = p [2 : length(p )])))
149
+
147
150
}
148
151
149
152
# calculating difference between the simulated and the calculated probability of player 1 winning
150
153
dif <- abs(pSimulated - pCalculated )
151
154
return (list (pSimulated , pCalculated , dif , pCumulative ))
152
155
}
153
156
157
+ dnegmnManual <- function (Y , beta , prob ) {
158
+
159
+ # trimmed down version of MGLM::dnegmn
160
+ # in particular, this one returns -Inf instead of an error
161
+ # the argument adjustments are identical to those inside MGLM::dnegmn
162
+
163
+ Y <- matrix (Y , 1 , length(Y ))
164
+ prob <- matrix (prob , nrow(Y ), length(prob ), byrow = TRUE )
165
+ beta <- matrix (beta , nrow(Y ), 1 )
166
+ beta <- matrix (beta , , 1 )
167
+
168
+ m <- rowSums(Y )
169
+ d <- ncol(Y )
170
+
171
+ # avoids 0 * log(0) = NaN
172
+ yTimesLogProb <- ifelse(prob == 0 & Y == 0 , 0 , Y * log(prob ))
173
+ logl <- lgamma(beta + rowSums(Y )) - lgamma(beta ) - rowSums(lgamma(Y + 1 )) + rowSums(yTimesLogProb ) + beta * log1p(- rowSums(prob ))
174
+ # logl <- lgamma(beta + rowSums(Y)) - lgamma(beta) - rowSums(lgamma(Y + 1)) + rowSums(Y * log(prob)) + beta * log1p(-rowSums(prob))
175
+ logl
176
+
177
+ }
178
+
154
179
compareSkillTwoPlayers <- function (m , n , t , alpha = 1 , beta = 1 , simulation ){
155
180
156
181
# first estimating the probability that player 1 wins
0 commit comments