在矩形中,產生符合泊松分佈的點,代碼實現參考[1]。
possion_point_rectangle.py
import random
import time
import numpy as np
import matplotlib.pyplot as plt
xMin=0;xMax=1;
yMin=0;yMax=1;
xDelta=xMax-xMin;yDelta=yMax-yMin; #rectangle dimensions
areaTotal=xDelta*yDelta;
#Point process parameters
lambda0=100; #intensity (ie mean density) of the Poisson process
numbPoints = np.random.poisson(lambda0*areaTotal)
rand=random.Random()
rand.seed(time.time())
xx=np.random.uniform(0,1,numbPoints)
yy=np.random.uniform(0,1,numbPoints)
print len(xx)
for i in range (numbPoints):
xx[i]=xDelta*xx[i]+xMin
yy[i]=yDelta*yy[i]+yMin
plt.scatter(xx,yy, edgecolor='b', facecolor='none', alpha=0.5 )
plt.xlabel("x")
plt.ylabel("y")
plt.show()
在圓內生成點,[2]中就有源代碼。
import numpy as np
import matplotlib.pyplot as plt
#Simulation window parameters
r=1; #radius of disk
xx0=0; yy0=0; #centre of disk
areaTotal=np.pi*r**2; #area of disk
#Point process parameters
lambda0=100; #intensity (ie mean density) of the Poisson process
numbPoints = np.random.poisson(lambda0*areaTotal);#Poisson number of points
theta=2*np.pi*np.random.uniform(0,1,numbPoints); #angular coordinates
rho=r*np.sqrt(np.random.uniform(0,1,numbPoints)); #radial coordinates
#Convert from polar to Cartesian coordinates
xx = rho * np.cos(theta);
yy = rho * np.sin(theta);
#Shift centre of disk to (xx0,yy0)
xx=xx+xx0; yy=yy+yy0;
#Plotting
plt.scatter(xx,yy, edgecolor='b', facecolor='none', alpha=0.5 );
plt.xlabel("x"); plt.ylabel("y");
plt.axis('equal');
plt.show()
對隨機數求根,原博客裏有點解釋:
To generate the random radial (or (\rho)) values, a reasonable guess would be to do the same as before and generate uniform random variables between zero and one, and then multiply them by the disk radius (r). But that would be wrong. Before multiplying uniform random variables by the radius, we must take the square root of all the random numbers. We then multiply them by the radius, generating random variables between (0) and (r). (We must take the square root because the area element of a sector or disk is proportional to the radius squared, and not the radius.) These random numbers do not have a uniform distribution, due to the square root, but in fact their distribution is an example of the triangular distribution, which is defined with three real-valued parameters (a), (b) and (c), and for our case, set (a=0) and (b=c=r).
[1]Simulating a homogeneous Poisson point process on a rectangle
[2] Simulating a Poisson point process on a disk