inconsistent behavior of mean, std, var, etc. on empty arrays

Open
#215 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
julia
Domain
data

Research direction

The issue names mean, std, var, median, and sum, and includes stack traces into Statistics.jl; start by reading those implementations and their tests for empty arrays, including dims and Unitful values. Define consistent behavior for empty inputs that preserves array shape and appropriate units, then add tests covering the listed cases and confirm they pass.

Written by the indexing model from the issue text.

Description

Hi, the various simple statistics give different behaviors between themselves when applied to length 0 vectors, and also between Unitful and non-Unitful vectors.

julia> ones(0)
Float64[]

julia> typeof(ones(0))
Vector{Float64} (alias for Array{Float64, 1})

julia> length(ones(0))
0

julia> sum(ones(0))
0.0

julia> mean(ones(0))
NaN

julia> std(ones(0))
NaN

julia> var(ones(0))
NaN

julia> median(ones(0))
ERROR: ArgumentError: median of an empty array is undefined, Float64[]
Stacktrace:
 [1] median!(v::Vector{Float64})
   @ Statistics ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:838
 [2] _median
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:904 [inlined]
 [3] median(v::Matrix{Float64})
   @ Statistics ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:900
 [4] top-level scope
   @ REPL[192]:1

All of these should be consistent. Clearly sum is giving a wrong answer, and it doesn't make sense for median to err and the others to not err. I think NaN doesn't make sense because something with no length is yielding something with a length. You could make an argument for an error, but for me the most sensible output is an empty vector of the same type, Float64[] in this case.

For example, what is the sum of all the elements of an empty vector? It's empty, I would say.

When the empty vector is a Unitful empty vector, we have the following behaviors, with std and var now also erring

julia> sum(ones(0)*(u"m/s"))
0.0 m s⁻¹

julia> mean(ones(0)*(u"m/s"))
NaN m s⁻¹

julia> std(ones(0)*(u"m/s"))
ERROR: DimensionError: m² s⁻² and NaN are not dimensionally compatible.
Stacktrace:
 [1] convert(::Type{Quantity{Float64, 𝐋² 𝐓⁻², Unitful.FreeUnits{(m², s⁻²), 𝐋² 𝐓⁻², nothing}}}, x::Float64)
   @ Unitful ~/.julia/packages/Unitful/ikxOk/src/conversion.jl:133
 [2] oftype(x::Quantity{Float64, 𝐋² 𝐓⁻², Unitful.FreeUnits{(m², s⁻²), 𝐋² 𝐓⁻², nothing}}, y::Float64)
   @ Base ./essentials.jl:716
 [3] _varm
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:354 [inlined]
 [4] varm
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:350 [inlined]
 [5] _var
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:399 [inlined]
 [6] var
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:386 [inlined]
 [7] _std
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:468 [inlined]
 [8] std(A::Vector{Quantity{Float64, 𝐋 𝐓⁻¹, Unitful.FreeUnits{(m, s⁻¹), 𝐋 𝐓⁻¹, nothing}}})
   @ Statistics ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:463
 [9] top-level scope
   @ REPL[221]:1

julia> var(ones(0)*(u"m/s"))
ERROR: DimensionError: m² s⁻² and NaN are not dimensionally compatible.
Stacktrace:
 [1] convert(::Type{Quantity{Float64, 𝐋² 𝐓⁻², Unitful.FreeUnits{(m², s⁻²), 𝐋² 𝐓⁻², nothing}}}, x::Float64)
   @ Unitful ~/.julia/packages/Unitful/ikxOk/src/conversion.jl:133
 [2] oftype(x::Quantity{Float64, 𝐋² 𝐓⁻², Unitful.FreeUnits{(m², s⁻²), 𝐋² 𝐓⁻², nothing}}, y::Float64)
   @ Base ./essentials.jl:716
 [3] _varm
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:354 [inlined]
 [4] varm
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:350 [inlined]
 [5] _var
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:399 [inlined]
 [6] var(A::Vector{Quantity{Float64, 𝐋 𝐓⁻¹, Unitful.FreeUnits{(m, s⁻¹), 𝐋 𝐓⁻¹, nothing}}})
   @ Statistics ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:386
 [7] top-level scope
   @ REPL[222]:1

julia> median(ones(0)*(u"m/s"))
ERROR: ArgumentError: median of an empty array is undefined, Quantity{Float64, 𝐋 𝐓⁻¹, Unitful.FreeUnits{(m, s⁻¹), 𝐋 𝐓⁻¹, nothing}}[]
Stacktrace:
 [1] median!(v::Vector{Quantity{Float64, 𝐋 𝐓⁻¹, Unitful.FreeUnits{(m, s⁻¹), 𝐋 𝐓⁻¹, nothing}}})
   @ Statistics ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:838
 [2] _median
   @ ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:904 [inlined]
 [3] median(v::Vector{Quantity{Float64, 𝐋 𝐓⁻¹, Unitful.FreeUnits{(m, s⁻¹), 𝐋 𝐓⁻¹, nothing}}})
   @ Statistics ~/.julia/packages/Statistics/2IU8F/src/Statistics.jl:900
 [4] top-level scope
   @ REPL[223]:1

Here is my proposed behavior for acting on arrays with an empty dimension, with dims included.

When acting on a matrix with a length 0 dimension, e.g. 0 x N, statistics (sum, mean, std, var, median) acting along that dimension return a 0 x N matrix of the appropriate type, for example

mean(ones(0,4), dims =1)
> 0×4 Matrix{Float64}

When that quantity is Unitful we should then have

mean(ones(0,4)*u"cm/s", dims =1)
> 0×4 Matrix{Quantity{Float64, 𝐋 𝐓⁻¹, Unitful.FreeUnits{(cm, s⁻¹), 𝐋 𝐓⁻¹, nothing}}}

and similarly for std, sum, and median, but var should give a quantity with the squared units

var(ones(0,4)*u"cm/s", dims =1)
> 0×4 Matrix{Quantity{Float64, 𝐋² 𝐓⁻², Unitful.FreeUnits{(cm², s⁻²), 𝐋² 𝐓⁻², nothing}}}

because those are the units we would expect if the array were not empty.

This can come up in the following scenario. You have a set of arrays and you perform an indexing operation on them, which occasionally can return an empty array. It would be good for the behavior of statistics applied to these empty arrays to be consistent.

Dominant language
Julia
Stars
93
Forks
51
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaStats/Statistics.jl

All issues in JuliaStats/Statistics.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.