如何在Python中计算PDF(概率密度函数)? [英] How do I calculate PDF (probability density function) in Python?
问题描述
下面我有下面的代码,用于打印PDF图表的特定均值和标准差.
I have the following code below that prints the PDF graph for a particular mean and standard deviation.
现在,我需要找到特定值的实际概率.因此,例如,如果我的平均值为0,而我的值为0,则我的概率为1.这通常是通过计算曲线下的面积来完成的.与此类似:
Now I need to find the actual probability, of a particular value. So for example if my mean is 0, and my value is 0, my probability is 1. This is usually done by calculating the area under the curve. Similar to this:
http://homepage.divms.uiowa.edu/~mbognar/applets/normal.html
我不确定如何解决这个问题
I am not sure how to approach this problem
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def normal(power, mean, std, val):
a = 1/(np.sqrt(2*np.pi)*std)
diff = np.abs(np.power(val-mean, power))
b = np.exp(-(diff)/(2*std*std))
return a*b
pdf_array = []
array = np.arange(-2,2,0.1)
print array
for i in array:
print i
pdf = normal(2, 0, 0.1, i)
print pdf
pdf_array.append(pdf)
plt.plot(array, pdf_array)
plt.ylabel('some numbers')
plt.axis([-2, 2, 0, 5])
plt.show()
print
推荐答案
除非您有理由自己实现此目的.所有这些功能都可以在 scipy.stats中使用.norm
Unless you have a reason to implement this yourself. All these functions are available in scipy.stats.norm
我认为您要求输入 cdf ,然后使用以下代码:
I think you asking for the cdf, then use this code:
from scipy.stats import norm
print(norm.cdf(x, mean, std))
这篇关于如何在Python中计算PDF(概率密度函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!