总值错误的分布图

2024-04-26 11:09:26 发布

您现在位置:Python中文网/ 问答频道 /正文

创建enter image description here 我制作了一个分布图,代码如下:

from numpy import *
import numpy as np
import matplotlib.pyplot as plt

sigma = 4.1

x = np.linspace(-6*sigma, 6*sigma, 200)

def distr(n):
    def g(x):
        return (1/(sigma*sqrt(2*pi)))*exp(-0.5*(x/sigma)**2)
    FxSum = 0
    a = list()
    for i in range(n):
        # divide into 200 parts and sum one by one
        numb = g(-6*sigma + (12*sigma*i)/n)
        FxSum += numb
        a.append(FxSum)
    return a

plt.plot(x, distr(len(x)))
plt.show()

enter image description here

当然,这是一种获得结果的方法,无需使用hist()、cdf()或Python库中的任何其他选项

为什么总数不是1?它不应该依赖于(例如)西格玛


Tags: 代码fromimportnumpyreturndefasnp
1条回答
网友
1楼 · 发布于 2024-04-26 11:09:26

几乎正确,但为了积分,必须将函数值g(x)乘以小间隔dx12*sigma/200)。这就是你总结的领域:

from numpy import *
import numpy as np
import matplotlib.pyplot as plt

sigma = 4.1

x = np.linspace(-6*sigma, 6*sigma, 200)

def distr(n):
    def g(x):
        return (1/(sigma*sqrt(2*pi)))*exp(-0.5*(x/sigma)**2)
    FxSum = 0
    a = list()
    for i in range(n):
        # divide into 200 parts and sum one by one
        numb = g(-6*sigma + (12*sigma*i)/n) * (12*sigma/200)
        FxSum += numb
        a.append(FxSum)
    return a

plt.plot(x, distr(len(x)))
plt.show()

相关问题 更多 >