JuliaStats / Statistics.jl

The Statistics stdlib that ships with Julia.
https://juliastats.org/Statistics.jl/dev/
Other
72 stars 40 forks source link

Allow function as first argument of median #141

Open KronosTheLate opened 1 year ago

KronosTheLate commented 1 year ago

See this asymmerty:

julia> using Statistics

julia> v = randn(10);

julia> mean(abs, v)
0.6470633745357454

julia> median(abs, v)
ERROR: MethodError: no method matching median(::typeof(abs), ::Vector{Float64})
Closest candidates are:
  median(::Any) at ~/.julia/juliaup/julia-1.8.5+0.x64.linux.gnu/share/julia/stdlib/v1.8/Statistics/src/Statistics.jl:855
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

Is there any reason for it? If not, a method for median should be added that takes a function as the first argument.

josemanuel22 commented 1 year ago

I understand that if this functionality is being considered to be added, we should also consider extending the quantile function analogously. Isn't that so? @KronosTheLate

KronosTheLate commented 1 year ago

I have only used the quantile function a handfull of times, and I have never had this need.

Also, I am not sure if the quantile can be conceptually lumped in with summary statistics like mean and median. It does not clearly feel so for me, as the quantile is not one number, but a function a parameter from 0 to 1.

So I am rather agnostic, and think that a case needs to be made as for why quantile should have such a method.

josemanuel22 commented 1 year ago

We will have to think about it, in the meantime, you could use something like:

v = randn(10)
median(mappedarray(abs, v)) = 0.3167044909082123
KronosTheLate commented 1 year ago

It is not hard to make you own version, it is just that mean and median are so similar in nature that it like a weird API for them to have different methods.

But sure, thinking through it is a good idea.

josemanuel22 commented 1 year ago

I'll take a look at it and try to upload a PR with the changes.