简体   繁体   English

计算高斯的标准差

[英]Calculate the standard deviation of a gaussian

I have a list of numbers, which when plotted against its length, gives me a gaussian. 我有一个数字列表,根据它的长度绘制,给我一个高斯。 I would like to calculate the standard deviation on this gaussian, but the value I get (using the np.std() function) is clearly too small (I get something like 0.00143… when it should be something like 8.234...). 我想计算这个高斯的标准偏差,但是我得到的值(使用np.std()函数)显然太小了(我得到的东西就像0.00143 ......当它应该像8.234那样......)。 I think I've been calculating the standard deviation on the y-axis and not on the x-axis (which is on what the standard deviation is supposed to be on), but I'm a bit stuck as to how to do that? 我想我一直在计算y轴上的标准偏差,而不是x轴上的标准偏差(这是标准偏差应该是什么),但我对如何做到这一点有点困惑?

I've included my code and a pic of the gaussian I'm trying to calculate the std dev on. 我已经包含了我的代码和高斯图片,我正在尝试计算std dev。

#max_k_value_counter counts the number of times the maximum value of k comes up.

max_k_value_counter_sum = sum(max_k_value_counter)
prob_max_k_value = [0] * len(max_k_value_counter)

# Calculate the probability of getting a particular value for k
for i in range(len(max_k_value_counter)):
        prob_max_k_value[i] = float(max_k_value_counter[i]) / max_k_value_counter_sum

print "Std dev on prob_max_k_value", np.std(prob_max_k_value)

# Plot p(k) vs k_max to calculate the errors on k
plt.plot(range(len(prob_max_k_value)), prob_max_k_value)
plt.xlim(0, 200)
plt.xlabel(r"$k$", fontsize=16)
plt.ylabel(r"$p(k)$", fontsize=16)
plt.show()

在此输入图像描述

you are measuring the standard deviation of probabilities not the actual values; 您正在测量概率的标准偏差而不是实际值; Here, is an example, where I draw from true standard normal distribution: 这里是一个例子,我从真正的标准正态分布中得出:

>>> from scipy.stats import norm
>>> xs   = np.linspace(-3, 3, 100)
>>> pdf  = norm.pdf(xs)
>>> prob = pdf / pdf.sum() # these are probabilities
>>> np.std(prob)           # note the very small value below
0.008473522157507624

The correct way here is to use this formula: 这里正确的方法是使用这个公式: 方差

to measure variance and then take the square root to get standard deviation; 测量方差,然后取平方根得到标准差; The first term is basically the second moment and the second term is mean squared: 第一项基本上是第二个时刻,第二项是均方:

>>> mu   = xs.dot(prob)               # mean value
>>> mom2 = np.power(xs, 2).dot(prob)  # 2nd moment
>>> var  = mom2 - mu**2               # variance
>>> np.sqrt(var)                      # standard deviation
0.98764819824739092

Note that the value we get is very close to 1 which is consistent with the fact that I draw from standard normal; 请注意,我们得到的值非常接近1,这与我从标准法线绘制的事实一致;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM