## Simulate a Homogeneous Poisson Processes (HPP)
## HPP with rate r over interval [0,L]
r=1 # rate
L=10 # interval length
Ts=c() # initialize vector of inter-event times
X=c() # initialize vector event times
i=1 # index for while loop
xlast = 0 # initialize most recent event time (absolute time)
while(xlast < L) { # while our last event is not yet past L...
## Step 1, generate a vector of inter-event times
## by drawing iid values from an exponential dist. with rate=r.
## Ts = that vector of times.
Ts[i] <- rexp(1,rate=r)
## Step 2, get "absolute" even times from inter-event times
## by addition...
## X[1] = Ts[1]
## X[2] = Ts[1] + Ts[2]
## X[3] = Ts[1] + Ts[2] + Ts[3]
## ...
## This can be achieved with a cummulative sum:
## X = cumsum(Ts)
##
## In this while loop, just keep track of the most recent.
xlast <- xlast + Ts[i]
i <- i + 1; # update our counter for the while loop...
} # end while loop.
## Fill out absolute even times
X = cumsum(Ts)
X
## remove the last one, which is > L
X <- X[-length(X)] # if length(x) is 10, X[-10] is the same as X[1:9]
## Plot the results
plot(X,X*0, pch='|', yaxt="n", ylab='', xlim=c(0,L),
main=paste("N =",length(X))) # Plot our points on [0,L]
abline(h=0)