How to create a uniform Poisson process in a circle?
I would like to generate N points in a circle C of center (0,0) and radius R = 200. The points follow the Poisson distribution. In other words, I would like to generate N uniform Poisson point process (HPPP) inside C.
I found this article Creating Homogeneous Poisson Processes . Section 2 has exactly what I want. Specifically, on page 4, Algorithm 3 generates HPPP points inside C.
I have implemented this code in Python as follows:
""" Main Codes """
import matplotlib.pyplot as plt
import numpy as np
lamb = 0.0005 # the rate
pi = np.pi # pi = 3.14...
r = 200 # the radius of the circle C
mean = lamb * pi * r ** 2 # the mean of the Poisson random variable n
n = np.random.poisson(mean) # the Poisson random variable (i.e., the number of points inside C)
u_1 = np.random.uniform(0.0, 1.0, n) # generate n uniformly distributed points
radii = np.zeros(n) # the radial coordinate of the points
for i in range(n):
radii[i] = r * (np.sqrt(u_1[i]))
u_2 = np.random.uniform(0.0, 1.0, n) # generate another n uniformly distributed points
angle = np.zeros(n) # the angular coordinate of the points
for i in range(n):
angle[i] = 2 * pi * u_2[i]
""" Plots """
fig = plt.gcf()
ax = fig.gca()
plt.xlim(-300, 300)
plt.ylim(-300, 300)
circ = plt.Circle((0, 0), radius=200, color='r', linewidth=2, fill=False)
plt.polar(angle, radii, 'bo')
ax.add_artist(circ)
plt.show()
First, I cannot see the points inside the circle. Secondly, I don't know why the points are not being generated within the circle as expected. Is there a problem in my code?
The output is shown below: Circle C is in red.
source to share
I found the answer. I just convert polar coordinates to cartesian coordinates and then draw with plt.plot()
not with plt.polar()
.
# Cartesian Coordinates
x = np.zeros(n)
y = np.zeros(n)
for i in range(n):
x[i] = radii[i] * np.cos(angle[i])
y[i] = radii[i] * np.sin(angle[i])
plt.plot(x,y,'bo')
So I am getting the desired output.
source to share