Skip to content

Instantly share code, notes, and snippets.

@armanbilge
Last active December 21, 2015 08:08
Show Gist options
  • Save armanbilge/6275603 to your computer and use it in GitHub Desktop.
Save armanbilge/6275603 to your computer and use it in GitHub Desktop.
Basic simulation of multiple events under independent Poisson processes and visualization of its stats
#!/usr/bin/env python
import sys
import numpy
import pylab
import random
lambd_A = random.random()
lambd_B = random.random()
lambd_C = random.random()
print >> sys.stderr, 'lambda_A =', lambd_A
print >> sys.stderr, 'lambda_B =', lambd_B
print >> sys.stderr, 'lambda_C =', lambd_C
lambd_sum = lambd_A + lambd_B + lambd_C
p_A = lambd_A / lambd_sum
times = []
for _ in range(int(sys.argv[1])):
time = random.expovariate(lambd_sum)
if p_A > random.random(): times.append(time)
n, bins, patches = pylab.hist(times, 50, normed=1, histtype='stepfilled')
pylab.setp(patches, 'facecolor', 'g', 'alpha', 0.75)
pylab.plot(bins, lambd_A * numpy.exp(-lambd_sum * bins), 'k--', linewidth=1.5)
pylab.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment