Added weighted median function and tests

Nice catch!  I rewrote this using a loop as you suggested.  The way I've
written it, the loop-based version is just a tiny bit (~1%) slower, but
it uses 20% less memory.  Here's a gist with the @time macros and
functions all bundled together, if you're curious:

https://gist.github.com/tensorjack/432bdbaa2aff38ee64ee

Re: sortrows.  sortrows sorts [v w.values] using just the first column
to sort both columns.  A bit awkward to explain in words, but here is an
example of what I mean:

julia> x=[1, 0, 3];
julia> w=[10, 2, 4];
julia> X=[x w]
3x2 Array{Int64,2}:
 1  10
 0   2
 3   4
julia> sortrows(X)
3x2 Array{Int64,2}:
 0   2
 1  10
 3   4

I included some more corner-case testing and caught a bug!  So, a very
good suggestion.  I'm sending an updated pull request now.  Thanks for
the feedback :)

- Jack

On 09/26/2014 03:13 AM, Milan Bouchet-Valat wrote:
>
> In src/weights.jl:
>
> > @@ -243,3 +243,24 @@ Base.mean{T<:Number,W<:Real}(A::AbstractArray{T}, w::WeightVec{W}, dim::Int) =
> >      mean!(Array(wmeantype(T, W), Base.reduced_dims(size(A), dim)), A, w, dim)
> >  
> >  
> > +###### Weighted median #####
> > +
> > +function Base.median{W<:Real}(v::RealVector, w::WeightVec{W})
> > +    sorted = sortrows([v w.values])
> > +    midpoint = 0.5 * sum(w.values)
> > +    if any(w.values .> midpoint)
> > +        (v[w.values .== maximum(w.values)])[1]
> > +    else
> > +        cumulative_weight = cumsum(sorted[:,2])
> > +        below_midpoint_index = find(cumulative_weight .<= midpoint)[end]
>
> It would be more memory-efficient to write a loop manually, computing
> the cumulative weight on the fly, so that you store only one value,
> not a whole array.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/JuliaStats/StatsBase.jl/pull/90/files#r18082169>.
>