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

Statistics.cor throws domain error with specific data #32264

Closed
manuelVo opened this issue Jun 7, 2019 · 2 comments · Fixed by #32271
Closed

Statistics.cor throws domain error with specific data #32264

manuelVo opened this issue Jun 7, 2019 · 2 comments · Fixed by #32271
Labels
statistics The Statistics stdlib module stdlib Julia's standard library

Comments

@manuelVo
Copy link

manuelVo commented Jun 7, 2019

This code causes Statistics.cor to throw a DomainError:
https://gist.github.com/manuelVo/dce2a962bbe6f5cb6181c8f55f3e1dbf

The data itself should be fine since the Error doesn't occur if y is a Vector{Int16}.

The thrown exception is the following:

ERROR: DomainError with -112.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
 [1] sqrt at ./math.jl:492 [inlined]
 [2] sqrt at ./math.jl:518 [inlined]
 [3] corm(::Array{UInt8,1}, ::Float64, ::Array{Int8,1}, ::Float64) at /build/julia/src/julia-1.1.1/usr/share/julia/stdlib/v1.1/Statistics/src/Statistics.jl:592
 [4] cor at /build/julia/src/julia-1.1.1/usr/share/julia/stdlib/v1.1/Statistics/src/Statistics.jl:629 [inlined]
 [5] a() at ./REPL[13]:4
 [6] top-level scope at none:0

The output of versioninfo() is

Julia Version 1.1.1
Commit 55e36cc308 (2019-05-16 04:10 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
@Liozou
Copy link
Member

Liozou commented Jun 7, 2019

Hello! Just to reduce your gist to a minimal example:

julia> using Statistics

julia> cor([0], Int8[80])
NaN

julia> cor([0], Int16[81])
NaN

julia> cor([0], Int8[81])
ERROR: DomainError with -95.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
 [1] throw_complex_domainerror(::Symbol, ::Float64) at ./math.jl:31
 [2] sqrt at ./math.jl:493 [inlined]
 [3] sqrt at ./math.jl:519 [inlined]
 [4] corm(::Array{Int64,1}, ::Float64, ::Array{Int8,1}, ::Float64) at /julia/usr/share/julia/stdlib/v1.3/Statistics/src/Statistics.jl:649
 [5] cor(::Array{Int64,1}, ::Array{Int8,1}) at /julia/usr/share/julia/stdlib/v1.3/Statistics/src/Statistics.jl:686
 [6] top-level scope at REPL[29]:1

This comes from an overflow in Int8 when computing abs2:

julia> abs2(Int8(81))
-95

which happens at the two lines

xx = zero(sqrt(abs2(x[1])))
yy = zero(sqrt(abs2(y[1])))

I never touched nor used this part of julia so I don't know what fix to propose. The two related issues that led to the current use of sqrt(abs2(x[1]))seem to be #17420 and #21093.

@andreasnoack, I think you were the last person to touch that function.

@dkarrasch
Copy link
Member

Those two lines could be replaced by

xx = zero(typeof(sqrt(abs2(one(eltype(x))))))
yy = zero(typeof(sqrt(abs2(one(eltype(y))))))

It suffices to determine the type, it's not important to compute the actual value abs2(x[1]).

@nalimilan nalimilan added statistics The Statistics stdlib module stdlib Julia's standard library labels Jun 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
statistics The Statistics stdlib module stdlib Julia's standard library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants