总价值错误的分配图

问题描述 投票:1回答:1

创建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?它不应该依赖于(例如)sigma。

python numpy distribution normal-distribution
1个回答
0
投票

几乎正确,但是要进行积分,您必须将函数值g(x)乘以微小间隔dx (12*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()
© www.soinside.com 2019 - 2024. All rights reserved.