如何用python求出某已知正态分布的概率密度
展开全部
Python正态分布概率计算方法,喜欢算法的伙伴们可以参考学习下。需要用到math模块。先了解一下这个模块方法,再来写代码会更好上手。
def st_norm(u):
'''标准正态分布'''
import math
x=abs(u)/math.sqrt(2)
T=(0.0705230784,0.0422820123,0.0092705272,
0.0001520143,0.0002765672,0.0000430638)
E=1-pow((1+sum([a*pow(x,(i+1))
for i,a in enumerate(T)])),-16)
p=0.5-0.5*E if u<0 else 0.5+0.5*E
return(p)
def norm(a,sigma,x):
'''一般正态分布'''
u=(x-a)/sigma
return(st_norm(u))
while 1:
'''输入一个数时默认为标准正态分布
输入三个数(空格隔开)时分别为期望、方差、x
输入 stop 停止'''
S=input('please input the parameters:\n')
if S=='stop':break
try:
L=[float(s) for s in S.split()]
except:
print('Input error!')
continue
if len(L)==1:
print('f(x)=%.5f'%st_norm(L[0]))
elif len(L)==3:
print('f(x)=%.5f'%norm(L[0],L[1],L[2]))
else:
print('Input error!')
def st_norm(u):
'''标准正态分布'''
import math
x=abs(u)/math.sqrt(2)
T=(0.0705230784,0.0422820123,0.0092705272,
0.0001520143,0.0002765672,0.0000430638)
E=1-pow((1+sum([a*pow(x,(i+1))
for i,a in enumerate(T)])),-16)
p=0.5-0.5*E if u<0 else 0.5+0.5*E
return(p)
def norm(a,sigma,x):
'''一般正态分布'''
u=(x-a)/sigma
return(st_norm(u))
while 1:
'''输入一个数时默认为标准正态分布
输入三个数(空格隔开)时分别为期望、方差、x
输入 stop 停止'''
S=input('please input the parameters:\n')
if S=='stop':break
try:
L=[float(s) for s in S.split()]
except:
print('Input error!')
continue
if len(L)==1:
print('f(x)=%.5f'%st_norm(L[0]))
elif len(L)==3:
print('f(x)=%.5f'%norm(L[0],L[1],L[2]))
else:
print('Input error!')
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询