From 65c95520c1fe9b160d13a34d0f21f825b36ac458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sat, 13 Feb 2021 19:46:38 +0100 Subject: [PATCH 1/8] fix handling of Missing in cor --- src/Statistics.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index 588d03c8..d78e97ca 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -665,7 +665,8 @@ end # corzm (non-exported, with centered data) -corzm(x::AbstractVector{T}) where {T} = one(float(T)) +corzm(x::AbstractVector{T}) where {T} = + one(float(T === Missing ? Missing : nonmissingtype(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)...))) @@ -679,7 +680,8 @@ corzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int=1) = # corm -corm(x::AbstractVector{T}, xmean) where {T} = one(float(T)) +corm(x::AbstractVector{T}, xmean) where {T} = + one(float(T === Missing ? Missing : nonmissingtype(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) @@ -713,7 +715,8 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = Return the number one. """ -cor(x::AbstractVector) = one(float(eltype(x))) +cor(x::AbstractVector) = + one(float(eltype(T === Missing ? Missing : nonmissingtype(T)))) """ cor(X::AbstractMatrix; dims::Int=1) From 5ddc204aa98c7ca8afccfad089e2bf00f739de7c Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sat, 13 Feb 2021 19:53:00 +0100 Subject: [PATCH 2/8] Add missing type parameter --- src/Statistics.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index d78e97ca..b937c22c 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -715,7 +715,7 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = Return the number one. """ -cor(x::AbstractVector) = +cor(x::AbstractVector) where {T} = one(float(eltype(T === Missing ? Missing : nonmissingtype(T)))) """ From 34273bdc9309939091295e491598cb3e4f9c00e0 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sun, 14 Feb 2021 13:06:02 +0100 Subject: [PATCH 3/8] Fix the fix --- src/Statistics.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index b937c22c..d40598fd 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -715,7 +715,7 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = Return the number one. """ -cor(x::AbstractVector) where {T} = +cor(x::AbstractVector{T}) where {T} = one(float(eltype(T === Missing ? Missing : nonmissingtype(T)))) """ From 5019df7ad169fa31b6d23846b00f852d8b43b527 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sun, 14 Feb 2021 13:06:47 +0100 Subject: [PATCH 4/8] Fix the fix of the fix --- src/Statistics.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index d40598fd..926f6afd 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -716,7 +716,7 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = Return the number one. """ cor(x::AbstractVector{T}) where {T} = - one(float(eltype(T === Missing ? Missing : nonmissingtype(T)))) + one(float(T === Missing ? Missing : nonmissingtype(T))) """ cor(X::AbstractMatrix; dims::Int=1) From d95d66a8b63cec084866cfac68047f2228f7931e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sun, 14 Feb 2021 14:22:05 +0100 Subject: [PATCH 5/8] Apply suggestions from code review --- src/Statistics.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index 926f6afd..0d247608 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -666,7 +666,7 @@ end # corzm (non-exported, with centered data) corzm(x::AbstractVector{T}) where {T} = - one(float(T === Missing ? Missing : nonmissingtype(T))) + T === Missing ? missing : one(float(nonmissingtype(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)...))) @@ -681,7 +681,7 @@ corzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int=1) = # corm corm(x::AbstractVector{T}, xmean) where {T} = - one(float(T === Missing ? Missing : nonmissingtype(T))) + T === Missing ? missing : one(float(nonmissingtype(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) @@ -716,7 +716,7 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = Return the number one. """ cor(x::AbstractVector{T}) where {T} = - one(float(T === Missing ? Missing : nonmissingtype(T))) + T === Missing ? missing : one(float(nonmissingtype(T))) """ cor(X::AbstractMatrix; dims::Int=1) From 6b8ab67c8b114601e7feeb244f09fc25e0fb3439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sun, 14 Feb 2021 14:48:14 +0100 Subject: [PATCH 6/8] Update src/Statistics.jl --- src/Statistics.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Statistics.jl b/src/Statistics.jl index 0d247608..5d5c2585 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -665,6 +665,10 @@ end # corzm (non-exported, with centered data) +if !isdefined(Base, :nonmissingtype) + nonmissingtype(::Type{T}) where {T} = Core.Compiler.typesubtract(T, Missing) +end + corzm(x::AbstractVector{T}) where {T} = T === Missing ? missing : one(float(nonmissingtype(T))) function corzm(x::AbstractMatrix, vardim::Int=1) From 746b2d9d48776e81a03d93ede140d9121cb62ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sun, 14 Feb 2021 15:54:25 +0100 Subject: [PATCH 7/8] Update runtests.jl --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index df0c0385..4ac7c52d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -689,7 +689,7 @@ end @test varm(z, 0.0) ≈ invoke(varm, Tuple{Any,Float64}, z, 0.0) ≈ sum(abs2, z)/9 @test isa(varm(z, 0.0), Float64) @test isa(invoke(varm, Tuple{Any,Float64}, z, 0.0), Float64) - @test cor(z) === 1.0 + @test cor(z) === 1.0+0.0im v = varm([1.0+2.0im], 0; corrected = false) @test v ≈ 5 @test isa(v, Float64) From fad50b709424a33c2d0f0a6adf9c499e60044b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sun, 14 Feb 2021 16:35:15 +0100 Subject: [PATCH 8/8] Update src/Statistics.jl --- src/Statistics.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index 5d5c2585..0d247608 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -665,10 +665,6 @@ end # corzm (non-exported, with centered data) -if !isdefined(Base, :nonmissingtype) - nonmissingtype(::Type{T}) where {T} = Core.Compiler.typesubtract(T, Missing) -end - corzm(x::AbstractVector{T}) where {T} = T === Missing ? missing : one(float(nonmissingtype(T))) function corzm(x::AbstractMatrix, vardim::Int=1)