当前位置: 首页>>代码示例>>Python>>正文


Python integrate.nquad方法代码示例

本文整理汇总了Python中scipy.integrate.nquad方法的典型用法代码示例。如果您正苦于以下问题:Python integrate.nquad方法的具体用法?Python integrate.nquad怎么用?Python integrate.nquad使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scipy.integrate的用法示例。


在下文中一共展示了integrate.nquad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: getThresholdMoments

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def getThresholdMoments(x,myP,whichModel):
    if whichModel==0: # Gaussian
        M1,err = nInt.quad(integrateGaussianMoment,-5,5,args=(x[0],myP,1)) 
        M2,err = nInt.quad(integrateGaussianMoment,-5,5,args=(x[0],myP,2)) 
    elif whichModel==1: # t
        lowerBound = np.maximum(x[1]-40,2)
        support = [[-7,7],[lowerBound,x[1]+40]]
        M1,err=nInt.nquad(thresholdMoment,support,args=(x[0],x[1],myP,whichModel,1))
        M2,err=nInt.nquad(thresholdMoment,support,args=(x[0],x[1],myP,whichModel,2))
    elif whichModel==2: # Variance-gamma
        invCdf = th.nvmPpf(myP,x[1],0)
        support = [[-7,7],[0,100]]
        M1,err=nInt.nquad(thresholdMoment,support,args=(x[0],x[1],myP,whichModel,1,invCdf))
        M2,err=nInt.nquad(thresholdMoment,support,args=(x[0],x[1],myP,whichModel,2,invCdf))
    elif whichModel==3: # Generalized hyperbolic        
        invCdf = th.nvmPpf(myP,x[1],1)
        support = [[-7,7],[0,100]]
        M1,err=nInt.nquad(thresholdMoment,support,args=(x[0],x[1],myP,whichModel,1,invCdf))
        M2,err=nInt.nquad(thresholdMoment,support,args=(x[0],x[1],myP,whichModel,2,invCdf))
    return M1,M2 
开发者ID:djbolder,项目名称:credit-risk-modelling,代码行数:22,代码来源:assetCorrelation.py

示例2: compute_expectation

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def compute_expectation(model, fun):
    # Computes the expectation of fun(th) wrt the posterior distribution,
    # given by exp(model.log_p_marg(th))
    th_shape = model.th_shape
    L = np.prod(th_shape)
    lims = [[-np.inf, np.inf]] * L
    def integrand(*args):
        th = np.array(args).reshape(th_shape)
        return np.exp(model.log_p_marg(th)) * fun(th)

    return integrate.nquad(integrand, lims)[0] 
开发者ID:HIPS,项目名称:firefly-monte-carlo,代码行数:13,代码来源:util.py

示例3: test_fixed_limits

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_fixed_limits(self):
        def func1(x0, x1, x2, x3):
            return x0**2 + x1*x2 - x3**3 + np.sin(x0) + (1 if
                        (x0 - 0.2*x3 - 0.5 - 0.25*x1 > 0) else 0)

        def opts_basic(*args):
            return {'points' : [0.2*args[2] + 0.5 + 0.25*args[0]]}

        res = nquad(func1, [[0,1], [-1,1], [.13,.8], [-.15,1]],
                    opts=[opts_basic, {}, {}, {}])
        assert_quad(res, 1.5267454070738635) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:13,代码来源:test_quadpack.py

示例4: test_variable_limits

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_variable_limits(self):
        scale = .1
        def func2(x0, x1, x2, x3, t0, t1):
            return x0*x1*x3**2 + np.sin(x2) + 1 + (1 if x0 + t1*x1 - t0 > 0 else 0)
        def lim0(x1, x2, x3, t0, t1):
            return [scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) - 1,
                    scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) + 1]
        def lim1(x2, x3, t0, t1):
            return [scale * (t0*x2 + t1*x3) - 1,
                    scale * (t0*x2 + t1*x3) + 1]
        def lim2(x3, t0, t1):
            return [scale * (x3 + t0**2*t1**3) - 1,
                    scale * (x3 + t0**2*t1**3) + 1]
        def lim3(t0, t1):
            return [scale * (t0 + t1) - 1, scale * (t0 + t1) + 1]
        def opts0(x1, x2, x3, t0, t1):
            return {'points':[t0 - t1*x1]}
        def opts1(x2, x3, t0, t1):
            return {}
        def opts2(x3, t0, t1):
            return {}
        def opts3(t0, t1):
            return {}

        res = nquad(func2, [lim0, lim1, lim2, lim3], args=(0,0),
                    opts=[opts0, opts1, opts2, opts3])
        assert_quad(res, 25.066666666666663) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:29,代码来源:test_quadpack.py

示例5: test_square_separate_ranges_and_opts

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_square_separate_ranges_and_opts(self):
        def f(y, x):
            return 1.0

        assert_quad(nquad(f, [[-1, 1], [-1, 1]], opts=[{}, {}]), 4.0) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:7,代码来源:test_quadpack.py

示例6: test_square_aliased_ranges_and_opts

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_square_aliased_ranges_and_opts(self):
        def f(y, x):
            return 1.0

        r = [-1, 1]
        opt = {}
        assert_quad(nquad(f, [r, r], opts=[opt, opt]), 4.0) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:9,代码来源:test_quadpack.py

示例7: test_square_separate_fn_ranges_and_opts

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_square_separate_fn_ranges_and_opts(self):
        def f(y, x):
            return 1.0
        def fn_range0(*args):
            return (-1, 1)
        def fn_range1(*args):
            return (-1, 1)
        def fn_opt0(*args):
            return {}
        def fn_opt1(*args):
            return {}

        ranges = [fn_range0, fn_range1]
        opts = [fn_opt0, fn_opt1]
        assert_quad(nquad(f, ranges, opts=opts), 4.0) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:17,代码来源:test_quadpack.py

示例8: test_matching_quad

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_matching_quad(self):
        def func(x):
            return x**2 + 1

        res, reserr = quad(func, 0, 4)
        res2, reserr2 = nquad(func, ranges=[[0, 4]])
        assert_almost_equal(res, res2)
        assert_almost_equal(reserr, reserr2) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:10,代码来源:test_quadpack.py

示例9: test_matching_dblquad

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_matching_dblquad(self):
        def func2d(x0, x1):
            return x0**2 + x1**3 - x0 * x1 + 1

        res, reserr = dblquad(func2d, -2, 2, lambda x:-3, lambda x:3)
        res2, reserr2 = nquad(func2d, [[-3, 3], (-2, 2)])
        assert_almost_equal(res, res2)
        assert_almost_equal(reserr, reserr2) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:10,代码来源:test_quadpack.py

示例10: test_matching_tplquad

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_matching_tplquad(self):
        def func3d(x0, x1, x2, c0, c1):
            return x0**2 + c0 * x1**3 - x0 * x1 + 1 + c1 * np.sin(x2)


        res = tplquad(func3d, -1, 2, lambda x:-2, lambda x:2,
                      lambda x, y : -np.pi, lambda x, y : np.pi,
                      args=(2, 3))
        res2 = nquad(func3d, [[-np.pi, np.pi], [-2, 2], (-1, 2)], args=(2, 3))
        assert_almost_equal(res, res2) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:12,代码来源:test_quadpack.py

示例11: ise

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def ise(estimateddensity, theoreticaldensity):
    def integrand(*x):
        value = np.array(x)
        return (estimateddensity(value) - theoreticaldensity(value))**2

    return integrate.nquad(integrand, [[-5., 5.], [-5., 5.]]) 
开发者ID:thalesians,项目名称:bayestsa,代码行数:8,代码来源:studykde.py

