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))

Gaussian distribution with Numpy and MCRand

柯西分布

要生成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))

Cauchy distribution with Numpy and MCRand

从现在开始,我们将只编写一些代码以及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')

Exponential distribution with Numpy and MCRand

瑞利分布

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)

Rayleigh distribution with Numpy and MCRand

麦克斯韦玻尔兹曼分布

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)

Maxwell-Boltzmann distribution with Numpy and MCRand

对称麦克斯韦玻尔兹曼分布

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)

Symmetric Maxwell-Boltzmann distribution with Numpy and MCRand

修正瑞利分布

最后,我们考虑一个由瑞利分布乘以x得到的一个虚构的概率分布。在某种程度上,我们做了一个对称的瑞利分布。然后,这个新的分布必须标准化,因此必须完成以下方程:

equation

通过数值积分,结果证明规范化常数必须是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)

Modified Rayleigh distribution with Numpy and MCRand

多维集成

要使用MCRand库执行多维积分,首先需要导入积分模块。此步骤可以通过以下方式完成

frommcrandimportuniform_integration

然后,我们必须定义要以NumPy-ndarray支持的方式集成的函数,因此必须对它进行一般性的定义。例如,让我们假设我们要求解以下积分:

equation

那么我们应该把函数定义为

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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Spring启动启用HTTPS   actionscript 3 java中的这个[“var”+“name”]   java只匹配给定集合中一个字符的一个匹配项   java Hibernate:防止角色表中出现多个相同的条目   javajersey+Spring注入servlet请求   java HtmlEditor javafx失去焦点   java Apache Wicket AjaxRequestTarget ListView组件未刷新或更新   mysql java。无法将lang.String转换为java。sql。时间戳   java将巨大的整数文件(在一行中)拆分为具有内存限制的已排序块   安卓如何完全关闭proguard?   安装Eclipse和Android SDK后的java“无AVD可用”消息   java动态显示图像视图   java在Spring中还有哪些WebsocketClient实现?   java Glassfish需要很长时间才能重新启动   使用Java简单串行连接器将pc与arduino连接   java如何在camel文件组件配置中结合readLockCheckInterval和maxMessagesPerPoll?   单击Android时的java预览图像   java如何将字节数组转换为ByteArrayOutputStream