Skip to content

Commit

Permalink
vectorise prob_discrete_erlang
Browse files Browse the repository at this point in the history
  • Loading branch information
TimTaylor committed Feb 7, 2024
1 parent f7e33b5 commit eb7151d
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions R/model_ebola.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,16 @@ prob_discrete_erlang <- function(shape, rate) {
n_bin <- 0
factorials <- factorial(seq(0, shape))

one_sub_cumulative_probs <- NULL
one_sub_cumulative_probs <- double(length = 1000L) # sufficient?
cumulative_prob <- 0
while (cumulative_prob <= 0.99) {
n_bin <- n_bin + 1

one_sub_cumulative_probs[n_bin] <- 0
for (j in seq(0, (shape - 1))) {
one_sub_cumulative_probs[n_bin] <-
one_sub_cumulative_probs[n_bin] +
(
exp(-n_bin * rate) * ((n_bin * rate)^j) / factorials[j + 1]
)
}
j <- seq_len(shape) - 1L
total <- exp(-n_bin * rate) * ((n_bin * rate)^j) / factorials[j + 1]
one_sub_cumulative_probs[n_bin] <- sum(total)
cumulative_prob <- 1 - one_sub_cumulative_probs[n_bin]
}
one_sub_cumulative_probs <- c(1, one_sub_cumulative_probs)
one_sub_cumulative_probs <- c(1, one_sub_cumulative_probs[1:n_bin])

density_prob <-
utils::head(one_sub_cumulative_probs, -1) -
Expand Down

0 comments on commit eb7151d

Please sign in to comment.