Last active
December 21, 2015 08:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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