2

I am trying to distribute a given (fixed) number of events of a year over the respective year. The empirical distribution can be modelled with a Poisson distribution because there are usually only between 1 and 14 events per day. I tried looping through the days of the year and generating a random number of events from a poisson distribution. The point at which I struggle is to distribute ALL events. If I overestimate the distribution, there are no events at all at the last weeks of the year and if I underestimate the distribution, there are several events left but they all need to take place in the considered year. Is there any way to distribute all events?

Thanks a lot for your help and inspirations!

SimNewbie
  • 23
  • 4
  • 1
    A mathematical formulation of the problem will help to answer more accurately. But, sounds like here one can use multivariate Poisson distribution. – msuzen Sep 08 '21 at 09:48

2 Answers2

3

The conditional distribution of a collection of independent Poisson variates, given their total, $n$, is a multinomial distribution with $p$-parameters proportional to the corresponding Poisson rates. That is, $p_i = \lambda_i/\lambda_\text{total},\: i=1,2,...,k$, where $\lambda_\text{total}=\sum_i \lambda_i$.

An easy way to randomly allocate them is discussed in the "unequal" case here How to sample n observations from a multinomial distribution using binomial (or poisson) sampling?. Essentially you generate one value, then given that value, compute the conditional multinomial distribution of the remainder given what has already been generated, progressing through all the values and updating the conditional distribution for the remaining "cells" at each step; whatever is left at the last cell is the count value for that cell.

Glen_b
  • 257,508
  • 32
  • 553
  • 939
  • I'm just curious... It seems to me that my solution is a lot simpler than the one you propose. Is it because: a) I got it wrong b) It's just the way I see it c) They are equivalent, just presented differently? – dariober Sep 08 '21 at 17:21
  • Thanks a lot for your answer, Glen_b. I have to admit that I did not fully understand it and thus resorted to dariobers' simpler approach. However, I cannot judge if your might have been the better one. – SimNewbie Sep 17 '21 at 12:50
1

If I understand correctly, you have, say, N = 365 * 7 = 2555 events to distribute across each day of the year in such way that each day has a number of events given by a Poisson with, in this case, $\lambda = 7$.

For a simulation, you could assign to each of the 365 days a weight given by a random draw from the desired distribution, e.g. $Poisson(\lambda = 7)$, or any distribution really. Then distribute the events accordingly. Here's the code R:

smart.round <- function(x) {
  # Round vector x while keeping the sum of the rounded output the same as
  # input. Credit: https://stackoverflow.com/a/32544987/1114453
  y <- floor(x)
  indices <- tail(order(x-y), round(sum(x)) - sum(y))
  y[indices] <- y[indices] + 1
  y
}

N <- 365 * 7

pp <- rpois(n= 365, lambda= 7)
pp <- pp/sum(pp)

# Distribute events. Make final sum equals N after rounding
year <- smart.round(pp * N)

hist(year, xlab= 'N events', ylab= 'N days')

enter image description here

dariober
  • 2,805
  • 11
  • 14
  • Thank you so much for your response, dariober. It made perfect sense to me. However, I had to resort to using a inverse gaussian function because the smart round turned out to be a bit problematic for a discrete function. At least in my case there were a lot of the same weights created and thus sorting after the fractional part led to all the same numbers being rounded up. But as I said, with the inverse gaussian it works perfectly! – SimNewbie Sep 17 '21 at 12:46