簡體   English   中英

在r中模擬復合泊松過程

[英]Simulate Compound poisson process in r

我正試圖模擬r中的復合泊松過程。 該過程由$ \\ sum_ {j = 1} ^ {N_t} Y_j $定義,其中$ Y_n $是iid序列獨立的$ N(0,1)$值,$ N_t $是帶參數$ 1 $的泊松過程。 我試圖在沒有運氣的情況下模擬這個。 我有一個算法來計算如下:將cPp從0模擬到T:

發起:$ k = 0 $

重復$ \\ sum_ {i = 1} ^ k T_i <T $

設置$ k = k + 1 $

模擬$ T_k \\ sim exp(\\ lambda)$(在我看來是$ \\ lambda = 1 $)

模擬$ Y_k \\ sim N(0,1)$(這只是一個特例,我希望能夠將此更改為任何分發)

軌跡由$ X_t = \\ sum_ {j = 1} ^ {N_t} Y_j $給出,其中$ N(t)= sup(k:\\ sum_ {i = 1} ^ k T_i \\ leq t)$

有人可以幫我在r中模擬這個,以便我可以繪制過程嗎? 我試過了,但無法完成它。

使用cumsum作為確定時間N_t和X_t的累積和。 該說明性代碼指定模擬的次數, n ,模擬nt的時間和x的值,以及(顯示它已完成的內容)繪制軌跡。

n <- 1e2
n.t <- cumsum(rexp(n))
x <- c(0,cumsum(rnorm(n)))
plot(stepfun(n.t, x), xlab="t", ylab="X")

數字

該算法由於依賴於低級優化功能,因此速度很快:我測試它的六年之久的系統每秒將產生超過三百萬(時間,值)對。


這通常足以用於模擬,但它不能完全滿足問題,它要求生成模擬到時間T.我們可以利用前面的代碼,但解決方案有點棘手。 它計算了在時間T之前在泊松過程中將發生多少次的合理上限。它產生到達間隔時間。 這包含在一個循環中,該循環將在(罕見)事件中重復實際未達到時間T的過程。

額外的復雜性不會改變漸近計算時間。

T <- 1e2            # Specify the end time
T.max <- 0          # Last time encountered
n.t <- numeric(0)   # Inter-arrival times
while (T.max < T) {
  #
  # Estimate how many random values to generate before exceeding T.
  #
  T.remaining <- T - T.max
  n <- ceiling(T.remaining + 3*sqrt(T.remaining))
  #
  # Continue the Poisson process.
  #
  n.new <- rexp(n)
  n.t <- c(n.t, n.new)
  T.max <- T.max + sum(n.new)
}
#
# Sum the inter-arrival times and cut them off after time T.
#
n.t <- cumsum(n.t)
n.t <- n.t[n.t <= T]
#
# Generate the iid random values and accumulate their sums.
#
x <- c(0,cumsum(rnorm(length(n.t))))
#
# Display the result.
#
plot(stepfun(n.t, x), xlab="t", ylab="X", sub=paste("n =", length(n.t)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM