Skip to content

Commit

Permalink
Sum loglikelihoods before exponentiating
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisaloo authored and jamesmbaazam committed Nov 10, 2023
1 parent ee2b201 commit 9a748f3
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions R/likelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,19 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
likelihoods[sx[!(sx %in% exclude)]]
})

## transform log-likelihoods into likelihoods if required
if (isFALSE(log)) {
chains_likelihood <- lapply(chains_likelihood, exp)
## if individual == FALSE, return the joint log-likelihood
## (sum of the log-likelihoods)
if (!individual) {
chains_likelihood <- vapply(chains_likelihood, sum, 0)
}

## if individual == FALSE, return the joint log-likelihood
## (sum of the log-likelihoods), if log == TRUE, else
## multiply the likelihoods
if (isFALSE(individual)) {
summarise_func <- ifelse(log, sum, prod)
chains_likelihood <- vapply(chains_likelihood, summarise_func, 0)
## transform log-likelihoods into likelihoods if required
if (!log) {
if (individual) {
chains_likelihood <- lapply(chains_likelihood, exp)
} else {
chains_likelihood <- exp(chains_likelihood)
}
}

return(chains_likelihood)
Expand Down

0 comments on commit 9a748f3

Please sign in to comment.