Skip to content

Commit f2eb543

Browse files
authored
fix edge case (#254)
* fix edge case * fix failing test * add test figure
1 parent 0cda979 commit f2eb543

File tree

4 files changed

+118
-106
lines changed

4 files changed

+118
-106
lines changed

R/LSgameofchance.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ LSgameofchance <- function(jaspResults, dataset, options, state = NULL){
3333
.quitAnalysis(gettext("Warning: The number of players must be at least 2. Adjust the inputs!"))
3434
if(nPlayers > 9)
3535
.quitAnalysis(gettext("Warning: The number of players must be at most 9. Adjust the inputs!"))
36-
36+
3737
if(pointsToWin < 1)
3838
.quitAnalysis(gettext(
3939
"Warning: The number of point(s) required to win should be at least 1!"

R/LSproblemofpointscommon.R

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ compareChanceNPlayers <- function(k1, t, p, simulation){
122122
}
123123

124124
# 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))
129127

130128
pCumulative <- matrix(0, nrow = length(p), ncol = simulation)
131129
pCumulative[recordGame[1],1] <- 1
@@ -139,18 +137,45 @@ compareChanceNPlayers <- function(k1, t, p, simulation){
139137
}
140138
}
141139

140+
142141
## Calculating the probability using negative multinomial distribution and resursive function
143142
pCalculated <- 0
143+
144+
combsK <- combinations(k)
144145
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+
147150
}
148151

149152
# calculating difference between the simulated and the calculated probability of player 1 winning
150153
dif <- abs(pSimulated - pCalculated)
151154
return (list(pSimulated, pCalculated, dif, pCumulative))
152155
}
153156

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+
154179
compareSkillTwoPlayers <- function(m, n, t, alpha = 1, beta = 1, simulation){
155180

156181
# first estimating the probability that player 1 wins

0 commit comments

Comments
 (0)