Я смотрю на код, приведенный ниже:
### forward sampling
ForwardSimulation <- function(start_state,Rate_matrix,length_time){
## Size of state space
nSt <- nrow(Rate_matrix)
## State space
StSp <- 1:nSt
## Choose state at time=0 from initial distribution
## X holds states of path
X <- vector(mode="numeric")
## beginning state is start_state
X[1]<- start_state
## T holds times of path (Note: more sensible would be to find T from some exponential distribution(maybe))
T <- vector(mode="numeric")
## In the beginning time is 0
T[1] <- 0
T[2] <- 1
X[2] <- sample(StSp[-X[1]],1,prob=Rate_matrix[X[1],-X[1]])
print(X[2])
print(-X[2])
print(Rate_matrix)
print(Rate_matrix[X[2],-X[2]])
## Simulate states and waiting times similarly as above
## until time is larger than length_time
cnt <- 2 ## counter
while (T[cnt] < length_time) {
T[cnt+1] <- cnt
X[cnt+1] <- sample(StSp[-X[cnt]],1,prob=Rate_matrix[X[cnt],-X[cnt]])
cnt <- cnt+1
}
## Output state changes and corresponding times
Path <- list()
Path$length_time <- c(T[1:(cnt-1)],length_time)
Path$St <- c(X[1:(cnt-1)],X[cnt-1])
return(Path)
}
###Setting up the matrix
r1 <- 1 # 1->2
r2 <- 0.75 # 2->3
r3 <- 0.5 # 3->1
r4 <- 0.5 # 3-> 2
Rate_matrix <- matrix(c(-r1, r1, 0, 0, -r2, r2, r3, r4, -(r3+r4)), nrow = 3, byrow = TRUE)
path_forward <- ForwardSimulation(1,Rate_matrix,5)
plot(path_forward$length_time,path_forward$St,type = 's',ylab = 'State',xlab = 'Time')
Вышеупомянутый код просто создает путь, используя CTMC с некоторой матрицей.Почему я не понимаю, что Rate_matrix [X [cnt], - X [cnt]] и StSp [-X [cnt]] делает в функции семпла.Я имею в виду, что означает "-X [cnt]" в этих случаях.Помощь будет оценена.