Skip to content

Instantly share code, notes, and snippets.

Last active September 19, 2019 10:53
What would you like to do?
A Python program to generate event counts using a Poisson process
import random
import math
_lambda = 5
_num_total_arrivals = 150
_num_arrivals = 0
_arrival_time = 0
_num_arrivals_in_unit_time = []
_time_tick = 1
for i in range(_num_total_arrivals):
#Get the next probability value from Uniform(0,1)
p = random.random()
#Plug it into the inverse of the CDF of Exponential(_lamnbda)
_inter_arrival_time = -math.log(1.0 - p)/_lambda
#Add the inter-arrival time to the running sum
_arrival_time = _arrival_time + _inter_arrival_time
#Increment the number of arrival per unit time
_num_arrivals = _num_arrivals + 1
if _arrival_time > _time_tick:
_num_arrivals = 0
_time_tick = _time_tick + 1
#print it all out
print('\nNumber of arrivals in successive unit length intervals ===>')
print('Mean arrival rate for sample:' + str(sum(_num_arrivals_in_unit_time)/len(_num_arrivals_in_unit_time)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment