Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cov(::Adjoint, ::Weights) is wrong #82

Closed
yha opened this issue Jul 26, 2021 · 1 comment
Closed

cov(::Adjoint, ::Weights) is wrong #82

yha opened this issue Jul 26, 2021 · 1 comment

Comments

@yha
Copy link

yha commented Jul 26, 2021

julia> x = reshape(1:6,2,3)
2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
 1  3  5
 2  4  6

julia> cov(x'; corrected=false)
2×2 Matrix{Float64}:
 2.66667  2.66667
 2.66667  2.66667

julia> cov(x', Weights([1,1,1]))  # Wrong
2×1 Matrix{Float64}:
 0.0
 0.0

julia> cov(Matrix(x'), Weights([1,1,1]))  # correct
2×2 Matrix{Float64}:
 2.66667  2.66667
 2.66667  2.66667

julia> versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-9800X CPU @ 3.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake-avx512)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 4
@nalimilan
Copy link
Member

This is a bad interaction between Statistics and StatsBase. In Statistics, cov(X, Y) takes the covariance between X and Y. In StatsBase, cov(X, w) takes the weighted covariance of X with itself using weights w. Unfortunately, in some cases it's ambiguous which method you can to call, and Julia ends up calling the former or the latter depending on which method signatures are more specific.

The solution is to use a weights keyword argument instead.

Closing as a duplicate of JuliaStats/StatsBase.jl#409. The bug is in StatsBase, not in Statistics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants