A webhook POSTs to our database each time a particular event occurs on our website. We receive about two of these requests per minute. I was mindlessly monitoring the log files one day and noticed it had been roughly 90 seconds since our database had been hit by this request. Before worrying, though, I wondered how rare that observation is. What is the likelihood of waiting longer than 1.5 minutes for the next request?

This is a probability problem that can be solved with an understanding of Poisson processes and the exponential distribution. A Poisson process is any process where independent events occur at constant known rate, e.g. babies are born at a hospital at a rate of three per hour, or calls come into a call center at a rate of 10 per minute. The exponential distribution is the probability distribution that models the waiting times between these events, e.g. the times between calls at the call center are exponentially distributed. To model Poisson processes and exponental distributions, we need to know two things: a time-unit t and a rate \lambda .

Poisson Distribution

Let's start with the Poisson distribution: If we let N(t) denote the number of events that occur between now and time t , then the probability that n events occur within the next t time-units, or P(N(t) = n) , is

 P(N(t) = n) = \frac{(\lambda t)^n e^{-\lambda t}}{n!}

As mentioned earlier, we receive an average of two requests from this webhook per minute. Thus, the time-unit t is one minute and the rate \lambda is two. Knowing these, we can answer questions such as:

What is the probability that we receive no requests in the next two minutes? 

 P(N(2) = 0) = \frac{(2 \cdot 2)^0 e^{-2 \cdot 2}}{0!} = e^{-4} \approx 0.0183

What is the probability that we receive at least two requests in the next three minutes?

 P(N(3) \geq 2)

 = 1 - P(N(3) = 1) - P(N(3) = 0)

 = 1 - \frac{(2 \cdot 3)^1 e^{-2 \cdot 3}}{1!} - \frac{(2 \cdot 3)^0 e^{-2 \cdot 3}}{0!}

 = 1 - 6e^{-6} - e^{-6}

 = 1 - 7e^{-6}

 \approx 0.9826

For those who prefer reading code, we can write a classPoissonthat's initialized with its rate \lambda :

Exponential Distribution

Let's move onto the exponential distribution. As mentioned earlier, the waiting times between events in a Poisson process are exponentially distributed. The exponential distribution can be derived from the Poisson distribution: Let X be the waiting time between now and the next event. The probability that X is greater than t is identical to the probability that 0 events occur between now and time t , which we already know:

 P(X > t) = P(N(t) = 0) = \frac{(\lambda t)^0 e^{-\lambda t}}{0!} = e^{-\lambda t}

We also know that the probability of X being less than or equal to t is the complement of X being greater than t :

 P(X \leq t) = 1 - P(X > t) = 1 - e^{-\lambda t}

Thus, the distribution function of the waiting times between events in a Poisson process is 1 - e^{-\lambda t} . With this, and recalling that our time-unit t is one minute and our rate \lambda is two requests per minute, we can answer questions such as:

  • What is the probability that the next request occurs within 15 seconds?

 P(X \leq 0.25) = 1 - e^{-0.25 \cdot 2} = 1 - e^{-0.5} \approx 0.3935

  • What is the probability that the next request is between 15 and 30 seconds from now?

 P(0.25 \leq X \leq 0.5) = P(X \leq 0.5) - P(X \leq 0.25)

 = (1 - e^{-2 \cdot 0.5}) - (1 - e^{-2 \cdot 0.25})

 = e^{-0.5} - e^{-1}

 \approx 0.2387

Conclusion

Now, referring back to the original question: What is the probability of waiting longer than 1.5 minutes for the next request?

 P(X > 1.5) = e^{-2 \cdot 1.5} = e^{-3} \approx 0.0498

The probability of waiting longer than 1.5 minutes for the next request is 4.98%.

原址:http://nbviewer.jupyter.org/github/nicolewhite/notebooks/blob/master/Poisson.ipynb

Leave a Comment