来自指数分布的R中的泊松过程

机器算法验证 r 模拟 指数分布 泊松过程
2022-04-02 13:42:29

我试图通过从指数分布的随机变量开始来模拟 R 中的泊松过程样本路径。例如,对于的值,我可以生成 500 个样本,然后我想在 [0,10] 的时间间隔上绘制泊松过程路径,例如,我如何在 R 中做到这一点?λ=0.5

试图

set.seed(1)
n <- 100
x <- cumsum(rexp(50, rate=0.5))
y <- cumsum(c(0, rep(1, 50)))
plot(stepfun(x, y), xlim = c(0, 10), do.points = FALSE, 
     main="L=0.5")

这似乎有效,尽管我不知道它的效率如何,因为我只是通过限制图形的 x 轴来获得我想要的

1个回答

, ,的连续到达计算为 独立指数间隔的累积和。所以这里的两个主要成分是然后,您使用步进插值(的额外点会有所帮助。Sii=12rexpcumsum[Si,i]type = "s"i=0Si:=0

在给定的时间间隔内,您事先不知道有多少到达将到来。因此,您可以使用带有 控制语句的循环,也可以模拟超出需要的内容,如此处所示。第二个选项在 R 中可能更有效。Sibreak

lambda <- 0.5
tMax <- 100

## find the number 'n' of exponential r.vs required by imposing that
## Pr{N(t) <= n} <= 1 - eps for a small 'eps'
n <- qpois(1 - 1e-8, lambda = lambda * tMax)

## simulate exponential interarrivals the
X <- rexp(n = n, rate = lambda)
S <- c(0, cumsum(X))
plot(x = S, y = 0:n, type = "s", xlim = c(0, tMax)) 

## several paths?
nSamp <- 50
## simulate exponential interarrivals
X <- matrix(rexp(n * nSamp, rate = lambda), ncol = nSamp,
            dimnames = list(paste("S", 1:n, sep = ""), paste("samp", 1:nSamp)))
## compute arrivals, and add a fictive arrival 'T0' for t = 0
S <- apply(X, 2, cumsum)
S <- rbind("T0" = rep(0, nSamp), S)
head(S)
## plot using steps
matplot(x = S, y = 0:n, type = "s", col = "darkgray",
        xlim = c(0, tMax),
        main = "Homogeneous Poisson Process paths", xlab = "t", ylab = "N(t)")