简体   繁体   English

在 R 中手动模拟泊松过程

[英]Manually simulating Poisson Process in R

The following problem tells us to generate a Poisson process step by step from ρ (inter-arrival time), and τ (arrival time).以下问题告诉我们从ρ (到达间隔时间)和τ (到达时间)逐步生成泊松过程。

One of the theoretical results presented in the lectures gives the following direct method for simulating Poisson process:讲座中提出的理论结果之一给出了以下模拟泊松过程的直接方法:

• Let τ 0 = 0. • 让τ 0 = 0。
• Generate iid exponential random variables ρ1, ρ2, . • 生成iid 指数随机变量ρ1、ρ2、...。 . . .. ..
• Let τ n = ρ 1 +. • 让τ n = ρ 1 +。 . . . . + ρ n for n = 1, 2, . + ρ n对于 n = 1, 2, . . . . . . .
• For each k = 0, 1, . • 对于每个k = 0, 1, 。 . . ., let N t = k for τ k ≤ t < τ k+1 . ., 令 N t = k 对于 τ k ≤ t < τ k+1

  1. Using this method, generate a realization of a Poisson process (N t ) t with λ = 0.5 on the interval [0, 20].使用此方法,在区间 [0, 20] 上生成 λ = 0.5 的泊松过程 (N t ) t的实现。
  2. Generate 10000 realizations of a Poisson process (N t ) t with λ = 0.5 and use your results to estimate E(N t ) and Var(N t ).生成 λ = 0.5 的泊松过程 (N t ) t的 10000 个实现,并使用您的结果来估计 E(N t ) 和 Var(N t )。 Compare the estimates with the theoretical values.将估计值与理论值进行比较。

My attempted solution:我尝试的解决方案:

First, I have generated the values of ρ using rexp() function in R.首先,我使用 R 中的rexp()函数生成了ρ的值。

rhos <-function(lambda, max1)
{
    vec <- vector()

    for (i in 1:max1) 
    {
        vec[i] <- rexp(0.5)
    }

    return (vec)
}

then, I created τ s by progressive summing of ρ s.然后,我通过对ρ s 进行累加求和来创建τ s。

taos <- function(lambda, max)
{
    rho_vec <- rhos(lambda, max)
    #print(rho_vec)

    vec <- vector()
    vec[1] <- 0
    sum <- 0
    for(i in 2:max)
    {
        sum <- sum + rho_vec[i]
        vec[i] <- sum
    }

    return (vec)
}

The following function is for finding the value of N t =k when the value of k is given.以下函数用于在给定 k 值时求出N t = k值。 Say, it is 7 , etc.说,它是7等。

Ntk <- function(lambda, max, k)
{
    tao_vec <- taos(lambda, max)
    val <- max(tao_vec[tao_vec < k])
}

y <- taos(0.5, 20)
x <- seq(0, 20-1, by=1)

plot(x,y, type="s")

Output:输出:

在此处输入图像描述

As you can see, the plot of the Poisson process is blank rather than a staircase.如您所见,泊松过程的图是空白的,而不是阶梯状的。

If I change rexp to exp , I get the following output:如果我将rexp更改为exp ,我会得到以下输出:

在此处输入图像描述

.. which is a staircase function but all steps are equal. .. 这是一个楼梯功能,但所有步骤都是平等的。

Why is my source code not producing the expected output?为什么我的源代码没有产生预期的输出?

It looks like you're using max1 to indicate how many times to sample the exponential distribution in your rhos function.看起来您正在使用 max1 来指示对rhos函数中的指数分布进行采样的次数。 I would recommend something like this:我会推荐这样的东西:

rhosGen <- function(lambda, maxTime){
  rhos <- NULL
  i <- 1
  while(sum(rhos) < maxTime){
    samp <- rexp(n = 1, rate = lambda)
    rhos[i] <- samp
    i <- i+1
  }
  return(head(rhos, -1))
}

This will continue to sample from the exponential until the sum of these holding times is larger than the length of the given interval.这将继续从指数采样,直到这些保持时间的总和大于给定间隔的长度。 head the removes the last sample so that all of the events that we keep track of definitely occur in our time interval of interest. head删除最后一个样本,以便我们跟踪的所有事件都肯定发生在我们感兴趣的时间间隔内。 From here you have to generate the taos by summing the previous holding times (rhos):从这里你必须通过总结以前的持有时间 (rhos) 来生成 taos:

taosGen <- function(lambda, maxTime){
  rhos <- rhosGen(lambda, maxTime)
  taos <- NULL
  cumSum <- 0
  for(i in 1:length(rhos)){
    taos[i] <- sum(rhos[1:i])
  }
  return(taos)
}

Now that you have the taos we know at what time each event in the time interval (0,maxTime) occurs.现在您有了 taos,我们知道时间间隔 (0,maxTime) 中的每个事件在什么时间发生。 This leads us to generating the associated Poisson Process by finding the value of the Nt for each t in the time interval:这导致我们通过查找时间间隔中每个 t 的 Nt 值来生成相关的泊松过程:

ppGen <- function(lambda, maxTime){
  taos <- taosGen(lambda, maxTime)
  pp <- NULL
  for(i in 1:maxTime){
    pp[i] <- sum(taos <= i)
  }
  return(pp)
}

This generates the value of the Poisson Process at each integer time in the interval.这会在间隔中的每个整数时间生成泊松过程的值。 I suspect that part of your issue was trying to put the tao values on the y-axis instead of the count of events that had occurred already.我怀疑您的部分问题是试图将 tao 值放在 y 轴上,而不是已经发生的事件数。 The following code worked for me to produce a random looking stair case, similar to your example.以下代码对我有用,可以生成一个外观随机的楼梯,类似于您的示例。

y <- ppGen(0.5, 20)
x <- seq(0, 20-1, by=1)

plot(x,y, type="s")

Here's another possible implementation.这是另一种可能的实现。 The idea is to generate a vector of wait times (tau), and plot that against the list of events we're waiting for (max1)这个想法是生成一个等待时间向量 (tau),并根据我们正在等待的事件列表 (max1) 绘制它

poi.process <- function(lambda,n){
  
  # initialize vector of total wait time for the arrival of each event:
  s<-numeric(n+1)
  # set S_0 = 0
  s[1] <-0
  # generate vector of iid Exp random variables:
  x <-replicate(n,rexp(1,lambda))
  # assign wait time to vector s in for loop:
  for (k in 1:n){
    
    s[k+1] <-sum(x[1:k])
    
  }
  # return vector of wait time
  return(s)
  
}

Plotting it using stepfun will get us something like this:使用stepfun绘制它会得到这样的结果:


n<-20

lambda <-3

# simulate list of wait time:
s_list <-poi.process(lambda,n)

# plot function:
plot(stepfun(0:(n-1), s_list), 
     do.points = TRUE,
     pch = 16,
     col.points = "red",
     verticals = FALSE,
     main = 'Realization of a Poisson process with lambda = 3',
     xlab = 'Time of arrival',
     ylab = 'Number of arrivals')

Sample Poisson process:样本泊松过程:

样本泊松过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM