Skip to content

Commit

Permalink
Merge pull request #221 from ntguardian/patch-1
Browse files Browse the repository at this point in the history
Corrected bug for continuous time Markov chain simulation with absorb…
  • Loading branch information
spedygiorgio authored Aug 27, 2024
2 parents 025b19e + afad90e commit 6d6dfd4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions R/ctmcProbabilistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ rctmc <- function(n, ctmc, initDist = numeric(), T = 0, include.T0 = TRUE,
i <- 1
while (i <= n){
idx <- which(ctmc@states == state)
t <- t + rexp(1, -ctmc@generator[idx, idx])
state <- ctmc@states[sample(1:dim(ctmc), 1, prob = trans[idx, ])]
if (ctmc@generator[idx, idx] == 0) {
# absorbing state; stay here forever
t <- Inf
} else {
t <- t + rexp(1, -ctmc@generator[idx, idx])
}

if(T > 0 & t > T)
if((T > 0 & t > T) | (is.infinite(t)))
break

state <- ctmc@states[sample(1:dim(ctmc), 1, prob = trans[idx, ])]
states <- c(states, state)
time <- c(time, t)
i <- i + 1
Expand All @@ -84,6 +89,7 @@ rctmc <- function(n, ctmc, initDist = numeric(), T = 0, include.T0 = TRUE,
stop("Not a valid output type")
}


#' @title Return the generator matrix for a corresponding transition matrix
#'
#' @description Calculate the generator matrix for a
Expand Down

0 comments on commit 6d6dfd4

Please sign in to comment.