I am trying to simulate number of claims in next 12 months using a homogeneous poisson process following the R codes:

lambda <- 17
# the length of time horizon for the simulation T_length <- 31
last_arrival <- 0
arrival_time <- c()
inter_arrival <- rexp(1, rate = lambda)
while (inter_arrival + last_arrival < T_length) { 
last_arrival <- inter_arrival + last_arrival 
arrival_time <- c(arrival_time,last_arrival) 
inter_arrival <- rexp(1, rate = lambda)

And I get a list with around 500 elements, then I repeat this for each of the twelve months, how do I plot the trajectory of the counting process?

  • 1
    $\begingroup$ see stats.stackexchange.com/questions/308730/… $\endgroup$
    – Glen_b
    Commented Sep 23, 2018 at 23:44
  • $\begingroup$ Just want to point out that @Glen_b provides a link to a great example but do note it is for a Compound Poisson Process (CPP), not a nonhomogenous Poisson Process (NHPP). The plotting the sample path part is the point, not the mathematics in the link. $\endgroup$ Commented Sep 29, 2018 at 4:55
  • $\begingroup$ This is correct; I should have made it clear that it wasn't the precise thing asked for here, but of some value in trying to draw one (but if it were the same thing as asked for here, I'd have closed it as a duplicate). $\endgroup$
    – Glen_b
    Commented Sep 29, 2018 at 5:24
  • $\begingroup$ For many more threads on this topic, search our site for queue simulation. $\endgroup$
    – whuber
    Commented Oct 30, 2019 at 14:36

2 Answers 2


The following code plots a line chart with the appropriate jumps.

n <- length(arrival_time)
counts <- 1:n

plot(arrival_time, counts, pch=16, ylim=c(0, n))
points(arrival_time, c(0, counts[-n]))
  x0 = c(0, arrival_time[-n]),
  y0 = c(0, counts[-n]),
  x1 = arrival_time,
  y1 = c(0, counts[-n])

Output: enter image description here

  • $\begingroup$ Thank you so much!! What if I wanted to combine all 12 months into one plot? is that possible? $\endgroup$
    – SugarMarsh
    Commented Sep 23, 2018 at 23:55

The code below plots the counting process $\{N(t),t \ge 0\}$ with rate $\lambda(t)$ taken from this example. It plots three arbitrary sample paths {5, 10, 15} from the 2000 generated.

It will work for a stationary Poisson Process (PP) with fixed rate $\lambda$ as well.

MATLAB code:

% MATLAB R2018b
% Plots paths p1, p2, p3
p1 = 5; p2 = 10;, p3 = 15;

figure, hold on, box on
s(1) = stairs([0 EventTimes{p1,:}],0:length([EventTimes{p1,:}]),'b-')
s(2) = stairs([0 EventTimes{p2,:}],0:length([EventTimes{p2,:}]),'r-')
s(3) = stairs([0 EventTimes{p3,:}],0:length([EventTimes{p3,:}]),'k-')

% Cosmetics
title('3 x Sample Paths for NHPP')
ylabel('N(t): Number of events by time t')

Sample paths plotted below with event times shown (on horizontal axis) for two of them (red & blue). SamplePaths

Update: Based on OP comment, For a fixed $t$, $N(t)\sim\text{Poisson}(m(t))$ where $m(t) = \int_0^t \lambda(s)ds$.

So based on the OP's comment regarding $N(365)$, $N(365)$ is distributed Poisson with mean $m(365) = \int_0^{365} \lambda(s)ds$.

  • $\begingroup$ how would I plot the distribution of the total number of events, so N(t)? $\endgroup$
    – SugarMarsh
    Commented Sep 29, 2018 at 10:46
  • $\begingroup$ How do you want to plot it? For a certain $t$? Can you give me more details? $\endgroup$ Commented Sep 29, 2018 at 14:25
  • $\begingroup$ For the entire period, number of events in 365 days, would it just be hist () $\endgroup$
    – SugarMarsh
    Commented Sep 29, 2018 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.