from math import pow, exp, factorial class Poisson: def __init__(self, rate): self.rate = rate def prob_exactly(self, n, t): rate = self.rate * t return pow(rate, n) * exp(-rate) / factorial(n) def prob_at_least(self, n, t): complements = range(n) total = 0.0 for c in complements: p = self.prob_exactly(c, t) total += p return 1 - total def prob_at_most(self, n, t): return 1 - self.prob_at_least(n + 1, t) pois = Poisson(2) pois.prob_exactly(0, 2) pois.prob_at_least(2, 3) class Exponential: def __init__(self, rate): self.rate = rate def prob_less_than_or_equal(self, t): rate = self.rate * t return 1 - exp(-rate) def prob_greater_than(self, t): return 1 - self.prob_less_than_or_equal(t) def prob_between(self, t1, t2): p1 = self.prob_less_than_or_equal(t1) p2 = self.prob_less_than_or_equal(t2) return p2 - p1 expo = Exponential(2) expo.prob_less_than_or_equal(0.25) expo.prob_between(0.25, 0.5) expo.prob_greater_than(1.5) pois.prob_exactly(0, 1.5)