JuliaStats / Statistics.jl

The Statistics stdlib that ships with Julia.
https://juliastats.org/Statistics.jl/dev/
Other
72 stars 40 forks source link

Inconsistencies in cov with missing values #69

Open yurivish opened 3 years ago

yurivish commented 3 years ago

I noticed that cov sometimes handles missing values and sometimes does not.

julia> cov(rand([1.0, missing], 3))
missing

julia> cov(rand([1.0, missing], 3, 3))
ERROR: MethodError: no method matching copy(::Missing)
Closest candidates are:
  copy(::Pkg.Resolve.Graph) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Resolve/graphtype.jl:367
  copy(::LinearAlgebra.Transpose{Bool,BitArray{2}}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/bitarray.jl:226
  copy(::Expr) at expr.jl:35
  ...
Stacktrace:
 [1] matmul3x3!(::Array{Missing,2}, ::Char, ::Char, ::Array{Missing,2}, ::Array{Missing,2}, ::LinearAlgebra.MulAddMul{true,true,Bool,Bool}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/matmul.jl:971
 [2] generic_matmatmul!(::Array{Missing,2}, ::Char, ::Char, ::Array{Missing,2}, ::Array{Missing,2}, ::LinearAlgebra.MulAddMul{true,true,Bool,Bool}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/matmul.jl:726
 [3] mul! at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/matmul.jl:391 [inlined]
 [4] mul! at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/matmul.jl:208 [inlined]
 [5] * at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/matmul.jl:153 [inlined]
 [6] unscaled_covzm(::Array{Missing,2}, ::Int64) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:508
 [7] covzm(::Array{Missing,2}, ::Int64; corrected::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:522
 [8] covm(::Array{Union{Missing, Float64},2}, ::Array{Union{Missing, Float64},2}, ::Int64; corrected::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:545
 [9] cov(::Array{Union{Missing, Float64},2}; dims::Int64, corrected::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:568
 [10] cov(::Array{Union{Missing, Float64},2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:568
 [11] top-level scope at REPL[7]:1

julia> cov(rand(3, 3))
3×3 Array{Float64,2}:
  0.0915287   -0.00275245  0.0168019
 -0.00275245   0.00455733  0.00010187
  0.0168019    0.00010187  0.00316671

This is with Julia v1.5.3:

julia> versioninfo()
Julia Version 1.5.3
Commit 788b2c77c1 (2020-11-09 13:37 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
nalimilan commented 3 years ago

See also https://github.com/JuliaLang/Statistics.jl/issues/63.