MCRand是一个实现多维积分和非均匀随机数生成的montecarlo方法库。
mcrand的Python项目详细描述
麦克兰德
MCRand是一个蒙特卡罗方法库。多维积分、非均匀随机数生成等
随机数发生器
在samples文件夹中,可以找到不同概率分布的MCRand和Numpy之间的比较。此外,我们使用该程序生成非标准分布的随机样本。无论如何,我们在这里展示了一个使用MCRand库生成samples/pdf.py输出的快速指南。在
要使用MCRand库生成随机数,我们首先需要导入随机生成器(RandGen)。此步骤可以通过以下方式完成
frommcrandimportsample
高斯分布
要使用MCRand随机生成器生成高斯分布数,我们首先需要定义高斯PDF
^{pr2}$然后,MCRand可以用于从x0
到{
x0=-5xf=5N=1000sigma=1mu=0gaussian_sample=sample(gaussian,x0,xf,N,mu,sigma)
最后,我们可以使用matplotlib.pyplot
绘制直方图和PDF
importmatplotlib.pyplotaspltplt.hist(gaussian_sample,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,gaussian(x,mu,sigma),color='r',label=r'Gaussian PDF $\mu=%.2f$, $\sigma=%.2f$'%(mu,sigma))
柯西分布
要生成Cauchy分布,我们需要定义它的PDF
defcauchy(x,x0,gamma):return1/(np.pi*gamma*(1+((x-x0)/(gamma))**2))
然后像以前一样使用MCRand的随机数发生器
x0=-10xf=10N=10**5x0_cauchy=0gamma=1cauchy_sample=sample(gaussian,x0,xf,N,mu,sigma)
最后我们绘制直方图和PDF
plt.hist(cauchy_sample,bins=50,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,cauchy(x,x0_cauchy,gamma),color='r',label=r'Cauchy PDF $\gamma=%.2f$, $x_0=%.2f$'%(gamma,x0_cauchy))
从现在开始,我们将只编写一些代码以及pdf.py文件的输出图形。在
指数分布
defexponential(x):returnnp.exp(-x)x0=0xf=10N=10**5rand=sample(exponential,x0,xf,N)plt.hist(numpy_rand,bins=30,density=True,color=(0,0,1,0.8),label='NumPy sample')plt.hist(rand,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')
瑞利分布
defrayleigh(x,sigma):return(x*np.exp(-(x**2)/(2*sigma**2)))/(sigma**2)x0=0xf=4sigma=1N=10**5rand=sample(rayleigh,x0,xf,N,sigma)plt.hist(rand,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,rayleigh(x,sigma),color='r',label=r'Rayleigh PDF $\sigma=%.2f$'%sigma)
麦克斯韦玻尔兹曼分布
defmaxwell_boltzmann(x,sigma):return(np.sqrt(2/np.pi))*(x**2*np.exp(-(x**2)/(2*sigma**2)))/(sigma**3)x0=0xf=10sigma=2N=10**5rand=sample(maxwell_boltzmann,x0,xf,N,sigma)plt.hist(rand,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,maxwell_boltzmann(x,sigma),color='r',label=r'Maxwell-Boltzmann PDF $\sigma=%.2f$'%sigma)
对称麦克斯韦玻尔兹曼分布
defsymmetric_maxwell_boltzmann(x,sigma):return0.5*(np.sqrt(2/np.pi))*(x**2*np.exp(-(x**2)/(2*sigma**2)))/(sigma**3)x0=-10xf=10sigma=2N=10**5rand=sample(symmetric_maxwell_boltzmann,x0,xf,N,sigma)plt.hist(rand,bins=40,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,symmetric_maxwell_boltzmann(x,sigma),color='r',label=r'Maxwell-Boltzmann PDF $\sigma=%.2f$'%sigma)
修正瑞利分布
最后,我们考虑一个由瑞利分布乘以x
得到的一个虚构的概率分布。在某种程度上,我们做了一个对称的瑞利分布。然后,这个新的分布必须标准化,因此必须完成以下方程:
通过数值积分,结果证明规范化常数必须是C=1/2.506628
。然后我们得到这个分布的概率密度函数。在
因此,MCRand可以用来生成随机数,分布如下
definvented(x,sigma):return(x**2*np.exp(-(x**2)/(2*sigma**2)))/(2.506628*sigma**2)x0=-4xf=4sigma=1N=10**5rand=sample(invented,x0,xf,N,sigma)plt.hist(rand,bins=40,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,invented(x,sigma),color='r',label=r'Modified Rayleigh PDF $\sigma=%.2f$'%sigma)
多维集成
要使用MCRand库执行多维积分,首先需要导入积分模块。此步骤可以通过以下方式完成
frommcrandimportuniform_integration
然后,我们必须定义要以NumPy-ndarray支持的方式集成的函数,因此必须对它进行一般性的定义。例如,让我们假设我们要求解以下积分:
那么我们应该把函数定义为
deffunc(x):returnnp.sum(np.power(x,2))
所以x数组的每个元素都代表一个变量。在
最后,为了得到包含错误的结果,我们可以运行以下代码
x0=[0,0]xf=[2,3]N=10**6result=uniform_integration(func,x0,xf,N)print(result)
结果如下表所示
(25.99767534344232,0.02023068196284685)
- 项目
标签: