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

Allow passing 0-length vectors to cor #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,14 @@ function corm(x::AbstractVector, mx, y::AbstractVector, my)
require_one_based_indexing(x, y)
n = length(x)
length(y) == n || throw(DimensionMismatch("inconsistent lengths"))
n > 0 || throw(ArgumentError("correlation only defined for non-empty vectors"))
if n == 0
T = promote_type(typeof(mx), typeof(my))
Copy link
Member

@nalimilan nalimilan Jan 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using an equation closer to the actual computation of the correlation, like the one used below with zero?

EDIT: though it's true that at JuliaLang/julia#29033 we used oftype(..., NaN).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this this way as x and y can have abstract eltype (e.g. Any), in which case you cannot use zero on it.

This code assumes that type of mx and my is somehow "derived" from x and y. As corm is unexported I thought it should be safe.

But we might use something like: sqrt(zero(mx)*zero(my)/(abs2(one(mx)*abs2(one(my)))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that formula looks good (with zero at the denominator)!

if T <: AbstractFloat
return T(NaN)
else
return NaN
end
end

@inbounds begin
# Initialize the accumulators
Expand Down