Warm tip: This article is reproduced from serverfault.com, please click

python-无法调试与中心极限定理相关的代码

(python - not able to debug this code related to central limit theorem)

发布于 2020-11-29 13:59:40

我遇到了这个问题,一部电梯最多可以承受9800磅的重量。有一箱货物,平均重量为205磅,标准差为15磅。我不得不找出升降机载有49个箱子的可能性。

代码:

import math
maxwt=int(input())
n=int(input())
mean=int(input())
var=int(input())

var_sum=math.sqrt(n)*var
meansum=mean*n

def cdf(x,mean,var):
    z=x-mean/var
    return  (1+math.erf(z/(math.sqrt(2))))/2

print(round(cdf(maxwt,meansum,var_sum),4))

输入:

9800
49
205
15

预期产量:

0.0098

我的输出:

1.0

这是我从其他地方获取的另一个代码,尽管我看不到此代码或我的代码有任何区别,但它返回正确的输出

import math

x = int(input())
n = int(input())
mu = int(input())
sigma = int(input())

mu_sum = n * mu 
sigma_sum = math.sqrt(n) * sigma

def cdf(x, mu, sigma):
    Z = (x - mu)/sigma
    return 0.5*(1 + math.erf(Z/(math.sqrt(2))))

print(round(cdf(x, mu_sum, sigma_sum), 4))
Questioner
Pranav Pushkar
Viewed
12
Manas Sambare 2020-11-29 22:10:20

正确的等式是:

z = (x-mean)/var

你忘记了括号。