# Poisson distribution
# from last time
# plot the pmf of Poisson distribution
lam = 1 # intensity
pmf_list = [lam^k * exp(-lam)/factorial(k) for k in range(0,10)]
list_plot(pmf_list)
# plot pmf and experimental dist on same list_plot
import matplotlib.pyplot as plt
import numpy as np
intens = 1 # expected number of hits in one unit of time
num_trials = 10000
time_steps = 50 # divide our unit of time into this many
#pieces. Will get large. In each piece prob of getting a hit
# is intens/time_steps
# Poisson is limit as time_steps -> infinity
hits_type = np.zeros(num_trials) # ith element will be the number of trials that
# got i hits (divided by num_trials to give a frequency)
for trial in range(num_trials):
num_hits = 0
for t in range(time_steps):
# in this step, prob of getting hit is intens/time_steps
hit = 0 # will be 0 if not hit, if 1 hit
rand = random()
if rand < intens/time_steps:
hit = 1
else:
hit = 0
num_hits += hit
hits_type[num_hits] +=1/num_trials
lam = 1 # intensity
# plot the pmf of Poisson distribution
pmf_list = [lam^k * exp(-lam)/factorial(k) for k in range(0,10)]
list_plot(pmf_list[0:30], color="red",marker="<")\
+ list_plot(hits_type[0:30])
# theoretical pmf plotted in red
# experimental results in blue
# they will be very close
# how to sample from an actual poisson dist?
# (above experiment samples from a good approx)
import numpy as np
np.random.poisson() # default is intensity 1
import numpy as np
import matplotlib.pyplot as plt
plt.hist([np.random.poisson() for n in range(100)])
# gives good approximation to pmf
# suppose we just know pmf: lam^k * exp(-lam)/factorial(k)
# how could we produce a sample from poisson?
# (without builtin random.poisson() function)
# using uniform (0,1) random
lam = 1
results =[]
for n in range(1000):
still_going = True
k=0
adjust = 0.0
while still_going:
rand = random()
if (rand < (lam^k * exp(-lam)/factorial(k))/(1-adjust)):
# above happens with probabily equal to
# prob that variable equals k, given that it is at least k
still_going=False
else:
adjust += N(lam^k * exp(-lam)/factorial(k))
# above keeps track of prob that rand var is at most k
k += 1
# print(adjust)
results.append(k) # the value of k is one sample from Poisson dist
plt.hist(results)
# one sample from a standard normal distribution
import numpy as np
np.random.normal()
# many sample from normal distribution
import numpy as np
import matplotlib.pyplot as plt
plt.hist([np.random.normal() for n in range(100000)], bins=51)
# empirically compute variance for Poisson X_lam
# Recall that E(X_lam) = lam
# Want to estimate: E((X_lam-E(X_lam))^2)
# = E((X_lam - lam)^2)
lam = 1
num_trials = 500000
N(sum([(np.random.poisson(lam) - lam)^2 \
for n in range(num_trials)]) / num_trials)
# variance approx 1
# empirically compute variance for Poisson X_lam
# Recall that E(X_lam) = lam
# Want to estimate: E((X_lam-E(X_lam))^2)
# = E((X_lam - lam)^2)
lam = 3
num_trials = 500000
N(sum([(np.random.poisson(lam) - lam)^2 \
for n in range(num_trials)]) / num_trials)
# variance approx
# Lesson: variance of Poisson X_lam is equal to lam
# (special property of Poisson)
# empirically compute variance for standard normal
# Recall that E(X) = 0
# Want to estimate: E((X-E(X))^2)
# = E(X^2)
num_trials = 500000
N(sum([np.random.normal()^2 \
for n in range(num_trials)]) / num_trials)
# variance approx 1
# standard normal has mean 0, variance 1
# package for graphs
import networkx as nx
G = nx.Graph() # create a new empty graph
G.add_node(0) # add node
G.add_node(1)
G.add_edge(0,1) # add edge connecting node 0 to node 1
nx.draw(G)
H = nx.Graph()
H.add_nodes_from([0,1,2]) # can make graph from list of vertices
H.add_edges_from([(0,1), (1,2)]) # specify list of edges
nx.draw(H)