学过概率论的应该都知道,现实世界得到的分布往往是抽样分布,而抽样分布标准差的无偏估计量是这样的:
在matlab官网当中这样解释:
S = std(A)
返回A
沿大小不等于 1 的第一个数组维度的元素的标准差。默认情况下,标准差按N-1
实现归一化,其中N
是观测值数量。
一个例子:
A = [4 -5 1; 2 3 5; -9 1 7];
S = std(A)
S = 1×3
7.0000 4.1633 3.0551
确实是按上述公式来的,但是numpy当中却不是这样计算的:
The standard deviation is the square root of the average of the squared deviations from the mean, i.e.,
std = sqrt(mean(x))
, wherex = abs(a - a.mean())**2
.
什么意思呢?相当于上面公式中分母为N了,那就不是无偏估计了,我对这一点很疑惑,numpy为什么这样写,请大佬解答。
代码如下:
import numpy as np
a=np.array(a)
a.std(axis=0)
out:array([5.715476 , 3.399346 , 2.4944384], dtype=float32)
至于pytorch,我也查了相关文档,发现torch用的也是和matlab一样的方法来计算标准差。
import torch
a=torch.tensor([[4, -5, 1],[2, 3, 5],[-9, 1, 7]],dtype=torch.float32)
a.std(dim=0)
out:tensor([7.0000, 4.1633, 3.0551])