示例12: test_integrate_2d

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_integrate_2d(self):
        np.random.seed(1234)
        c = np.random.rand(4, 5, 16, 17)
        x = np.linspace(0, 1, 16+1)**1
        y = np.linspace(0, 1, 17+1)**2

        # make continuously differentiable so that nquad() has an
        # easier time
        c = c.transpose(0, 2, 1, 3)
        cx = c.reshape(c.shape[0], c.shape[1], -1).copy()
        _ppoly.fix_continuity(cx, x, 2)
        c = cx.reshape(c.shape)
        c = c.transpose(0, 2, 1, 3)
        c = c.transpose(1, 3, 0, 2)
        cx = c.reshape(c.shape[0], c.shape[1], -1).copy()
        _ppoly.fix_continuity(cx, y, 2)
        c = cx.reshape(c.shape)
        c = c.transpose(2, 0, 3, 1).copy()

        # Check integration
        p = NdPPoly(c, (x, y))

        for ranges in [[(0, 1), (0, 1)],
                       [(0, 0.5), (0, 1)],
                       [(0, 1), (0, 0.5)],
                       [(0.3, 0.7), (0.6, 0.2)]]:

            ig = p.integrate(ranges)
            ig2, err2 = nquad(lambda x, y: p((x, y)), ranges,
                              opts=[dict(epsrel=1e-5, epsabs=1e-5)]*2)
            assert_allclose(ig, ig2, rtol=1e-5, atol=1e-5,
                            err_msg=repr(ranges)) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:34,代码来源:test_interpolate.py

示例13: test_fixed_limits

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_fixed_limits(self):
        def func1(x0, x1, x2, x3):
            val = (x0**2 + x1*x2 - x3**3 + np.sin(x0) +
                   (1 if (x0 - 0.2*x3 - 0.5 - 0.25*x1 > 0) else 0))
            return val

        def opts_basic(*args):
            return {'points': [0.2*args[2] + 0.5 + 0.25*args[0]]}

        res = nquad(func1, [[0, 1], [-1, 1], [.13, .8], [-.15, 1]],
                    opts=[opts_basic, {}, {}, {}], full_output=True)
        assert_quad(res[:-1], 1.5267454070738635)
        assert_(res[-1]['neval'] > 0 and res[-1]['neval'] < 4e5) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:15,代码来源:test_quadpack.py

示例14: test_variable_limits

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_variable_limits(self):
        scale = .1

        def func2(x0, x1, x2, x3, t0, t1):
            val = (x0*x1*x3**2 + np.sin(x2) + 1 +
                   (1 if x0 + t1*x1 - t0 > 0 else 0))
            return val

        def lim0(x1, x2, x3, t0, t1):
            return [scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) - 1,
                    scale * (x1**2 + x2 + np.cos(x3)*t0*t1 + 1) + 1]

        def lim1(x2, x3, t0, t1):
            return [scale * (t0*x2 + t1*x3) - 1,
                    scale * (t0*x2 + t1*x3) + 1]

        def lim2(x3, t0, t1):
            return [scale * (x3 + t0**2*t1**3) - 1,
                    scale * (x3 + t0**2*t1**3) + 1]

        def lim3(t0, t1):
            return [scale * (t0 + t1) - 1, scale * (t0 + t1) + 1]

        def opts0(x1, x2, x3, t0, t1):
            return {'points': [t0 - t1*x1]}

        def opts1(x2, x3, t0, t1):
            return {}

        def opts2(x3, t0, t1):
            return {}

        def opts3(t0, t1):
            return {}

        res = nquad(func2, [lim0, lim1, lim2, lim3], args=(0, 0),
                    opts=[opts0, opts1, opts2, opts3])
        assert_quad(res, 25.066666666666663) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:40,代码来源:test_quadpack.py

示例15: test_square_aliased_fn_ranges_and_opts

# 需要导入模块: from scipy import integrate [as 别名]
# 或者: from scipy.integrate import nquad [as 别名]
def test_square_aliased_fn_ranges_and_opts(self):
        def f(y, x):
            return 1.0

        def fn_range(*args):
            return (-1, 1)

        def fn_opt(*args):
            return {}

        ranges = [fn_range, fn_range]
        opts = [fn_opt, fn_opt]
        assert_quad(nquad(f, ranges, opts=opts), 4.0) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:15,代码来源:test_quadpack.py


注:本文中的scipy.integrate.nquad方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。