python - mcint 模块 Python-Monte Carlo 集成

标签 python montecarlo

我正在尝试运行一个代码,通过使用蒙特卡罗积分对一维高斯分布方程进行积分来输出高斯分布。我正在尝试使用 mcint 模块。我定义了 mcint 模块中使用的高斯方程和采样器函数。我不确定 mcint 函数中的“测量”部分的作用以及它应该设置为什么。有谁知道应该采取什么措施吗?我怎么知道将其设置为什么?

from matplotlib import pyplot as mp
import numpy as np
import mcint
import random


#f equation
def gaussian(x,x0,sig0,time,var):
    [velocity,diffussion_coeffient] = var
    mu = x0 + (velocity*time)
    sig = sig0 + np.sqrt(2.0*diffussion_coeffient*time)
    return (1/(np.sqrt(2.0*np.pi*(sig**2.0))))*(np.exp((-(x-mu)**2.0)/(2.0*(sig**2.0))))

#random variables that are generated during the integration
def sampler(varinterval):
    while True:
        velocity = random.uniform(varinterval[0][0],varinterval[0][1])
        diffussion_coeffient = random.uniform(varinterval[1][0],varinterval[1][1])
        yield (velocity,diffussion_coeffient)



if __name__ == "__main__":

    x0 = 0
    #ranges for integration
    velocitymin = -3.0
    velocitymax = 3.0
    diffussion_coeffientmin = 0.01
    diffussion_coeffientmax = 0.89
    varinterval = [[velocitymin,velocitymax],[diffussion_coeffientmin,diffussion_coeffientmax]]

    time = 1
    sig0 = 0.05

    x = np.linspace(-20, 20, 120)

    res = []
    for i in np.linspace(-10, 10, 120):
        result, error = mcint.integrate(lambda v: gaussian(i,x0,sig0,time,v), sampler(varinterval), measure=1, n=1000)
        res.append(result)

    mp.plot(x,res)
    mp.show()

最佳答案

this你说的是哪个模块?如果是这样的话,整个源代码只有 17 行长(在撰写本文时)。相关行是最后一行,内容如下:

return (measure*sample_mean, measure*math.sqrt(sample_var/n))

如您所见,measure 参数(其默认值为 unity)用于缩放 integrate 方法返回的值。

关于python - mcint 模块 Python-Monte Carlo 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118650/

相关文章:

python - PyTorch 中 nn.Linear 的类定义是什么?

python - PyCharm 的隐藏特性

java - 使用蒙特卡罗方法找到最低价格的理想量子

java - 蒙特卡罗树搜索不起作用

c - openmp中的Pi不依赖于线程

python - 无法让我的程序在 python matplotlib 中为多个补丁设置动画

Python从网站上抓取fb评论

Python字符串匹配完全等于Postgresql相似度函数

c# - 随机选择设置位的有效方法

algorithm - 实现蒙特卡洛树搜索 - 游戏状态节点与可能的移动节点