From fa8810c4a6c4f7dbfdd3c9d6b8059a731fc64ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sun, 3 Jan 2021 12:31:15 +0100 Subject: [PATCH 1/3] change real to float in cor of a single collection As in `cor` we get square root I think it is safe to assume that the result should be floating point. An example of current surprising behavior: ``` julia> cor([im]) true ``` --- src/Statistics.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index 2ead403a..2f890839 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -657,7 +657,7 @@ end # corzm (non-exported, with centered data) -corzm(x::AbstractVector{T}) where {T} = one(real(T)) +corzm(x::AbstractVector{T}) where {T} = one(float(T)) function corzm(x::AbstractMatrix, vardim::Int=1) c = unscaled_covzm(x, vardim) return cov2cor!(c, collect(sqrt(c[i,i]) for i in 1:min(size(c)...))) @@ -671,7 +671,7 @@ corzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int=1) = # corm -corm(x::AbstractVector{T}, xmean) where {T} = one(real(T)) +corm(x::AbstractVector{T}, xmean) where {T} = one(float(T)) corm(x::AbstractMatrix, xmean, vardim::Int=1) = corzm(x .- xmean, vardim) function corm(x::AbstractVector, mx, y::AbstractVector, my) require_one_based_indexing(x, y) @@ -705,7 +705,7 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = Return the number one. """ -cor(x::AbstractVector) = one(real(eltype(x))) +cor(x::AbstractVector) = one(float(eltype(x))) """ cor(X::AbstractMatrix; dims::Int=1) From d783ff7d842c0538fe7104fed30e178f553a0ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sun, 3 Jan 2021 16:21:55 +0100 Subject: [PATCH 2/3] add tests --- test/runtests.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 3fd56eab..fab2f4fd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -516,6 +516,20 @@ end @test cor(tmp, tmp) <= 1.0 @test cor(tmp, tmp2) <= 1.0 end + + @test cor(Int[]) === 1.0 + @test cor([im]) ===1.0 + 0.0im + @test_throws MethodError cor([]) + @test_throws MethodError cor(Any[1.0]) + + @test cor([1, missing]) === 1.0 + @test ismissing(cor([missing])) + @test_throws MethodError cor(Any[1.0, missing]) + + @test Statistics.corm([true], 1.0) === 1.0 + @test_throws MethodError Statistics.corm(Any[0.0, 1.0], 0.5) + @test Statistics.corzm([true]) === 1.0 + @test_throws MethodError Statistics.corzm(Any[0.0, 1.0]) end @testset "quantile" begin From c6bfe2fc1f91eb23c08f34b868d66e3f3d88130b Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sun, 3 Jan 2021 18:51:46 +0100 Subject: [PATCH 3/3] Update test/runtests.jl --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index fab2f4fd..df0c0385 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -518,7 +518,7 @@ end end @test cor(Int[]) === 1.0 - @test cor([im]) ===1.0 + 0.0im + @test cor([im]) === 1.0 + 0.0im @test_throws MethodError cor([]) @test_throws MethodError cor(Any[1.0])