Julia: Statistics.cor throws domain error with specific data

Created on 7 Jun 2019  ·  2Comments  ·  Source: JuliaLang/julia

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)
statistics stdlib

Most helpful comment

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]).

All 2 comments

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 https://github.com/JuliaLang/julia/blob/7b34f1b86ecd20e1e1fd8a9543470b6f2ddb23a2/stdlib/Statistics/src/Statistics.jl#L648-L649

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.

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]).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamed2 picture iamed2  ·  3Comments

Keno picture Keno  ·  3Comments

musm picture musm  ·  3Comments

omus picture omus  ·  3Comments

StefanKarpinski picture StefanKarpinski  ·  3Comments