inconsistent behavior of mean, std, var, etc. on empty arrays
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from JuliaStats/Statistics.jl
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
JuliaStats/Statistics.jl#221 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
JuliaStats/Statistics.jl#219 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
JuliaStats/Statistics.jl#207 · 1 comment ·
-
Difficulty 3/5 Half a day Newbie friendliness 55/100
JuliaStats/Statistics.jl#220 ·
-
TransformationsOpen
Difficulty 5/5 Over a week Newbie friendliness 30/100
JuliaStats/Statistics.jl#209 · 2 comments ·
All issues in JuliaStats/Statistics.jl
Similar issues
-
docs
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
path: splitext treats directory references and leading dots as extensionsPossibly taken @Hiten0305l claimed this today. Open
Difficulty 2/5 Half a day Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
JuliaRegistries/RegistryCI.jl#758 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaParallel/Hwloc.jl#93 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day