Statistics cov(A,平均值=0)给出了不精确的误差

Statistics cov(A,平均值=0)给出了不精确的误差,statistics,julia,Statistics,Julia,设置mean=0时,计算矩阵中行的协方差时,我得到incecacterror(): julia> A = [1 -1 -1; -1 1 1; -1 1 -1; 1 -1 -1; 1 -1 1] 5x3 Array{Int64,2}: 1 -1 -1 -1 1 1 -1 1 -1 1 -1 -1 1 -1 1 julia> cov(A) # Works 3x3 Array{Float64,2}: 1.2 -1.2 -0.2 -1.

设置
mean=0
时,计算矩阵中行的协方差时,我得到
incecacterror()

julia> A = [1 -1 -1; -1 1 1; -1 1 -1; 1 -1 -1; 1 -1 1]
5x3 Array{Int64,2}:
  1  -1  -1
 -1   1   1
 -1   1  -1
  1  -1  -1
  1  -1   1

julia> cov(A) # Works
3x3 Array{Float64,2}:
  1.2  -1.2  -0.2
 -1.2   1.2   0.2
 -0.2   0.2   1.2

julia> cov(A, mean=[0. 0. 0.]) # Works
3x3 Array{Float64,2}:
  1.25  -1.25  -0.25
 -1.25   1.25   0.25
 -0.25   0.25   1.25

julia> cov(A, mean=0) # Expecting same output as above
ERROR: InexactError()
 in generic_scale! at linalg/generic.jl:18
 in covzm at statistics.jl:253
 in cov at statistics.jl:286
说:

平均值:允许用户提供已知的平均值。默认情况下,它设置为nothing,这表示平均值未知,函数将计算平均值用户可以使用mean=0表示输入数据居中,因此无需减去平均值

Julia source()清楚地测试是否通过了
mean=0

function cov(x::AbstractMatrix; vardim::Int=1, corrected::Bool=true, mean=nothing)
    mean == 0 ? covzm(x; vardim=vardim, corrected=corrected) :
    mean == nothing ? covm(x, _vmean(x, vardim); vardim=vardim, corrected=corrected) :
    isa(mean, AbstractArray) ? covm(x, mean; vardim=vardim, corrected=corrected) :
    error("Invalid value of mean.")
end

我是做错了什么还是这是一个bug?

我认为这应该被视为bug。在逐步完成代码后,generic.jl的第18行出现了相关错误,尤其是以下函数

function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number)
    length(C) == length(X) || error("C must be the same length as X")
    for i = 1:length(X)
        @inbounds C[i] = X[i]*s
    end
    C
end
其中3变量形式由2变量形式调用,如下所示:

generic_scale!(X::AbstractArray, s::Number) = generic_scale!(X, X, s)
此时,s的值为0.25,X的结果如下

x = unscaled_covzm(A, 1)
3x3 Array{Int64,2}:
  5  -5  -1
 -5   5   1
 -1   1   5
将上述定义剪切并粘贴到REPL中,可以直接测试通用_量表

julia> generic_scale!(x,x,0.25)
ERROR: InexactError()
 in generic_scale! at none:4
原因是X是Int64的矩阵,这导致convert抛出一个不精确的错误,因为1.25不能用int表示。如果X是Float64,则没有错误:

julia> x = unscaled_covzm(A, 1)*1.0
3x3 Array{Float64,2}:
  5.0  -5.0  -1.0
 -5.0   5.0   1.0
 -1.0   1.0   5.0

julia> generic_scale!(x,x,0.25)
3x3 Array{Float64,2}:
  1.25  -1.25  -0.25
 -1.25   1.25   0.25
 -0.25   0.25   1.25
因此,如果您根据您的示例定义一个Float64版本的

julia> B=A*1.0
5x3 Array{Float64,2}:
  1.0  -1.0  -1.0
 -1.0   1.0   1.0
 -1.0   1.0  -1.0
  1.0  -1.0  -1.0
  1.0  -1.0   1.0

julia> cov(B,mean=0)
3x3 Array{Float64,2}:
  1.25  -1.25  -0.25
 -1.25   1.25   0.25
 -0.25   0.25   1.25

也许这不是正确的答案。我只想说,如果你在你的台词中用“mean=[0.]”替换“mean=0”,朱莉娅会得到以下答案:


谢谢因此,bug要么在
无标度covzm
(因为它应该返回
Float64
数组)要么在
通用标度(因为它应该接受
Int
数组)。
julia> cov(A,mean=[0.])
3x3 Array{Float64,2}:
  1.25  -1.25  -0.25
 -1.25   1.25   0.25
 -0.25   0.25   1.25