# Median vs 50th Quantile giving different answers

**URL:** <https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414>\
**Category:** Statistics\
**Tags:** stats\
**Created:** [December 7, 2018, 10:48am UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414 "2018-12-07T10:48:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidzentlermunro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidzentlermunro/32/5967_2.png) [@davidzentlermunro](https://discourse.julialang.org/u/davidzentlermunro)\
**Post date:** [December 7, 2018, 10:48am UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414/1 "2018-12-07T10:48:42Z")

</div>

I’m getting different answers for median(v,w::AnalyticWeights) and quantile(v,w::AnalyticWeights,0.5) and not sure why. Any ideas?

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [December 7, 2018, 11:01am UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414/2 "2018-12-07T11:01:33Z")

</div>

Please provide an example

---

<div class="post-metadata">

**Author:** ![davidzentlermunro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidzentlermunro/32/5967_2.png) [@davidzentlermunro](https://discourse.julialang.org/u/davidzentlermunro)\
**Post date:** [December 7, 2018, 11:38am UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414/3 "2018-12-07T11:38:14Z")

</div>

```julia
using StatsBase; using Distributions
v=[1; 4; 3; 2; 2.5; 7];w=[0.1;0.3;0.05;0.05;0.2;0.3]
median(v,weights(w)::AbstractWeights)
quantile(v,weights(w)::AbstractWeights,0.5)

```

Median returns 4.0 and Quantile returns 3.5.

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [December 7, 2018, 12:37pm UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414/4 "2018-12-07T12:37:26Z")

</div>

Sometimes a quantile isn’t uniquely defined (often solved by taking the average of the endpoint of the interval of quantile points). However, it only makes sense to use a definition that ensures that median and 0.5 quantile are identical.

In this case, it seems that things are worse. To me, it seems that the result of `quantile` is just wrong. The 0.5 quantile and the median, say m, _is_ the same thing and should satisfy P(X \geqslant m) \geqslant \frac{1}{2} and P(X \leqslant m) \geqslant \frac{1}{2}. For your inputs, I get

```julia
julia> x = [1; 4; 3; 2; 2.5; 7];

julia> w = [0.1;0.3;0.05;0.05;0.2;0.3];

julia> sum(w[x .<= 3.5])
0.4

```

so 3.5 is not a median. To see that 4 is the unique median, you can create the following table and see that the row with x=4 is the only one that has probabilities higher than \frac{1}{2}.

```julia
julia> p = sortperm(x);

julia> table(cumsum(w[p]), reverse(cumsum(reverse(w[p]))), x[p], names = [Symbol("P(X<=x)"), Symbol("P(X>=x)"), :x])
Table with 6 rows, 3 columns:
P(X<=x) P(X>=x) x
─────────────────────
0.1 1.0 1.0
0.15 0.9 2.0
0.35 0.85 2.5
0.4 0.65 3.0
0.7 0.6 4.0
1.0 0.3 7.0

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 7, 2018, 12:48pm UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414/5 "2018-12-07T12:48:38Z")

</div>

See also [https://github.com/JuliaStats/StatsBase.jl/pull/316](https://github.com/JuliaStats/StatsBase.jl/pull/316) and discussion at [https://github.com/JuliaStats/StatsBase.jl/issues/313](https://github.com/JuliaStats/StatsBase.jl/issues/313). Matthieu Gomez is the person to contact about this, but he isn’t on Discourse AFAICT.

Note that `quantile(v, fweights(w))` gives yet another answer (`7.0`).

---

<div class="post-metadata">

**Author:** ![davidzentlermunro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidzentlermunro/32/5967_2.png) [@davidzentlermunro](https://discourse.julialang.org/u/davidzentlermunro)\
**Post date:** [December 7, 2018, 2:28pm UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414/7 "2018-12-07T14:28:42Z")

</div>

It’s not clear why 3 should be considered a central element here though given the weight vector.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 18, 2019, 1:22pm UTC](https://discourse.julialang.org/t/median-vs-50th-quantile-giving-different-answers/18414/8 "2019-02-18T13:22:09Z")

</div>

The inconsistency has been fixed by having `median` call `quantile(x, w, 0.5)`. See [this issue](https://github.com/JuliaStats/StatsBase.jl/issues/435) and [the associated PR](https://github.com/JuliaStats/StatsBase.jl/pull/436).
