You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cov(rand(5),rand(5),dims=1) fails, while both cov(rand(5),rand(5)) and cov(rand(5),rand(5,1),dims=1) work.
Why bother? Well, to be consistent with the other functions in Statistics. For instance, std(rand(5),dims=1) gives a 1x1 vector. It is surely a convenience for users to be able to interchange Nx1 matrices and N-vectors in such basic calculations.
The quick fix is to add a dummy dims argument to the current method (in Statistics.jl) as cov(x::AbstractVector, y::AbstractVector; dims::Int=1, corrected::Bool=true) =. This would produce a Number. A better fix might be produce a 1x1 vector to be consistent with std and other functions.
The text was updated successfully, but these errors were encountered:
I have spent some time with the cov() and cor() functions in Statistics - and I get the impression that the cov() function could use an overhaul. For instance, it is several times slower than cor(). However, it is a somewhat heavy machinery.
In the short run, the issue with the missing cov(Vector,Vector,dims=1) method, and similarly for cor(), can be solved by the crude approach suggested below. The performance seems to be within 1% of the existing code, and attempts to rely on dispatching on dims (via the covm()) do not improve.
function cov(x::AbstractVector, y::AbstractVector; dims=:, corrected::Bool=true)
result = covm(x, mean(x), y, mean(y); corrected=corrected)
if dims === (:)
return result
elseif isa(dims,Int) && dims >= 1
dims == 1 ? result2 = fill(result,1,1) : result2 = fill(NaN,length(x),length(y))
return result2
else
error("wrong value of dims")
end
end
function cor(x::AbstractVector, y::AbstractVector; dims=:)
result = corm(x, mean(x), y, mean(y))
if dims === (:)
return result
elseif isa(dims,Int) && dims >= 1
dims == 1 ? result2 = fill(result,1,1) : result2 = fill(NaN,length(x),length(y))
return result2
else
error("wrong value of dims")
end
end
cov(rand(5),rand(5),dims=1)
fails, while bothcov(rand(5),rand(5))
andcov(rand(5),rand(5,1),dims=1)
work.Why bother? Well, to be consistent with the other functions in
Statistics
. For instance,std(rand(5),dims=1)
gives a 1x1 vector. It is surely a convenience for users to be able to interchange Nx1 matrices and N-vectors in such basic calculations.The quick fix is to add a dummy
dims
argument to the current method (in Statistics.jl) ascov(x::AbstractVector, y::AbstractVector; dims::Int=1, corrected::Bool=true) =
. This would produce a Number. A better fix might be produce a 1x1 vector to be consistent withstd
and other functions.The text was updated successfully, but these errors were encountered: