Stepfun 函数马尔可夫
Stepfun function markov
不要被我的长代码吓到。我想知道的是关于最后一部分,情节(步骤有趣......部分。当我将其输入 Rstudio 时,我得到 "stepfun "x" must be ordered increasingly"
这里有谁知道我必须做什么才能正确完成这个吗?
bd_process <- function(lambda, mu, initial_state = 0, steps = 100) {
time_now <- 0
state_now <- initial_state
time <- 0
state <- initial_state
for (i in 1:steps) {
if (state_now == 3) {
lambda_now <- 0
} else {
lambda_now <- lambda
}
if (state_now == 0) {
mu_now <- 0
} else {
mu_now <- mu
}
time_to_transition <- rexp(mu, rate = 1) + rexp(lambda, rate = 1)
X <- rexp(mu, rate = 1)
Y <- rexp(lambda, rate = 1)
if (X < Y) {
state_now <- state_now - 1
} else {
state_now <- state_now + 1
}
time_now <- time_now + time_to_transition
time <- c(time, time_now)
state <- c(state, state_now)
}
list(time = time, state = state) }
set.seed(19930628)
proposal1 <- bd_process(lambda = 2, mu = 10)
proposal2 <- bd_process(lambda = 6, mu = 10)
proposal3 <- bd_process(lambda = 10, mu = 10)
time1 <- proposal1$time
state1 <- proposal1$state
plot(stepfun(time1[-1], state1),
do.points = FALSE,
xlab = "Tid",
ylab = "Tillstånd",
main = "",
yaxt = "n")
axis(2, at = c(0, 1, 2, 3), las = 2)
我不知道你的代码在做什么,但你已经让我们不要担心了。目前看来您只构建了 "time intervals" 但现在需要沿着适当的时间轴 "stack them together" 或 "integrate" 它们。为了绘制阶跃函数的模拟图,您应该使用 cumsum
来构造递增的 time1
向量。因为 "time" 和 "state" 变量的长度如此不同,所以对函数参数的快速修复是修剪 time1
向量,因此它是 state1
变量的正确长度,并且您没有错误:
plot(stepfun(cumsum(time1[2:101]), state1),
do.points = FALSE,
xlab = "Tid",
ylab = "Tillstånd",
main = "",
yaxt = "n")
axis(2, at = c(0, 1, 2, 3), las = 2)
也许如果您 "march step-by-step" 通读代码并使用注释解释代码(对您自己和我们其他人),您就会明白为什么 time1
的数量是你有 state1
个。我怀疑这可能与使用 "mu" 作为 rexp(mu, rate = 1)
的第一个参数有关。 R 中随机数生成器的第一个参数通常是一个正整数,用于确定分布的长度(随机数的数量)。
不要被我的长代码吓到。我想知道的是关于最后一部分,情节(步骤有趣......部分。当我将其输入 Rstudio 时,我得到 "stepfun "x" must be ordered increasingly"
这里有谁知道我必须做什么才能正确完成这个吗?
bd_process <- function(lambda, mu, initial_state = 0, steps = 100) {
time_now <- 0
state_now <- initial_state
time <- 0
state <- initial_state
for (i in 1:steps) {
if (state_now == 3) {
lambda_now <- 0
} else {
lambda_now <- lambda
}
if (state_now == 0) {
mu_now <- 0
} else {
mu_now <- mu
}
time_to_transition <- rexp(mu, rate = 1) + rexp(lambda, rate = 1)
X <- rexp(mu, rate = 1)
Y <- rexp(lambda, rate = 1)
if (X < Y) {
state_now <- state_now - 1
} else {
state_now <- state_now + 1
}
time_now <- time_now + time_to_transition
time <- c(time, time_now)
state <- c(state, state_now)
}
list(time = time, state = state) }
set.seed(19930628)
proposal1 <- bd_process(lambda = 2, mu = 10)
proposal2 <- bd_process(lambda = 6, mu = 10)
proposal3 <- bd_process(lambda = 10, mu = 10)
time1 <- proposal1$time
state1 <- proposal1$state
plot(stepfun(time1[-1], state1),
do.points = FALSE,
xlab = "Tid",
ylab = "Tillstånd",
main = "",
yaxt = "n")
axis(2, at = c(0, 1, 2, 3), las = 2)
我不知道你的代码在做什么,但你已经让我们不要担心了。目前看来您只构建了 "time intervals" 但现在需要沿着适当的时间轴 "stack them together" 或 "integrate" 它们。为了绘制阶跃函数的模拟图,您应该使用 cumsum
来构造递增的 time1
向量。因为 "time" 和 "state" 变量的长度如此不同,所以对函数参数的快速修复是修剪 time1
向量,因此它是 state1
变量的正确长度,并且您没有错误:
plot(stepfun(cumsum(time1[2:101]), state1),
do.points = FALSE,
xlab = "Tid",
ylab = "Tillstånd",
main = "",
yaxt = "n")
axis(2, at = c(0, 1, 2, 3), las = 2)
也许如果您 "march step-by-step" 通读代码并使用注释解释代码(对您自己和我们其他人),您就会明白为什么 time1
的数量是你有 state1
个。我怀疑这可能与使用 "mu" 作为 rexp(mu, rate = 1)
的第一个参数有关。 R 中随机数生成器的第一个参数通常是一个正整数,用于确定分布的长度(随机数的数量)。