scipy.stat.norm.pdf总计不等于一个 - python

我正在尝试从任意点伽玛计算高斯积分,如下所示:

import numpy as np
from scipy.stats import norm

def func(mu, sigma, gamma):
    x = np.linspace(mu-4*sigma, mu + 4*sigma, 100)
    y = norm.pdf(x, mu, sigma)

    area = y[x>=gamma].sum()

    print(f"Area is ~ {area:.3f}")

    plt.plot(x,y, label=f'μ0 = {mu}, σ={sigma}')
    plt.fill_between(x[x>gamma],y[x>gamma], alpha=.5, label=f'area={area:.3f}')
    plt.title("Example")
    plt.legend(loc='best')
    plt.show()

# Execute the function
func(0,10,-20)

输出为:
    面积约为1.211

scipy.stat.norm.pdf总计不等于一个 - python

蓝色区域是集成的,但是即使它不是完整功能,也可以添加一个以上(更多)。

我发现与this相关,但没有帮助。

为什么加起来不止一个?

参考方案

就像@Warren Weckesser所说的那样,问题是我没有考虑dx项(intergal的基本大小)

幸运的是,这很容易修复,因此,为了完整起见,它就来了...

import numpy as np
from scipy.stats import norm

def func(mu, sigma, gamma):
    # Notice the retstep=True param, it will return the dx needed
    x, dx = np.linspace(mu-4*sigma, mu + 4*sigma, 100, retstep=True)
    y = norm.pdf(x, mu, sigma)

    # multiply all with dx
    area = y[x>=gamma].sum() * dx

    print(f"Area is ~ {area:.3f}")

    plt.plot(x,y, label=f'μ0 = {mu}, σ={sigma}')
    plt.fill_between(x[x>gamma],y[x>gamma], alpha=.5, label=f'area={area:.3f}')
    plt.title("Example")
    plt.legend(loc='best')
    plt.show()

func(0,10,-20)

现在输出有意义,它是Area is ~ 0.978

图像是

scipy.stat.norm.pdf总计不等于一个 - python

非常感谢@Warren Weckesser!

Python / Scipy过滤器离散化 - python

我目前正在尝试从Matlab转向Python,并在多个方面取得了成功。但是,我经常使用的Matlab信号处理工具箱中的一个函数是impinvar函数,用于从其模拟版本计算数字滤波器。在Scipy.signal中,我仅发现bilinear函数可以执行类似的操作。但是,与Matlab bilinear function相比,它不需要可选参数来对频率进行一些预变形…

如何修复“ -scipy.misc没有属性” imresize”” - python

我有我朋友的一些代码。他运转平稳,但我遇到module **scipy.misc** has no attribute *imresize*我正在搜索,已安装枕头(PIL),scipy,scikit等..但是不起作用我问我的朋友,但他忘了安装了什么。 参考方案 如果从许多最新版本的scipy.misc.imresize中选择the documentation…

如何用边界条件求解微分方程,其中之一是使用python的不等式 - python

可以设置psi(z> L)<eps?我有一个系统:dD/dz = .... dPsi/dz = .... 和边界条件:D(0) = 1,6*Pi Psi(z>L) < eps 我读到有关scipy.integrate.solve_bvp和odeint的信息,但所有示例都具有如下边界条件:y(0) = y(1) = 0有谁知道如何设置这…

scipy interp1d中的错误 - python

我不了解interp1d报告的结果。我收到NAN,我应该收到号码。In [131]: bb Out[131]: array([ 0. , 1.80286595, 1.87443683, 2.70410611, 3.02764722, 3.11305985, 3.11534355, 3.18695351, 3.20693444]) In [132]: alph…

Scipy从文本文件构造网络图 - python

我有以下表示文本文件中图形的数据: a,b,1 a,c,2 b,c,1 etc.. 我需要使用SciPy建立此图的矩阵表示。现在,我阅读了字典中的所有内容:graph = { "a" : [("b",1), ("c",2)], "b" : [("b",1)] …