# Goal: sample from poisson distribution
# Cookie dough with chocolate chips example
# Average number of chips in one fistful of dough is lambda (intensity of Poisson process)
# Consider dividing fistful into n pieces, each of which has one or zero chips
# prob of having chip is lambda/n.
# Get actual Poisson distribution by taking n very large.
# Binomial distribution
# Binom(n,p): Take coin with prob p of being heads. Flip n times. Count the number of heads.
# The coin flips are independent of one another.
# In previous cell, total number of chips is given Binom(n,lambda/n)
# sum n copies of randint(0,1) is same as Binom(n,1/2)
# simulate binomial distribution
def binom(n,p):
"""Sample from Binom(n,p) distribution."""
run_sum = 0 # running total of number of successes
for i in range(n):
if random()<p: # this happens with probability p
run_sum += 1
return run_sum
binom(3,1/2)
import matplotlib.pyplot as plt
plt.hist([binom(3,1/3) for i in range(10000)], bins=range(0,5), align="left", rwidth=0.5)
# if we keep p constant, make n large, Central Limit Theorem and Law of Large numbers
# are relevant
plt.hist([binom(100,1/2) for i in range(10000)])
# Look at approximation to Poisson
lam = 3.0 # intensity of the process
n=100
p=lam/n
plt.hist([binom(n,p) for i in range(10000)],bins=range(0,15), align="left", rwidth=0.5)
lam = 3.0 # intensity of the process
n=1000
p=lam/n
plt.hist([binom(n,p) for i in range(10000)],bins=range(0,15), align="left", rwidth=0.5)
# Observation: the above two distributions (n=100,n=1000) are very similar
# suggest that as n->infinity, we get some limiting distribution
# Poisson distribution is this limit.
# Poisson(lam) is limit of Binom(n,lam/n) as n-> infinity
# Properties of Poisson(lambda), lambda>0 real number
# - Discrete distribution (not continuous)
# - Average value is lambda
# - Prob = k is greater than 0 if k non-negative integer
lam = 1.0 # intensity of the process
n=1000
p=lam/n
plt.hist([binom(n,p) for i in range(10000)],bins=range(0,15), align="left", rwidth=0.5)
lam = 10.0 # intensity of the process
n=1000
p=lam/n
plt.hist([binom(n,p) for i in range(10000)],bins=range(0,40), align="left", rwidth=0.5)
# Poisson can be defined in terms of its Probability Mass Function (pmf)
# (analagoue of Probability Density Function, but for discrete r.v.)
# pmf is a function f(x) that gives prob that a distribution takes some particular value x
# For instance, randint(0,1) pmf is f(0)=1/2, f(1)=1/2, f(x)=0 for any other x
#pmf for Poisson(lam):
# f(k) = lam^k * exp(-lam)/k!
# (note function is not even defined for k not a non-negative integer)
# PMF of Poisson(1)
lam=1
list_plot([lam^k * exp(-lam)/factorial(k) for k in range(0,10)])
# prob of Poisson(1) equaling 9:
f(k)=lam^k * exp(-lam)/factorial(k)
N(f(9))
N(f(15)) # goes to 0 very quickly
# PMF of Poisson(3)
lam=3
list_plot([lam^k * exp(-lam)/factorial(k) for k in range(0,10)])